forked from grafana/certmagic-gcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
100 lines (87 loc) · 2.38 KB
/
module.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package certmagicgcs
import (
"context"
"fmt"
"os"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/certmagic"
"github.com/google/tink/go/aead"
"github.com/google/tink/go/insecurecleartextkeyset"
"github.com/google/tink/go/keyset"
)
// Interface guards
var (
_ caddyfile.Unmarshaler = (*CaddyStorageGCS)(nil)
_ caddy.StorageConverter = (*CaddyStorageGCS)(nil)
)
// CaddyStorageGCS implements a caddy storage backend for Google Cloud Storage.
type CaddyStorageGCS struct {
// BucketName is the name of the storage bucket.
BucketName string `json:"bucket-name"`
// EncryptionKeySet is the path of a json tink encryption keyset
EncryptionKeySet string `json:"encryption-key-set"`
}
func init() {
caddy.RegisterModule(CaddyStorageGCS{})
}
// CaddyModule returns the Caddy module information.
func (CaddyStorageGCS) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.storage.gcs",
New: func() caddy.Module {
return new(CaddyStorageGCS)
},
}
}
// CertMagicStorage returns a cert-magic storage.
func (s *CaddyStorageGCS) CertMagicStorage() (certmagic.Storage, error) {
config := StorageConfig{
BucketName: s.BucketName,
}
if len(s.EncryptionKeySet) > 0 {
f, err := os.Open(s.EncryptionKeySet)
if err != nil {
return nil, err
}
defer f.Close()
r := keyset.NewJSONReader(f)
// TODO: Add the ability to read an encrypted keyset / or envelope encryption
// see https://github.com/google/tink/blob/e5c9356ed471be08a63eb5ea3ad0e892544e5a1c/go/keyset/handle_test.go#L84-L86
// or https://github.com/google/tink/blob/master/docs/GOLANG-HOWTO.md
kh, err := insecurecleartextkeyset.Read(r)
if err != nil {
return nil, err
}
kp, err := aead.New(kh)
if err != nil {
return nil, err
}
config.AEAD = kp
}
return NewStorage(context.Background(), config)
}
// Validate caddy gcs storage configuration.
func (s *CaddyStorageGCS) Validate() error {
if s.BucketName == "" {
return fmt.Errorf("bucket name must be defined")
}
return nil
}
// UnmarshalCaddyfile unmarshall caddy file.
func (s *CaddyStorageGCS) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
key := d.Val()
var value string
if !d.Args(&value) {
continue
}
switch key {
case "bucket-name":
s.BucketName = value
case "encryption-key-set":
s.EncryptionKeySet = value
}
}
return nil
}