Skip to content

Commit

Permalink
feat: Use loadfile that allows reading from local file or uri
Browse files Browse the repository at this point in the history
close edgexfoundry#553

Signed-off-by: Brian McGinn <brian.mcginn@intel.com>
  • Loading branch information
brian-intel committed Jun 30, 2023
1 parent d15a138 commit 195fdb8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions bootstrap/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ import (
"errors"
"fmt"
"math"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync"
"time"

"github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/file"
"github.com/edgexfoundry/go-mod-bootstrap/v3/bootstrap/utils"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/mitchellh/copystructure"
Expand Down Expand Up @@ -62,6 +62,8 @@ const (
// UpdatedStream defines the stream type that is notified by ListenForChanges when a configuration update is received.
type UpdatedStream chan struct{}

var defaultTimeout = 15 * time.Second

type Processor struct {
lc logger.LoggingClient
flags flags.Common
Expand Down Expand Up @@ -197,7 +199,7 @@ func (cp *Processor) Process(
// NOTE: Some security services don't use any common configuration and don't use the configuration provider.
commonConfigLocation := environment.GetCommonConfigFileName(cp.lc, cp.flags.CommonConfig())
if commonConfigLocation != "" {
err := cp.loadCommonConfigFromFile(commonConfigLocation, serviceConfig, serviceType)
err := cp.loadCommonConfigFromFile(commonConfigLocation, serviceConfig, serviceType, secretProvider)
if err != nil {
return err
}
Expand All @@ -213,7 +215,7 @@ func (cp *Processor) Process(
// Now load the private config from a local file if any of these conditions are true
if !useProvider || !cp.providerHasConfig || cp.overwriteConfig {
filePath := GetConfigFileLocation(cp.lc, cp.flags)
configMap, err := cp.loadConfigYamlFromFile(filePath)
configMap, err := cp.loadConfigYamlFromFile(filePath, defaultTimeout, secretProvider)
if err != nil {
return err
}
Expand Down Expand Up @@ -393,11 +395,12 @@ func (cp *Processor) loadCommonConfig(
func (cp *Processor) loadCommonConfigFromFile(
configFile string,
serviceConfig interfaces.Configuration,
serviceType string) error {
serviceType string,
secretProvider interfaces.SecretProviderExt) error {

var err error

commonConfig, err := cp.loadConfigYamlFromFile(configFile)
commonConfig, err := cp.loadConfigYamlFromFile(configFile, defaultTimeout, secretProvider)
if err != nil {
return err
}
Expand Down Expand Up @@ -465,7 +468,7 @@ func (cp *Processor) getAccessTokenCallback(serviceKey string, secretProvider in
// 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(updatableConfig interfaces.UpdatableConfig, sectionName string) error {
func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.UpdatableConfig, sectionName string, secretProvider interfaces.SecretProviderExt) error {
if cp.envVars == nil {
cp.envVars = environment.NewVariables(cp.lc)
}
Expand All @@ -474,7 +477,7 @@ func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.Updatabl
if configClient == nil {
cp.lc.Info("Skipping use of Configuration Provider for custom configuration: Provider not available")
filePath := GetConfigFileLocation(cp.lc, cp.flags)
configMap, err := cp.loadConfigYamlFromFile(filePath)
configMap, err := cp.loadConfigYamlFromFile(filePath, defaultTimeout, secretProvider)
if err != nil {
return err
}
Expand Down Expand Up @@ -508,7 +511,7 @@ func (cp *Processor) LoadCustomConfigSection(updatableConfig interfaces.Updatabl
cp.lc.Info("Loaded custom configuration from Configuration Provider, no overrides applied")
} else {
filePath := GetConfigFileLocation(cp.lc, cp.flags)
configMap, err := cp.loadConfigYamlFromFile(filePath)
configMap, err := cp.loadConfigYamlFromFile(filePath, defaultTimeout, secretProvider)
if err != nil {
return err
}
Expand Down Expand Up @@ -636,9 +639,9 @@ func CreateProviderClient(
}

// loadConfigYamlFromFile attempts to read the specified configuration yaml file
func (cp *Processor) loadConfigYamlFromFile(yamlFile string) (map[string]any, error) {
func (cp *Processor) loadConfigYamlFromFile(yamlFile string, timeout time.Duration, provider interfaces.SecretProvider) (map[string]any, error) {
cp.lc.Infof("Loading configuration file from %s", yamlFile)
contents, err := os.ReadFile(yamlFile)
contents, err := file.Load(yamlFile, timeout, provider)
if err != nil {
return nil, fmt.Errorf("failed to read configuration file %s: %s", yamlFile, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestLoadCommonConfigFromFile(t *testing.T) {
proc := NewProcessor(f, env, timer, ctx, &wg, nil, dic)

// call load common config
err := proc.loadCommonConfigFromFile(tc.config, tc.serviceConfig, tc.serviceType)
err := proc.loadCommonConfigFromFile(tc.config, tc.serviceConfig, tc.serviceType, nil)
// make assertions
require.NotNil(t, cancel)
if tc.expectedErr == "" {
Expand Down

0 comments on commit 195fdb8

Please sign in to comment.