-
Notifications
You must be signed in to change notification settings - Fork 601
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update policy principal validation logic (#3400)
* Change up the IAM policy validation * Add testing for CDK and sub not join
- Loading branch information
Showing
5 changed files
with
201 additions
and
23 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
""" | ||
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, _keywords | ||
from cfnlint.jsonschema.validators import CfnTemplateValidator | ||
from cfnlint.rules import CloudFormationLintRule | ||
|
||
|
||
class Error(CloudFormationLintRule): | ||
id = "E1111" | ||
|
||
def validate(self, validator, s, instance, schema): | ||
print(instance) | ||
if s: | ||
yield ValidationError( | ||
"Error", | ||
rule=self, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def validator(): | ||
validator = CfnTemplateValidator(schema={}) | ||
validator = validator.extend( | ||
validators={ | ||
"error": Error().validate, | ||
} | ||
) | ||
return validator({}) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,instance,schema,expected", | ||
[ | ||
( | ||
"Valid anyOf", | ||
"foo", | ||
[{"const": "foo"}, {"const": "bar"}], | ||
[], | ||
), | ||
( | ||
"Valid anyOf with error rule", | ||
"foo", | ||
[{"const": "foo"}, {"error": True}], | ||
[], | ||
), | ||
( | ||
"Invalid anyOf with error rule", | ||
"foo", | ||
[{"error": True}, {"error": True}], | ||
[ | ||
ValidationError( | ||
"'foo' is not valid under any of the given schemas", | ||
path=deque([]), | ||
schema_path=deque([]), | ||
context=[ | ||
ValidationError( | ||
"Error", | ||
rule=Error(), | ||
path=deque([]), | ||
validator="error", | ||
schema_path=deque([0, "error"]), | ||
), | ||
ValidationError( | ||
"Error", | ||
rule=Error(), | ||
path=deque([]), | ||
validator="error", | ||
schema_path=deque([1, "error"]), | ||
), | ||
], | ||
), | ||
], | ||
), | ||
], | ||
) | ||
def test_anyof(name, instance, schema, validator, expected): | ||
errs = list(_keywords.anyOf(validator, schema, instance, schema)) | ||
assert errs == expected, f"{name!r} got errors {errs!r}" |
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,44 @@ | ||
""" | ||
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
SPDX-License-Identifier: MIT-0 | ||
""" | ||
|
||
import pytest | ||
|
||
from cfnlint.rules.functions.SubNotJoin import SubNotJoin | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def rule(): | ||
rule = SubNotJoin() | ||
yield rule | ||
|
||
|
||
@pytest.fixture | ||
def template(): | ||
return { | ||
"Resources": { | ||
"MyResource": { | ||
"Type": "AWS::S3::Bucket", | ||
}, | ||
"CDK": { | ||
"Type": "AWS::CDK::Metadata", | ||
}, | ||
}, | ||
} | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"name,instance,schema,expected", | ||
[ | ||
( | ||
"Invalid Fn::Join with an empty string", | ||
{"Fn::Join": ["", ["foo", "bar"]]}, | ||
{"type": "string"}, | ||
[], | ||
), | ||
], | ||
) | ||
def test_validate(name, instance, schema, expected, rule, validator): | ||
errs = list(rule.validate(validator, schema, instance, {})) | ||
assert errs == expected, f"Test {name!r} got {errs!r}" |
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