@@ -28,12 +28,16 @@ const testSessionToken = "test-session-token"
2828const testRoleARN = "arn:aws:iam::111122223333:role/pod-a-role"
2929const testWebIdentityToken = "test-web-identity-token"
3030
31+ const testContainerAuthorizationToken = "test-container-authorization-token"
32+ const testContainerCredentialsFullURI = "http://169.254.170.23/v1/credentials"
33+
3134const testPodID = "2a17db00-0bf3-4052-9b3f-6c89dcee5d79"
3235const testVolumeID = "test-vol"
3336const testProfilePrefix = testPodID + "-" + testVolumeID + "-"
3437
3538const testPodLevelServiceAccountToken = testPodID + "-" + testVolumeID + ".token"
36- const testDriverLevelServiceAccountToken = "token"
39+ const testWebIdentityServiceAccountToken = "token"
40+ const testEKSPodIdentityServiceAccountToken = "eks-pod-identity-token"
3741
3842const testPodServiceAccount = "test-sa"
3943const testPodNamespace = "test-ns"
@@ -98,9 +102,27 @@ func TestProvidingDriverLevelCredentials(t *testing.T) {
98102 assert .Equals (t , credentialprovider .AuthenticationSourceDriver , source )
99103 assert .Equals (t , envprovider.Environment {
100104 "AWS_ROLE_ARN" : testRoleARN ,
101- "AWS_WEB_IDENTITY_TOKEN_FILE" : filepath .Join (testEnvPath , testDriverLevelServiceAccountToken ),
105+ "AWS_WEB_IDENTITY_TOKEN_FILE" : filepath .Join (testEnvPath , testWebIdentityServiceAccountToken ),
106+ }, env )
107+ assertWebIdentityTokenFile (t , filepath .Join (writePath , testWebIdentityServiceAccountToken ))
108+ }
109+ })
110+
111+ t .Run ("only container credentials" , func (t * testing.T ) {
112+ for _ , authSource := range authenticationSourceVariants {
113+ setEnvForContainerCredentials (t )
114+
115+ writePath := t .TempDir ()
116+ provideCtx := provideCtx (t , writePath , authSource )
117+
118+ env , source , err := provider .Provide (context .Background (), provideCtx )
119+ assert .NoError (t , err )
120+ assert .Equals (t , credentialprovider .AuthenticationSourceDriver , source )
121+ assert .Equals (t , envprovider.Environment {
122+ "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE" : filepath .Join (testEnvPath , testEKSPodIdentityServiceAccountToken ),
123+ "AWS_CONTAINER_CREDENTIALS_FULL_URI" : testContainerCredentialsFullURI ,
102124 }, env )
103- assertWebIdentityTokenFile (t , filepath .Join (writePath , testDriverLevelServiceAccountToken ))
125+ assertContainerTokenFile (t , filepath .Join (writePath , testEKSPodIdentityServiceAccountToken ))
104126 }
105127 })
106128
@@ -148,10 +170,55 @@ func TestProvidingDriverLevelCredentials(t *testing.T) {
148170 "AWS_CONFIG_FILE" : "/test-env/" + testProfilePrefix + "s3-csi-config" ,
149171 "AWS_SHARED_CREDENTIALS_FILE" : "/test-env/" + testProfilePrefix + "s3-csi-credentials" ,
150172 "AWS_ROLE_ARN" : testRoleARN ,
151- "AWS_WEB_IDENTITY_TOKEN_FILE" : filepath .Join (testEnvPath , testDriverLevelServiceAccountToken ),
173+ "AWS_WEB_IDENTITY_TOKEN_FILE" : filepath .Join (testEnvPath , testWebIdentityServiceAccountToken ),
174+ }, env )
175+ assertLongTermCredentials (t , writePath )
176+ assertWebIdentityTokenFile (t , filepath .Join (writePath , testWebIdentityServiceAccountToken ))
177+ }
178+ })
179+
180+ t .Run ("long-term and container credentials" , func (t * testing.T ) {
181+ for _ , authSource := range authenticationSourceVariants {
182+ setEnvForLongTermCredentials (t )
183+ setEnvForContainerCredentials (t )
184+
185+ writePath := t .TempDir ()
186+ provideCtx := provideCtx (t , writePath , authSource )
187+
188+ env , source , err := provider .Provide (context .Background (), provideCtx )
189+ assert .NoError (t , err )
190+ assert .Equals (t , credentialprovider .AuthenticationSourceDriver , source )
191+ assert .Equals (t , envprovider.Environment {
192+ "AWS_PROFILE" : testProfilePrefix + "s3-csi" ,
193+ "AWS_CONFIG_FILE" : "/test-env/" + testProfilePrefix + "s3-csi-config" ,
194+ "AWS_SHARED_CREDENTIALS_FILE" : "/test-env/" + testProfilePrefix + "s3-csi-credentials" ,
195+ "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE" : filepath .Join (testEnvPath , testEKSPodIdentityServiceAccountToken ),
196+ "AWS_CONTAINER_CREDENTIALS_FULL_URI" : testContainerCredentialsFullURI ,
152197 }, env )
153198 assertLongTermCredentials (t , writePath )
154- assertWebIdentityTokenFile (t , filepath .Join (writePath , testDriverLevelServiceAccountToken ))
199+ assertContainerTokenFile (t , filepath .Join (writePath , testEKSPodIdentityServiceAccountToken ))
200+ }
201+ })
202+
203+ t .Run ("sts web identity credentials and containter credentials" , func (t * testing.T ) {
204+ for _ , authSource := range authenticationSourceVariants {
205+ setEnvForContainerCredentials (t )
206+ setEnvForStsWebIdentityCredentials (t )
207+
208+ writePath := t .TempDir ()
209+ provideCtx := provideCtx (t , writePath , authSource )
210+
211+ env , source , err := provider .Provide (context .Background (), provideCtx )
212+ assert .NoError (t , err )
213+ assert .Equals (t , credentialprovider .AuthenticationSourceDriver , source )
214+ assert .Equals (t , envprovider.Environment {
215+ "AWS_ROLE_ARN" : testRoleARN ,
216+ "AWS_WEB_IDENTITY_TOKEN_FILE" : filepath .Join (testEnvPath , testWebIdentityServiceAccountToken ),
217+ "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE" : filepath .Join (testEnvPath , testEKSPodIdentityServiceAccountToken ),
218+ "AWS_CONTAINER_CREDENTIALS_FULL_URI" : testContainerCredentialsFullURI ,
219+ }, env )
220+ assertContainerTokenFile (t , filepath .Join (writePath , testEKSPodIdentityServiceAccountToken ))
221+ assertWebIdentityTokenFile (t , filepath .Join (writePath , testWebIdentityServiceAccountToken ))
155222 }
156223 })
157224
@@ -215,6 +282,31 @@ func TestProvidingDriverLevelCredentials(t *testing.T) {
215282 assert .Equals (t , envprovider.Environment {}, env )
216283 })
217284
285+ t .Run ("incomplete container credentials" , func (t * testing.T ) {
286+ // Only set container credentials full URI without token file
287+ t .Setenv ("AWS_CONTAINER_CREDENTIALS_FULL_URI" , testContainerCredentialsFullURI )
288+
289+ provider := credentialprovider .New (nil , dummyRegionProvider )
290+
291+ provideCtx := provideCtx (t , t .TempDir (), credentialprovider .AuthenticationSourceDriver )
292+
293+ env , source , err := provider .Provide (context .Background (), provideCtx )
294+ assert .NoError (t , err )
295+ assert .Equals (t , credentialprovider .AuthenticationSourceDriver , source )
296+ assert .Equals (t , envprovider.Environment {}, env )
297+
298+ // Only set token file without role ARN
299+ tokenPath := filepath .Join (t .TempDir (), "token" )
300+ assert .NoError (t , os .WriteFile (tokenPath , []byte (testContainerAuthorizationToken ), 0600 ))
301+ t .Setenv ("AWS_ROLE_ARN" , "" )
302+ t .Setenv ("AWS_WEB_IDENTITY_TOKEN_FILE" , tokenPath )
303+
304+ env , source , err = provider .Provide (context .Background (), provideCtx )
305+ assert .NoError (t , err )
306+ assert .Equals (t , credentialprovider .AuthenticationSourceDriver , source )
307+ assert .Equals (t , envprovider.Environment {}, env )
308+ })
309+
218310 t .Run ("no credentials" , func (t * testing.T ) {
219311 for _ , authSource := range authenticationSourceVariants {
220312 writePath := t .TempDir ()
@@ -817,6 +909,16 @@ func TestCleanup(t *testing.T) {
817909
818910//-- Utilities for tests
819911
912+ func provideCtx (t * testing.T , writePath string , authSource string ) credentialprovider.ProvideContext {
913+ return credentialprovider.ProvideContext {
914+ AuthenticationSource : authSource ,
915+ WritePath : writePath ,
916+ EnvPath : testEnvPath ,
917+ PodID : testPodID ,
918+ VolumeID : testVolumeID ,
919+ }
920+ }
921+
820922func setEnvForLongTermCredentials (t * testing.T ) {
821923 t .Setenv ("AWS_ACCESS_KEY_ID" , testAccessKeyID )
822924 t .Setenv ("AWS_SECRET_ACCESS_KEY" , testSecretAccessKey )
@@ -848,6 +950,18 @@ func setEnvForStsWebIdentityCredentials(t *testing.T) {
848950 t .Setenv ("AWS_WEB_IDENTITY_TOKEN_FILE" , tokenPath )
849951}
850952
953+ func setEnvForContainerCredentials (t * testing.T ) {
954+ t .Helper ()
955+
956+ t .Setenv ("MOUNTER_KIND" , "pod" )
957+
958+ tokenPath := filepath .Join (t .TempDir (), "token" )
959+ assert .NoError (t , os .WriteFile (tokenPath , []byte (testContainerAuthorizationToken ), 0600 ))
960+
961+ t .Setenv ("AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE" , tokenPath )
962+ t .Setenv ("AWS_CONTAINER_CREDENTIALS_FULL_URI" , testContainerCredentialsFullURI )
963+ }
964+
851965func assertWebIdentityTokenFile (t * testing.T , path string ) {
852966 t .Helper ()
853967
@@ -856,6 +970,14 @@ func assertWebIdentityTokenFile(t *testing.T, path string) {
856970 assert .Equals (t , []byte (testWebIdentityToken ), got )
857971}
858972
973+ func assertContainerTokenFile (t * testing.T , path string ) {
974+ t .Helper ()
975+
976+ got , err := os .ReadFile (path )
977+ assert .NoError (t , err )
978+ assert .Equals (t , []byte (testContainerAuthorizationToken ), got )
979+ }
980+
859981type tokens = map [string ]struct {
860982 Token string `json:"token"`
861983 ExpirationTimestamp time.Time
0 commit comments