11const core = require ( '@actions/core' ) ;
22const assert = require ( 'assert' ) ;
3-
3+ const aws = require ( 'aws-sdk' ) ;
44const run = require ( '.' ) ;
55
66jest . mock ( '@actions/core' ) ;
@@ -49,6 +49,9 @@ const mockStsAssumeRole = jest.fn();
4949
5050jest . mock ( 'aws-sdk' , ( ) => {
5151 return {
52+ config : {
53+ getCredentials : jest . fn ( )
54+ } ,
5255 STS : jest . fn ( ( ) => ( {
5356 getCallerIdentity : mockStsCallerIdentity ,
5457 assumeRole : mockStsAssumeRole ,
@@ -82,6 +85,27 @@ describe('Configure AWS Credentials', () => {
8285 }
8386 } ) ;
8487
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+
85109 mockStsAssumeRole . mockImplementation ( ( ) => {
86110 return {
87111 promise ( ) {
@@ -134,6 +158,59 @@ describe('Configure AWS Credentials', () => {
134158 expect ( core . setSecret ) . toHaveBeenCalledWith ( FAKE_ACCOUNT_ID ) ;
135159 } ) ;
136160
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+
137214 test ( 'session token is optional' , async ( ) => {
138215 const mockInputs = { ...CREDS_INPUTS , 'aws-region' : 'eu-west-1' } ;
139216 core . getInput = jest
@@ -154,12 +231,19 @@ describe('Configure AWS Credentials', () => {
154231 expect ( core . setSecret ) . toHaveBeenCalledWith ( FAKE_ACCOUNT_ID ) ;
155232 } ) ;
156233
157- test ( 'session token is cleared if necessary ' , async ( ) => {
234+ test ( 'existing env var creds are cleared ' , async ( ) => {
158235 const mockInputs = { ...CREDS_INPUTS , 'aws-region' : 'eu-west-1' } ;
159236 core . getInput = jest
160237 . fn ( )
161238 . mockImplementation ( mockGetInput ( mockInputs ) ) ;
239+ process . env . AWS_ACCESS_KEY_ID = 'foo' ;
240+ process . env . AWS_SECRET_ACCESS_KEY = 'bar' ;
162241 process . env . AWS_SESSION_TOKEN = 'helloworld' ;
242+ aws . config . credentials = {
243+ accessKeyId : 'foo' ,
244+ secretAccessKey : 'bar' ,
245+ sessionToken : 'helloworld'
246+ } ;
163247
164248 await run ( ) ;
165249 expect ( mockStsAssumeRole ) . toHaveBeenCalledTimes ( 0 ) ;
@@ -174,6 +258,9 @@ describe('Configure AWS Credentials', () => {
174258 expect ( core . exportVariable ) . toHaveBeenCalledWith ( 'AWS_REGION' , 'eu-west-1' ) ;
175259 expect ( core . setOutput ) . toHaveBeenCalledWith ( 'aws-account-id' , FAKE_ACCOUNT_ID ) ;
176260 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 ( ) ;
177264 } ) ;
178265
179266 test ( 'validates region name' , async ( ) => {
0 commit comments