Skip to content

Commit 455c514

Browse files
authored
fix: parsing arn with multiple slashes when importing auth (#13009)
1 parent b839ff1 commit 455c514

File tree

7 files changed

+94
-5
lines changed

7 files changed

+94
-5
lines changed

packages/amplify-cli-core/API.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import Ajv from 'ajv';
1010
import { ApiKeyConfig } from '@aws-amplify/graphql-transformer-interfaces';
11+
import { ARN } from '@aws-sdk/util-arn-parser';
1112
import { BuildType } from '@aws-amplify/amplify-function-plugin-interface';
1213
import * as cdk from 'aws-cdk-lib';
1314
import { ChildProcess } from 'child_process';
@@ -1505,6 +1506,9 @@ export const packageManagers: Record<PackageManagerType, PackageManager>;
15051506
// @public (undocumented)
15061507
export type PackageManagerType = 'yarn' | 'npm' | 'pnpm' | 'custom';
15071508

1509+
// @public (undocumented)
1510+
export const parseArn: (arn: string) => ARN;
1511+
15081512
// @public (undocumented)
15091513
export function parseHelpCommands(input: $TSAny, commandsInfo: Array<CommandInfo>): {
15101514
command: string;

packages/amplify-cli-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"@aws-amplify/amplify-function-plugin-interface": "1.11.0",
3333
"@aws-amplify/amplify-prompts": "2.8.0",
3434
"@aws-amplify/graphql-transformer-interfaces": "^2.2.2",
35+
"@aws-sdk/util-arn-parser": "^3.310.0",
3536
"@yarnpkg/lockfile": "^1.1.0",
3637
"ajv": "^6.12.6",
3738
"aws-cdk-lib": "~2.68.0",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { ARN, parse } from '@aws-sdk/util-arn-parser';
2+
3+
export const parseArn = (arn: string): ARN => {
4+
return parse(arn);
5+
};

packages/amplify-cli-core/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './arn-parser';
12
export * from './doc-links';
23
export * from './fileSize';
34
/* eslint-disable import/no-cycle */
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { $TSContext } from '@aws-amplify/amplify-cli-core';
2+
import { createIdentityPoolService } from '../../aws-utils/IdentityPoolService';
3+
import { loadConfiguration } from '../../configuration-manager';
4+
5+
let mockCognitoIdentityRoles = {
6+
authenticated: 'arn:aws:iam::123456789012:role/service-role/my-auth-role',
7+
unauthenticated: 'arn:aws:iam::123456789012:role/service-role/my-unauth-role',
8+
};
9+
10+
jest.mock('aws-sdk', () => {
11+
return {
12+
CognitoIdentity: jest.fn(() => {
13+
return {
14+
config: {},
15+
getIdentityPoolRoles: jest.fn().mockImplementation(() => ({
16+
promise: async () => {
17+
return {
18+
Roles: mockCognitoIdentityRoles,
19+
};
20+
},
21+
})),
22+
};
23+
}),
24+
};
25+
});
26+
27+
jest.mock('../../configuration-manager', () => {
28+
return {
29+
loadConfiguration: jest.fn().mockReturnValue({}) as jest.MockedFunction<typeof loadConfiguration>,
30+
};
31+
});
32+
33+
describe('IdentityPoolService', () => {
34+
it('should correctly parse arn if it contains multiple forward slashes', async () => {
35+
const idpService = await createIdentityPoolService({} as unknown as $TSContext, {});
36+
expect(await idpService.getIdentityPoolRoles('mockIdpId')).toEqual({
37+
authRoleArn: 'arn:aws:iam::123456789012:role/service-role/my-auth-role',
38+
authRoleName: 'service-role/my-auth-role',
39+
unauthRoleArn: 'arn:aws:iam::123456789012:role/service-role/my-unauth-role',
40+
unauthRoleName: 'service-role/my-unauth-role',
41+
});
42+
});
43+
44+
it('should correctly parse arn if it contains a single forward slash', async () => {
45+
const idpService = await createIdentityPoolService({} as unknown as $TSContext, {});
46+
mockCognitoIdentityRoles = {
47+
authenticated: 'arn:aws:iam::123456789012:role/my-auth-role',
48+
unauthenticated: 'arn:aws:iam::123456789012:role/my-unauth-role',
49+
};
50+
51+
expect(await idpService.getIdentityPoolRoles('mockIdpId')).toEqual({
52+
authRoleArn: 'arn:aws:iam::123456789012:role/my-auth-role',
53+
authRoleName: 'my-auth-role',
54+
unauthRoleArn: 'arn:aws:iam::123456789012:role/my-unauth-role',
55+
unauthRoleName: 'my-unauth-role',
56+
});
57+
});
58+
59+
it('should fail to parse arn if it contains no forward slash', async () => {
60+
const idpService = await createIdentityPoolService({} as unknown as $TSContext, {});
61+
mockCognitoIdentityRoles = {
62+
authenticated: 'arn:aws:iam::123456789012:my-auth-role',
63+
unauthenticated: 'arn:aws:iam::123456789012:my-unauth-role',
64+
};
65+
66+
await expect(idpService.getIdentityPoolRoles('mockIdpId')).rejects.toBeDefined();
67+
});
68+
});

packages/amplify-provider-awscloudformation/src/aws-utils/IdentityPoolService.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { $TSAny, $TSContext, AmplifyFault, AmplifyError } from '@aws-amplify/amplify-cli-core';
1+
import { $TSAny, $TSContext, AmplifyFault, AmplifyError, parseArn } from '@aws-amplify/amplify-cli-core';
22
import { IIdentityPoolService } from '@aws-amplify/amplify-util-import';
33
import { CognitoIdentity } from 'aws-sdk';
44
import { PaginationKey, IdentityPool, IdentityPoolShortDescription, ListIdentityPoolsResponse } from 'aws-sdk/clients/cognitoidentity';
@@ -101,10 +101,10 @@ export class IdentityPoolService implements IIdentityPoolService {
101101
let resourceName;
102102

103103
if (arn) {
104-
const parts = arn.split('/');
105-
106-
if (parts.length === 2) {
107-
resourceName = parts[1];
104+
const fullRoleName = parseArn(arn).resource;
105+
const parts = fullRoleName.split('/');
106+
if (parts.length >= 2) {
107+
resourceName = parts.slice(1).join('/');
108108
}
109109
}
110110

yarn.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ __metadata:
389389
"@aws-amplify/amplify-function-plugin-interface": 1.10.3
390390
"@aws-amplify/amplify-prompts": 2.8.0
391391
"@aws-amplify/graphql-transformer-interfaces": ^2.2.2
392+
"@aws-sdk/util-arn-parser": ^3.310.0
392393
"@types/ejs": ^3.1.1
393394
"@types/fs-extra": ^8.0.1
394395
"@types/hjson": ^2.4.2
@@ -4646,6 +4647,15 @@ __metadata:
46464647
languageName: node
46474648
linkType: hard
46484649

4650+
"@aws-sdk/util-arn-parser@npm:^3.310.0":
4651+
version: 3.310.0
4652+
resolution: "@aws-sdk/util-arn-parser@npm:3.310.0"
4653+
dependencies:
4654+
tslib: ^2.5.0
4655+
checksum: 7214c1291748751976d2d5125d79d49dcb40a0f2276b6da41403c2fd4ecdeb611a604afe06d35c74f66231af78234367698c472b18b671f6e1685890d2508563
4656+
languageName: node
4657+
linkType: hard
4658+
46494659
"@aws-sdk/util-base64-browser@npm:3.37.0":
46504660
version: 3.37.0
46514661
resolution: "@aws-sdk/util-base64-browser@npm:3.37.0"

0 commit comments

Comments
 (0)