Skip to content

Commit

Permalink
chore: Remove deprecated support for non-path SSM API
Browse files Browse the repository at this point in the history
Version 2.0 of chamber deprecated use of the non-path-based API for the
SSM parameter store. This commit removes support for it completely.

Since the deprecation occurred so long ago, this changeset does not
preserve the ability to migrate to using the path-based API. Migration
may be performed with a 2.x version of chamber prior to upgrading to
a 3.x version.
  • Loading branch information
bhavanki committed Jun 3, 2024
1 parent b4e1590 commit 0f8609a
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 483 deletions.
13 changes: 2 additions & 11 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ func execRun(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("Failed to get secret store: %w", err)
}
_, noPaths := os.LookupEnv("CHAMBER_NO_PATHS")

if pristine && verbose {
fmt.Fprintf(os.Stderr, "chamber: pristine mode engaged\n")
Expand All @@ -109,11 +108,7 @@ func execRun(cmd *cobra.Command, args []string) error {
}
var err error
env = environ.Environ(os.Environ())
if noPaths {
err = env.LoadStrictNoPaths(secretStore, strictValue, pristine, services...)
} else {
err = env.LoadStrict(secretStore, strictValue, pristine, services...)
}
err = env.LoadStrict(secretStore, strictValue, pristine, services...)

Check warning on line 111 in cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

cmd/exec.go#L111

Added line #L111 was not covered by tests
if err != nil {
return err
}
Expand All @@ -125,11 +120,7 @@ func execRun(cmd *cobra.Command, args []string) error {
collisions := make([]string, 0)
var err error
// TODO: these interfaces should look the same as Strict*, so move pristine in there
if noPaths {
err = env.LoadNoPaths(secretStore, service, &collisions)
} else {
err = env.Load(secretStore, service, &collisions)
}
err = env.Load(secretStore, service, &collisions)

Check warning on line 123 in cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

cmd/exec.go#L123

Added line #L123 was not covered by tests
if err != nil {
return fmt.Errorf("Failed to list store contents: %w", err)
}
Expand Down
4 changes: 0 additions & 4 deletions cmd/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,7 @@ func findValueMatch(secrets []store.Secret, searchTerm string) []store.SecretId
}

func path(s string) string {
_, noPaths := os.LookupEnv("CHAMBER_NO_PATHS")
sep := "/"
if noPaths {
sep = "."
}

tokens := strings.Split(s, sep)
secretPath := strings.Join(tokens[1:len(tokens)-1], "/")
Expand Down
4 changes: 0 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@ func list(cmd *cobra.Command, args []string) error {
}

func key(s string) string {
_, noPaths := os.LookupEnv("CHAMBER_NO_PATHS")
sep := "/"
if noPaths {
sep = "."
}

tokens := strings.Split(s, sep)
secretKey := tokens[len(tokens)-1]
Expand Down
24 changes: 4 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
// Regex's used to validate service and key names
var (
validKeyFormat = regexp.MustCompile(`^[\w\-\.]+$`)
validServiceFormat = regexp.MustCompile(`^[\w\-\.]+$`)
validServicePathFormat = regexp.MustCompile(`^[\w\-\.]+(\/[\w\-\.]+)*$`)
validServiceFormatWithLabel = regexp.MustCompile(`^[\w\-\.\:]+$`)
validServicePathFormatWithLabel = regexp.MustCompile(`^[\w\-\.]+((\/[\w\-\.]+)+(\:[\w\-\.]+)*)?$`)

verbose bool
Expand Down Expand Up @@ -113,30 +111,16 @@ func Execute(vers string, writeKey string) {
}

func validateService(service string) error {
_, noPaths := os.LookupEnv("CHAMBER_NO_PATHS")
if noPaths {
if !validServiceFormat.MatchString(service) {
return fmt.Errorf("Failed to validate service name '%s'. Only alphanumeric, dashes, full stops and underscores are allowed for service names", service)
}
} else {
if !validServicePathFormat.MatchString(service) {
return fmt.Errorf("Failed to validate service name '%s'. Only alphanumeric, dashes, forward slashes, full stops and underscores are allowed for service names. Service names must not start or end with a forward slash", service)
}
if !validServicePathFormat.MatchString(service) {
return fmt.Errorf("Failed to validate service name '%s'. Only alphanumeric, dashes, forward slashes, full stops and underscores are allowed for service names. Service names must not start or end with a forward slash", service)
}

return nil
}

func validateServiceWithLabel(service string) error {
_, noPaths := os.LookupEnv("CHAMBER_NO_PATHS")
if noPaths {
if !validServiceFormatWithLabel.MatchString(service) {
return fmt.Errorf("Failed to validate service name '%s'. Only alphanumeric, dashes, full stops and underscores are allowed for service names, and colon followed by a label name", service)
}
} else {
if !validServicePathFormatWithLabel.MatchString(service) {
return fmt.Errorf("Failed to validate service name '%s'. Only alphanumeric, dashes, forward slashes, full stops and underscores are allowed for service names, and colon followed by a label name. Service names must not start or end with a forward slash or colon", service)
}
if !validServicePathFormatWithLabel.MatchString(service) {
return fmt.Errorf("Failed to validate service name '%s'. Only alphanumeric, dashes, forward slashes, full stops and underscores are allowed for service names, and colon followed by a label name. Service names must not start or end with a forward slash or colon", service)
}

return nil
Expand Down
36 changes: 0 additions & 36 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -73,41 +72,6 @@ func TestValidations(t *testing.T) {
})
}

// Test Service format without PATH
os.Setenv("CHAMBER_NO_PATHS", "true")
validServiceNoPathFormat := []string{
"foo",
"foo.",
".foo",
"foo.bar",
"foo-bar",
"foo-bar.foo",
"foo-bar.foo-bar",
"foo.bar.foo",
"foo.bar.foo-bar",
}

for _, k := range validServiceNoPathFormat {
t.Run("Service without PATH validation should return Nil", func(t *testing.T) {
result := validateService(k)
assert.Nil(t, result)
})
}

invalidServiceNoPathFormat := []string{
"/foo",
"foo//bar",
"foo/bar",
}

for _, k := range invalidServiceNoPathFormat {
t.Run("Service without PATH validation should return Error", func(t *testing.T) {
result := validateService(k)
assert.Error(t, result)
})
}
os.Unsetenv("CHAMBER_NO_PATHS")

// Test Service format with PATH and Label
validServicePathFormatWithLabel := []string{
"foo",
Expand Down
40 changes: 11 additions & 29 deletions environ/environ.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,16 @@ func fromMap(m map[string]string) Environ {
}

// like cmd/list.key, but without the env var lookup
func key(s string, noPaths bool) string {
func key(s string) string {
sep := "/"
if noPaths {
sep = "."
}
tokens := strings.Split(s, sep)
secretKey := tokens[len(tokens)-1]
return secretKey
}

// transforms a secret key to an env var name, i.e. upppercase, substitute `-` -> `_`
func secretKeyToEnvVarName(k string, noPaths bool) string {
return normalizeEnvVarName(key(k, noPaths))
func secretKeyToEnvVarName(k string) string {
return normalizeEnvVarName(key(k))
}

func normalizeEnvVarName(k string) string {
Expand All @@ -86,15 +83,14 @@ func normalizeEnvVarName(k string) string {

// load loads environment variables into e from s given a service
// collisions will be populated with any keys that get overwritten
// noPaths enables the behavior as if CHAMBER_NO_PATHS had been set
func (e *Environ) load(s store.Store, service string, collisions *[]string, noPaths bool) error {
func (e *Environ) load(s store.Store, service string, collisions *[]string) error {

Check warning on line 86 in environ/environ.go

View check run for this annotation

Codecov / codecov/patch

environ/environ.go#L86

Added line #L86 was not covered by tests
rawSecrets, err := s.ListRaw(utils.NormalizeService(service))
if err != nil {
return err
}
envVarKeys := make([]string, 0)
for _, rawSecret := range rawSecrets {
envVarKey := secretKeyToEnvVarName(rawSecret.Key, noPaths)
envVarKey := secretKeyToEnvVarName(rawSecret.Key)

Check warning on line 93 in environ/environ.go

View check run for this annotation

Codecov / codecov/patch

environ/environ.go#L93

Added line #L93 was not covered by tests

envVarKeys = append(envVarKeys, envVarKey)

Expand All @@ -109,45 +105,31 @@ func (e *Environ) load(s store.Store, service string, collisions *[]string, noPa
// Load loads environment variables into e from s given a service
// collisions will be populated with any keys that get overwritten
func (e *Environ) Load(s store.Store, service string, collisions *[]string) error {
return e.load(s, service, collisions, false)
}

// LoadNoPaths is identical to Load, but uses v1-style "."-separated paths
//
// Deprecated like all noPaths functionality
func (e *Environ) LoadNoPaths(s store.Store, service string, collisions *[]string) error {
return e.load(s, service, collisions, true)
return e.load(s, service, collisions)

Check warning on line 108 in environ/environ.go

View check run for this annotation

Codecov / codecov/patch

environ/environ.go#L108

Added line #L108 was not covered by tests
}

// LoadStrict loads all services from s in strict mode: env vars in e with value equal to valueExpected
// are the only ones substituted. If there are any env vars in s that are also in e, but don't have their value
// set to valueExpected, this is an error.
func (e *Environ) LoadStrict(s store.Store, valueExpected string, pristine bool, services ...string) error {
return e.loadStrict(s, valueExpected, pristine, false, services...)
}

// LoadNoPathsStrict is identical to LoadStrict, but uses v1-style "."-separated paths
//
// Deprecated like all noPaths functionality
func (e *Environ) LoadStrictNoPaths(s store.Store, valueExpected string, pristine bool, services ...string) error {
return e.loadStrict(s, valueExpected, pristine, true, services...)
return e.loadStrict(s, valueExpected, pristine, services...)

Check warning on line 115 in environ/environ.go

View check run for this annotation

Codecov / codecov/patch

environ/environ.go#L115

Added line #L115 was not covered by tests
}

func (e *Environ) loadStrict(s store.Store, valueExpected string, pristine bool, noPaths bool, services ...string) error {
func (e *Environ) loadStrict(s store.Store, valueExpected string, pristine bool, services ...string) error {

Check warning on line 118 in environ/environ.go

View check run for this annotation

Codecov / codecov/patch

environ/environ.go#L118

Added line #L118 was not covered by tests
for _, service := range services {
rawSecrets, err := s.ListRaw(utils.NormalizeService(service))
if err != nil {
return err
}
err = e.loadStrictOne(rawSecrets, valueExpected, pristine, noPaths)
err = e.loadStrictOne(rawSecrets, valueExpected, pristine)

Check warning on line 124 in environ/environ.go

View check run for this annotation

Codecov / codecov/patch

environ/environ.go#L124

Added line #L124 was not covered by tests
if err != nil {
return err
}
}
return nil
}

func (e *Environ) loadStrictOne(rawSecrets []store.RawSecret, valueExpected string, pristine bool, noPaths bool) error {
func (e *Environ) loadStrictOne(rawSecrets []store.RawSecret, valueExpected string, pristine bool) error {
parentMap := e.Map()
parentExpects := map[string]struct{}{}
for k, v := range parentMap {
Expand All @@ -162,7 +144,7 @@ func (e *Environ) loadStrictOne(rawSecrets []store.RawSecret, valueExpected stri

envVarKeysAdded := map[string]struct{}{}
for _, rawSecret := range rawSecrets {
envVarKey := secretKeyToEnvVarName(rawSecret.Key, noPaths)
envVarKey := secretKeyToEnvVarName(rawSecret.Key)

parentVal, parentOk := parentMap[envVarKey]
// skip injecting secrets that are not present in the parent
Expand Down
2 changes: 1 addition & 1 deletion environ/environ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestEnvironStrict(t *testing.T) {
if strictVal == "" {
strictVal = "chamberme"
}
err := tc.e.loadStrictOne(rawSecrets, strictVal, tc.pristine, false)
err := tc.e.loadStrictOne(rawSecrets, strictVal, tc.pristine)
if err != nil {
assert.EqualValues(t, tc.expectedErr, err)
} else {
Expand Down
Loading

0 comments on commit 0f8609a

Please sign in to comment.