-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add selective parameter expansion flags and implementation
- Loading branch information
Showing
9 changed files
with
178 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: "Test the build" | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
name: test if ssm-parent can be built | ||
runs-on: ubuntu-latest | ||
steps: | ||
- | ||
name: checkout | ||
uses: actions/checkout@v2 | ||
- | ||
name: set up Go | ||
uses: actions/setup-go@v1 | ||
with: | ||
go-version: 1.17.x | ||
- | ||
name: cache modules | ||
uses: actions/cache@v2 | ||
with: | ||
path: ~/go/pkg/mod | ||
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-go- | ||
- | ||
name: download dependencies | ||
run: go mod download | ||
- | ||
name: build the app | ||
run: go build | ||
- | ||
name: test the app | ||
run: go test -v ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ssm | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/buildkite/interpolate" | ||
) | ||
|
||
// expandArgs expands arguments using env vars | ||
func ExpandArgs(args []string) []string { | ||
var expanded []string | ||
for _, arg := range args { | ||
arg = expandValue(arg) | ||
expanded = append(expanded, arg) | ||
} | ||
return expanded | ||
} | ||
|
||
// expandValue interpolates values using env vars | ||
func expandValue(val string) string { | ||
e, err := interpolate.Interpolate(env, val) | ||
if err == nil { | ||
return strings.TrimSpace(string(e)) | ||
} | ||
return val | ||
} | ||
|
||
// expandParameters expands values using shell-like syntax | ||
func expandParameters(parameters map[string]string, expand bool, expandValues []string) error { | ||
|
||
// if global expand is true then just it for all | ||
if expand { | ||
for key, value := range parameters { | ||
parameters[key] = expandValue(value) | ||
} | ||
// can return early as we've done the job | ||
return nil | ||
} | ||
// check if all values that we ask to expand present in the parameters | ||
// otherwise, it's a configuration error | ||
for _, val := range expandValues { | ||
if _, ok := parameters[val]; !ok { | ||
return fmt.Errorf("env var %s is present in the expand-values but doesn't exist in the environment", val) | ||
} else { | ||
// if the var is present we expand it | ||
parameters[val] = expandValue(parameters[val]) | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ssm | ||
|
||
import "testing" | ||
|
||
func TestExpandPresentNotPresent(t *testing.T) { | ||
parameters := make(map[string]string) | ||
|
||
if err := expandParameters(parameters, false, []string{"test"}); err == nil { | ||
t.Errorf("expected error when supplying var which is not present in params, but got nil") | ||
} | ||
|
||
// set a value | ||
parameters["test"] = "test value" | ||
|
||
if err := expandParameters(parameters, false, []string{"test"}); err != nil { | ||
t.Errorf("expected no error, but got: %s", err) | ||
} | ||
} | ||
|
||
func TestExpandSelectiveExpansions(t *testing.T) { | ||
t.Setenv("ENVIRONMENT", "teststaging") | ||
|
||
parameters := map[string]string{ | ||
"DATABASE_NAME": "DB_$ENVIRONMENT", // should be expanded | ||
"SOME_SECRET": "abc$abc", // should not be expanded | ||
} | ||
|
||
// don't want to expand all here, just specific vars | ||
if err := expandParameters(parameters, false, []string{"DATABASE_NAME"}); err != nil { | ||
t.Errorf("expected no error, but got: %s", err) | ||
} | ||
|
||
if parameters["DATABASE_NAME"] != "DB_teststaging" { | ||
t.Errorf("DATABASE_NAME should be expanded to 'DB_teststaging', but got '%s'", parameters["DATABASE_NAME"]) | ||
} | ||
if parameters["SOME_SECRET"] != "abc$abc" { | ||
t.Errorf("SOME_SECRET should not be expanded and be 'abc$abc', but got '%s'", parameters["SOME_SECRET"]) | ||
} | ||
} | ||
func TestExpandExpansions(t *testing.T) { | ||
t.Setenv("ENVIRONMENT", "teststaging") | ||
t.Setenv("abc", "def") | ||
|
||
parameters := map[string]string{ | ||
"DATABASE_NAME": "DB_$ENVIRONMENT", // should be expanded | ||
"SOME_SECRET": "abc$abc", // should not be expanded | ||
} | ||
want := map[string]string{ | ||
"DATABASE_NAME": "DB_teststaging", | ||
"SOME_SECRET": "abcdef", | ||
} | ||
|
||
// want to expand all | ||
if err := expandParameters(parameters, true, []string{}); err != nil { | ||
t.Errorf("expected no error, but got: %s", err) | ||
} | ||
|
||
for key := range want { | ||
if parameters[key] != want[key] { | ||
t.Errorf("%s should be expanded to '%s', but got '%s'", key, want[key], parameters[key]) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters