-
Notifications
You must be signed in to change notification settings - Fork 825
fix(amplify-category-storage): make IAM policy names environment-specific #14962
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
Open
w3lld1
wants to merge
3
commits into
aws-amplify:dev
Choose a base branch
from
w3lld1:category-storage/unique-policy-names-14961
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
7c10bd9
fix(amplify-category-storage): make IAM policy names environment-spec…
w3lld1 3f565c8
fix(amplify-category-storage): guard against missing env name in S3 p…
sharonyajain b9e589c
fix(amplify-category-storage): only env-scope S3 policy names for imp…
sharonyajain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
...c/__tests__/provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { $TSContext } from '@aws-amplify/amplify-cli-core'; | ||
| import { AmplifyS3ResourceStackTransform } from '../../../../provider-utils/awscloudformation/cdk-stack-builder/s3-stack-transform'; | ||
| import { S3UserInputs } from '../../../../provider-utils/awscloudformation/service-walkthrough-types/s3-user-input-types'; | ||
| import { S3InputState } from '../../../../provider-utils/awscloudformation/service-walkthroughs/s3-user-input-state'; | ||
|
|
||
| jest.mock('@aws-amplify/amplify-cli-core', () => ({ | ||
| pathManager: { | ||
| getBackendDirPath: jest.fn().mockReturnValue('mockbackendpath'), | ||
| }, | ||
| AmplifyError: class AmplifyError extends Error { | ||
| constructor(code: string, options: { message: string }) { | ||
| super(options.message); | ||
| this.name = code; | ||
| } | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../../../../provider-utils/awscloudformation/service-walkthroughs/s3-user-input-state'); | ||
|
|
||
| const userInput: S3UserInputs = { | ||
| resourceName: 'storage', | ||
| bucketName: 'bucket', | ||
| policyUUID: 'abc123', | ||
| storageAccess: undefined, | ||
| guestAccess: [], | ||
| authAccess: [], | ||
| }; | ||
|
|
||
| const importedAuthMeta = { auth: { cognitoauth: { service: 'Cognito', serviceType: 'imported' } } }; | ||
| const managedAuthMeta = { auth: { cognitoauth: { service: 'Cognito', serviceType: 'managed' } } }; | ||
|
|
||
| const buildContext = (meta: unknown, envName: string): $TSContext => | ||
| ({ | ||
| amplify: { | ||
| getProjectMeta: jest.fn().mockReturnValue(meta), | ||
| getEnvInfo: jest.fn().mockReturnValue({ envName }), | ||
| }, | ||
| } as unknown as $TSContext); | ||
|
|
||
| const stubInputState = (): void => { | ||
| jest.spyOn(S3InputState.prototype, 'getCliInputPayload').mockReturnValue(userInput); | ||
| jest.spyOn(S3InputState.prototype, 'getUserInput').mockReturnValue(userInput); | ||
| jest.spyOn(S3InputState, 'getCfnPermissionsFromInputPermissions').mockReturnValue([]); | ||
| }; | ||
|
|
||
| describe('AmplifyS3ResourceStackTransform', () => { | ||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('appends the environment name to IAM policy names when auth is imported', () => { | ||
| stubInputState(); | ||
| const transform = new AmplifyS3ResourceStackTransform('storage', buildContext(importedAuthMeta, 'prod')); | ||
| transform.generateCfnInputParameters(); | ||
|
|
||
| expect(transform.getCFNInputParams()).toMatchObject({ | ||
| s3PrivatePolicy: 'Private_policy_abc123_prod', | ||
| s3ProtectedPolicy: 'Protected_policy_abc123_prod', | ||
| s3PublicPolicy: 'Public_policy_abc123_prod', | ||
| s3ReadPolicy: 'read_policy_abc123_prod', | ||
| s3UploadsPolicy: 'Uploads_policy_abc123_prod', | ||
| authPolicyName: 's3_amplify_abc123_prod', | ||
| unauthPolicyName: 's3_amplify_abc123_prod', | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps the legacy (env-agnostic) IAM policy names when auth is not imported', () => { | ||
| // Managed auth gives each environment its own roles, so names never collide. Keeping the | ||
| // legacy name avoids forcing a policy replacement on every existing storage app on upgrade. | ||
| stubInputState(); | ||
| const transform = new AmplifyS3ResourceStackTransform('storage', buildContext(managedAuthMeta, 'prod')); | ||
| transform.generateCfnInputParameters(); | ||
|
|
||
| expect(transform.getCFNInputParams()).toMatchObject({ | ||
| s3PrivatePolicy: 'Private_policy_abc123', | ||
| s3ProtectedPolicy: 'Protected_policy_abc123', | ||
| s3PublicPolicy: 'Public_policy_abc123', | ||
| s3ReadPolicy: 'read_policy_abc123', | ||
| s3UploadsPolicy: 'Uploads_policy_abc123', | ||
| authPolicyName: 's3_amplify_abc123', | ||
| unauthPolicyName: 's3_amplify_abc123', | ||
| }); | ||
| }); | ||
|
|
||
| it('throws when auth is imported but the current environment name cannot be determined', () => { | ||
| // Imported auth needs the env suffix; a blank envName would otherwise degrade to a | ||
| // shared "_undefined" name and re-collide, so we fail fast instead. | ||
| stubInputState(); | ||
| const transform = new AmplifyS3ResourceStackTransform('storage', buildContext(importedAuthMeta, '')); | ||
|
|
||
| expect(() => transform.generateCfnInputParameters()).toThrow(/environment name/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.