Skip to content

Commit

Permalink
Merge pull request #436 from berviantoleo/feat/update-aws-sdk-v3
Browse files Browse the repository at this point in the history
feat: Update to AWS SDK v3
  • Loading branch information
arjraman committed Jun 19, 2023
2 parents 303ccb9 + b088af0 commit 946b2aa
Show file tree
Hide file tree
Showing 4 changed files with 4,212 additions and 3,222 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const core = require('@actions/core');
const exec = require('@actions/exec');
const aws = require('aws-sdk');
const proxy = require('https-proxy-agent');
const { ECRClient, GetAuthorizationTokenCommand } = require("@aws-sdk/client-ecr");
const { ECRPUBLICClient, GetAuthorizationTokenCommand: GetAuthorizationTokenCommandPublic } = require("@aws-sdk/client-ecr-public");

const ECR_LOGIN_GITHUB_ACTION_USER_AGENT = 'amazon-ecr-login-for-github-actions';
const ECR_PUBLIC_REGISTRY_URI = 'public.ecr.aws';
Expand All @@ -28,32 +29,32 @@ const REGISTRY_TYPES = {
public: 'public'
};


function configureProxy(httpProxy) {
const proxyFromEnv = process.env.HTTP_PROXY || process.env.http_proxy;

if (httpProxy || proxyFromEnv) {
let proxyToSet;

if (httpProxy){
if (httpProxy) {
core.info(`Setting proxy from action input: ${httpProxy}`);
proxyToSet = httpProxy;
} else {
core.info(`Setting proxy from environment: ${proxyFromEnv}`);
proxyToSet = proxyFromEnv;
}

aws.config.update({
httpOptions: { agent: proxy(proxyToSet) }
});
const httpOptions = { agent: proxy(proxyToSet) };
return httpOptions;
}
return null;
}

async function getEcrAuthTokenWrapper(authTokenRequest) {
const ecr = new aws.ECR({
async function getEcrAuthTokenWrapper(authTokenRequest, httpOptions) {
const ecr = new ECRClient({
customUserAgent: ECR_LOGIN_GITHUB_ACTION_USER_AGENT
});
const authTokenResponse = await ecr.getAuthorizationToken(authTokenRequest).promise();
const command = new GetAuthorizationTokenCommand(authTokenRequest);
const authTokenResponse = await ecr.send(command, httpOptions);
if (!authTokenResponse) {
throw new Error('Amazon ECR authorization token returned no data');
} else if (!authTokenResponse.authorizationData || !Array.isArray(authTokenResponse.authorizationData)) {
Expand All @@ -65,11 +66,12 @@ async function getEcrAuthTokenWrapper(authTokenRequest) {
return authTokenResponse;
}

async function getEcrPublicAuthTokenWrapper(authTokenRequest) {
const ecrPublic = new aws.ECRPUBLIC({
async function getEcrPublicAuthTokenWrapper(authTokenRequest, httpOptions) {
const ecrPublic = new ECRPUBLICClient({
customUserAgent: ECR_LOGIN_GITHUB_ACTION_USER_AGENT
});
const authTokenResponse = await ecrPublic.getAuthorizationToken(authTokenRequest).promise();
const command = new GetAuthorizationTokenCommandPublic(authTokenRequest);
const authTokenResponse = await ecrPublic.send(command, httpOptions);
if (!authTokenResponse) {
throw new Error('Amazon ECR Public authorization token returned no data');
} else if (!authTokenResponse.authorizationData) {
Expand Down Expand Up @@ -107,7 +109,7 @@ async function run() {
}

// Configures proxy
configureProxy(httpProxy);
const proxyOptions = configureProxy(httpProxy);

// Get the ECR/ECR Public authorization token(s)
const authTokenRequest = {};
Expand All @@ -120,15 +122,15 @@ async function run() {
authTokenRequest.registryIds = registryIds;
}
const authTokenResponse = registryType === REGISTRY_TYPES.private ?
await getEcrAuthTokenWrapper(authTokenRequest) :
await getEcrPublicAuthTokenWrapper(authTokenRequest);
await getEcrAuthTokenWrapper(authTokenRequest, proxyOptions) :
await getEcrPublicAuthTokenWrapper(authTokenRequest, proxyOptions);

// Login to each registry
for (const authData of authTokenResponse.authorizationData) {
const authToken = Buffer.from(authData.authorizationToken, 'base64').toString('utf-8');
const creds = authToken.split(':', 2);
const proxyEndpoint = authData.proxyEndpoint;
const registryUri = proxyEndpoint.replace(/^https?:\/\//,'');
const registryUri = proxyEndpoint.replace(/^https?:\/\//, '');

core.info(`Logging into registry ${registryUri}`);

Expand Down Expand Up @@ -179,6 +181,7 @@ async function run() {
}

module.exports = {
configureProxy,
run,
replaceSpecialCharacters
};
Expand Down

0 comments on commit 946b2aa

Please sign in to comment.