Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cfg: added check for key prefixes; #1051

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/cfg/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Config interface {
GetStringSlice(key string, optionalDefault ...[]string) []string
GetTime(key string, optionalDefault ...time.Time) time.Time
IsSet(string) bool
HasPrefix(prefix string) bool
UnmarshalDefaults(val interface{}, additionalDefaults ...UnmarshalDefaults)
UnmarshalKey(key string, val interface{}, additionalDefaults ...UnmarshalDefaults)
}
Expand Down Expand Up @@ -297,6 +298,16 @@ func (c *config) IsSet(key string) bool {
return c.isSet(key)
}

func (c *config) HasPrefix(prefix string) bool {
envPrefix := c.resolveEnvKey(c.envKeyPrefix, prefix)

if c.envProvider.PrefixExists(envPrefix) {
return true
}

return c.IsSet(prefix)
}

func (c *config) Option(options ...Option) error {
for _, opt := range options {
if err := opt(c); err != nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/cfg/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,31 @@ func (s *ConfigTestSuite) TestConfig_IsSet() {
s.False(s.config.IsSet("missing"))
}

func (s *ConfigTestSuite) TestConfig_HasPrefix() {
s.setupConfigValues(map[string]interface{}{
"i": 1,
"ms": map[string]interface{}{
"b": true,
},
})

s.True(s.config.HasPrefix("i"))
s.False(s.config.HasPrefix("k"))
s.True(s.config.HasPrefix("ms"))
s.True(s.config.HasPrefix("ms.b"))
s.False(s.config.HasPrefix("ms.b.c"))

s.setupEnvironment(map[string]string{
"FOO": "bar",
"NESTED_KEY_A": "1",
})

s.True(s.config.HasPrefix("foo"))
s.False(s.config.HasPrefix("baz"))
s.True(s.config.HasPrefix("nested.key"))
s.False(s.config.HasPrefix("nested.keys"))
}

func (s *ConfigTestSuite) TestConfig_Get() {
s.setupConfigValues(map[string]interface{}{
"i": 1,
Expand Down
32 changes: 30 additions & 2 deletions pkg/cfg/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package cfg

import (
"os"
"strings"

"github.com/justtrackio/gosoline/pkg/funk"
)

type EnvProvider interface {
LookupEnv(key string) (string, bool)
PrefixExists(prefix string) bool
SetEnv(key string, value string) error
}

Expand All @@ -20,6 +24,20 @@ func (o *osEnvProvider) LookupEnv(key string) (string, bool) {
return os.LookupEnv(key)
}

func (o *osEnvProvider) PrefixExists(prefix string) bool {
envs := os.Environ()

for _, env := range envs {
key, _, _ := strings.Cut(env, "=")

if strings.HasPrefix(key, prefix) {
return true
}
}

return false
}

func (o *osEnvProvider) SetEnv(key string, value string) error {
return os.Setenv(key, value)
}
Expand All @@ -28,9 +46,9 @@ type memoryEnvProvider struct {
values map[string]string
}

func NewMemoryEnvProvider() *memoryEnvProvider {
func NewMemoryEnvProvider(initialValues ...map[string]string) *memoryEnvProvider {
return &memoryEnvProvider{
values: make(map[string]string),
values: funk.MergeMaps(initialValues...),
}
}

Expand All @@ -40,6 +58,16 @@ func (m *memoryEnvProvider) LookupEnv(key string) (string, bool) {
return val, ok
}

func (o *memoryEnvProvider) PrefixExists(prefix string) bool {
for key := range o.values {
if strings.HasPrefix(key, prefix) {
return true
}
}

return false
}

func (m *memoryEnvProvider) SetEnv(key string, value string) error {
m.values[key] = value
return nil
Expand Down
23 changes: 23 additions & 0 deletions pkg/cfg/environment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cfg_test

import (
"os"
"testing"

"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/stretchr/testify/assert"
)

func TestOsPrefixExists(t *testing.T) {
os.Setenv("GOSOLINE_TEST", "foobar")

provider := cfg.NewOsEnvProvider()
assert.True(t, provider.PrefixExists("GOSOLINE"))
}

func TestMemoryPrefixExists(t *testing.T) {
provider := cfg.NewMemoryEnvProvider(map[string]string{
"GOSOLINE_TEST": "foobar",
})
assert.True(t, provider.PrefixExists("GOSOLINE"))
}
Comment on lines +18 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we test for something that can not have been set beforehand by a test in OS env to make sure this test is not erraneously passing?

42 changes: 42 additions & 0 deletions pkg/cfg/mocks/Config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions pkg/cfg/mocks/GosoConf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/cloud/aws/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetAssumeRoleCredentialsProvider(ctx context.Context, roleArn string) (aws.
}

func UnmarshalCredentials(config cfg.Config) *Credentials {
if !config.IsSet("cloud.aws.credentials") {
if !config.HasPrefix("cloud.aws.credentials") {
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/cloud/aws/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (s *CredentialsTestSuite) SetupTest() {
}

func (s *CredentialsTestSuite) TestNoConfiguredProvider() {
s.config.On("IsSet", "cloud.aws.credentials").Return(false)
s.config.On("HasPrefix", "cloud.aws.credentials").Return(false)

provider, err := gosoAws.GetCredentialsProvider(s.ctx, s.config, gosoAws.ClientSettings{})

Expand All @@ -39,7 +39,7 @@ func (s *CredentialsTestSuite) TestNoConfiguredProvider() {
}

func (s *CredentialsTestSuite) TestStaticCredentialsProvider() {
s.config.On("IsSet", "cloud.aws.credentials").Return(true)
s.config.On("HasPrefix", "cloud.aws.credentials").Return(true)
s.config.On("UnmarshalKey", "cloud.aws.credentials", mock.AnythingOfType("*aws.Credentials")).Run(func(args mock.Arguments) {
credentials := args.Get(1).(*gosoAws.Credentials)

Expand Down
Loading