@@ -2,6 +2,7 @@ package main
22
33import  (
44	"errors" 
5+ 	"fmt" 
56	"testing" 
67)
78
@@ -30,13 +31,13 @@ func TestECRContext_Retrieve(t *testing.T) {
3031			},
3132		},
3233		{
33- 			name :        "Missing access key with session token present" ,
34- 			accountID :   "123456789012" ,
35- 			expectedErr : errors .New ("ecrContext: environment variable AWS_ACCESS_KEY_ID_123456789012 not found" ),
34+ 			name :      "Missing access key with session token present" ,
35+ 			accountID : "123456789012" ,
3636			envVars : map [string ]string {
3737				"AWS_SESSION_TOKEN_123456789012" :     "AQoEXAMPLEH4..." ,
3838				"AWS_SECRET_ACCESS_KEY_123456789012" : "wJalr..." ,
3939			},
40+ 			expectedErr : errors .New ("ecrContext: environment variable AWS_ACCESS_KEY_ID_123456789012 not found" ),
4041		},
4142		{
4243			name :        "Missing secret key with access key present" ,
@@ -47,25 +48,13 @@ func TestECRContext_Retrieve(t *testing.T) {
4748			},
4849		},
4950		{
50- 			name :        "Missing both keys - fallback to standard AWS credentials" ,
51- 			accountID :   "123456789012" ,
52- 			expectedErr : errors .New ("ecrContext: no account credentials found and standard AWS_ACCESS_KEY_ID not found" ),
53- 		},
54- 		{
55- 			name :      "Valid credentials in FedRAMP" ,
56- 			accountID : "123456789012" ,
57- 			envVars : map [string ]string {
58- 				"AWS_ACCESS_KEY_ID_123456789012" :     "AKIA..." ,
59- 				"AWS_SECRET_ACCESS_KEY_123456789012" : "wJalr..." ,
60- 			},
61- 		},
62- 		{
63- 			name :      "Standard AWS credentials when no suffixed vars exist" ,
51+ 			name :      "No suffixed credentials" ,
6452			accountID : "123456789012" ,
6553			envVars : map [string ]string {
6654				"AWS_ACCESS_KEY_ID" :     "STD-AKIA..." ,
6755				"AWS_SECRET_ACCESS_KEY" : "STD-wJalr..." ,
6856			},
57+ 			expectedErr : fmt .Errorf ("ecrContext: environment variable %s not found" , envAwsAccessKeyID + "_123456789012" ),
6958		},
7059	}
7160	for  _ , tc  :=  range  useCases  {
@@ -122,3 +111,89 @@ func TestECRContext_Retrieve(t *testing.T) {
122111		})
123112	}
124113}
114+ 
115+ func  TestECRContext_HasAccountSuffixedCredentials (t  * testing.T ) {
116+ 	useCases  :=  []struct  {
117+ 		name       string 
118+ 		accountID  string 
119+ 		envVars    map [string ]string 
120+ 		expected   bool 
121+ 	}{
122+ 		{
123+ 			name :      "Has suffixed credentials for account" ,
124+ 			accountID : "123456789012" ,
125+ 			envVars : map [string ]string {
126+ 				"AWS_ACCESS_KEY_ID_123456789012" :     "AKIA..." ,
127+ 				"AWS_SECRET_ACCESS_KEY_123456789012" : "wJalr..." ,
128+ 			},
129+ 			expected : true ,
130+ 		},
131+ 		{
132+ 			name :      "No credentials" ,
133+ 			accountID : "123456789012" ,
134+ 			envVars :   map [string ]string {},
135+ 			expected :  false ,
136+ 		},
137+ 		{
138+ 			name :      "Has suffixed access key only" ,
139+ 			accountID : "123456789012" ,
140+ 			envVars : map [string ]string {
141+ 				"AWS_ACCESS_KEY_ID_123456789012" : "AKIA..." ,
142+ 			},
143+ 			expected : false ,
144+ 		},
145+ 		{
146+ 			name :      "Has suffixed secret key only" ,
147+ 			accountID : "123456789012" ,
148+ 			envVars : map [string ]string {
149+ 				"AWS_SECRET_ACCESS_KEY_123456789012" : "wJalr..." ,
150+ 			},
151+ 			expected : false ,
152+ 		},
153+ 		{
154+ 			name :      "Has non-suffixed credentials for account" ,
155+ 			accountID : "123456789012" ,
156+ 			envVars : map [string ]string {
157+ 				"AWS_ACCESS_KEY_ID" :     "AKIA..." ,
158+ 				"AWS_SECRET_ACCESS_KEY" : "wJalr..." ,
159+ 			},
160+ 			expected : false ,
161+ 		},
162+ 		{
163+ 			name :      "Has suffixed credentials for no account" ,
164+ 			accountID : "" ,
165+ 			envVars : map [string ]string {
166+ 				"AWS_ACCESS_KEY_ID_123456789012" :     "AKIA..." ,
167+ 				"AWS_SECRET_ACCESS_KEY_123456789012" : "wJalr..." ,
168+ 			},
169+ 			expected : false ,
170+ 		},
171+ 		{
172+ 			name :      "Has suffixed credentials for different account" ,
173+ 			accountID : "987654321098" ,
174+ 			envVars : map [string ]string {
175+ 				"AWS_ACCESS_KEY_ID_123456789012" :     "AKIA..." ,
176+ 				"AWS_SECRET_ACCESS_KEY_123456789012" : "wJalr..." ,
177+ 			},
178+ 			expected : false ,
179+ 		},
180+ 	}
181+ 
182+ 	for  _ , tc  :=  range  useCases  {
183+ 		t .Run (tc .name , func (t  * testing.T ) {
184+ 			// Set environment variables 
185+ 			for  k , v  :=  range  tc .envVars  {
186+ 				t .Setenv (k , v )
187+ 			}
188+ 
189+ 			provider  :=  & ecrContext {
190+ 				AccountID : tc .accountID ,
191+ 			}
192+ 
193+ 			result  :=  provider .HasAccountSuffixedCredentials ()
194+ 			if  result  !=  tc .expected  {
195+ 				t .Errorf ("expected %v but got %v" , tc .expected , result )
196+ 			}
197+ 		})
198+ 	}
199+ }
0 commit comments