-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
role.tf
110 lines (97 loc) · 2.36 KB
/
role.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
resource "aws_iam_role" "access_bastion" {
name = var.resource_names.prefix
description = "Role used to connect to the bastion instance."
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = "sts:AssumeRole",
Principal = { "AWS" : var.iam_user_arns }
}]
})
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "access_bastion" {
role = aws_iam_role.access_bastion.name
policy_arn = aws_iam_policy.access_bastion.arn
}
resource "aws_iam_policy" "access_bastion" {
name = var.resource_names.prefix
path = var.iam_role_path
description = "Allows the access to the bastion host."
policy = data.aws_iam_policy_document.access_bastion.json
tags = var.tags
}
data "aws_iam_policy_document" "access_bastion" {
statement {
sid = "Ec2FindBastion"
effect = "Allow"
actions = [
"ec2:DescribeInstances",
]
resources = [
"*"
]
}
# not critical as we use a condition and thus allowing a very limited number of resources only
# tfsec:ignore:aws-iam-no-policy-wildcards
statement {
sid = "SSHBastion"
effect = "Allow"
actions = [
"ec2-instance-connect:SendSSHPublicKey",
"ssm:StartSession"
]
resources = ["*"]
condition {
test = "ForAnyValue:StringEqualsIfExists"
values = [var.bastion_access_tag_value]
variable = "aws:ResourceTag/bastion-access"
}
}
statement {
sid = "SsmStartSession"
effect = "Allow"
actions = [
"ssm:StartSession"
]
resources = [
"arn:aws:ssm:${data.aws_region.this.name}::document/AWS-StartSSHSession"
]
}
statement {
sid = "SsmGetDocument"
effect = "Allow"
actions = [
"ssm:GetDocument",
]
resources = [
"arn:aws:ssm:::document/SSM-SessionManagerRunShell"
]
}
# terminate session is not critical
# tfsec:ignore:aws-iam-no-policy-wildcards
statement {
sid = "SsmTerminateSession"
effect = "Allow"
actions = [
"ssm:TerminateSession",
]
resources = [
"*"
]
}
statement {
sid = "SsmDescribeSSMConnection"
effect = "Allow"
actions = [
"ssm:DescribeSessions",
"ssm:GetConnectionStatus",
"ssm:DescribeInstanceProperties",
]
resources = [
"*"
]
}
}