1
1
const core = require ( '@actions/core' ) ;
2
2
const assert = require ( 'assert' ) ;
3
-
3
+ const aws = require ( 'aws-sdk' ) ;
4
4
const run = require ( '.' ) ;
5
5
6
6
jest . mock ( '@actions/core' ) ;
@@ -49,6 +49,9 @@ const mockStsAssumeRole = jest.fn();
49
49
50
50
jest . mock ( 'aws-sdk' , ( ) => {
51
51
return {
52
+ config : {
53
+ getCredentials : jest . fn ( )
54
+ } ,
52
55
STS : jest . fn ( ( ) => ( {
53
56
getCallerIdentity : mockStsCallerIdentity ,
54
57
assumeRole : mockStsAssumeRole ,
@@ -82,6 +85,27 @@ describe('Configure AWS Credentials', () => {
82
85
}
83
86
} ) ;
84
87
88
+ aws . config . getCredentials . mockReset ( ) ;
89
+ aws . config . getCredentials
90
+ . mockImplementationOnce ( callback => {
91
+ if ( ! aws . config . credentials ) {
92
+ aws . config . credentials = {
93
+ accessKeyId : FAKE_ACCESS_KEY_ID ,
94
+ secretAccessKey : FAKE_SECRET_ACCESS_KEY
95
+ }
96
+ }
97
+ callback ( null ) ;
98
+ } )
99
+ . mockImplementationOnce ( callback => {
100
+ if ( ! aws . config . credentials ) {
101
+ aws . config . credentials = {
102
+ accessKeyId : FAKE_STS_ACCESS_KEY_ID ,
103
+ secretAccessKey : FAKE_STS_SECRET_ACCESS_KEY
104
+ }
105
+ }
106
+ callback ( null ) ;
107
+ } ) ;
108
+
85
109
mockStsAssumeRole . mockImplementation ( ( ) => {
86
110
return {
87
111
promise ( ) {
@@ -134,6 +158,59 @@ describe('Configure AWS Credentials', () => {
134
158
expect ( core . setSecret ) . toHaveBeenCalledWith ( FAKE_ACCOUNT_ID ) ;
135
159
} ) ;
136
160
161
+ test ( 'action with no accessible credentials fails' , async ( ) => {
162
+ process . env . SHOW_STACK_TRACE = 'false' ;
163
+ const mockInputs = { 'aws-region' : FAKE_REGION } ;
164
+ core . getInput = jest
165
+ . fn ( )
166
+ . mockImplementation ( mockGetInput ( mockInputs ) ) ;
167
+ aws . config . getCredentials . mockReset ( ) ;
168
+ aws . config . getCredentials . mockImplementation ( callback => {
169
+ callback ( new Error ( 'No credentials to load' ) ) ;
170
+ } ) ;
171
+
172
+ await run ( ) ;
173
+
174
+ expect ( core . setFailed ) . toHaveBeenCalledWith ( "Credentials could not be loaded, please check your action inputs: No credentials to load" ) ;
175
+ } ) ;
176
+
177
+ test ( 'action with empty credentials fails' , async ( ) => {
178
+ process . env . SHOW_STACK_TRACE = 'false' ;
179
+ const mockInputs = { 'aws-region' : FAKE_REGION } ;
180
+ core . getInput = jest
181
+ . fn ( )
182
+ . mockImplementation ( mockGetInput ( mockInputs ) ) ;
183
+ aws . config . getCredentials . mockReset ( ) ;
184
+ aws . config . getCredentials . mockImplementation ( callback => {
185
+ aws . config . credentials = {
186
+ accessKeyId : ''
187
+ }
188
+ callback ( null ) ;
189
+ } ) ;
190
+
191
+ await run ( ) ;
192
+
193
+ expect ( core . setFailed ) . toHaveBeenCalledWith ( "Credentials could not be loaded, please check your action inputs: Access key ID empty after loading credentials" ) ;
194
+ } ) ;
195
+
196
+ test ( 'action fails when credentials are not set in the SDK correctly' , async ( ) => {
197
+ process . env . SHOW_STACK_TRACE = 'false' ;
198
+ core . getInput = jest
199
+ . fn ( )
200
+ . mockImplementation ( mockGetInput ( ASSUME_ROLE_INPUTS ) ) ;
201
+ aws . config . getCredentials . mockReset ( ) ;
202
+ aws . config . getCredentials . mockImplementation ( callback => {
203
+ aws . config . credentials = {
204
+ accessKeyId : FAKE_ACCESS_KEY_ID
205
+ }
206
+ callback ( null ) ;
207
+ } ) ;
208
+
209
+ await run ( ) ;
210
+
211
+ expect ( core . setFailed ) . toHaveBeenCalledWith ( "Unexpected failure: Credentials loaded by the SDK do not match the access key ID configured by the action" ) ;
212
+ } ) ;
213
+
137
214
test ( 'session token is optional' , async ( ) => {
138
215
const mockInputs = { ...CREDS_INPUTS , 'aws-region' : 'eu-west-1' } ;
139
216
core . getInput = jest
@@ -154,12 +231,19 @@ describe('Configure AWS Credentials', () => {
154
231
expect ( core . setSecret ) . toHaveBeenCalledWith ( FAKE_ACCOUNT_ID ) ;
155
232
} ) ;
156
233
157
- test ( 'session token is cleared if necessary ' , async ( ) => {
234
+ test ( 'existing env var creds are cleared ' , async ( ) => {
158
235
const mockInputs = { ...CREDS_INPUTS , 'aws-region' : 'eu-west-1' } ;
159
236
core . getInput = jest
160
237
. fn ( )
161
238
. mockImplementation ( mockGetInput ( mockInputs ) ) ;
239
+ process . env . AWS_ACCESS_KEY_ID = 'foo' ;
240
+ process . env . AWS_SECRET_ACCESS_KEY = 'bar' ;
162
241
process . env . AWS_SESSION_TOKEN = 'helloworld' ;
242
+ aws . config . credentials = {
243
+ accessKeyId : 'foo' ,
244
+ secretAccessKey : 'bar' ,
245
+ sessionToken : 'helloworld'
246
+ } ;
163
247
164
248
await run ( ) ;
165
249
expect ( mockStsAssumeRole ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -174,6 +258,9 @@ describe('Configure AWS Credentials', () => {
174
258
expect ( core . exportVariable ) . toHaveBeenCalledWith ( 'AWS_REGION' , 'eu-west-1' ) ;
175
259
expect ( core . setOutput ) . toHaveBeenCalledWith ( 'aws-account-id' , FAKE_ACCOUNT_ID ) ;
176
260
expect ( core . setSecret ) . toHaveBeenCalledWith ( FAKE_ACCOUNT_ID ) ;
261
+ expect ( aws . config . credentials . accessKeyId ) . toBe ( FAKE_ACCESS_KEY_ID ) ;
262
+ expect ( aws . config . credentials . secretAccessKey ) . toBe ( FAKE_SECRET_ACCESS_KEY ) ;
263
+ expect ( aws . config . credentials . sessionToken ) . toBeUndefined ( ) ;
177
264
} ) ;
178
265
179
266
test ( 'validates region name' , async ( ) => {
0 commit comments