-
Notifications
You must be signed in to change notification settings - Fork 602
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rule W1051 to validate if dyn ref when arn
- Loading branch information
Showing
5 changed files
with
180 additions
and
4 deletions.
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
61 changes: 61 additions & 0 deletions
61
src/cfnlint/rules/functions/DynamicReferenceSecretsManagerArn.py
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,61 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from typing import Any | ||
|
||
import regex as re | ||
|
||
from cfnlint.jsonschema import ValidationError, Validator | ||
from cfnlint.rules import CloudFormationLintRule | ||
|
||
|
||
class DynamicReferenceSecretsManagerArn(CloudFormationLintRule): | ||
id = "W1051" | ||
shortdesc = ( | ||
"Validate dynamic references to secrets manager " | ||
"are not used when a secrets manager ARN was expected" | ||
) | ||
description = ( | ||
"Certain properties expect a secret manager ARN. This rule " | ||
"validates if you may be accidently using a secret in place " | ||
"of the ARN" | ||
) | ||
source_url = "https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html#dynamic-references-secretsmanager" | ||
tags = ["functions", "dynamic reference"] | ||
|
||
def validate(self, validator: Validator, s: Any, instance: Any, schema: Any): | ||
|
||
if "Fn::Sub" == validator.context.path.path[-1]: | ||
if not re.match(r"^(\\\")?{{resolve:secretsmanager:.*}}(\\\")?$", instance): | ||
return | ||
|
||
if len(validator.context.path.path) < 3: | ||
return | ||
|
||
if ( | ||
validator.context.path.path[0] != "Resources" | ||
or validator.context.path.path[2] != "Properties" | ||
): | ||
return | ||
|
||
fields = [ | ||
"SecretArn", | ||
"SecretARN", | ||
"SecretsManagerSecretId", | ||
"SecretsManagerOracleAsmSecretId", | ||
"SecretsManagerSecurityDbEncryptionSecretId", | ||
"SecretsManagerConfiguration", | ||
] | ||
|
||
for field in fields: | ||
if any(field == p for p in validator.context.path.path): | ||
yield ValidationError( | ||
( | ||
f"Dynamic reference {instance!r} to secrets manager when " | ||
f"the field {field!r} expects the ARN to the secret and " | ||
"not the secret" | ||
), | ||
rule=self, | ||
) |
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
94 changes: 94 additions & 0 deletions
94
test/unit/rules/functions/test_dynamic_reference_secrets_manager_arn.py
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,94 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
from collections import deque | ||
|
||
import pytest | ||
|
||
from cfnlint.jsonschema import ValidationError | ||
from cfnlint.rules.functions.DynamicReferenceSecretsManagerArn import ( | ||
DynamicReferenceSecretsManagerArn, | ||
) | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rule(): | ||
rule = DynamicReferenceSecretsManagerArn() | ||
yield rule | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,instance,path,expected", | ||
[ | ||
( | ||
"Valid secrets manager", | ||
"{{resolve:secretsmanager:Parameter}}", | ||
{ | ||
"path": deque(["Resources", "MyResource", "Properties", "Password"]), | ||
}, | ||
[], | ||
), | ||
( | ||
"Valid sub not to secrets manaager", | ||
"secretsmanager", | ||
{ | ||
"path": deque( | ||
["Resources", "MyResource", "Properties", "Password", "Fn::Sub"] | ||
), | ||
}, | ||
[], | ||
), | ||
( | ||
"Valid secrets manager", | ||
"{{resolve:secretsmanager:Parameter}}", | ||
{ | ||
"path": deque(["Resources", "MyResource", "Properties", "Password"]), | ||
}, | ||
[], | ||
), | ||
( | ||
"Invalid secrets manager when ARN was expected", | ||
"{{resolve:secretsmanager:secret}}", | ||
{ | ||
"path": deque(["Resources", "MyResource", "Properties", "SecretArn"]), | ||
}, | ||
[ | ||
ValidationError( | ||
( | ||
"Dynamic reference '{{resolve:secretsmanager:secret}}'" | ||
" to secrets manager when the field 'SecretArn' expects " | ||
"the ARN to the secret and not the secret" | ||
), | ||
rule=DynamicReferenceSecretsManagerArn(), | ||
) | ||
], | ||
), | ||
( | ||
"Invalid secrets manager when ARN was expected", | ||
'\\"{{resolve:secretsmanager:${MyParameter}}}\\"', | ||
{ | ||
"path": deque( | ||
["Resources", "MyResource", "Properties", "SecretArn", "Fn::Sub"] | ||
), | ||
}, | ||
[ | ||
ValidationError( | ||
( | ||
"Dynamic reference " | ||
"'\\\\\"{{resolve:secretsmanager:${MyParameter}}}\\\\\"'" | ||
" to secrets manager when the field 'SecretArn' " | ||
"expects the ARN to the secret and not the secret" | ||
), | ||
rule=DynamicReferenceSecretsManagerArn(), | ||
) | ||
], | ||
), | ||
], | ||
indirect=["path"], | ||
) | ||
def test_validate(name, instance, path, expected, validator, rule): | ||
errs = list(rule.validate(validator, {}, instance, {})) | ||
|
||
assert errs == expected, f"Test {name!r} got {errs!r}" |