/
config.go
441 lines (397 loc) · 14.5 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2019 - MinIO, Inc. All rights reserved.
// Use of this source code is governed by the AGPLv3
// license that can be found in the LICENSE file.
package main
import (
"context"
"errors"
"fmt"
stdlog "log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/minio/kes"
"github.com/minio/kes/internal/auth"
"github.com/minio/kes/internal/aws"
"github.com/minio/kes/internal/azure"
"github.com/minio/kes/internal/fortanix"
"github.com/minio/kes/internal/fs"
"github.com/minio/kes/internal/gcp"
"github.com/minio/kes/internal/gemalto"
"github.com/minio/kes/internal/generic"
"github.com/minio/kes/internal/key"
"github.com/minio/kes/internal/mem"
"github.com/minio/kes/internal/vault"
"github.com/minio/kes/internal/yml"
)
// connect tries to establish a connection to the KMS specified in the ServerConfig
func connect(config *yml.ServerConfig, quiet quiet, errorLog *stdlog.Logger) (key.Store, error) {
switch {
case config.KeyStore.Fs.Path.Value() != "":
f, err := os.Stat(config.KeyStore.Fs.Path.Value())
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, fmt.Errorf("failed to open %q: %v", config.KeyStore.Fs.Path.Value(), err)
}
if err == nil && !f.IsDir() {
return nil, fmt.Errorf("%q is not a directory", config.KeyStore.Fs.Path.Value())
}
if errors.Is(err, os.ErrNotExist) {
msg := fmt.Sprintf("Creating directory '%s' ... ", config.KeyStore.Fs.Path.Value())
quiet.Print(msg)
if err = os.MkdirAll(config.KeyStore.Fs.Path.Value(), 0o700); err != nil {
return nil, fmt.Errorf("failed to create directory %q: %v", config.KeyStore.Fs.Path.Value(), err)
}
quiet.ClearMessage(msg)
}
return &fs.Store{
Dir: config.KeyStore.Fs.Path.Value(),
ErrorLog: errorLog,
}, nil
case config.KeyStore.Generic.Endpoint.Value() != "":
genericStore := &generic.Store{
Endpoint: config.KeyStore.Generic.Endpoint.Value(),
KeyPath: config.KeyStore.Generic.TLS.PrivateKey.Value(),
CertPath: config.KeyStore.Generic.TLS.Certificate.Value(),
CAPath: config.KeyStore.Generic.TLS.CAPath.Value(),
ErrorLog: errorLog,
}
msg := fmt.Sprintf("Authenticating to generic KeyStore '%s' ... ", config.KeyStore.Generic.Endpoint.Value())
quiet.Print(msg)
if err := genericStore.Authenticate(); err != nil {
return nil, fmt.Errorf("failed to connect to generic KeyStore: %v", err)
}
quiet.ClearMessage(msg)
return genericStore, nil
case config.KeyStore.Vault.Endpoint.Value() != "":
msg := fmt.Sprintf("Authenticating to Hashicorp Vault '%s' ... ", config.KeyStore.Vault.Endpoint.Value())
quiet.Print(msg)
vaultStore, err := vault.Connect(context.Background(), &vault.Config{
Endpoint: config.KeyStore.Vault.Endpoint.Value(),
Engine: config.KeyStore.Vault.Engine.Value(),
APIVersion: config.KeyStore.Vault.APIVersion.Value(),
Prefix: config.KeyStore.Vault.Prefix.Value(),
Namespace: config.KeyStore.Vault.Namespace.Value(),
AppRole: vault.AppRole{
Engine: config.KeyStore.Vault.AppRole.Engine.Value(),
ID: config.KeyStore.Vault.AppRole.ID.Value(),
Secret: config.KeyStore.Vault.AppRole.Secret.Value(),
Retry: config.KeyStore.Vault.AppRole.Retry.Value(),
},
K8S: vault.Kubernetes{
Engine: config.KeyStore.Vault.Kubernetes.Engine.Value(),
Role: config.KeyStore.Vault.Kubernetes.Role.Value(),
JWT: config.KeyStore.Vault.Kubernetes.JWT.Value(),
Retry: config.KeyStore.Vault.Kubernetes.Retry.Value(),
},
StatusPingAfter: config.KeyStore.Vault.Status.Ping.Value(),
ErrorLog: errorLog,
ClientKeyPath: config.KeyStore.Vault.TLS.PrivateKey.Value(),
ClientCertPath: config.KeyStore.Vault.TLS.Certificate.Value(),
CAPath: config.KeyStore.Vault.TLS.CAPath.Value(),
})
if err != nil {
return nil, fmt.Errorf("failed to connect to Vault: %v", err)
}
quiet.ClearMessage(msg)
return vaultStore, nil
case config.KeyStore.Fortanix.SDKMS.Endpoint.Value() != "":
fortanixStore := &fortanix.KeyStore{
Endpoint: config.KeyStore.Fortanix.SDKMS.Endpoint.Value(),
GroupID: config.KeyStore.Fortanix.SDKMS.GroupID.Value(),
APIKey: fortanix.APIKey(config.KeyStore.Fortanix.SDKMS.Login.APIKey.Value()),
ErrorLog: errorLog,
CAPath: config.KeyStore.Fortanix.SDKMS.TLS.CAPath.Value(),
}
msg := fmt.Sprintf("Authenticating to Fortanix SDKMS '%s' ... ", fortanixStore.Endpoint)
quiet.Print(msg)
if err := fortanixStore.Authenticate(context.Background()); err != nil {
return nil, fmt.Errorf("failed to connect to Fortanix SDKMS: %v", err)
}
quiet.ClearMessage(msg)
return fortanixStore, nil
case config.KeyStore.Aws.SecretsManager.Endpoint.Value() != "":
awsStore := &aws.SecretsManager{
Addr: config.KeyStore.Aws.SecretsManager.Endpoint.Value(),
Region: config.KeyStore.Aws.SecretsManager.Region.Value(),
KMSKeyID: config.KeyStore.Aws.SecretsManager.KmsKey.Value(),
ErrorLog: errorLog,
Login: aws.Credentials{
AccessKey: config.KeyStore.Aws.SecretsManager.Login.AccessKey.Value(),
SecretKey: config.KeyStore.Aws.SecretsManager.Login.SecretKey.Value(),
SessionToken: config.KeyStore.Aws.SecretsManager.Login.SessionToken.Value(),
},
}
msg := fmt.Sprintf("Authenticating to AWS SecretsManager '%s' ... ", awsStore.Addr)
quiet.Print(msg)
if err := awsStore.Authenticate(); err != nil {
return nil, fmt.Errorf("failed to connect to AWS Secrets Manager: %v", err)
}
quiet.ClearMessage(msg)
return awsStore, nil
case config.KeyStore.GCP.SecretManager.ProjectID.Value() != "":
msg := fmt.Sprintf("Authenticating to GCP SecretManager Project: '%s' ... ", config.KeyStore.GCP.SecretManager.ProjectID.Value())
quiet.Print(msg)
gcpStore, err := gcp.Connect(context.Background(), &gcp.Config{
Endpoint: config.KeyStore.GCP.SecretManager.Endpoint.Value(),
ProjectID: config.KeyStore.GCP.SecretManager.ProjectID.Value(),
Credentials: gcp.Credentials{
ClientID: config.KeyStore.GCP.SecretManager.Credentials.ClientID.Value(),
Client: config.KeyStore.GCP.SecretManager.Credentials.Client.Value(),
KeyID: config.KeyStore.GCP.SecretManager.Credentials.KeyID.Value(),
Key: config.KeyStore.GCP.SecretManager.Credentials.Key.Value(),
},
ErrorLog: errorLog,
})
if err != nil {
return nil, fmt.Errorf("failed to connect to GCP SecretManager: %v", err)
}
quiet.ClearMessage(msg)
return gcpStore, nil
case config.KeyStore.Azure.KeyVault.Endpoint.Value() != "":
azureStore := &azure.KeyVault{
Endpoint: config.KeyStore.Azure.KeyVault.Endpoint.Value(),
ErrorLog: errorLog,
}
msg := fmt.Sprintf("Authenticating to Azure KeyVault '%s' ... ", config.KeyStore.Azure.KeyVault.Endpoint)
quiet.Print(msg)
switch c := config.KeyStore.Azure.KeyVault.Credentials; {
case c.TenantID.Value() != "" || c.ClientID.Value() != "" || c.Secret.Value() != "":
err := azureStore.AuthenticateWithCredentials(azure.Credentials{
TenantID: config.KeyStore.Azure.KeyVault.Credentials.TenantID.Value(),
ClientID: config.KeyStore.Azure.KeyVault.Credentials.ClientID.Value(),
Secret: config.KeyStore.Azure.KeyVault.Credentials.Secret.Value(),
})
if err != nil {
return nil, fmt.Errorf("failed to connect to Azure KeyVault: %v", err)
}
case config.KeyStore.Azure.KeyVault.ManagedIdentity.ClientID.Value() != "":
err := azureStore.AuthenticateWithIdentity(azure.ManagedIdentity{
ClientID: config.KeyStore.Azure.KeyVault.ManagedIdentity.ClientID.Value(),
})
if err != nil {
return nil, fmt.Errorf("failed to connect to Azure KeyVault: %v", err)
}
default:
return nil, errors.New("failed to connect to Azure KeyVault: no client credentials or managed identity")
}
quiet.ClearMessage(msg)
return azureStore, nil
case config.KeyStore.Gemalto.KeySecure.Endpoint.Value() != "":
gemaltoStore := &gemalto.KeySecure{
Endpoint: config.KeyStore.Gemalto.KeySecure.Endpoint.Value(),
CAPath: config.KeyStore.Gemalto.KeySecure.TLS.CAPath.Value(),
ErrorLog: errorLog,
Login: gemalto.Credentials{
Token: config.KeyStore.Gemalto.KeySecure.Login.Token.Value(),
Domain: config.KeyStore.Gemalto.KeySecure.Login.Domain.Value(),
Retry: config.KeyStore.Gemalto.KeySecure.Login.Retry.Value(),
},
}
msg := fmt.Sprintf("Authenticating to Gemalto KeySecure '%s' ... ", gemaltoStore.Endpoint)
quiet.Print(msg)
if err := gemaltoStore.Authenticate(); err != nil {
return nil, fmt.Errorf("failed to connect to Gemalto KeySecure: %v", err)
}
quiet.ClearMessage(msg)
return gemaltoStore, nil
default:
return &mem.Store{}, nil
}
}
func description(config *yml.ServerConfig) (kind, endpoint string, err error) {
switch {
case config.KeyStore.Fs.Path.Value() != "":
kind = "Filesystem"
if endpoint, err = filepath.Abs(config.KeyStore.Fs.Path.Value()); err != nil {
endpoint = config.KeyStore.Fs.Path.Value()
}
case config.KeyStore.Generic.Endpoint.Value() != "":
kind = "Generic"
endpoint = config.KeyStore.Generic.Endpoint.Value()
case config.KeyStore.Vault.Endpoint.Value() != "":
kind = "Hashicorp Vault"
endpoint = config.KeyStore.Vault.Endpoint.Value()
case config.KeyStore.Fortanix.SDKMS.Endpoint.Value() != "":
kind = "Fortanix SDKMS"
endpoint = config.KeyStore.Fortanix.SDKMS.Endpoint.Value()
case config.KeyStore.Aws.SecretsManager.Endpoint.Value() != "":
kind = "AWS SecretsManager"
endpoint = config.KeyStore.Aws.SecretsManager.Endpoint.Value()
case config.KeyStore.Gemalto.KeySecure.Endpoint.Value() != "":
kind = "Gemalto KeySecure"
endpoint = config.KeyStore.Gemalto.KeySecure.Endpoint.Value()
case config.KeyStore.GCP.SecretManager.ProjectID.Value() != "":
kind = "GCP SecretManager"
endpoint = config.KeyStore.GCP.SecretManager.Endpoint.Value() + " | Project: " + config.KeyStore.GCP.SecretManager.ProjectID.Value()
case config.KeyStore.Azure.KeyVault.Endpoint.Value() != "":
kind = "Azure KeyVault"
endpoint = config.KeyStore.Azure.KeyVault.Endpoint.Value()
default:
kind = "In-Memory"
endpoint = "non-persistent"
}
return kind, endpoint, nil
}
// policySetFromConfig returns an in-memory PolicySet
// from the given ServerConfig.
func policySetFromConfig(config *yml.ServerConfig) (auth.PolicySet, error) {
policies := &policySet{
policies: make(map[string]*auth.Policy),
}
for name, policy := range config.Policies {
if _, ok := policies.policies[name]; ok {
return nil, fmt.Errorf("policy %q already exists", name)
}
policies.policies[name] = &auth.Policy{
Allow: policy.Allow,
Deny: policy.Deny,
CreatedAt: time.Now().UTC(),
CreatedBy: config.Admin.Identity.Value(),
}
}
return policies, nil
}
type policySet struct {
lock sync.RWMutex
policies map[string]*auth.Policy
}
var _ auth.PolicySet = (*policySet)(nil) // compiler check
func (p *policySet) Set(_ context.Context, name string, policy *auth.Policy) error {
p.lock.Lock()
defer p.lock.Unlock()
p.policies[name] = policy
return nil
}
func (p *policySet) Get(_ context.Context, name string) (*auth.Policy, error) {
p.lock.RLock()
defer p.lock.RUnlock()
policy, ok := p.policies[name]
if !ok {
return nil, kes.ErrPolicyNotFound
}
return policy, nil
}
func (p *policySet) Delete(_ context.Context, name string) error {
p.lock.Lock()
defer p.lock.Unlock()
delete(p.policies, name)
return nil
}
func (p *policySet) List(_ context.Context) (auth.PolicyIterator, error) {
p.lock.RLock()
defer p.lock.RUnlock()
names := make([]string, 0, len(p.policies))
for name := range p.policies {
names = append(names, name)
}
return &policyIterator{
values: names,
}, nil
}
type policyIterator struct {
values []string
current string
}
var _ auth.PolicyIterator = (*policyIterator)(nil) // compiler check
func (i *policyIterator) Next() bool {
next := len(i.values) > 0
if next {
i.current = i.values[0]
}
return next
}
func (i *policyIterator) Name() string { return i.current }
func (i *policyIterator) Close() error { return nil }
// identitySetFromConfig returns an in-memory IdentitySet
// from the given ServerConfig.
func identitySetFromConfig(config *yml.ServerConfig) (auth.IdentitySet, error) {
identities := &identitySet{
admin: config.Admin.Identity.Value(),
roles: map[kes.Identity]auth.IdentityInfo{},
}
for name, policy := range config.Policies {
for _, id := range policy.Identities {
if id.Value().IsUnknown() {
continue
}
if id.Value() == config.Admin.Identity.Value() {
return nil, fmt.Errorf("identity %q is already an admin identity", id.Value())
}
if _, ok := identities.roles[id.Value()]; ok {
return nil, fmt.Errorf("identity %q is already assigned", id.Value())
}
for _, proxyID := range config.TLS.Proxy.Identities {
if id.Value() == proxyID.Value() {
return nil, fmt.Errorf("identity %q is already a TLS proxy identity", id.Value())
}
}
identities.roles[id.Value()] = auth.IdentityInfo{
Policy: name,
CreatedAt: time.Now().UTC(),
CreatedBy: config.Admin.Identity.Value(),
}
}
}
return identities, nil
}
type identitySet struct {
admin kes.Identity
lock sync.RWMutex
roles map[kes.Identity]auth.IdentityInfo
}
var _ auth.IdentitySet = (*identitySet)(nil) // compiler check
func (i *identitySet) Admin(ctx context.Context) (kes.Identity, error) { return i.admin, nil }
func (i *identitySet) Assign(_ context.Context, policy string, identity kes.Identity) error {
if i.admin == identity {
return kes.NewError(http.StatusBadRequest, "identity is root")
}
i.lock.Lock()
defer i.lock.Unlock()
i.roles[identity] = auth.IdentityInfo{
Policy: policy,
CreatedAt: time.Now().UTC(),
}
return nil
}
func (i *identitySet) Get(_ context.Context, identity kes.Identity) (auth.IdentityInfo, error) {
i.lock.RLock()
defer i.lock.RUnlock()
policy, ok := i.roles[identity]
if !ok {
return auth.IdentityInfo{}, kes.ErrNotAllowed
}
return policy, nil
}
func (i *identitySet) Delete(_ context.Context, identity kes.Identity) error {
i.lock.Lock()
defer i.lock.Unlock()
delete(i.roles, identity)
return nil
}
func (i *identitySet) List(_ context.Context) (auth.IdentityIterator, error) {
i.lock.RLock()
defer i.lock.RUnlock()
values := make([]kes.Identity, 0, len(i.roles))
for identity := range i.roles {
values = append(values, identity)
}
return &identityIterator{
values: values,
}, nil
}
type identityIterator struct {
values []kes.Identity
current kes.Identity
}
var _ auth.IdentityIterator = (*identityIterator)(nil) // compiler check
func (i *identityIterator) Next() bool {
next := len(i.values) > 0
if next {
i.current = i.values[0]
}
return next
}
func (i *identityIterator) Identity() kes.Identity { return i.current }
func (i *identityIterator) Close() error { return nil }