Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-project): code artifact login command #1798

Merged
merged 2 commits into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/javascript/node-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,14 +920,14 @@ export class NodePackage extends Component {
{ exec: "which aws" }, // check that AWS CLI is installed
...this.scopedPackagesOptions.map((scopedPackagesOption) => {
const { registryUrl, scope } = scopedPackagesOption;
const { domain, repository, region, accountId } =
const { domain, region, accountId, registry } =
extractCodeArtifactDetails(registryUrl);
// reference: https://docs.aws.amazon.com/codeartifact/latest/ug/npm-auth.html
const commands = [
`npm config set ${scope}:registry ${registryUrl}`,
`CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain} --repository ${repository} --region ${region} --domain-owner ${accountId})`,
`npm config set //${registryUrl}:_authToken=$CODEARTIFACT_AUTH_TOKEN`,
`npm config set //${registryUrl}:always-auth=true`,
`CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain} --region ${region} --domain-owner ${accountId} --query authorizationToken --output text)`,
`npm config set //${registry}:_authToken=$CODEARTIFACT_AUTH_TOKEN`,
`npm config set //${registry}:always-auth=true`,
];
return {
exec: commands.join("; "),
Expand Down
6 changes: 3 additions & 3 deletions src/javascript/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function renderBundleName(entrypoint: string) {
* Regex for AWS CodeArtifact registry
*/
export const codeArtifactRegex =
/^https:\/\/(?<domain>[^\.]+)-(?<accountId>\d{12})\.d\.codeartifact\.(?<region>[^\.]+).*\.amazonaws\.com\/.*\/(?<repository>\w+)/;
/^https:\/\/(?<registry>(?<domain>[^\.]+)-(?<accountId>\d{12})\.d\.codeartifact\.(?<region>[^\.]+).*\.amazonaws\.com\/.*\/(?<repository>\w+)\/)/;

/**
* gets AWS details from the Code Artifact registry URL
Expand All @@ -28,8 +28,8 @@ export const codeArtifactRegex =
export function extractCodeArtifactDetails(registryUrl: string) {
const match = registryUrl.match(codeArtifactRegex);
if (match?.groups) {
const { domain, accountId, region, repository } = match.groups;
return { domain, accountId, region, repository };
const { domain, accountId, region, repository, registry } = match.groups;
return { domain, accountId, region, repository, registry };
}
throw new Error("Could not get CodeArtifact details from npm Registry");
}
Expand Down
12 changes: 7 additions & 5 deletions test/javascript/node-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,8 @@ describe("scoped private packages", () => {
const scope = "@stub-scope";
const defaultAccessKeyIdSecret = "AWS_ACCESS_KEY_ID";
const defaultSecretAccessKeySecret = "AWS_SECRET_ACCESS_KEY";
const registryUrl = `https://${domain}-${accountId}.d.codeartifact.${region}.amazonaws.com/npm/${repository}/`;
const registry = `${domain}-${accountId}.d.codeartifact.${region}.amazonaws.com/npm/${repository}/`;
const registryUrl = `https://${registry}`;

test("adds AWS Code Artifact Login step prior to install to build workflow", () => {
const project = new TestNodeProject({
Expand Down Expand Up @@ -1096,7 +1097,7 @@ describe("scoped private packages", () => {
exec: "which aws",
},
{
exec: `npm config set ${scope}:registry ${registryUrl}; CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain} --repository ${repository} --region ${region} --domain-owner ${accountId}); npm config set //${registryUrl}:_authToken=$CODEARTIFACT_AUTH_TOKEN; npm config set //${registryUrl}:always-auth=true`,
exec: `npm config set ${scope}:registry ${registryUrl}; CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain} --region ${region} --domain-owner ${accountId} --query authorizationToken --output text); npm config set //${registry}:_authToken=$CODEARTIFACT_AUTH_TOKEN; npm config set //${registry}:always-auth=true`,
},
],
});
Expand All @@ -1108,7 +1109,8 @@ describe("scoped private packages", () => {
const region2 = "my-region-2";
const repository2 = "MyRepository2";
const scope2 = "@stub-scope-2";
const registryUrl2 = `https://${domain2}-${accountId2}.d.codeartifact.${region2}.amazonaws.com/npm/${repository2}/`;
const registry2 = `${domain2}-${accountId2}.d.codeartifact.${region2}.amazonaws.com/npm/${repository2}/`;
const registryUrl2 = `https://${registry2}`;
const project = new TestNodeProject({
scopedPackagesOptions: [
{
Expand All @@ -1132,10 +1134,10 @@ describe("scoped private packages", () => {
exec: "which aws",
},
{
exec: `npm config set ${scope}:registry ${registryUrl}; CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain} --repository ${repository} --region ${region} --domain-owner ${accountId}); npm config set //${registryUrl}:_authToken=$CODEARTIFACT_AUTH_TOKEN; npm config set //${registryUrl}:always-auth=true`,
exec: `npm config set ${scope}:registry ${registryUrl}; CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain} --region ${region} --domain-owner ${accountId} --query authorizationToken --output text); npm config set //${registry}:_authToken=$CODEARTIFACT_AUTH_TOKEN; npm config set //${registry}:always-auth=true`,
},
{
exec: `npm config set ${scope2}:registry ${registryUrl2}; CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain2} --repository ${repository2} --region ${region2} --domain-owner ${accountId2}); npm config set //${registryUrl2}:_authToken=$CODEARTIFACT_AUTH_TOKEN; npm config set //${registryUrl2}:always-auth=true`,
exec: `npm config set ${scope2}:registry ${registryUrl2}; CODEARTIFACT_AUTH_TOKEN=$(aws codeartifact get-authorization-token --domain ${domain2} --region ${region2} --domain-owner ${accountId2} --query authorizationToken --output text); npm config set //${registry2}:_authToken=$CODEARTIFACT_AUTH_TOKEN; npm config set //${registry2}:always-auth=true`,
},
],
});
Expand Down