| 
									
										
										
										
											2022-02-28 08:28:55 +01:00
										 |  |  | import {beforeEach, describe, expect, jest, test} from '@jest/globals'; | 
					
						
							| 
									
										
										
										
											2021-12-20 10:00:03 +01:00
										 |  |  | import {AuthorizationData} from '@aws-sdk/client-ecr'; | 
					
						
							| 
									
										
										
										
											2023-09-08 09:35:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-21 14:45:16 +02:00
										 |  |  | import * as aws from '../src/aws'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('isECR', () => { | 
					
						
							|  |  |  |   test.each([ | 
					
						
							|  |  |  |     ['registry.gitlab.com', false], | 
					
						
							|  |  |  |     ['gcr.io', false], | 
					
						
							| 
									
										
										
										
											2020-12-11 07:15:35 +01:00
										 |  |  |     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', true], | 
					
						
							| 
									
										
										
										
											2020-12-17 20:21:48 +01:00
										 |  |  |     ['876820548815.dkr.ecr.cn-north-1.amazonaws.com.cn', true], | 
					
						
							|  |  |  |     ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', true], | 
					
						
							| 
									
										
										
										
											2025-07-21 18:43:51 +02:00
										 |  |  |     ['012345678901.dkr-ecr.eu-north-1.on.aws', true], | 
					
						
							| 
									
										
										
										
											2025-08-01 14:27:31 +02:00
										 |  |  |     ['public.ecr.aws', true], | 
					
						
							|  |  |  |     ['ecr-public.aws.com', true] | 
					
						
							| 
									
										
										
										
											2020-08-21 14:45:16 +02:00
										 |  |  |   ])('given registry %p', async (registry, expected) => { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |     expect(aws.isECR(registry)).toEqual(expected); | 
					
						
							| 
									
										
										
										
											2020-08-21 14:45:16 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-11 07:15:35 +01:00
										 |  |  | describe('isPubECR', () => { | 
					
						
							|  |  |  |   test.each([ | 
					
						
							|  |  |  |     ['registry.gitlab.com', false], | 
					
						
							|  |  |  |     ['gcr.io', false], | 
					
						
							|  |  |  |     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', false], | 
					
						
							| 
									
										
										
										
											2020-12-17 20:21:48 +01:00
										 |  |  |     ['876820548815.dkr.ecr.cn-north-1.amazonaws.com.cn', false], | 
					
						
							|  |  |  |     ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', false], | 
					
						
							| 
									
										
										
										
											2025-07-21 18:43:51 +02:00
										 |  |  |     ['012345678901.dkr-ecr.eu-north-1.on.aws', false], | 
					
						
							| 
									
										
										
										
											2025-08-01 14:27:31 +02:00
										 |  |  |     ['public.ecr.aws', true], | 
					
						
							|  |  |  |     ['ecr-public.aws.com', true] | 
					
						
							| 
									
										
										
										
											2020-12-11 07:15:35 +01:00
										 |  |  |   ])('given registry %p', async (registry, expected) => { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |     expect(aws.isPubECR(registry)).toEqual(expected); | 
					
						
							| 
									
										
										
										
											2020-08-21 14:45:16 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('getRegion', () => { | 
					
						
							| 
									
										
										
										
											2020-12-11 07:15:35 +01:00
										 |  |  |   test.each([ | 
					
						
							|  |  |  |     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', 'eu-west-3'], | 
					
						
							| 
									
										
										
										
											2020-12-17 20:21:48 +01:00
										 |  |  |     ['876820548815.dkr.ecr.cn-north-1.amazonaws.com.cn', 'cn-north-1'], | 
					
						
							|  |  |  |     ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', 'cn-northwest-1'], | 
					
						
							| 
									
										
										
										
											2025-07-21 18:43:51 +02:00
										 |  |  |     ['012345678901.dkr-ecr.eu-north-1.on.aws', 'eu-north-1'], | 
					
						
							| 
									
										
										
										
											2020-12-11 07:15:35 +01:00
										 |  |  |     ['public.ecr.aws', 'us-east-1'] | 
					
						
							|  |  |  |   ])('given registry %p', async (registry, expected) => { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |     expect(aws.getRegion(registry)).toEqual(expected); | 
					
						
							| 
									
										
										
										
											2020-12-11 07:15:35 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-21 14:45:16 +02:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-12-17 20:21:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('getAccountIDs', () => { | 
					
						
							|  |  |  |   test.each([ | 
					
						
							|  |  |  |     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', undefined, ['012345678901']], | 
					
						
							| 
									
										
										
										
											2021-07-06 20:24:08 +02:00
										 |  |  |     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', '012345678910,023456789012', ['012345678901', '012345678910', '023456789012']], | 
					
						
							|  |  |  |     ['012345678901.dkr.ecr.eu-west-3.amazonaws.com', '012345678901,012345678910,023456789012', ['012345678901', '012345678910', '023456789012']], | 
					
						
							|  |  |  |     ['390948362332.dkr.ecr.cn-northwest-1.amazonaws.com.cn', '012345678910,023456789012', ['390948362332', '012345678910', '023456789012']], | 
					
						
							| 
									
										
										
										
											2025-07-21 18:43:51 +02:00
										 |  |  |     ['876820548815.dkr-ecr.eu-north-1.on.aws', '012345678910,023456789012', ['876820548815', '012345678910', '023456789012']], | 
					
						
							| 
									
										
										
										
											2020-12-17 20:21:48 +01:00
										 |  |  |     ['public.ecr.aws', undefined, []] | 
					
						
							|  |  |  |   ])('given registry %p', async (registry, accountIDsEnv, expected) => { | 
					
						
							|  |  |  |     if (accountIDsEnv) { | 
					
						
							|  |  |  |       process.env.AWS_ACCOUNT_IDS = accountIDsEnv; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |     expect(aws.getAccountIDs(registry)).toEqual(expected); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const mockEcrGetAuthToken = jest.fn(); | 
					
						
							|  |  |  | const mockEcrPublicGetAuthToken = jest.fn(); | 
					
						
							| 
									
										
										
										
											2021-12-20 10:00:03 +01:00
										 |  |  | jest.mock('@aws-sdk/client-ecr', () => { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |   return { | 
					
						
							|  |  |  |     ECR: jest.fn(() => ({ | 
					
						
							|  |  |  |       getAuthorizationToken: mockEcrGetAuthToken | 
					
						
							| 
									
										
										
										
											2021-12-20 10:00:03 +01:00
										 |  |  |     })) | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | jest.mock('@aws-sdk/client-ecr-public', () => { | 
					
						
							|  |  |  |   return { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |     ECRPUBLIC: jest.fn(() => ({ | 
					
						
							|  |  |  |       getAuthorizationToken: mockEcrPublicGetAuthToken | 
					
						
							|  |  |  |     })) | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('getRegistriesData', () => { | 
					
						
							|  |  |  |   beforeEach(() => { | 
					
						
							|  |  |  |     jest.clearAllMocks(); | 
					
						
							|  |  |  |     delete process.env.AWS_ACCOUNT_IDS; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   // prettier-ignore
 | 
					
						
							|  |  |  |   test.each([ | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       '012345678901.dkr.ecr.aws-region-1.amazonaws.com', | 
					
						
							|  |  |  |       'dkr.ecr.aws-region-1.amazonaws.com', undefined, | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           registry: '012345678901.dkr.ecr.aws-region-1.amazonaws.com', | 
					
						
							|  |  |  |           username: '012345678901', | 
					
						
							|  |  |  |           password: 'world' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       '012345678901.dkr.ecr.eu-west-3.amazonaws.com', | 
					
						
							|  |  |  |       'dkr.ecr.eu-west-3.amazonaws.com', | 
					
						
							|  |  |  |       '012345678910,023456789012', | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           registry: '012345678901.dkr.ecr.eu-west-3.amazonaws.com', | 
					
						
							|  |  |  |           username: '012345678901', | 
					
						
							|  |  |  |           password: 'world' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           registry: '012345678910.dkr.ecr.eu-west-3.amazonaws.com', | 
					
						
							|  |  |  |           username: '012345678910', | 
					
						
							|  |  |  |           password: 'world' | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           registry: '023456789012.dkr.ecr.eu-west-3.amazonaws.com', | 
					
						
							|  |  |  |           username: '023456789012', | 
					
						
							|  |  |  |           password: 'world' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |       'public.ecr.aws', | 
					
						
							|  |  |  |       undefined, | 
					
						
							|  |  |  |       undefined, | 
					
						
							|  |  |  |       [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           registry: 'public.ecr.aws', | 
					
						
							|  |  |  |           username: 'AWS', | 
					
						
							|  |  |  |           password: 'world' | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ] | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   ])('given registry %p', async (registry, fqdn, accountIDsEnv, expected: aws.RegistryData[]) => { | 
					
						
							|  |  |  |     if (accountIDsEnv) { | 
					
						
							|  |  |  |       process.env.AWS_ACCOUNT_IDS = accountIDsEnv; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const accountIDs = aws.getAccountIDs(registry); | 
					
						
							|  |  |  |     const authData: AuthorizationData[] = []; | 
					
						
							|  |  |  |     if (accountIDs.length == 0) { | 
					
						
							|  |  |  |       mockEcrPublicGetAuthToken.mockImplementation(() => { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:00:03 +01:00
										 |  |  |         return Promise.resolve({ | 
					
						
							|  |  |  |           authorizationData: { | 
					
						
							|  |  |  |             authorizationToken: Buffer.from(`AWS:world`).toString('base64'), | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |           } | 
					
						
							| 
									
										
										
										
											2021-12-20 10:00:03 +01:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |       }); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       aws.getAccountIDs(registry).forEach(accountID => { | 
					
						
							|  |  |  |         authData.push({ | 
					
						
							|  |  |  |           authorizationToken: Buffer.from(`${accountID}:world`).toString('base64'), | 
					
						
							|  |  |  |           proxyEndpoint: `${accountID}.${fqdn}` | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       mockEcrGetAuthToken.mockImplementation(() => { | 
					
						
							| 
									
										
										
										
											2021-12-20 10:00:03 +01:00
										 |  |  |         return Promise.resolve({ | 
					
						
							|  |  |  |           authorizationData: authData | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-12-20 10:43:09 +01:00
										 |  |  |       }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     const regData = await aws.getRegistriesData(registry); | 
					
						
							|  |  |  |     expect(regData).toEqual(expected); | 
					
						
							| 
									
										
										
										
											2020-12-17 20:21:48 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |