-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
288 lines (234 loc) · 5.81 KB
/
config.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package ConfigBuilder
import (
"github.com/keloran/go-config/auth/authentik"
"github.com/keloran/go-config/bugfixes"
"net/http"
"github.com/bugfixes/go-bugfixes/logs"
"github.com/keloran/go-config/auth/keycloak"
"github.com/keloran/go-config/database"
"github.com/keloran/go-config/influx"
"github.com/keloran/go-config/local"
"github.com/keloran/go-config/mongo"
"github.com/keloran/go-config/rabbit"
"github.com/keloran/go-config/vault"
vaultHelper "github.com/keloran/vault-helper"
)
type Config struct {
VaultHelper *vaultHelper.VaultHelper
VaultPaths vault.Paths
VaultInject bool
Local local.System
Vault vault.System
Database database.System
Keycloak keycloak.System
Mongo mongo.System
Rabbit rabbit.System
Influx influx.System
Bugfixes bugfixes.System
Authentik authentik.System
// Project level properties
ProjectProperties map[string]interface{}
}
type BuildOption func(*Config) error
func NewConfig(vh vaultHelper.VaultHelper) *Config {
return &Config{
VaultHelper: &vh,
}
}
func Local(cfg *Config) error {
l, err := local.Build()
if err != nil {
return logs.Errorf("build local: %v", err)
}
cfg.Local = *l
return nil
}
func Vault(cfg *Config) error {
v, vh, err := vault.Build()
if err != nil {
return logs.Errorf("build vault: %v", err)
}
cfg.Vault = *v
cfg.VaultHelper = &vh
return nil
}
func Database(cfg *Config) error {
d := database.NewSystem()
if cfg.VaultHelper != nil {
vd := database.VaultDetails{}
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.Database.Details != "" {
vd.DetailsPath = cfg.VaultPaths.Database.Details
}
if cfg.VaultPaths.Database.Credentials != "" {
vd.CredPath = cfg.VaultPaths.Database.Credentials
}
}
d.Setup(vd, *cfg.VaultHelper)
}
_, err := d.Build()
if err != nil {
return logs.Errorf("database failed to build: %v", err)
}
cfg.Database = *d
return nil
}
func Mongo(cfg *Config) error {
m := mongo.NewSystem()
if cfg.VaultHelper != nil {
vd := mongo.VaultDetails{}
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.Mongo.Details != "" {
vd.DetailsPath = cfg.VaultPaths.Mongo.Details
}
if cfg.VaultPaths.Mongo.Credentials != "" {
vd.CredPath = cfg.VaultPaths.Mongo.Credentials
}
}
m.Setup(vd, *cfg.VaultHelper)
}
_, err := m.Build()
if err != nil {
return logs.Errorf("failed to build mongo: %v", err)
}
cfg.Mongo = *m
return nil
}
func Keycloak(cfg *Config) error {
k := keycloak.NewSystem()
if cfg.VaultHelper != nil {
vd := keycloak.VaultDetails{}
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.Keycloak.Details != "" {
vd.DetailsPath = cfg.VaultPaths.Keycloak.Details
}
}
k.Setup(vd, *cfg.VaultHelper)
}
_, err := k.Build()
if err != nil {
return logs.Errorf("failed to build keycloak: %v", err)
}
cfg.Keycloak = *k
return nil
}
func Authentik(cfg *Config) error {
a := authentik.NewSystem()
if cfg.VaultHelper != nil {
vd := authentik.VaultDetails{}
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.Authentik.Details != "" {
vd.DetailsPath = cfg.VaultPaths.Authentik.Details
}
}
a.Setup(vd, *cfg.VaultHelper)
}
_, err := a.Build()
if err != nil {
return logs.Errorf("failed to build authentik: %v", err)
}
cfg.Authentik = *a
return nil
}
func Rabbit(cfg *Config) error {
r := rabbit.NewSystem(&http.Client{})
if cfg.VaultHelper != nil {
vd := rabbit.VaultDetails{}
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.Rabbit.Details != "" {
vd.DetailsPath = cfg.VaultPaths.Rabbit.Details
}
if cfg.VaultPaths.Rabbit.Credentials != "" {
vd.CredPath = cfg.VaultPaths.Rabbit.Credentials
}
}
r.Setup(vd, *cfg.VaultHelper)
}
_, err := r.Build()
if err != nil {
return logs.Errorf("failed to build rabbit: %v", err)
}
cfg.Rabbit = *r
return nil
}
func Influx(cfg *Config) error {
i := influx.NewSystem()
if cfg.VaultHelper != nil {
vd := influx.VaultDetails{}
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.Influx.Details != "" {
vd.DetailsPath = cfg.VaultPaths.Influx.Details
}
}
i.Setup(vd, *cfg.VaultHelper)
}
_, err := i.Build()
if err != nil {
return logs.Errorf("failed to build influx: %v", err)
}
cfg.Influx = *i
return nil
}
func Bugfixes(cfg *Config) error {
b := bugfixes.NewSystem()
if cfg.VaultHelper != nil {
vd := b.VaultDetails
if cfg.VaultPaths != (vault.Paths{}) {
if cfg.VaultPaths.BugFixes.Details != "" {
vd.DetailsPath = cfg.VaultPaths.BugFixes.Details
}
}
b.Setup(vd, *cfg.VaultHelper)
}
_, err := b.Build()
if err != nil {
return logs.Errorf("failed to build bugfixes: %v", err)
}
bf := &logs.BugFixes{}
bf.Setup(b.AgentKey, b.AgentSecret)
b.Logger = bf
cfg.Bugfixes = *b
return nil
}
func Build(opts ...BuildOption) (*Config, error) {
cfg := &Config{}
if err := cfg.Build(opts...); err != nil {
return nil, logs.Errorf("build config: %v", err)
}
return cfg, nil
}
func BuildLocal(opts ...BuildOption) (*Config, error) {
cfg := &Config{}
if err := cfg.Build(opts...); err != nil {
return nil, logs.Errorf("build config: %v", err)
}
return cfg, nil
}
func BuildLocalVH(mockVault vaultHelper.VaultHelper, opts ...BuildOption) (*Config, error) {
cfg := &Config{
VaultHelper: &mockVault,
}
if err := cfg.Build(opts...); err != nil {
return nil, logs.Errorf("build config: %v", err)
}
return cfg, nil
}
func (c *Config) Build(opts ...BuildOption) error {
for _, opt := range opts {
if err := opt(c); err != nil {
return logs.Errorf("build configOptions: %v", err)
}
}
return nil
}
type ProjectConfigurator interface {
Build(*Config) error
}
func WithProjectConfigurator(pc ProjectConfigurator) BuildOption {
return func(c *Config) error {
if err := pc.Build(c); err != nil {
return logs.Errorf("withProjectConfigurator: %v", err)
}
return nil
}
}