Skip to content

Commit

Permalink
feat: added unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
mvanholsteijn committed Sep 17, 2023
1 parent 12d3989 commit 51735a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
20 changes: 13 additions & 7 deletions pkg/cmd/env/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ func splitEnvironmentVariable(envEntry string) (string, string) {
}

// NewEnvironmentWithCredentials creates a new environment variable array adding the environment variables AWS_ACCESS_KEY_ID,
// AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN. requires the credential AccessKeyId, SecretAccessKey and SessionToken to be set.
// AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN.
func NewEnvironmentWithCredentials(env []string, credentials *awssts.Credentials) []string {
result := make([]string, 0, len(env)+3)
credentialValues := map[string]string{
"AWS_ACCESS_KEY_ID": *credentials.AccessKeyId,
"AWS_SECRET_ACCESS_KEY": *credentials.SecretAccessKey,
"AWS_SESSION_TOKEN": *credentials.SessionToken,
credentialValues := map[string]*string{
"AWS_ACCESS_KEY_ID": credentials.AccessKeyId,
"AWS_SECRET_ACCESS_KEY": credentials.SecretAccessKey,
"AWS_SESSION_TOKEN": credentials.SessionToken,
}

for _, envEntry := range env {
Expand All @@ -47,8 +47,14 @@ func NewEnvironmentWithCredentials(env []string, credentials *awssts.Credentials
result = append(result, envEntry)
}
}
for name, value := range credentialValues {
result = append(result, fmt.Sprintf("%s=%s", name, value))

for _, name := range []string{"AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN"} {
value, _ := credentialValues[name]
if value != nil {
result = append(result, fmt.Sprintf("%s=%s", name, *value))
} else {
result = append(result, fmt.Sprintf("%s=", name))
}
}

return result
Expand Down
45 changes: 45 additions & 0 deletions pkg/cmd/env/exec_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package env

import (
"reflect"
"testing"

awssts "github.com/aws/aws-sdk-go/service/sts"
)

func TestNewEnvironmentWithCredentials(t *testing.T) {
type args struct {
env []string
AccessKeyId, SecretAccessKey, SessionToken string
}
tests := []struct {
name string
args args
want []string
}{
{
name: "simple", args: args{[]string{"A=B", "C=D"}, "key", "secret", "token"},
want: []string{"A=B", "C=D", "AWS_ACCESS_KEY_ID=key", "AWS_SECRET_ACCESS_KEY=secret", "AWS_SESSION_TOKEN=token"},
},
{
name: "simple", args: args{[]string{"AWS_ACCESS_KEY_ID=B", "C=D"}, "key", "secret", "token"},
want: []string{"C=D", "AWS_ACCESS_KEY_ID=key", "AWS_SECRET_ACCESS_KEY=secret", "AWS_SESSION_TOKEN=token"},
},
{
name: "override all", args: args{[]string{"AWS_ACCESS_KEY_ID=key", "AWS_SECRET_ACCESS_KEY=secret", "AWS_SESSION_TOKEN=token"}, "new_key", "new_secret", "new_token"},
want: []string{"AWS_ACCESS_KEY_ID=new_key", "AWS_SECRET_ACCESS_KEY=new_secret", "AWS_SESSION_TOKEN=new_token"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
credentials := awssts.Credentials{
AccessKeyId: &tt.args.AccessKeyId,
SecretAccessKey: &tt.args.SecretAccessKey,
SessionToken: &tt.args.SessionToken}

if got := NewEnvironmentWithCredentials(tt.args.env, &credentials); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewEnvironmentWithCredentials() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion terraform/templates/gitlab-ci.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ get-credential-helper:
- gitlab-aws-credential-helper


dotenv-demo:
env-demo:
stage: build
image:
name: public.ecr.aws/aws-cli/aws-cli:2.13.17
Expand Down

0 comments on commit 51735a0

Please sign in to comment.