-
Notifications
You must be signed in to change notification settings - Fork 79
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: fix policies with overridden table names #3075
Merged
palpatim
merged 4 commits into
release-api-plugin-stable
from
palpatim.fix.policies-with-overridden-table-name
Dec 17, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
This file contains 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
This file contains 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
119 changes: 119 additions & 0 deletions
119
packages/amplify-e2e-tests/src/__tests__/api-override-ddb-table-name.test.ts
This file contains 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,119 @@ | ||
import path from 'path'; | ||
import * as fs from 'fs-extra'; | ||
import { | ||
addApiWithoutSchema, | ||
amplifyOverrideApi, | ||
amplifyPush, | ||
amplifyPushOverride, | ||
createNewProjectDir, | ||
deleteProject, | ||
deleteProjectDir, | ||
getAppSyncApi, | ||
getDDBTable, | ||
getProjectMeta, | ||
getRolePolicy, | ||
initJSProjectWithProfile, | ||
listRolePolicies, | ||
listRoleNamesContaining, | ||
replaceOverrideFileWithProjectInfo, | ||
updateApiSchema, | ||
updateSchema, | ||
} from 'amplify-category-api-e2e-core'; | ||
|
||
describe('Override table name', () => { | ||
let projRoot: string; | ||
let projFolderName: string; | ||
beforeEach(async () => { | ||
projFolderName = 'overridename'; | ||
projRoot = await createNewProjectDir(projFolderName); | ||
}); | ||
|
||
afterEach(async () => { | ||
const metaFilePath = path.join(projRoot, 'amplify', '#current-cloud-backend', 'amplify-meta.json'); | ||
if (fs.existsSync(metaFilePath)) { | ||
await deleteProject(projRoot); | ||
} | ||
deleteProjectDir(projRoot); | ||
}); | ||
|
||
it('Generates correct permissions policies for DynamoDB tables with overridden names', async () => { | ||
const now = Math.floor(Date.now() / 1000); | ||
const modelName = `Override${now}`; | ||
|
||
const schema = /* GraphQL */ ` | ||
type ${modelName} @model { | ||
id: ID! | ||
content: String | ||
} | ||
`; | ||
|
||
const envName = 'integtest'; | ||
const projName = 'overridetest'; | ||
const cliInputsFilePath = path.join(projRoot, 'amplify', 'backend', 'api', `${projName}`, 'cli-inputs.json'); | ||
await initJSProjectWithProfile(projRoot, { name: projName, envName }); | ||
await addApiWithoutSchema(projRoot); | ||
|
||
updateSchema(projRoot, projName, schema); | ||
expect(fs.existsSync(cliInputsFilePath)).toBe(true); | ||
|
||
await amplifyPush(projRoot); | ||
|
||
await amplifyOverrideApi(projRoot, {}); | ||
|
||
const overriddenTableName = `OverrideTest${now}Custom`; | ||
const overrideCode = /* TypeScript */ ` | ||
export function override(props: any) { | ||
props.models['${modelName}'].modelDDBTable.tableName = '${overriddenTableName}'; | ||
} | ||
`; | ||
const destOverrideFilePath = path.join(projRoot, 'amplify', 'backend', 'api', `${projName}`, 'override.ts'); | ||
fs.writeFileSync(destOverrideFilePath, overrideCode); | ||
|
||
await amplifyPushOverride(projRoot); | ||
|
||
const meta = getProjectMeta(projRoot); | ||
const region = meta.providers.awscloudformation.Region; | ||
const { output } = meta.api.overridetest; | ||
const { GraphQLAPIIdOutput, GraphQLAPIEndpointOutput, GraphQLAPIKeyOutput } = output; | ||
const { graphqlApi } = await getAppSyncApi(GraphQLAPIIdOutput, region); | ||
|
||
expect(graphqlApi).toBeDefined(); | ||
expect(graphqlApi.apiId).toEqual(GraphQLAPIIdOutput); | ||
expect(GraphQLAPIIdOutput).toBeDefined(); | ||
expect(GraphQLAPIEndpointOutput).toBeDefined(); | ||
expect(GraphQLAPIKeyOutput).toBeDefined(); | ||
|
||
const defaultTableName = `${modelName}-${graphqlApi.apiId}-${envName}`; | ||
const error = { message: null }; | ||
try { | ||
const defaultTable = await getDDBTable(defaultTableName, region); | ||
expect(defaultTable).toBeUndefined(); | ||
} catch (ex) { | ||
Object.assign(error, ex); | ||
} | ||
expect(error).toBeDefined(); | ||
expect(error.message).toContain(`${defaultTableName} not found`); | ||
|
||
const actualTable = await getDDBTable(overriddenTableName, region); | ||
expect(actualTable).toBeDefined(); | ||
|
||
// Validate policy. The role will be created with the prefix {modelName}IAMRole. It should have 2 policies: one created by Amplify, one | ||
// created by AppSync's CDK call during the `addDynamoDbDataSource` flow. We expect the policy statements for the latter to refer to the | ||
// overridden table name. | ||
const matchingRoleNames = await listRoleNamesContaining(modelName, region); | ||
expect(matchingRoleNames).toBeDefined(); | ||
expect(matchingRoleNames.length).toEqual(1); | ||
const roleName = matchingRoleNames[0]; | ||
|
||
const policies = await listRolePolicies(roleName, region); | ||
expect(policies).toBeDefined(); | ||
expect(policies.length).toBe(2); | ||
|
||
const defaultPolicy = policies.find((p) => p.startsWith(`${modelName}IAMRoleDefault`)); | ||
expect(defaultPolicy).toBeDefined(); | ||
|
||
const policyObject = await getRolePolicy(roleName, defaultPolicy, region); | ||
expect(policyObject).toBeDefined(); | ||
expect(policyObject.Statement[0].Resource[0]).toContain(overriddenTableName); | ||
}); | ||
}); |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does this need paginated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Late response to this comment: the
listRoles
response is paginated, usingMarker
as the "next token" equivalent. See https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/iam/command/ListRolesCommand/