Skip to content

Commit c7cee15

Browse files
authored
fix(appsync): lambda authorizer permission is not scoped to appsync api arn (#31567)
### Issue # (if applicable) Closes #31550. ### Reason for this change When using a lambda authorizer with a GraphqlAPI, the cdk automatically creates the AWS::Lambda::Permission required for the AppSync API to invoke the lambda authorizer. It does not however add a SourceArn. This conflicts with the control tower policy [[CT.LAMBDA.PR.2]](https://docs.aws.amazon.com/controltower/latest/controlreference/lambda-rules.html#ct-lambda-pr-2-description), and it is in general good practice to scope permissions. ### Description of changes Added new feature flag `APPSYNC_GRAPHQLAPI_SCOPE_LAMBDA_FUNCTION_PERMISSION`. Currently, when using a Lambda authorizer with an AppSync GraphQL API, the AWS CDK automatically generates the necessary AWS::Lambda::Permission to allow the AppSync API to invoke the Lambda authorizer. This permission is overly permissive because it lacks a SourceArn, meaning it allows invocations from any source. When this feature flag is enabled, the AWS::Lambda::Permission will be properly scoped with the SourceArn corresponding to the specific AppSync GraphQL API. ```ts ... config?.handler.addPermission(`${id}-appsync`, { principal: new ServicePrincipal('appsync.amazonaws.com'), action: 'lambda:InvokeFunction', sourceArn: this.arn, // <-- added when feature flag is enabled }); ... ``` ### Description of how you validated changes Unit + integ tests with feature flag enabled. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 52f676c commit c7cee15

16 files changed

+820
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Query {
2+
getMessage: String
3+
}

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.graphql-lambda-permission.js.snapshot/GraphqlApiLambdaPermissionTestDefaultTestDeployAssert7720B39B.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.graphql-lambda-permission.js.snapshot/GraphqlApiLambdaPermissionTestDefaultTestDeployAssert7720B39B.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.graphql-lambda-permission.js.snapshot/aws-graphql-lambda-permissions.assets.json

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
{
2+
"Resources": {
3+
"AuthorizerFunctionServiceRole5B2A061B": {
4+
"Type": "AWS::IAM::Role",
5+
"Properties": {
6+
"AssumeRolePolicyDocument": {
7+
"Statement": [
8+
{
9+
"Action": "sts:AssumeRole",
10+
"Effect": "Allow",
11+
"Principal": {
12+
"Service": "lambda.amazonaws.com"
13+
}
14+
}
15+
],
16+
"Version": "2012-10-17"
17+
},
18+
"ManagedPolicyArns": [
19+
{
20+
"Fn::Join": [
21+
"",
22+
[
23+
"arn:",
24+
{
25+
"Ref": "AWS::Partition"
26+
},
27+
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
28+
]
29+
]
30+
}
31+
]
32+
}
33+
},
34+
"AuthorizerFunctionB4DBAA43": {
35+
"Type": "AWS::Lambda::Function",
36+
"Properties": {
37+
"Code": {
38+
"ZipFile": "\n exports.handler = async (event) => {\n console.log(\"Authorization event:\", JSON.stringify(event));\n \n const isAuthorized = true;\n if (isAuthorized) {\n return {\n isAuthorized: true,\n resolverContext: {\n userId: 'user-id-example'\n }\n };\n } else {\n return {\n isAuthorized: false\n };\n }\n };\n "
39+
},
40+
"Handler": "index.handler",
41+
"Role": {
42+
"Fn::GetAtt": [
43+
"AuthorizerFunctionServiceRole5B2A061B",
44+
"Arn"
45+
]
46+
},
47+
"Runtime": "nodejs18.x"
48+
},
49+
"DependsOn": [
50+
"AuthorizerFunctionServiceRole5B2A061B"
51+
]
52+
},
53+
"AuthorizerFunctionapiappsync4E3369BF": {
54+
"Type": "AWS::Lambda::Permission",
55+
"Properties": {
56+
"Action": "lambda:InvokeFunction",
57+
"FunctionName": {
58+
"Fn::GetAtt": [
59+
"AuthorizerFunctionB4DBAA43",
60+
"Arn"
61+
]
62+
},
63+
"Principal": "appsync.amazonaws.com",
64+
"SourceArn": {
65+
"Fn::GetAtt": [
66+
"apiC8550315",
67+
"Arn"
68+
]
69+
}
70+
}
71+
},
72+
"apiC8550315": {
73+
"Type": "AWS::AppSync::GraphQLApi",
74+
"Properties": {
75+
"AuthenticationType": "AWS_LAMBDA",
76+
"LambdaAuthorizerConfig": {
77+
"AuthorizerUri": {
78+
"Fn::GetAtt": [
79+
"AuthorizerFunctionB4DBAA43",
80+
"Arn"
81+
]
82+
}
83+
},
84+
"Name": "api"
85+
}
86+
},
87+
"apiSchema0EA92056": {
88+
"Type": "AWS::AppSync::GraphQLSchema",
89+
"Properties": {
90+
"ApiId": {
91+
"Fn::GetAtt": [
92+
"apiC8550315",
93+
"ApiId"
94+
]
95+
},
96+
"Definition": "type Query {\n getMessage: String\n}"
97+
}
98+
}
99+
},
100+
"Parameters": {
101+
"BootstrapVersion": {
102+
"Type": "AWS::SSM::Parameter::Value<String>",
103+
"Default": "/cdk-bootstrap/hnb659fds/version",
104+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
105+
}
106+
},
107+
"Rules": {
108+
"CheckBootstrapVersion": {
109+
"Assertions": [
110+
{
111+
"Assert": {
112+
"Fn::Not": [
113+
{
114+
"Fn::Contains": [
115+
[
116+
"1",
117+
"2",
118+
"3",
119+
"4",
120+
"5"
121+
],
122+
{
123+
"Ref": "BootstrapVersion"
124+
}
125+
]
126+
}
127+
]
128+
},
129+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
130+
}
131+
]
132+
}
133+
}
134+
}

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.graphql-lambda-permission.js.snapshot/cdk.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.graphql-lambda-permission.js.snapshot/integ.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-appsync/test/integ.graphql-lambda-permission.js.snapshot/manifest.json

Lines changed: 142 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)