Skip to content

Commit

Permalink
feat: add support to use AWS session token if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
L30Bola committed Jun 17, 2024
1 parent a8d4cbf commit a0a5195
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
golang 1.21.7
golang 1.21.9
12 changes: 12 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ func SetEnvVarsFromAWSCredentials(awsConfig aws.Credentials) error {
if err != nil {
return err
}
err = os.Unsetenv("AWS_SESSION_TOKEN")
if err != nil {
return err
}

err = os.Setenv("AWS_ACCESS_KEY_ID", awsConfig.AccessKeyID)
if err != nil {
Expand All @@ -69,6 +73,12 @@ func SetEnvVarsFromAWSCredentials(awsConfig aws.Credentials) error {
if err != nil {
return err
}
if awsConfig.SessionToken != "" {
err = os.Setenv("AWS_SESSION_TOKEN", awsConfig.SessionToken)
if err != nil {
return err
}
}

return nil
}
Expand All @@ -84,10 +94,12 @@ func GetAWSCredentialsFromKopsControlPlaneSecret(ctx context.Context, c client.C
}
accessKeyID := string(secret.Data["AccessKeyID"])
secretAccessKey := string(secret.Data["SecretAccessKey"])
sessionToken := string(secret.Data["SessionToken"])

creds := &aws.Credentials{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
SessionToken: sessionToken,
}

return creds, nil
Expand Down
19 changes: 15 additions & 4 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestSetAWSEnvFromKopsControlPlaneSecret(t *testing.T) {
description: "Should successfully set AWS envs",
expectedError: false,
k8sObjects: []client.Object{
newAWSCredentialSecret("11111111-credential", "kubernetes-kops-operator-system"),
newAWSCredentialSecret("11111111-credential", "kubernetes-kops-operator-system", "session-token"),
},
},
}
Expand All @@ -154,12 +154,14 @@ func TestSetAWSEnvFromKopsControlPlaneSecret(t *testing.T) {
awsCredentials := aws.Credentials{
AccessKeyID: "11111111-credential",
SecretAccessKey: "kubernetes-kops-operator-system",
SessionToken: "session-token",
}
err := SetEnvVarsFromAWSCredentials(awsCredentials)
if !tc.expectedError {
g.Expect(err).NotTo(HaveOccurred())
g.Expect(os.Getenv("AWS_ACCESS_KEY_ID")).To(Equal("11111111-credential"))
g.Expect(os.Getenv("AWS_SECRET_ACCESS_KEY")).To(Equal("kubernetes-kops-operator-system"))
g.Expect(os.Getenv("AWS_SESSION_TOKEN")).To(Equal("session-token"))
} else {
g.Expect(err).To(HaveOccurred())
}
Expand All @@ -176,13 +178,21 @@ func TestGetAwsCredentialsFromKopsControlPlaneSecret(t *testing.T) {
expectedError bool
}{
{
description: "Should successfully set AWS envs",
description: "Should successfully set AWS envs without session token",
expectedError: false,
k8sObjects: []client.Object{
newAWSCredentialSecret("accessTest", "secretTest"),
newAWSCredentialSecret("accessTest", "secretTest", ""),
},
expectedAwsCredential: &aws.Credentials{AccessKeyID: "accessTest", SecretAccessKey: "secretTest"},
},
{
description: "Should successfully set AWS envs with session token",
expectedError: false,
k8sObjects: []client.Object{
newAWSCredentialSecret("accessTest", "secretTest", "sessionToken"),
},
expectedAwsCredential: &aws.Credentials{AccessKeyID: "accessTest", SecretAccessKey: "secretTest", SessionToken: "sessionToken"},
},
{
description: "Should fail if can't get secret",
expectedError: true,
Expand All @@ -209,7 +219,7 @@ func TestGetAwsCredentialsFromKopsControlPlaneSecret(t *testing.T) {
}
}

func newAWSCredentialSecret(accessKey, secret string) *corev1.Secret {
func newAWSCredentialSecret(accessKey, secret, sessionToken string) *corev1.Secret {
return &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "11111111-credential",
Expand All @@ -218,6 +228,7 @@ func newAWSCredentialSecret(accessKey, secret string) *corev1.Secret {
Data: map[string][]byte{
"AccessKeyID": []byte(accessKey),
"SecretAccessKey": []byte(secret),
"SessionToken": []byte(sessionToken),
},
}
}
Expand Down

0 comments on commit a0a5195

Please sign in to comment.