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