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

Exception for lambda authorizer uri #3720

Merged
merged 2 commits into from
Sep 26, 2024
Merged
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
21 changes: 4 additions & 17 deletions src/cfnlint/rules/resources/HardCodedArnProperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ def __init__(self):
"type": "boolean",
},
}
self.exceptions = {
"AWS::ApiGateway::Authorizer": [
["Properties", "AuthorizerUri"],
]
}

self.configure()

Expand Down Expand Up @@ -102,17 +97,6 @@ def match(self, cfn: Template) -> RuleMatches:
path = ["Resources"] + parameter_string_path[:-1]
candidate = parameter_string_path[-1]

resource_name = path[1]
_type = cfn.template.get("Resources", {}).get(resource_name, {}).get("Type")
is_exception = False
if _type in self.exceptions:
for exception in self.exceptions[_type]:
if all(x[0] == x[1] for x in zip(path[2:], exception)):
is_exception = True

if is_exception:
continue

# ruff: noqa: E501
# !Sub arn:${AWS::Partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
# is valid even with aws as the account #. This handles empty string
Expand All @@ -135,8 +119,11 @@ def match(self, cfn: Template) -> RuleMatches:
" incorrectly placed Pseudo Parameters"
)
matches.append(RuleMatch(path, message.format(path[1])))

# Lambda is added for authorizer's Uniform Resource Identifier (URI)
# https://github.com/aws-cloudformation/cfn-lint/issues/3716
if self.config["accountId"] and not re.match(
r"^\$\{\w+}|\$\{AWS::AccountId}|aws|$", candidate[2]
r"^\$\{\w+}|\$\{AWS::AccountId}|aws|lambda|$", candidate[2]
):
message = (
"ARN in Resource {0} contains hardcoded AccountId in ARN or"
Expand Down
8 changes: 0 additions & 8 deletions test/fixtures/templates/bad/hard_coded_arn_properties.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,3 @@ Resources:
- !Sub arn:${AWS::Partition}:sns:${AWS::Partition}:${AWS::AccountId}:TestTopic
Roles:
- !Ref SampleRole

Authorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
AuthorizerUri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:Name/invocations
RestApiId: RestApiId
Type: REQUEST
Name: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:Name/invocations
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ Resources:
RestApiId: RestApiId
Type: REQUEST
Name: Name
Stack:
Type: AWS::CloudFormation::Stack
DeletionPolicy: Delete
UpdateReplacePolicy: Delete
Properties:
TemplateURL: !Sub https://s3_bucket_name.s3.${AWS::Region}.amazonaws.com/template.yaml
Parameters:
AuthorizerUri: !Sub arn:${AWS::Partition}:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/arn:${AWS::Partition}:lambda:${AWS::Region}:${AWS::AccountId}:function:FunctionName/invocations
2 changes: 1 addition & 1 deletion test/unit/rules/resources/test_hardcodedarnproperties.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_file_negative_region(self):
def test_file_negative_accountid(self):
self.helper_file_negative(
"test/fixtures/templates/bad/hard_coded_arn_properties.yaml",
2,
1,
ConfigMixIn(
[],
include_experimental=True,
Expand Down
Loading