Skip to content

Commit

Permalink
Merge pull request #179 from articulate/fix/remove-service-name-requi…
Browse files Browse the repository at this point in the history
…rement

fix: remove service name requirement
  • Loading branch information
mloberg authored Oct 17, 2023
2 parents 3893476 + 92d3601 commit c20d427
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 56 deletions.
39 changes: 26 additions & 13 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,40 @@ type Config struct {

// ConsulPaths returns the paths from Consul to load
func (c *Config) ConsulPaths() []string {
return []string{
paths := []string{
"global/env_vars",
fmt.Sprintf("global/%s/env_vars", c.Environment),
fmt.Sprintf("services/%s/env_vars", c.Service),
fmt.Sprintf("services/%s/%s/env_vars", c.Service, c.Environment),
}

if c.Service != "" {
paths = append(
paths,
fmt.Sprintf("services/%s/env_vars", c.Service),
fmt.Sprintf("services/%s/%s/env_vars", c.Service, c.Environment),
)
}

return paths
}

// VaultPaths returns the paths from Vault to load
func (c *Config) VaultPaths() []string {
if c.Environment == "stage" || c.Environment == "prod" {
return []string{
"secret/global/env_vars",
fmt.Sprintf("secret/global/%s/env_vars", c.Environment),
fmt.Sprintf("secret/services/%s/env_vars", c.Service),
fmt.Sprintf("secret/services/%s/%s/env_vars", c.Service, c.Environment),
}
isPublic := c.Environment == "stage" || c.Environment == "prod"
paths := []string{}

if isPublic {
paths = append(paths, "secret/global/env_vars")
}

return []string{
fmt.Sprintf("secret/global/%s/env_vars", c.Environment),
fmt.Sprintf("secret/services/%s/%s/env_vars", c.Service, c.Environment),
paths = append(paths, fmt.Sprintf("secret/global/%s/env_vars", c.Environment))

if c.Service != "" {
if isPublic {
paths = append(paths, fmt.Sprintf("secret/services/%s/env_vars", c.Service))
}

paths = append(paths, fmt.Sprintf("secret/services/%s/%s/env_vars", c.Service, c.Environment))
}

return paths
}
114 changes: 78 additions & 36 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,85 @@ import (
)

func TestConfig_ConsulPaths(t *testing.T) {
c := Config{
Service: "foo",
Environment: "stage",
}

assert.Equal(t, []string{
"global/env_vars",
"global/stage/env_vars",
"services/foo/env_vars",
"services/foo/stage/env_vars",
}, c.ConsulPaths())
t.Parallel()

t.Run("full config", func(t *testing.T) {
t.Parallel()

c := Config{Service: "foo", Environment: "stage"}
assert.Equal(t, []string{
"global/env_vars",
"global/stage/env_vars",
"services/foo/env_vars",
"services/foo/stage/env_vars",
}, c.ConsulPaths())
})

t.Run("no service", func(t *testing.T) {
t.Parallel()

c := Config{Environment: "stage"}
assert.Equal(t, []string{
"global/env_vars",
"global/stage/env_vars",
}, c.ConsulPaths())
})
}

func TestConfig_VaultPaths(t *testing.T) {
c := Config{
Service: "foo",
Environment: "stage",
}

assert.Equal(t, []string{
"secret/global/env_vars",
"secret/global/stage/env_vars",
"secret/services/foo/env_vars",
"secret/services/foo/stage/env_vars",
}, c.VaultPaths())

c.Environment = "prod"
assert.Equal(t, []string{
"secret/global/env_vars",
"secret/global/prod/env_vars",
"secret/services/foo/env_vars",
"secret/services/foo/prod/env_vars",
}, c.VaultPaths())

c.Environment = "dev"
assert.Equal(t, []string{
"secret/global/dev/env_vars",
"secret/services/foo/dev/env_vars",
}, c.VaultPaths())
t.Parallel()

t.Run("stage", func(t *testing.T) {
t.Parallel()

c := Config{Service: "foo", Environment: "stage"}
assert.Equal(t, []string{
"secret/global/env_vars",
"secret/global/stage/env_vars",
"secret/services/foo/env_vars",
"secret/services/foo/stage/env_vars",
}, c.VaultPaths())
})

t.Run("prod", func(t *testing.T) {
t.Parallel()

c := Config{Service: "foo", Environment: "prod"}
assert.Equal(t, []string{
"secret/global/env_vars",
"secret/global/prod/env_vars",
"secret/services/foo/env_vars",
"secret/services/foo/prod/env_vars",
}, c.VaultPaths())
})

t.Run("dev", func(t *testing.T) {
t.Parallel()

c := Config{Service: "foo", Environment: "dev"}
assert.Equal(t, []string{
"secret/global/dev/env_vars",
"secret/services/foo/dev/env_vars",
}, c.VaultPaths())
})

t.Run("no service", func(t *testing.T) {
t.Parallel()

c := Config{Environment: "prod"}

assert.Equal(t, []string{
"secret/global/env_vars",
"secret/global/prod/env_vars",
}, c.VaultPaths())
})

t.Run("no service dev", func(t *testing.T) {
t.Parallel()

c := Config{Environment: "dev"}
assert.Equal(t, []string{
"secret/global/dev/env_vars",
}, c.VaultPaths())
})
}
10 changes: 3 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,12 @@ func main() {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}

cfg := Config{
cfg := &Config{
Service: os.Getenv("SERVICE_NAME"),
Environment: os.Getenv("SERVICE_ENV"),
Region: os.Getenv("AWS_REGION"),
}

if cfg.Service == "" {
log.Fatal().Msg("SERVICE_NAME cannot be blank")
}

if cfg.Environment == "" {
log.Warn().Msg("SERVICE_ENV is blank, defaulting to dev")
cfg.Environment = "dev"
Expand Down Expand Up @@ -76,7 +72,7 @@ func main() {
os.Exit(run(os.Args[1], os.Args[2:], env.Environ(), logger))
}

func loadConsul(addr string, c Config, l zerolog.Logger) Dict {
func loadConsul(addr string, c *Config, l zerolog.Logger) Dict {
l.Debug().Msg("Loading values from Consul")

client, err := NewConsul(addr)
Expand All @@ -92,7 +88,7 @@ func loadConsul(addr string, c Config, l zerolog.Logger) Dict {
return loadValues(client, l, paths)
}

func loadVault(ctx context.Context, addr string, c Config, l zerolog.Logger) Dict {
func loadVault(ctx context.Context, addr string, c *Config, l zerolog.Logger) Dict {
l.Debug().Msg("Loading values from Vault")

client, err := NewVault(addr, c.Region)
Expand Down

0 comments on commit c20d427

Please sign in to comment.