-
Notifications
You must be signed in to change notification settings - Fork 54
/
secure.go
456 lines (369 loc) · 14.3 KB
/
secure.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*******************************************************************************
* Copyright 2018 Dell Inc.
* Copyright 2020 Intel Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package secret
import (
"context"
"errors"
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/edgexfoundry/go-mod-secrets/v2/pkg"
gometrics "github.com/rcrowley/go-metrics"
"github.com/edgexfoundry/go-mod-core-contracts/v2/dtos/common"
"github.com/hashicorp/go-multierror"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-secrets/v2/pkg/token/authtokenloader"
"github.com/edgexfoundry/go-mod-secrets/v2/pkg/token/runtimetokenprovider"
"github.com/edgexfoundry/go-mod-secrets/v2/secrets"
)
const (
TokenTypeConsul = "consul"
AccessTokenAuthError = "HTTP response with status code 403"
//nolint: gosec
SecretsAuthError = "Received a '403' response"
)
// SecureProvider implements the SecretProvider interface
type SecureProvider struct {
secretClient secrets.SecretClient
lc logger.LoggingClient
loader authtokenloader.AuthTokenLoader
// runtimeTokenProvider is for delayed start services
runtimeTokenProvider runtimetokenprovider.RuntimeTokenProvider
serviceKey string
configuration interfaces.Configuration
secretsCache map[string]map[string]string // secret's path, key, value
cacheMutex *sync.RWMutex
lastUpdated time.Time
ctx context.Context
registeredSecretCallbacks map[string]func(path string)
securitySecretsRequested gometrics.Counter
securitySecretsStored gometrics.Counter
securityConsulTokensRequested gometrics.Counter
securityConsulTokenDuration gometrics.Timer
}
// NewSecureProvider creates & initializes Provider instance for secure secrets.
func NewSecureProvider(ctx context.Context, config interfaces.Configuration, lc logger.LoggingClient,
loader authtokenloader.AuthTokenLoader, runtimeTokenLoader runtimetokenprovider.RuntimeTokenProvider,
serviceKey string) *SecureProvider {
provider := &SecureProvider{
configuration: config,
lc: lc,
loader: loader,
runtimeTokenProvider: runtimeTokenLoader,
serviceKey: serviceKey,
secretsCache: make(map[string]map[string]string),
cacheMutex: &sync.RWMutex{},
lastUpdated: time.Now(),
ctx: ctx,
registeredSecretCallbacks: make(map[string]func(path string)),
securitySecretsRequested: gometrics.NewCounter(),
securitySecretsStored: gometrics.NewCounter(),
securityConsulTokensRequested: gometrics.NewCounter(),
securityConsulTokenDuration: gometrics.NewTimer(),
}
return provider
}
// SetClient sets the secret client that is used to access the secure secrets
func (p *SecureProvider) SetClient(client secrets.SecretClient) {
p.secretClient = client
}
// GetSecret retrieves secrets from a secret store.
// path specifies the type or location of the secrets to retrieve.
// keys specifies the secrets which to retrieve. If no keys are provided then all the keys associated with the
// specified path will be returned.
func (p *SecureProvider) GetSecret(path string, keys ...string) (map[string]string, error) {
p.securitySecretsRequested.Inc(1)
if cachedSecrets := p.getSecretsCache(path, keys...); cachedSecrets != nil {
return cachedSecrets, nil
}
if p.secretClient == nil {
return nil, errors.New("can't get secrets. Secure secret provider is not properly initialized")
}
secureSecrets, err := p.secretClient.GetSecrets(path, keys...)
retry, err := p.reloadTokenOnAuthError(err)
if retry {
// Retry with potential new token
secureSecrets, err = p.secretClient.GetSecrets(path, keys...)
}
if err != nil {
return nil, err
}
p.updateSecretsCache(path, secureSecrets)
return secureSecrets, nil
}
func (p *SecureProvider) getSecretsCache(path string, keys ...string) map[string]string {
secureSecrets := make(map[string]string)
// Synchronize cache access
p.cacheMutex.RLock()
defer p.cacheMutex.RUnlock()
// check cache for keys
allKeysExistInCache := false
cachedSecrets, cacheExists := p.secretsCache[path]
value := ""
if cacheExists {
for _, key := range keys {
value, allKeysExistInCache = cachedSecrets[key]
if !allKeysExistInCache {
return nil
}
secureSecrets[key] = value
}
// return secureSecrets if the requested keys exist in cache
if allKeysExistInCache {
return secureSecrets
}
}
return nil
}
func (p *SecureProvider) updateSecretsCache(path string, secrets map[string]string) {
// Synchronize cache access
p.cacheMutex.Lock()
defer p.cacheMutex.Unlock()
if _, cacheExists := p.secretsCache[path]; !cacheExists {
p.secretsCache[path] = secrets
}
for key, value := range secrets {
p.secretsCache[path][key] = value
}
}
// StoreSecret stores the secrets to a secret store.
// it sets the values requested at provided keys
// path specifies the type or location of the secrets to store
// secrets map specifies the "key": "value" pairs of secrets to store
func (p *SecureProvider) StoreSecret(path string, secrets map[string]string) error {
p.securitySecretsStored.Inc(1)
if p.secretClient == nil {
return errors.New("can't store secrets. Secure secret provider is not properly initialized")
}
err := p.secretClient.StoreSecrets(path, secrets)
retry, err := p.reloadTokenOnAuthError(err)
if retry {
// Retry with potential new token
err = p.secretClient.StoreSecrets(path, secrets)
}
if err != nil {
return err
}
// Execute Callbacks on registered secret paths.
p.SecretUpdatedAtPath(path)
// Synchronize cache access before clearing
p.cacheMutex.Lock()
// Clearing cache because adding a new secret(p) possibly invalidates the previous cache
p.secretsCache = make(map[string]map[string]string)
p.cacheMutex.Unlock()
//indicate to the SDK that the cache has been invalidated
p.lastUpdated = time.Now()
return nil
}
func (p *SecureProvider) reloadTokenOnAuthError(err error) (bool, error) {
if err == nil {
return false, nil
}
if !strings.Contains(err.Error(), SecretsAuthError) &&
!strings.Contains(err.Error(), AccessTokenAuthError) {
return false, err
}
// Reload token in case new token was created causing the auth error
token, err := p.loader.Load(p.configuration.GetBootstrap().SecretStore.TokenFile)
if err != nil {
return false, err
}
err = p.secretClient.SetAuthToken(p.ctx, token)
if err != nil {
return false, err
}
return true, nil
}
// SecretsUpdated is not need for secure secrets as this is handled when secrets are stored.
func (p *SecureProvider) SecretsUpdated() {
// Do nothing
}
// SecretsLastUpdated returns the last time secure secrets were updated
func (p *SecureProvider) SecretsLastUpdated() time.Time {
return p.lastUpdated
}
// GetAccessToken returns the access token for the requested token type.
func (p *SecureProvider) GetAccessToken(tokenType string, serviceKey string) (string, error) {
p.securityConsulTokensRequested.Inc(1)
started := time.Now()
defer p.securityConsulTokenDuration.UpdateSince(started)
switch tokenType {
case TokenTypeConsul:
token, err := p.secretClient.GenerateConsulToken(serviceKey)
retry, err := p.reloadTokenOnAuthError(err)
if retry {
// Retry with potential new token
token, err = p.secretClient.GenerateConsulToken(serviceKey)
}
if err != nil {
return "", err
}
return token, nil
default:
return "", fmt.Errorf("invalid access token type '%s'", tokenType)
}
}
// DefaultTokenExpiredCallback is the default implementation of tokenExpiredCallback function
// It utilizes the tokenFile to re-read the token and enable retry if any update from the expired token
func (p *SecureProvider) DefaultTokenExpiredCallback(expiredToken string) (replacementToken string, retry bool) {
tokenFile := p.configuration.GetBootstrap().SecretStore.TokenFile
// during the callback, we want to re-read the token from the disk
// specified by tokenFile and set the retry to true if a new token
// is different from the expiredToken
reReadToken, err := p.loader.Load(tokenFile)
if err != nil {
p.lc.Error(fmt.Sprintf("fail to load auth token from tokenFile %s: %v", tokenFile, err))
return "", false
}
if reReadToken == expiredToken {
p.lc.Error("No new replacement token found for the expired token")
return reReadToken, false
}
return reReadToken, true
}
func (p *SecureProvider) RuntimeTokenExpiredCallback(expiredToken string) (replacementToken string, retry bool) {
newToken, err := p.runtimeTokenProvider.GetRawToken(p.serviceKey)
if err != nil {
p.lc.Errorf("failed to get a new token for service: %s: %v", p.serviceKey, err)
return "", false
}
return newToken, true
}
// LoadServiceSecrets loads the service secrets from the specified file and stores them in the service's SecretStore
func (p *SecureProvider) LoadServiceSecrets(secretStoreConfig config.SecretStoreInfo) error {
contents, err := os.ReadFile(secretStoreConfig.SecretsFile)
if err != nil {
return fmt.Errorf("seeding secrets failed: %s", err.Error())
}
data, seedingErrs := p.seedSecrets(contents)
if secretStoreConfig.DisableScrubSecretsFile {
p.lc.Infof("Scrubbing of secrets file disable.")
return seedingErrs
}
if err := os.WriteFile(secretStoreConfig.SecretsFile, data, 0); err != nil {
return fmt.Errorf("seeding secrets failed: unable to overwrite file with secret data removed: %s", err.Error())
}
p.lc.Infof("Scrubbing of secrets file complete.")
return seedingErrs
}
func (p *SecureProvider) seedSecrets(contents []byte) ([]byte, error) {
serviceSecrets, err := UnmarshalServiceSecretsJson(contents)
if err != nil {
return nil, fmt.Errorf("seeding secrets failed unmarshaling JSON: %s", err.Error())
}
p.lc.Infof("Seeding %d Service Secrets", len(serviceSecrets.Secrets))
var seedingErrs error
for index, secret := range serviceSecrets.Secrets {
if secret.Imported {
p.lc.Infof("Secret for '%s' already imported. Skipping...", secret.Path)
continue
}
// At this pint the JSON validation and above check cover all the required validation, so go to store secret.
path, data := prepareSecret(secret)
err := p.StoreSecret(path, data)
if err != nil {
message := fmt.Sprintf("failed to store secret for '%s': %s", secret.Path, err.Error())
p.lc.Errorf(message)
seedingErrs = multierror.Append(seedingErrs, errors.New(message))
continue
}
p.lc.Infof("Secret for '%s' successfully stored.", secret.Path)
serviceSecrets.Secrets[index].Imported = true
serviceSecrets.Secrets[index].SecretData = make([]common.SecretDataKeyValue, 0)
}
// Now need to write the file back over with the imported secrets' secretData removed.
data, err := serviceSecrets.MarshalJson()
if err != nil {
return nil, fmt.Errorf("seeding secrets failed marshaling back to JSON to clear secrets: %s", err.Error())
}
return data, seedingErrs
}
func prepareSecret(secret ServiceSecret) (string, map[string]string) {
var secretsKV = make(map[string]string)
for _, secret := range secret.SecretData {
secretsKV[secret.Key] = secret.Value
}
path := strings.TrimSpace(secret.Path)
return path, secretsKV
}
// HasSecret returns true if the service's SecretStore contains a secret at the specified path.
func (p *SecureProvider) HasSecret(path string) (bool, error) {
_, err := p.GetSecret(path)
if err != nil {
_, ok := err.(pkg.ErrPathNotFound)
if ok {
return false, nil
}
return false, err
}
return true, nil
}
// ListSecretPaths returns a list of paths for the current service from an insecure/secure secret store.
func (p *SecureProvider) ListSecretPaths() ([]string, error) {
if p.secretClient == nil {
return nil, errors.New("can't get secret paths. Secure secret provider is not properly initialized")
}
secureSecrets, err := p.secretClient.GetKeys("")
retry, err := p.reloadTokenOnAuthError(err)
if retry {
// Retry with potential new token
secureSecrets, err = p.secretClient.GetKeys("")
}
if err != nil {
return nil, fmt.Errorf("unable to get secret paths: %v", err)
}
return secureSecrets, nil
}
// RegisteredSecretUpdatedCallback registers a callback for a secret.
func (p *SecureProvider) RegisteredSecretUpdatedCallback(path string, callback func(path string)) error {
if _, ok := p.registeredSecretCallbacks[path]; ok {
return fmt.Errorf("there is a callback already registered for path '%v'", path)
}
// Register new call back for path.
p.registeredSecretCallbacks[path] = callback
return nil
}
// SecretUpdatedAtPath performs updates and callbacks for an updated secret or path.
func (p *SecureProvider) SecretUpdatedAtPath(path string) {
p.lastUpdated = time.Now()
if p.registeredSecretCallbacks != nil {
// Execute Callback for provided path.
for k, v := range p.registeredSecretCallbacks {
if k == path {
p.lc.Debugf("invoking callback registered for path: '%s'", path)
v(path)
return
}
}
}
}
// DeregisterSecretUpdatedCallback removes a secret's registered callback path.
func (p *SecureProvider) DeregisterSecretUpdatedCallback(path string) {
// Remove path from map.
delete(p.registeredSecretCallbacks, path)
}
// GetMetricsToRegister returns all metric objects that needs to be registered.
func (p *SecureProvider) GetMetricsToRegister() map[string]interface{} {
return map[string]interface{}{
secretsRequestedMetricName: p.securitySecretsRequested,
secretsStoredMetricName: p.securitySecretsStored,
securityConsulTokensRequestedName: p.securityConsulTokensRequested,
securityConsulTokenDurationName: p.securityConsulTokenDuration,
}
}