+14
-18
@@ -1,5 +1,5 @@
|
||||
import {AuthorizationData} from '@aws-sdk/client-ecr';
|
||||
import * as aws from '../src/aws';
|
||||
import {AuthorizationData} from 'aws-sdk/clients/ecr';
|
||||
|
||||
describe('isECR', () => {
|
||||
test.each([
|
||||
@@ -55,11 +55,15 @@ describe('getAccountIDs', () => {
|
||||
|
||||
const mockEcrGetAuthToken = jest.fn();
|
||||
const mockEcrPublicGetAuthToken = jest.fn();
|
||||
jest.mock('aws-sdk', () => {
|
||||
jest.mock('@aws-sdk/client-ecr', () => {
|
||||
return {
|
||||
ECR: jest.fn(() => ({
|
||||
getAuthorizationToken: mockEcrGetAuthToken
|
||||
})),
|
||||
}))
|
||||
};
|
||||
});
|
||||
jest.mock('@aws-sdk/client-ecr-public', () => {
|
||||
return {
|
||||
ECRPUBLIC: jest.fn(() => ({
|
||||
getAuthorizationToken: mockEcrPublicGetAuthToken
|
||||
}))
|
||||
@@ -126,15 +130,11 @@ describe('getRegistriesData', () => {
|
||||
const authData: AuthorizationData[] = [];
|
||||
if (accountIDs.length == 0) {
|
||||
mockEcrPublicGetAuthToken.mockImplementation(() => {
|
||||
return {
|
||||
promise() {
|
||||
return Promise.resolve({
|
||||
authorizationData: {
|
||||
authorizationToken: Buffer.from(`AWS:world`).toString('base64'),
|
||||
}
|
||||
});
|
||||
return Promise.resolve({
|
||||
authorizationData: {
|
||||
authorizationToken: Buffer.from(`AWS:world`).toString('base64'),
|
||||
}
|
||||
};
|
||||
});
|
||||
});
|
||||
} else {
|
||||
aws.getAccountIDs(registry).forEach(accountID => {
|
||||
@@ -144,13 +144,9 @@ describe('getRegistriesData', () => {
|
||||
});
|
||||
});
|
||||
mockEcrGetAuthToken.mockImplementation(() => {
|
||||
return {
|
||||
promise() {
|
||||
return Promise.resolve({
|
||||
authorizationData: authData
|
||||
});
|
||||
}
|
||||
};
|
||||
return Promise.resolve({
|
||||
authorizationData: authData
|
||||
});
|
||||
});
|
||||
}
|
||||
const regData = await aws.getRegistriesData(registry);
|
||||
|
||||
+27897
-39044
File diff suppressed because one or more lines are too long
+2
-1
@@ -30,7 +30,8 @@
|
||||
"@actions/core": "^1.6.0",
|
||||
"@actions/exec": "^1.1.0",
|
||||
"@actions/io": "^1.1.1",
|
||||
"aws-sdk": "^2.1046.0"
|
||||
"@aws-sdk/client-ecr": "^3.44.0",
|
||||
"@aws-sdk/client-ecr-public": "^3.43.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/jest": "^26.0.23",
|
||||
|
||||
+16
-9
@@ -1,5 +1,6 @@
|
||||
import * as core from '@actions/core';
|
||||
import * as aws from 'aws-sdk';
|
||||
import {ECR} from '@aws-sdk/client-ecr';
|
||||
import {ECRPUBLIC} from '@aws-sdk/client-ecr-public';
|
||||
|
||||
const ecrRegistryRegex = /^(([0-9]{12})\.dkr\.ecr\.(.+)\.amazonaws\.com(.cn)?)(\/([^:]+)(:.+)?)?$/;
|
||||
|
||||
@@ -53,15 +54,22 @@ export const getRegistriesData = async (registry: string, username?: string, pas
|
||||
authTokenRequest['registryIds'] = accountIDs;
|
||||
}
|
||||
|
||||
const credentials =
|
||||
username && password
|
||||
? {
|
||||
accessKeyId: username,
|
||||
secretAccessKey: password
|
||||
}
|
||||
: undefined;
|
||||
|
||||
if (isPubECR(registry)) {
|
||||
core.info(`AWS Public ECR detected with ${region} region`);
|
||||
const ecrPublic = new aws.ECRPUBLIC({
|
||||
const ecrPublic = new ECRPUBLIC({
|
||||
customUserAgent: 'docker-login-action',
|
||||
accessKeyId: username || process.env.AWS_ACCESS_KEY_ID || '',
|
||||
secretAccessKey: password || process.env.AWS_SECRET_ACCESS_KEY || '',
|
||||
credentials,
|
||||
region: region
|
||||
});
|
||||
const authTokenResponse = await ecrPublic.getAuthorizationToken(authTokenRequest).promise();
|
||||
const authTokenResponse = await ecrPublic.getAuthorizationToken(authTokenRequest);
|
||||
if (!authTokenResponse.authorizationData || !authTokenResponse.authorizationData.authorizationToken) {
|
||||
throw new Error('Could not retrieve an authorization token from AWS Public ECR');
|
||||
}
|
||||
@@ -76,13 +84,12 @@ export const getRegistriesData = async (registry: string, username?: string, pas
|
||||
];
|
||||
} else {
|
||||
core.info(`AWS ECR detected with ${region} region`);
|
||||
const ecr = new aws.ECR({
|
||||
const ecr = new ECR({
|
||||
customUserAgent: 'docker-login-action',
|
||||
accessKeyId: username || process.env.AWS_ACCESS_KEY_ID || '',
|
||||
secretAccessKey: password || process.env.AWS_SECRET_ACCESS_KEY || '',
|
||||
credentials,
|
||||
region: region
|
||||
});
|
||||
const authTokenResponse = await ecr.getAuthorizationToken(authTokenRequest).promise();
|
||||
const authTokenResponse = await ecr.getAuthorizationToken(authTokenRequest);
|
||||
if (!Array.isArray(authTokenResponse.authorizationData) || !authTokenResponse.authorizationData.length) {
|
||||
throw new Error('Could not retrieve an authorization token from AWS ECR');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user