-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement support for AWSCredentialsOverrideRef
Signed-off-by: Michael Shen <mshen@redhat.com>
- Loading branch information
Showing
8 changed files
with
231 additions
and
7 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,12 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: Role | ||
metadata: | ||
name: aws-vpce-operator-override-secret | ||
namespace: openshift-aws-vpce-operator | ||
rules: | ||
- apiGroups: | ||
- "" | ||
resources: | ||
- secrets | ||
verbs: | ||
- get |
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,13 @@ | ||
apiVersion: rbac.authorization.k8s.io/v1 | ||
kind: RoleBinding | ||
metadata: | ||
name: aws-vpce-operator-override-secret | ||
namespace: openshift-aws-vpce-operator | ||
subjects: | ||
- kind: ServiceAccount | ||
name: aws-vpce-operator | ||
namespace: openshift-aws-vpce-operator | ||
roleRef: | ||
apiGroup: rbac.authorization.k8s.io | ||
kind: Role | ||
name: aws-vpce-operator |
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,70 @@ | ||
/* | ||
Copyright 2022. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package secrets | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
defaultAWSAccessKeyId = "aws_access_key_id" //#nosec G101 | ||
defaultAWSSecretAccessKey = "aws_secret_access_key" //#nosec G101 | ||
) | ||
|
||
// ParseAWSCredentialOverride takes in an AWS region and a secret reference and attempts to assemble an aws.Config | ||
// Currently only supports parsing AWS IAM User credentials | ||
func ParseAWSCredentialOverride(ctx context.Context, c client.Client, region string, ref *corev1.SecretReference) (aws.Config, error) { | ||
if ref == nil { | ||
return aws.Config{}, errors.New("AWS Credential Override secret reference must not be nil") | ||
} | ||
|
||
secret := new(corev1.Secret) | ||
if err := c.Get(ctx, client.ObjectKey{Namespace: ref.Namespace, Name: ref.Name}, secret); err != nil { | ||
return aws.Config{}, err | ||
} | ||
|
||
if b64AccessKeyId, ok := secret.Data[defaultAWSAccessKeyId]; ok { | ||
if b64SecretAccessKey, ok := secret.Data[defaultAWSSecretAccessKey]; ok { | ||
accessKeyId, err := base64.StdEncoding.DecodeString(string(b64AccessKeyId)) | ||
if err != nil { | ||
return aws.Config{}, err | ||
} | ||
|
||
secretAccessKey, err := base64.StdEncoding.DecodeString(string(b64SecretAccessKey)) | ||
if err != nil { | ||
return aws.Config{}, err | ||
} | ||
|
||
return config.LoadDefaultConfig(ctx, | ||
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(string(accessKeyId), string(secretAccessKey), "")), | ||
config.WithRegion(region), | ||
) | ||
} | ||
} | ||
|
||
return aws.Config{}, fmt.Errorf("could not parse credential override secret, requires data keys %s and %s", defaultAWSAccessKeyId, defaultAWSSecretAccessKey) | ||
} |
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,87 @@ | ||
/* | ||
Copyright 2022. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package secrets | ||
|
||
import ( | ||
"context" | ||
"encoding/base64" | ||
"testing" | ||
|
||
"github.com/openshift/aws-vpce-operator/pkg/testutil" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
mockAWSAccessKeyId = "mock_access_key_id" //#nosec G101 | ||
mockAWSSecretAccessKey = "mock_secret_access_key" //#nosec G101 | ||
) | ||
|
||
func TestParseAWSCredentialOverride(t *testing.T) { | ||
|
||
tests := []struct { | ||
secret *corev1.Secret | ||
expectErr bool | ||
}{ | ||
{ | ||
secret: &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "override", | ||
Namespace: "override-ns", | ||
}, | ||
Data: map[string][]byte{ | ||
defaultAWSAccessKeyId: []byte(base64.StdEncoding.EncodeToString([]byte(mockAWSAccessKeyId))), | ||
defaultAWSSecretAccessKey: []byte(base64.StdEncoding.EncodeToString([]byte(mockAWSSecretAccessKey))), | ||
}, | ||
}, | ||
expectErr: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run("", func(t *testing.T) { | ||
mock := testutil.NewTestMock(t, test.secret) | ||
ref := &corev1.SecretReference{ | ||
Name: test.secret.Name, | ||
Namespace: test.secret.Namespace, | ||
} | ||
cfg, err := ParseAWSCredentialOverride(context.TODO(), mock.Client, "us-east-1", ref) | ||
if test.expectErr { | ||
if err == nil { | ||
t.Errorf("expected err, got nil") | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("expected no err, got %v", err) | ||
} | ||
|
||
creds, err := cfg.Credentials.Retrieve(context.TODO()) | ||
if err != nil { | ||
t.Errorf("unexpected err: %v", err) | ||
} | ||
|
||
if creds.AccessKeyID != mockAWSAccessKeyId { | ||
t.Errorf("expected %s, got %s", mockAWSAccessKeyId, creds.AccessKeyID) | ||
} | ||
|
||
if creds.SecretAccessKey != mockAWSSecretAccessKey { | ||
t.Errorf("expected %s, got %s", mockAWSSecretAccessKey, creds.SecretAccessKey) | ||
} | ||
} | ||
}) | ||
} | ||
} |