2021-12-20 04:43:09 -05:00
|
|
|
import * as core from '@actions/core';
|
2021-12-20 04:00:03 -05:00
|
|
|
import {ECR} from '@aws-sdk/client-ecr';
|
|
|
|
import {ECRPUBLIC} from '@aws-sdk/client-ecr-public';
|
2020-08-21 08:45:16 -04:00
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
|
|
|
|
|
|
|
|
export const isECR = (registry: string): boolean => {
|
|
|
|
return ecrRegistryRegex.test(registry) || isPubECR(registry);
|
2020-12-11 01:15:35 -05:00
|
|
|
};
|
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
export const isPubECR = (registry: string): boolean => {
|
2020-12-11 01:15:35 -05:00
|
|
|
return registry === 'public.ecr.aws';
|
2020-08-21 08:45:16 -04:00
|
|
|
};
|
|
|
|
|
2020-12-17 14:21:48 -05:00
|
|
|
export const getRegion = (registry: string): string => {
|
|
|
|
if (isPubECR(registry)) {
|
2020-12-11 01:15:35 -05:00
|
|
|
return process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';
|
|
|
|
}
|
2020-12-17 14:21:48 -05:00
|
|
|
const matches = registry.match(ecrRegistryRegex);
|
|
|
|
if (!matches) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return matches[3];
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAccountIDs = (registry: string): string[] => {
|
|
|
|
if (isPubECR(registry)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const matches = registry.match(ecrRegistryRegex);
|
|
|
|
if (!matches) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
let accountIDs: Array<string> = [matches[2]];
|
|
|
|
if (process.env.AWS_ACCOUNT_IDS) {
|
|
|
|
accountIDs.push(...process.env.AWS_ACCOUNT_IDS.split(','));
|
|
|
|
}
|
|
|
|
return accountIDs.filter((item, index) => accountIDs.indexOf(item) === index);
|
2020-08-21 09:07:43 -04:00
|
|
|
};
|
|
|
|
|
2021-12-20 04:43:09 -05:00
|
|
|
export interface RegistryData {
|
|
|
|
registry: string;
|
|
|
|
username: string;
|
|
|
|
password: string;
|
|
|
|
}
|
2020-08-21 08:45:16 -04:00
|
|
|
|
2021-12-20 04:43:09 -05:00
|
|
|
export const getRegistriesData = async (registry: string, username?: string, password?: string): Promise<RegistryData[]> => {
|
|
|
|
const region = getRegion(registry);
|
|
|
|
const accountIDs = getAccountIDs(registry);
|
2020-08-21 08:56:11 -04:00
|
|
|
|
2021-12-20 04:43:09 -05:00
|
|
|
const authTokenRequest = {};
|
|
|
|
if (accountIDs.length > 0) {
|
|
|
|
core.debug(`Requesting AWS ECR auth token for ${accountIDs.join(', ')}`);
|
|
|
|
authTokenRequest['registryIds'] = accountIDs;
|
2020-08-21 09:27:22 -04:00
|
|
|
}
|
|
|
|
|
2021-12-20 04:00:03 -05:00
|
|
|
const credentials =
|
|
|
|
username && password
|
|
|
|
? {
|
|
|
|
accessKeyId: username,
|
|
|
|
secretAccessKey: password
|
|
|
|
}
|
|
|
|
: undefined;
|
|
|
|
|
2021-12-20 04:43:09 -05:00
|
|
|
if (isPubECR(registry)) {
|
|
|
|
core.info(`AWS Public ECR detected with ${region} region`);
|
2021-12-20 04:00:03 -05:00
|
|
|
const ecrPublic = new ECRPUBLIC({
|
2021-12-20 04:43:09 -05:00
|
|
|
customUserAgent: 'docker-login-action',
|
2021-12-20 04:00:03 -05:00
|
|
|
credentials,
|
2021-12-20 04:43:09 -05:00
|
|
|
region: region
|
2020-08-21 09:27:22 -04:00
|
|
|
});
|
2021-12-20 04:00:03 -05:00
|
|
|
const authTokenResponse = await ecrPublic.getAuthorizationToken(authTokenRequest);
|
2021-12-20 04:43:09 -05:00
|
|
|
if (!authTokenResponse.authorizationData || !authTokenResponse.authorizationData.authorizationToken) {
|
|
|
|
throw new Error('Could not retrieve an authorization token from AWS Public ECR');
|
|
|
|
}
|
|
|
|
const authToken = Buffer.from(authTokenResponse.authorizationData.authorizationToken, 'base64').toString('utf-8');
|
|
|
|
const creds = authToken.split(':', 2);
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
registry: 'public.ecr.aws',
|
|
|
|
username: creds[0],
|
|
|
|
password: creds[1]
|
|
|
|
}
|
|
|
|
];
|
2020-08-21 09:27:22 -04:00
|
|
|
} else {
|
2021-12-20 04:43:09 -05:00
|
|
|
core.info(`AWS ECR detected with ${region} region`);
|
2021-12-20 04:00:03 -05:00
|
|
|
const ecr = new ECR({
|
2021-12-20 04:43:09 -05:00
|
|
|
customUserAgent: 'docker-login-action',
|
2021-12-20 04:00:03 -05:00
|
|
|
credentials,
|
2021-12-20 04:43:09 -05:00
|
|
|
region: region
|
2020-08-21 09:27:22 -04:00
|
|
|
});
|
2021-12-20 04:00:03 -05:00
|
|
|
const authTokenResponse = await ecr.getAuthorizationToken(authTokenRequest);
|
2021-12-20 04:43:09 -05:00
|
|
|
if (!Array.isArray(authTokenResponse.authorizationData) || !authTokenResponse.authorizationData.length) {
|
|
|
|
throw new Error('Could not retrieve an authorization token from AWS ECR');
|
|
|
|
}
|
|
|
|
const regDatas: RegistryData[] = [];
|
|
|
|
for (const authData of authTokenResponse.authorizationData) {
|
|
|
|
const authToken = Buffer.from(authData.authorizationToken || '', 'base64').toString('utf-8');
|
|
|
|
const creds = authToken.split(':', 2);
|
|
|
|
regDatas.push({
|
|
|
|
registry: authData.proxyEndpoint || '',
|
|
|
|
username: creds[0],
|
|
|
|
password: creds[1]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return regDatas;
|
2020-08-21 08:45:16 -04:00
|
|
|
}
|
|
|
|
};
|