Skip to content

Commit 5949f2e

Browse files
author
Vieltojarvi
committed
remove describeUserpoolCommand
1 parent d89dd6c commit 5949f2e

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

packages/cli/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"@aws-sdk/client-cloudformation": "^3.624.0",
4848
"@aws-sdk/client-cognito-identity-provider": "^3.624.0",
4949
"@aws-sdk/client-s3": "^3.624.0",
50+
"@aws-sdk/client-sts": "^3.758.0",
5051
"@aws-sdk/credential-provider-ini": "^3.624.0",
5152
"@aws-sdk/credential-providers": "^3.624.0",
5253
"@aws-sdk/region-config-resolver": "^3.614.0",

packages/cli/src/seed-policy-generation/generate_seed_policy_template.test.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@ import { beforeEach, describe, it, mock } from 'node:test';
22
import assert from 'assert';
33
import { BackendIdentifier } from '@aws-amplify/plugin-types';
44
import { AWSAmplifyBackendOutputs } from '../../../client-config/src/client-config-schema/client_config_v1.3.js';
5-
import {
6-
CognitoIdentityProviderClient,
7-
DescribeUserPoolClientCommandInput,
8-
DescribeUserPoolClientCommandOutput,
9-
UserPoolType,
10-
} from '@aws-sdk/client-cognito-identity-provider';
115
import { generateSeedPolicyTemplate } from './generate_seed_policy_template.js';
126
import { generateClientConfig } from '@aws-amplify/client-config';
137
import { AmplifyUserError } from '@aws-amplify/platform-core';
148
import { App, Stack } from 'aws-cdk-lib';
159
import { AccountPrincipal, Policy, Role } from 'aws-cdk-lib/aws-iam';
1610
import { Template } from 'aws-cdk-lib/assertions';
11+
import {
12+
GetCallerIdentityCommandInput,
13+
GetCallerIdentityCommandOutput,
14+
STSClient,
15+
} from '@aws-sdk/client-sts';
1716

1817
const testBackendId = 'testBackendId';
1918
const testSandboxName = 'testSandboxName';
@@ -42,35 +41,34 @@ void describe('generate inline policy for seed', () => {
4241
},
4342
} as AWSAmplifyBackendOutputs)
4443
);
45-
const mockCognitoIdProviderClient = {
44+
45+
const mockStsClient = {
4646
send: mock.fn<
4747
(
48-
input: DescribeUserPoolClientCommandInput
49-
) => Promise<DescribeUserPoolClientCommandOutput>
48+
input: GetCallerIdentityCommandInput
49+
) => Promise<GetCallerIdentityCommandOutput>
5050
>(async () =>
5151
Promise.resolve({
52-
$metadata: {},
53-
UserPool: {
54-
UserPoolId: testUserpoolId,
55-
Arn: testArn,
56-
} as UserPoolType,
57-
})
52+
Account: '123456789012',
53+
Arn: '',
54+
UserId: '',
55+
} as GetCallerIdentityCommandOutput)
5856
),
5957
};
6058

6159
const app = new App();
6260
const stack = new Stack(app);
6361

6462
beforeEach(() => {
65-
mockCognitoIdProviderClient.send.mock.resetCalls();
6663
mockConfigGenerator.mock.resetCalls();
64+
mockStsClient.send.mock.resetCalls();
6765
});
6866

6967
void it('returns a policy with expected seed permissions', async () => {
7068
const policyDoc = await generateSeedPolicyTemplate(
7169
testBackendIdentifier,
7270
mockConfigGenerator as unknown as typeof generateClientConfig,
73-
mockCognitoIdProviderClient as unknown as CognitoIdentityProviderClient
71+
mockStsClient as unknown as STSClient
7472
);
7573

7674
const policy = new Policy(stack, 'testSeedPolicy', { document: policyDoc });

packages/cli/src/seed-policy-generation/generate_seed_policy_template.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ import { Effect, PolicyDocument, PolicyStatement } from 'aws-cdk-lib/aws-iam';
22
import { generateClientConfig } from '@aws-amplify/client-config';
33
import { BackendIdentifier } from '@aws-amplify/plugin-types';
44
import {
5-
CognitoIdentityProviderClient,
6-
DescribeUserPoolCommand,
7-
} from '@aws-sdk/client-cognito-identity-provider';
8-
import {
9-
AmplifyFault,
105
AmplifyUserError,
116
ParameterPathConversions,
127
} from '@aws-amplify/platform-core';
8+
import { GetCallerIdentityCommand, STSClient } from '@aws-sdk/client-sts';
139

1410
/**
1511
* Generates policy template which allows seed to be run
@@ -19,7 +15,7 @@ import {
1915
export const generateSeedPolicyTemplate = async (
2016
backendId: BackendIdentifier,
2117
generateClientConfiguration = generateClientConfig,
22-
cognitoIdProvider = new CognitoIdentityProviderClient()
18+
stsClient = new STSClient()
2319
): Promise<PolicyDocument> => {
2420
const seedPolicy = new PolicyDocument();
2521
const clientConfig = await generateClientConfiguration(backendId, '1.3');
@@ -31,9 +27,11 @@ export const generateSeedPolicyTemplate = async (
3127
'Please add an auth resource to your sandbox and rerun this command',
3228
});
3329
}
34-
const userPoolId = clientConfig.auth?.user_pool_id;
30+
// /const userPoolId = clientConfig.auth?.user_pool_id;
3531

36-
const userpoolOutput = await cognitoIdProvider.send(
32+
const stsResponse = await stsClient.send(new GetCallerIdentityCommand({}));
33+
const arn = `arn:aws:cognito-idp:${clientConfig.auth.aws_region}:${stsResponse.Account}:userpool/${clientConfig.auth.user_pool_id}`;
34+
/*const userpoolOutput = await cognitoIdProvider.send(
3735
new DescribeUserPoolCommand({ UserPoolId: userPoolId })
3836
);
3937
const userpoolArn = userpoolOutput.UserPool?.Arn;
@@ -44,11 +42,11 @@ export const generateSeedPolicyTemplate = async (
4442
'Either the userpool is missing or the userpool exists but it is missing an arn',
4543
resolution: 'Ensure your userpool exists and has an arn',
4644
});
47-
}
45+
}*/
4846
const cognitoGrant = new PolicyStatement({
4947
effect: Effect.ALLOW,
5048
actions: ['cognito-idp:AdminCreateUser', 'cognito-idp:AdminAddUserToGroup'],
51-
resources: [userpoolArn],
49+
resources: [arn],
5250
});
5351

5452
const backendParamPrefix =

0 commit comments

Comments
 (0)