@@ -56,8 +56,10 @@ resource "aws_iam_policy" "ckr_log_policy" {
56
56
EOF
57
57
}
58
58
59
-
59
+ # SSM is a valid location of the cloud-key-rotator, so allow ssm:PutParameter
60
+ # if enabled
60
61
resource "aws_iam_policy" "ckr_ssm_policy" {
62
+ count = var. enable_ssm_location ? 1 : 0
61
63
name = " CloudKeyRotatorSsmPolicy"
62
64
path = " /"
63
65
@@ -79,30 +81,62 @@ resource "aws_iam_policy" "ckr_ssm_policy" {
79
81
EOF
80
82
}
81
83
84
+ resource "aws_iam_policy" "ckr_policy" {
85
+ name = " CloudKeyRotatorPolicy"
86
+ path = " /"
87
+ policy = jsonencode (
88
+ {
89
+ Statement = [
90
+ {
91
+ Action = [
92
+ " iam:DeleteAccessKey" ,
93
+ " iam:CreateAccessKey" ,
94
+ " iam:ListAccessKeys" ,
95
+ ]
96
+ Effect = " Allow"
97
+ Resource = [
98
+ " arn:aws:iam::*:user/*" ,
99
+ ]
100
+ },
101
+ {
102
+ Action = " iam:ListUsers"
103
+ Effect = " Allow"
104
+ Resource = " arn:aws:iam::*:*"
105
+ },
106
+ {
107
+ Action = " secretsmanager:GetSecretValue"
108
+ Effect = " Allow"
109
+ Resource = [
110
+ aws_secretsmanager_secret.ckr- config.arn,
111
+ ]
112
+ },
113
+ ]
114
+ Version = " 2012-10-17"
115
+ }
116
+ )
117
+ }
82
118
83
119
resource "aws_iam_role_policy_attachment" "attach-ckr-log-policy" {
84
120
role = aws_iam_role. cloudkeyrotator_role . name
85
121
policy_arn = aws_iam_policy. ckr_log_policy . arn
86
122
}
87
123
88
- resource "aws_iam_role_policy_attachment" "attach-ckr-iam-policy" {
89
- role = aws_iam_role. cloudkeyrotator_role . name
90
- policy_arn = " arn:aws:iam::aws:policy/IAMFullAccess"
91
- }
92
- resource "aws_iam_role_policy_attachment" "attach-ckr-secret-policy" {
124
+ resource "aws_iam_role_policy_attachment" "attach-ckr-policy" {
93
125
role = aws_iam_role. cloudkeyrotator_role . name
94
- policy_arn = " arn:aws:iam::aws:policy/SecretsManagerReadWrite "
126
+ policy_arn = aws_iam_policy . ckr_policy . arn
95
127
}
96
128
129
+ # only create ssm attachment if SSM is enabled (indicating it's being used
130
+ # as a cloud-key-rotator location)
97
131
resource "aws_iam_role_policy_attachment" "attach-ckr-ssm-policy" {
132
+ count = var. enable_ssm_location ? 1 : 0
98
133
role = aws_iam_role. cloudkeyrotator_role . name
99
- policy_arn = aws_iam_policy. ckr_ssm_policy . arn
134
+ policy_arn = aws_iam_policy. ckr_ssm_policy [ 0 ] . arn
100
135
}
101
136
102
- # Lambda
103
137
104
138
resource "aws_lambda_function" "cloud_key_rotator" {
105
-
139
+ description = " A function for rotating cloud keys "
106
140
s3_bucket = " ckr-terraform-module-code"
107
141
s3_key = " cloud-key-rotator_${ var . ckr_version } _lambda.zip"
108
142
function_name = " cloud-key-rotator"
0 commit comments