Skip to content

Commit 2c72627

Browse files
authored
Replace data "aws_iam_policy_document" with manual jsonencode (#109)
This way it's easier for users to copy-paste the policies from our code into the AWS console directly when they are in a ready-made JSON format.
1 parent f69e3fb commit 2c72627

File tree

2 files changed

+55
-49
lines changed

2 files changed

+55
-49
lines changed

asset-account/terraform/stack-set/examples/self-managed/admin.tf

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,52 @@ module "elastio_asset_account" {
2525
resource "aws_iam_role" "admin" {
2626
provider = aws.admin
2727

28-
assume_role_policy = data.aws_iam_policy_document.admin_trust.json
29-
name = "AWSCloudFormationStackSetAdministrationRole"
30-
}
31-
32-
data "aws_iam_policy_document" "admin_trust" {
33-
statement {
34-
actions = ["sts:AssumeRole"]
35-
effect = "Allow"
36-
37-
principals {
38-
identifiers = ["cloudformation.amazonaws.com"]
39-
type = "Service"
40-
}
41-
42-
# Conditions to prevent the confused deputy attack
43-
condition {
44-
test = "StringEquals"
45-
variable = "aws:SourceAccount"
46-
values = [local.admin_account_id]
47-
}
28+
name = "AWSCloudFormationStackSetAdministrationRole"
4829

49-
condition {
50-
test = "StringLike"
51-
variable = "aws:SourceArn"
52-
values = ["arn:aws:cloudformation:*:${local.admin_account_id}:stackset/*"]
30+
# Allow assuming for CFN with some `Condition` elements to prevent the confused deputy attack
31+
# as described in AWS docs: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacksets-prereqs-self-managed.html#confused-deputy-mitigation
32+
assume_role_policy = jsonencode(
33+
{
34+
"Version" : "2012-10-17",
35+
"Statement" : [
36+
{
37+
"Effect" : "Allow",
38+
"Principal" : {
39+
"Service" : "cloudformation.amazonaws.com"
40+
},
41+
"Action" : "sts:AssumeRole",
42+
"Condition" : {
43+
"StringEquals" : {
44+
"aws:SourceAccount" : local.admin_account_id
45+
},
46+
"StringLike" : {
47+
"aws:SourceArn" : "arn:aws:cloudformation:*:${local.admin_account_id}:stackset/*"
48+
}
49+
}
50+
}
51+
],
5352
}
54-
}
55-
}
56-
57-
data "aws_iam_policy_document" "admin_execution" {
58-
statement {
59-
actions = ["sts:AssumeRole"]
60-
effect = "Allow"
61-
resources = ["arn:aws:iam::*:role/AWSCloudFormationStackSetExecutionRole"]
62-
}
53+
)
6354
}
6455

6556
resource "aws_iam_role_policy" "admin_execution" {
6657
provider = aws.admin
6758

68-
name = "AssumeExecutionRole"
69-
policy = data.aws_iam_policy_document.admin_execution.json
70-
role = aws_iam_role.admin.name
59+
name = "AssumeExecutionRole"
60+
role = aws_iam_role.admin.name
61+
62+
# Allow assuming the execution role in any (*) account to avoid coupling the
63+
# target accounts with assets with this policy.
64+
policy = jsonencode(
65+
{
66+
"Version" : "2012-10-17",
67+
"Statement" : [
68+
{
69+
"Effect" : "Allow",
70+
"Action" : "sts:AssumeRole",
71+
"Resource" : "arn:aws:iam::*:role/AWSCloudFormationStackSetExecutionRole"
72+
}
73+
]
74+
}
75+
)
7176
}

asset-account/terraform/stack-set/examples/self-managed/asset.tf

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
resource "aws_iam_role" "execution" {
22
provider = aws.asset
33

4-
name = "AWSCloudFormationStackSetExecutionRole"
5-
assume_role_policy = data.aws_iam_policy_document.execution_trust.json
6-
}
7-
8-
data "aws_iam_policy_document" "execution_trust" {
9-
statement {
10-
actions = ["sts:AssumeRole"]
11-
effect = "Allow"
12-
13-
principals {
14-
identifiers = [aws_iam_role.admin.arn]
15-
type = "AWS"
4+
name = "AWSCloudFormationStackSetExecutionRole"
5+
assume_role_policy = jsonencode(
6+
{
7+
"Version" : "2012-10-17",
8+
"Statement" : [
9+
{
10+
"Effect" : "Allow",
11+
"Action" : ["sts:AssumeRole"],
12+
"Principal" : {
13+
"AWS" : aws_iam_role.admin.arn
14+
}
15+
}
16+
]
1617
}
17-
}
18+
)
1819
}
1920

2021
# Specifies the set of permissions required for the deployment of the Cloudfomation stack

0 commit comments

Comments
 (0)