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: authRole,unauthRole define resources according to @auth directive operations #2117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,19 @@ test('simple model with private IAM auth rule, few operations, and amplify admin

expect(out.schema).toContain('getPost(id: ID!): Post @aws_iam');
expect(out.schema).toContain('listPosts(filter: ModelPostFilterInput, limit: Int, nextToken: String): ModelPostConnection @aws_iam');

const policyResources = _.filter(out.rootStack.Resources, (r) => r.Type === 'AWS::IAM::ManagedPolicy');
expect(policyResources).toHaveLength(1);
const resources = _.get(policyResources, '[0].Properties.PolicyDocument.Statement[0].Resource');
const typeFieldList = _.map(resources, (r) => _.get(r, 'Fn::Sub[1]')).map((r) => `${_.get(r, 'typeName')}.${_.get(r, 'fieldName', '*')}`);
expect(typeFieldList).toEqual([
'Post.*',
'Query.getPost',
'Query.listPosts',
'Mutation.updatePost',
'Subscription.onCreatePost',
'Subscription.onUpdatePost',
'Subscription.onDeletePost',
]);
expect(out.resolvers['Mutation.updatePost.auth.1.res.vtl']).toMatchSnapshot();
expect(out.resolvers['Mutation.updatePost.auth.1.res.vtl']).toContain(
'#if( ($ctx.identity.userArn == $ctx.stash.authRole) || ($ctx.identity.cognitoIdentityPoolId == $ctx.stash.identityPoolId && $ctx.identity.cognitoIdentityAuthType == "authenticated") )',
Expand Down Expand Up @@ -308,9 +320,6 @@ test('simple model with AdminUI enabled should add IAM policy only for fields th
'Post.*',
'Query.getPost',
'Query.listPosts',
'Mutation.createPost',
'Mutation.updatePost',
'Mutation.deletePost',
'Subscription.onCreatePost',
'Subscription.onUpdatePost',
'Subscription.onDeletePost',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,12 +556,13 @@ export class AuthTransformer extends TransformerAuthBase implements TransformerA
const addServiceDirective = (typeName: string, operation: ModelOperation, operationName: string | null = null): void => {
if (operationName) {
const includeDefault = this.doesTypeHaveRulesForOperation(acm, operation);
const providers = this.getAuthProviders(acm.getRolesPerOperation(operation, operation === 'delete'));
const rolesPerOperation = acm.getRolesPerOperation(operation, operation === 'delete');
const providers = this.getAuthProviders(rolesPerOperation);
const operationDirectives = this.getServiceDirectives(providers, includeDefault);
if (operationDirectives.length > 0) {
addDirectivesToOperation(ctx, typeName, operationName, operationDirectives);
}
this.addOperationToResourceReferences(typeName, operationName, acm.getRoles());
this.addOperationToResourceReferences(typeName, operationName, rolesPerOperation);
}
};
// default model operations
Expand Down