-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddy.go
132 lines (105 loc) · 2.67 KB
/
caddy.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package caddy_gcp_secret_manager
import (
"encoding/json"
"errors"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"go.uber.org/zap"
"google.golang.org/api/option"
"io/fs"
"os"
)
var (
ErrInvalidJSON = errors.New("invalid JSON")
ErrCredentialsFile = errors.New("unable to read credentials file")
)
func init() {
caddy.RegisterModule(CaddyGcpSecretManagerStorage{})
}
func (c CaddyGcpSecretManagerStorage) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "caddy.storage.gcp-secret-manager",
New: func() caddy.Module {
return new(CaddyGcpSecretManagerStorage)
},
}
}
type CaddyGcpSecretManagerStorage struct {
storage *SecretManagerStorage
logger *zap.Logger
Fs fs.FS
ProjectID string
CredentialsFile string
}
func (c *CaddyGcpSecretManagerStorage) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
for d.Next() {
d.NextArg()
if v := d.Val(); v != "" && v != "gcp-secret-manager" {
c.ProjectID = v
}
for nest := d.Nesting(); d.NextBlock(nest); {
switch v := d.Val(); v {
case "project_id":
if !d.NextArg() {
return d.ArgErr()
}
if c.ProjectID == "" {
c.ProjectID = d.Val()
}
break
case "credentials_file":
if !d.NextArg() {
return d.ArgErr()
}
c.CredentialsFile = d.Val()
break
default:
return d.Errf("Unknown option '%v'", v)
}
}
if c.ProjectID == "" {
return d.Errf("project_id must be set")
}
}
return nil
}
func (c CaddyGcpSecretManagerStorage) Provision(context caddy.Context) error {
c.logger = context.Logger(c)
return nil
}
func (c CaddyGcpSecretManagerStorage) Validate() error {
var files fs.FS
var opts []option.ClientOption
if c.Fs != nil {
files = c.Fs
} else {
files = os.DirFS("/")
}
if c.CredentialsFile != "" {
file, err := files.Open(c.CredentialsFile)
if err != nil {
return errors.Join(ErrCredentialsFile, err)
}
var buffer []byte
if _, err := file.Read(buffer); err != nil {
return errors.Join(ErrCredentialsFile, err)
}
if !json.Valid(buffer) {
return errors.Join(ErrInvalidJSON, ErrCredentialsFile)
}
}
storage, err := NewSecretManagerStorage(c.ProjectID, opts...)
if err != nil {
return err
}
c.storage = storage
return nil
}
func (c CaddyGcpSecretManagerStorage) Cleanup() error {
return c.storage.Close()
}
var _ caddy.Module = (*CaddyGcpSecretManagerStorage)(nil)
var _ caddy.Provisioner = (*CaddyGcpSecretManagerStorage)(nil)
var _ caddy.Validator = (*CaddyGcpSecretManagerStorage)(nil)
var _ caddy.CleanerUpper = (*CaddyGcpSecretManagerStorage)(nil)
var _ caddyfile.Unmarshaler = (*CaddyGcpSecretManagerStorage)(nil)