-
Notifications
You must be signed in to change notification settings - Fork 54
/
config.go
590 lines (489 loc) · 19.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/*******************************************************************************
* Copyright 2019 Dell Inc.
* Copyright 2022 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 config
import (
"context"
"errors"
"fmt"
"github.com/edgexfoundry/go-mod-bootstrap/v2/config"
"io/ioutil"
"math"
"reflect"
"sync"
"time"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/container"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/environment"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/flags"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/interfaces"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/secret"
"github.com/edgexfoundry/go-mod-bootstrap/v2/bootstrap/startup"
"github.com/edgexfoundry/go-mod-bootstrap/v2/di"
"github.com/edgexfoundry/go-mod-configuration/v2/configuration"
"github.com/edgexfoundry/go-mod-configuration/v2/pkg/types"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/pelletier/go-toml"
)
const (
writableKey = "/Writable"
ConfigVersion = "2.0"
)
// UpdatedStream defines the stream type that is notified by ListenForChanges when a configuration update is received.
type UpdatedStream chan struct{}
type Processor struct {
lc logger.LoggingClient
flags flags.Common
envVars *environment.Variables
startupTimer startup.Timer
ctx context.Context
wg *sync.WaitGroup
configUpdated UpdatedStream
dic *di.Container
overwriteConfig bool
}
// NewProcessor creates a new configuration Processor
func NewProcessor(
flags flags.Common,
envVars *environment.Variables,
startupTimer startup.Timer,
ctx context.Context,
wg *sync.WaitGroup,
configUpdated UpdatedStream,
dic *di.Container,
) *Processor {
return &Processor{
lc: container.LoggingClientFrom(dic.Get),
flags: flags,
envVars: envVars,
startupTimer: startupTimer,
ctx: ctx,
wg: wg,
configUpdated: configUpdated,
dic: dic,
}
}
func NewProcessorForCustomConfig(
flags flags.Common,
ctx context.Context,
wg *sync.WaitGroup,
dic *di.Container) *Processor {
return &Processor{
lc: container.LoggingClientFrom(dic.Get),
flags: flags,
ctx: ctx,
wg: wg,
dic: dic,
}
}
func (cp *Processor) Process(
serviceKey string,
configStem string,
serviceConfig interfaces.Configuration,
useSecretProvider bool) error {
// Create some shorthand for frequently used items
envVars := cp.envVars
cp.overwriteConfig = cp.flags.OverwriteConfig()
// Local configuration must be loaded first in case need registry config info and/or
// need to push it to the Configuration Provider.
if err := cp.loadFromFile(serviceConfig, "service"); err != nil {
return err
}
// Override file-based configuration with envVars variables.
// Variables variable overrides have precedence over all others,
// so make sure they are applied before config is used for anything.
overrideCount, err := envVars.OverrideConfiguration(serviceConfig)
if err != nil {
return err
}
// Now that configuration has been loaded from file and overrides applied,
// the Secret Provider can be initialized and added to the DIC, but only if it is configured to be used.
var secretProvider interfaces.SecretProvider
if useSecretProvider {
secretProvider, err = secret.NewSecretProvider(serviceConfig, cp.ctx, cp.startupTimer, cp.dic, serviceKey)
if err != nil {
return fmt.Errorf("failed to create SecretProvider: %s", err.Error())
}
}
configProviderUrl := cp.flags.ConfigProviderUrl()
// Create new ProviderInfo and initialize it from command-line flag or Variables
configProviderInfo, err := NewProviderInfo(cp.envVars, configProviderUrl)
if err != nil {
return err
}
switch configProviderInfo.UseProvider() {
case true:
var accessToken string
var getAccessToken types.GetAccessTokenCallback
// secretProvider will be nil if not configured to be used. In that case, no access token required.
if secretProvider != nil {
// Define the callback function to retrieve the Access Token
getAccessToken = func() (string, error) {
accessToken, err = secretProvider.GetAccessToken(configProviderInfo.serviceConfig.Type, serviceKey)
if err != nil {
return "", fmt.Errorf(
"failed to get Configuration Provider (%s) access token: %s",
configProviderInfo.serviceConfig.Type,
err.Error())
}
cp.lc.Infof("Using Configuration Provider access token of length %d", len(accessToken))
return accessToken, nil
}
} else {
cp.lc.Info("Not configured to use Config Provider access token")
}
configClient, err := cp.createProviderClient(serviceKey, configStem, getAccessToken, configProviderInfo.ServiceConfig())
if err != nil {
return fmt.Errorf("failed to create Configuration Provider client: %s", err.Error())
}
for cp.startupTimer.HasNotElapsed() {
if err := cp.processWithProvider(
configClient,
serviceConfig,
overrideCount,
); err != nil {
cp.lc.Error(err.Error())
select {
case <-cp.ctx.Done():
return errors.New("aborted Updating to/from Configuration Provider")
default:
cp.startupTimer.SleepForInterval()
continue
}
}
break
}
cp.listenForChanges(serviceConfig, configClient)
cp.dic.Update(di.ServiceConstructorMap{
container.ConfigClientInterfaceName: func(get di.Get) interface{} {
return configClient
},
})
case false:
cp.logConfigInfo("Using local configuration from file", overrideCount)
}
// Now that configuration has been loaded and overrides applied the log level can be set as configured.
err = cp.lc.SetLogLevel(serviceConfig.GetLogLevel())
return err
}
// LoadCustomConfigSection loads the specified custom configuration section from file or Configuration provider.
// Section will be seed if Configuration provider does yet have it. This is used for structures custom configuration
// in App and Device services
func (cp *Processor) LoadCustomConfigSection(config interfaces.UpdatableConfig, sectionName string) error {
var overrideCount = -1
var err error
source := "file"
if cp.envVars == nil {
cp.envVars = environment.NewVariables(cp.lc)
}
configClient := container.ConfigClientFrom(cp.dic.Get)
if configClient == nil {
cp.lc.Info("Skipping use of Configuration Provider for custom configuration: Provider not available")
if err := cp.loadFromFile(config, "custom"); err != nil {
return err
}
} else {
cp.lc.Infof("Checking if custom configuration ('%s') exists in Configuration Provider", sectionName)
exists, err := configClient.HasSubConfiguration(sectionName)
if err != nil {
return fmt.Errorf(
"unable to determine if custom configuration exists in Configuration Provider: %s",
err.Error())
}
if exists && !cp.flags.OverwriteConfig() {
source = "Configuration Provider"
rawConfig, err := configClient.GetConfiguration(config)
if err != nil {
return fmt.Errorf(
"unable to get custom configuration from Configuration Provider: %s",
err.Error())
}
if ok := config.UpdateFromRaw(rawConfig); !ok {
return fmt.Errorf("unable to update custom configuration from Configuration Provider")
}
} else {
if err := cp.loadFromFile(config, "custom"); err != nil {
return err
}
// Must apply override before pushing into Configuration Provider
overrideCount, err = cp.envVars.OverrideConfiguration(config)
if err != nil {
return fmt.Errorf("unable to apply environment overrides: %s", err.Error())
}
err = configClient.PutConfiguration(reflect.ValueOf(config).Elem().Interface(), true)
if err != nil {
return fmt.Errorf("error pushing custom config to Configuration Provider: %s", err.Error())
}
var overwriteMessage = ""
if exists && cp.flags.OverwriteConfig() {
overwriteMessage = "(overwritten)"
}
cp.lc.Infof("Custom Config loaded from file and pushed to Configuration Provider %s", overwriteMessage)
}
}
// Still need to apply overrides if only loaded from file or only loaded from Configuration Provider,
// i.e. Did Not load from file and push to Configuration Provider
if overrideCount == -1 {
overrideCount, err = cp.envVars.OverrideConfiguration(config)
if err != nil {
return fmt.Errorf("unable to apply environment overrides: %s", err.Error())
}
}
cp.lc.Infof("Loaded custom configuration from %s (%d envVars overrides applied)", source, overrideCount)
return nil
}
// ListenForCustomConfigChanges listens for changes to the specified custom configuration section. When changes occur it
// applies the changes to the custom configuration section and signals the the changes have occurred.
func (cp *Processor) ListenForCustomConfigChanges(
configToWatch interface{},
sectionName string,
changedCallback func(interface{})) {
configClient := container.ConfigClientFrom(cp.dic.Get)
if configClient == nil {
cp.lc.Warnf("unable to watch custom configuration for changes: Configuration Provider not enabled")
return
}
cp.wg.Add(1)
go func() {
defer cp.wg.Done()
errorStream := make(chan error)
defer close(errorStream)
updateStream := make(chan interface{})
defer close(updateStream)
configClient.WatchForChanges(updateStream, errorStream, configToWatch, sectionName)
isFirstUpdate := true
for {
select {
case <-cp.ctx.Done():
configClient.StopWatching()
cp.lc.Infof("Watching for '%s' configuration changes has stopped", sectionName)
return
case ex := <-errorStream:
cp.lc.Error(ex.Error())
case raw := <-updateStream:
// Config Provider sends an update as soon as the watcher is connected even though there are not
// any changes to the configuration. This causes an issue during start-up if there is an
// envVars override of one of the Writable fields, so we must ignore the first update.
if isFirstUpdate {
isFirstUpdate = false
continue
}
cp.lc.Infof("Updated custom configuration '%s' has been received from the Configuration Provider", sectionName)
changedCallback(raw)
}
}
}()
cp.lc.Infof("Watching for custom configuration changes has started for `%s`", sectionName)
}
// createProviderClient creates and returns a configuration.Client instance and logs Client connection information
func (cp *Processor) createProviderClient(
serviceKey string,
configStem string,
getAccessToken types.GetAccessTokenCallback,
providerConfig types.ServiceConfig) (configuration.Client, error) {
var err error
// The passed in configStem already contains the trailing '/' in most cases so must verify and add if missing.
if configStem[len(configStem)-1] != '/' {
configStem = configStem + "/"
}
// Note: Can't use filepath.Join as it uses `\` on Windows which Consul doesn't recognize as a path separator.
providerConfig.BasePath = fmt.Sprintf("%s%s/%s", configStem, ConfigVersion, serviceKey)
if getAccessToken != nil {
providerConfig.AccessToken, err = getAccessToken()
if err != nil {
return nil, err
}
providerConfig.GetAccessToken = getAccessToken
}
cp.lc.Info(fmt.Sprintf(
"Using Configuration provider (%s) from: %s with base path of %s",
providerConfig.Type,
providerConfig.GetUrl(),
providerConfig.BasePath))
return configuration.NewConfigurationClient(providerConfig)
}
// LoadFromFile attempts to read and unmarshal toml-based configuration into a configuration struct.
func (cp *Processor) loadFromFile(config interface{}, configType string) error {
configDir := environment.GetConfDir(cp.lc, cp.flags.ConfigDirectory())
profileDir := environment.GetProfileDir(cp.lc, cp.flags.Profile())
configFileName := environment.GetConfigFileName(cp.lc, cp.flags.ConfigFileName())
filePath := configDir + "/" + profileDir + configFileName
contents, err := ioutil.ReadFile(filePath)
if err != nil {
return fmt.Errorf("could not load %s configuration file (%s): %s", configType, filePath, err.Error())
}
if err = toml.Unmarshal(contents, config); err != nil {
return fmt.Errorf("could not load %s configuration file (%s): %s", configType, filePath, err.Error())
}
cp.lc.Info(fmt.Sprintf("Loaded %s configuration from %s", configType, filePath))
return nil
}
// ProcessWithProvider puts configuration if doesnt exist in provider (i.e. self-seed) or
// gets configuration from provider and updates the service's configuration with envVars overrides after receiving
// them from the provider so that envVars override supersede any changes made in the provider.
func (cp *Processor) processWithProvider(
configClient configuration.Client,
serviceConfig interfaces.Configuration,
overrideCount int) error {
if !configClient.IsAlive() {
return errors.New("configuration provider is not available")
}
hasConfig, err := configClient.HasConfiguration()
if err != nil {
return fmt.Errorf("could not determine if Configuration provider has configuration: %s", err.Error())
}
if !hasConfig || cp.overwriteConfig {
// Variables overrides already applied previously so just push to Configuration Provider
// Note that serviceConfig is a pointer, so we have to use reflection to dereference it.
err = configClient.PutConfiguration(reflect.ValueOf(serviceConfig).Elem().Interface(), true)
if err != nil {
return fmt.Errorf("could not push configuration into Configuration Provider: %s", err.Error())
}
cp.logConfigInfo("Configuration has been pushed to into Configuration Provider", overrideCount)
} else {
rawConfig, err := configClient.GetConfiguration(serviceConfig)
if err != nil {
return fmt.Errorf("could not get configuration from Configuration provider: %s", err.Error())
}
if !serviceConfig.UpdateFromRaw(rawConfig) {
return errors.New("configuration from Configuration provider failed type check")
}
overrideCount, err := cp.envVars.OverrideConfiguration(serviceConfig)
if err != nil {
return err
}
cp.logConfigInfo("Configuration has been pulled from Configuration provider", overrideCount)
}
return nil
}
// listenForChanges leverages the Configuration Provider client's WatchForChanges() method to receive changes to and update the
// service's configuration writable sub-struct. It's assumed the log level is universally part of the
// writable struct and this function explicitly updates the loggingClient's log level when new configuration changes
// are received.
func (cp *Processor) listenForChanges(serviceConfig interfaces.Configuration, configClient configuration.Client) {
lc := cp.lc
isFirstUpdate := true
cp.wg.Add(1)
go func() {
defer cp.wg.Done()
errorStream := make(chan error)
defer close(errorStream)
updateStream := make(chan interface{})
defer close(updateStream)
go configClient.WatchForChanges(updateStream, errorStream, serviceConfig.EmptyWritablePtr(), writableKey)
for {
select {
case <-cp.ctx.Done():
configClient.StopWatching()
lc.Infof("Watching for '%s' configuration changes has stopped", writableKey)
return
case ex := <-errorStream:
lc.Errorf("error occurred during listening to the configuration changes: %s", ex.Error())
case raw, ok := <-updateStream:
if !ok {
return
}
// Config Provider sends an update as soon as the watcher is connected even though there are not
// any changes to the configuration. This causes an issue during start-up if there is an
// envVars override of one of the Writable fields, so we must ignore the first update.
if isFirstUpdate {
isFirstUpdate = false
continue
}
previousInsecureSecrets := serviceConfig.GetInsecureSecrets()
previousLogLevel := serviceConfig.GetLogLevel()
previousTelemetryInterval := serviceConfig.GetTelemetryInfo().Interval
if !serviceConfig.UpdateWritableFromRaw(raw) {
lc.Error("ListenForChanges() type check failed")
return
}
currentInsecureSecrets := serviceConfig.GetInsecureSecrets()
currentLogLevel := serviceConfig.GetLogLevel()
currentTelemetryInterval := serviceConfig.GetTelemetryInfo().Interval
lc.Info("Writeable configuration has been updated from the Configuration Provider")
// Note: Updates occur one setting at a time so only have to look for single changes
switch {
case currentLogLevel != previousLogLevel:
_ = lc.SetLogLevel(serviceConfig.GetLogLevel())
lc.Info(fmt.Sprintf("Logging level changed to %s", currentLogLevel))
// InsecureSecrets (map) will be nil if not in the original TOML used to seed the Config Provider,
// so ignore it if this is the case.
case currentInsecureSecrets != nil &&
!reflect.DeepEqual(currentInsecureSecrets, previousInsecureSecrets):
lc.Info("Insecure Secrets have been updated")
secretProvider := container.SecretProviderFrom(cp.dic.Get)
if secretProvider != nil {
// Find the updated secret's path and perform call backs.
updatedSecrets := getSecretPathsChanged(previousInsecureSecrets, currentInsecureSecrets)
for _, v := range updatedSecrets {
secretProvider.SecretUpdatedAtPath(v)
}
}
case currentTelemetryInterval != previousTelemetryInterval:
lc.Info("Telemetry interval has been updated. Processing new value...")
interval, err := time.ParseDuration(currentTelemetryInterval)
if err != nil {
lc.Errorf("update telemetry interval value is invalid time duration, using previous value: %s", err.Error())
break
}
if interval == 0 {
lc.Infof("0 specified for metrics reporting interval. Setting to max duration to effectively disable reporting.")
interval = math.MaxInt64
}
metricsManager := container.MetricsManagerFrom(cp.dic.Get)
if metricsManager == nil {
lc.Error("metrics manager not available while updating telemetry interval")
break
}
metricsManager.ResetInterval(interval)
default:
// Signal that configuration updates exists that have not already been processed.
if cp.configUpdated != nil {
cp.configUpdated <- struct{}{}
}
}
}
}
}()
}
// logConfigInfo logs the config info message with number over overrides that occurred.
func (cp *Processor) logConfigInfo(message string, overrideCount int) {
cp.lc.Infof("%s (%d envVars overrides applied)", message, overrideCount)
}
// getSecretPathsChanged returns a slice of paths that have changed secrets or are new.
func getSecretPathsChanged(prevVals config.InsecureSecrets, curVals config.InsecureSecrets) []string {
var updatedPaths []string
for key, prevVal := range prevVals {
curVal := curVals[key]
// Catches removed secrets
if curVal.Secrets == nil {
updatedPaths = append(updatedPaths, prevVal.Path)
continue
}
// Catches changes to secret data or to the path name
if !reflect.DeepEqual(prevVal, curVal) {
updatedPaths = append(updatedPaths, curVal.Path)
// Catches path name changes, so also include the previous path
if prevVal.Path != curVal.Path {
updatedPaths = append(updatedPaths, prevVal.Path)
}
}
}
for key, curVal := range curVals {
// Catches new secrets added
if prevVals[key].Secrets == nil {
updatedPaths = append(updatedPaths, curVal.Path)
}
}
return updatedPaths
}