Skip to content

Commit 6fa8198

Browse files
authored
Merge pull request #43 from masso00/feature/intrinsic_stackid
Add support for the AWS::StackId intrinsic parameter
2 parents b730339 + 90d26e9 commit 6fa8198

8 files changed

Lines changed: 40 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ Supported pseudo parameters:
218218
- AWS::AccountId
219219
- AWS::Partition
220220
- AWS::StackName (returns the literal string "StackName")
221+
- AWS::StackId (returns a dummy StackId)
221222
- AWS::NoValue
222223

223224
[Dynamic SSM references](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/dynamic-references.html) are supported, but must have a version number. This is to help ensure the same parameter that is validated is the one that is deployed. This restriction can be overridden with the --allow-dynamic-ref-without-version argument.

cfn_policy_validator/parsers/utils/intrinsic_functions/ref_evaluator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ def evaluate(self, resource_logical_name_or_param, visited_nodes=None):
5757
# just return some default value, we won't know this in advance
5858
return "StackName"
5959

60+
if resource_logical_name_or_param == "AWS::StackId":
61+
# build a well-formatted default StackId, we won't know this in advance
62+
return ":".join(["arn", self.account_config.partition, "cloudformation", self.account_config.region,
63+
self.account_config.account_id, "stack/StackName/00000000-0000-0000-0000-000000000000"])
64+
6065
if resource_logical_name_or_param == "AWS::NoValue":
6166
return NoValue()
6267

cfn_policy_validator/tests/parsers_tests/utils_tests/intrinsic_functions_tests/test_ref_evaluator.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def test_returns_the_region(self):
7272

7373
class WhenEvaluatingAPolicyWithARefToStackName(unittest.TestCase):
7474
@mock_node_evaluator_setup()
75-
def test_returns_the_partition(self):
75+
def test_returns_the_stack_name(self):
7676
template = load_resources({
7777
'ResourceA': {
7878
'Type': 'AWS::Random::Service',
@@ -89,6 +89,27 @@ def test_returns_the_partition(self):
8989
result = node_evaluator.eval(template['Resources']['ResourceA']['Properties']['PropertyA'])
9090
self.assertEqual(result, 'StackName')
9191

92+
class WhenEvaluatingAPolicyWithARefToStackId(unittest.TestCase):
93+
@mock_node_evaluator_setup()
94+
def test_returns_the_stack_id(self):
95+
template = load_resources({
96+
'ResourceA': {
97+
'Type': 'AWS::Random::Service',
98+
'Properties': {
99+
'PropertyA': {
100+
"Ref": "AWS::StackId"
101+
}
102+
}
103+
}
104+
})
105+
106+
node_evaluator = build_node_evaluator(template)
107+
108+
result = node_evaluator.eval(template['Resources']['ResourceA']['Properties']['PropertyA'])
109+
self.assertEqual(result, ":".join(["arn", account_config.partition, "cloudformation",
110+
account_config.region, account_config.account_id,
111+
"stack/StackName/00000000-0000-0000-0000-000000000000"]))
112+
92113

93114
class WhenEvaluatingAPolicyWithARefToAnArn(unittest.TestCase):
94115
@mock_node_evaluator_setup()

scripts/run_all_tests.sh

100644100755
File mode changed.

scripts/run_integration_tests.sh

100644100755
File mode changed.

scripts/run_unit_tests.sh

100644100755
File mode changed.

test_files/test_file_2.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@
2929
"BucketName": {
3030
"Fn::Sub": "${EnvironmentName}-app-artifacts"
3131
},
32-
"AccessControl": "BucketOwnerFullControl"
32+
"AccessControl": "BucketOwnerFullControl",
33+
"Tags": [
34+
{
35+
"Key": "parentStackId",
36+
"Value": {
37+
"Ref": "AWS::StackId"
38+
}
39+
}
40+
]
3341
}
3442
},
3543
"MyTopic": {

test_files/test_file_2.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Resources:
2020
Properties:
2121
BucketName: !Sub ${EnvironmentName}-app-artifacts
2222
AccessControl: BucketOwnerFullControl
23+
Tags:
24+
- Key: 'ParentStackId'
25+
Value: !Ref AWS::StackId
2326

2427
MyTopic:
2528
Type: AWS::SNS::Topic

0 commit comments

Comments
 (0)