github.com/argoproj/argo-events@v1.9.1/eventsources/common/aws/aws_test.go (about)

     1  /*
     2  Copyright 2018 BlackRock, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package aws
    18  
    19  import (
    20  	"context"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/smartystreets/goconvey/convey"
    25  	corev1 "k8s.io/api/core/v1"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/client-go/kubernetes/fake"
    28  )
    29  
    30  func TestAWS(t *testing.T) {
    31  	client := fake.NewSimpleClientset()
    32  	namespace := "test"
    33  	secretName := "test-secret"
    34  	accessKey := "YWNjZXNz"
    35  	secretKey := "c2VjcmV0"
    36  	LabelAccessKey := "access"
    37  	LabelSecretKey := "secret"
    38  
    39  	convey.Convey("Given credentials are in ENV by using envFrom, create AWS credential", t, func() {
    40  		os.Setenv(secretName+"_"+LabelAccessKey, accessKey)
    41  		os.Setenv(secretName+"_"+LabelSecretKey, secretKey)
    42  
    43  		creds, err := GetAWSCredFromEnvironment(&corev1.SecretKeySelector{
    44  			Key: LabelAccessKey,
    45  			LocalObjectReference: corev1.LocalObjectReference{
    46  				Name: secretName,
    47  			},
    48  		}, &corev1.SecretKeySelector{
    49  			Key: LabelSecretKey,
    50  			LocalObjectReference: corev1.LocalObjectReference{
    51  				Name: secretName,
    52  			},
    53  		})
    54  
    55  		convey.So(err, convey.ShouldBeNil)
    56  		convey.So(creds, convey.ShouldNotBeNil)
    57  
    58  		value, err := creds.Get()
    59  		convey.So(err, convey.ShouldBeNil)
    60  		convey.So(value.AccessKeyID, convey.ShouldEqual, accessKey)
    61  		convey.So(value.SecretAccessKey, convey.ShouldEqual, secretKey)
    62  
    63  		convey.Convey("Get a new aws session", func() {
    64  			session, err := GetAWSSession(creds, "mock-region")
    65  			convey.So(err, convey.ShouldBeNil)
    66  			convey.So(session, convey.ShouldNotBeNil)
    67  		})
    68  	})
    69  
    70  	convey.Convey("Given kubernetes secret that hold credentials, create AWS credential", t, func() {
    71  		secret, err := client.CoreV1().Secrets(namespace).Create(context.TODO(), &corev1.Secret{
    72  			ObjectMeta: metav1.ObjectMeta{
    73  				Name:      secretName,
    74  				Namespace: namespace,
    75  			},
    76  			Data: map[string][]byte{
    77  				LabelAccessKey: []byte(accessKey),
    78  				LabelSecretKey: []byte(secretKey),
    79  			},
    80  		}, metav1.CreateOptions{})
    81  
    82  		convey.So(err, convey.ShouldBeNil)
    83  		convey.So(secret, convey.ShouldNotBeNil)
    84  	})
    85  
    86  	convey.Convey("create AWS credential using already present config/IAM role", t, func() {
    87  		convey.Convey("Get a new aws session", func() {
    88  			session, err := GetAWSSessionWithoutCreds("mock-region")
    89  			convey.So(err, convey.ShouldBeNil)
    90  			convey.So(session, convey.ShouldNotBeNil)
    91  		})
    92  	})
    93  
    94  	convey.Convey("create AWS credential using assume roleARN", t, func() {
    95  		convey.Convey("Get a new aws session", func() {
    96  			session, err := GetAWSAssumeRoleCreds("moke-roleARN", "mock-region")
    97  			convey.So(err, convey.ShouldBeNil)
    98  			convey.So(session, convey.ShouldNotBeNil)
    99  		})
   100  	})
   101  }