Skip to content

Commit edb543f

Browse files
authored
Merge pull request #62 from cisagov/improvement/optional-ssm-parameters
Make SSM parameters optional
2 parents 15d847b + f2d7e2a commit edb543f

7 files changed

Lines changed: 26 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ module "example" {
8282
| Name | Description | Type | Default | Required |
8383
|------|-------------|------|---------|:--------:|
8484
| entity | The name of the entity (usually a GitHub repository) being tested (e.g. molecule-iam-user-tf-module). | `string` | n/a | yes |
85-
| ssm\_parameters | The AWS SSM parameters that the IAM user needs to be able to read (e.g. ["/example/parameter1", "/example/config/*"]). | `list(string)` | n/a | yes |
85+
| ssm\_parameters | The AWS SSM parameters that the IAM user needs to be able to read (e.g. ["/example/parameter1", "/example/config/*"]). | `list(string)` | `[]` | no |
8686

8787
## Outputs ##
8888

8989
| Name | Description |
9090
|------|-------------|
9191
| access\_key | The IAM access key associated with the CI IAM user created by this module. |
92-
| role | The IAM role that the CI user can assume to read SSM parameters. |
92+
| role | The IAM role that the CI user can assume to perform testing. |
9393
| user | The CI IAM user created by this module. |
9494
<!-- END_TF_DOCS -->
9595

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
# IAM policy document that allows assumption of the ParameterStoreReadOnly
22
# role in the Images account for this user
33
data "aws_iam_policy_document" "assume_parameterstorereadonly_role_doc" {
4+
count = local.ssm_needed
5+
46
statement {
57
actions = [
68
"sts:AssumeRole",
79
"sts:TagSession",
810
]
911
effect = "Allow"
1012
resources = [
11-
module.parameterstorereadonly_role.role.arn
13+
module.parameterstorereadonly_role[0].role.arn
1214
]
1315
}
1416
}
1517

1618
# The IAM policy allowing this user to assume their custom
1719
# ParameterStoreReadOnly role in the Images account
1820
resource "aws_iam_user_policy" "assume_parameterstorereadonly" {
19-
name = "Images-Assume${module.parameterstorereadonly_role.role.name}"
20-
policy = data.aws_iam_policy_document.assume_parameterstorereadonly_role_doc.json
21+
count = local.ssm_needed
22+
23+
name = "Images-Assume${module.parameterstorereadonly_role[0].role.name}"
24+
policy = data.aws_iam_policy_document.assume_parameterstorereadonly_role_doc[0].json
2125
user = module.ci_user.user.name
2226
}

locals.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
locals {
2-
user_name = format("test-%s", var.entity)
3-
role_name = format("Test-%s", var.entity)
42
role_description = format("A role that can be assumed to allow for CI testing of %s via Molecule.", var.entity)
3+
role_name = format("Test-%s", var.entity)
4+
5+
# Determine if the CI user needs to be able to read SSM parameters
6+
ssm_needed = length(var.ssm_parameters) > 0 ? 1 : 0
7+
8+
user_name = format("test-%s", var.entity)
59
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ output "access_key" {
55
}
66

77
output "role" {
8-
description = "The IAM role that the CI user can assume to read SSM parameters."
8+
description = "The IAM role that the CI user can assume to perform testing."
99
value = module.ci_user.role
1010
}
1111

parameterstorereadonly_role.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ data "aws_caller_identity" "users" {
99
}
1010

1111
module "parameterstorereadonly_role" {
12+
count = local.ssm_needed
1213
source = "github.com/cisagov/ssm-read-role-tf-module"
1314

1415
providers = {

user.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ module "ci_user" {
1313

1414
# Attach the AWS SSM Parameter Store read role policy to the CI role
1515
resource "aws_iam_role_policy_attachment" "ssm" {
16+
count = local.ssm_needed
1617
provider = aws.images-provisionaccount
1718

18-
policy_arn = module.parameterstorereadonly_role.policy.arn
19+
policy_arn = module.parameterstorereadonly_role[0].policy.arn
1920
role = module.ci_user.role.name
2021
}

variables.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ variable "entity" {
1010
type = string
1111
}
1212

13-
variable "ssm_parameters" {
14-
description = "The AWS SSM parameters that the IAM user needs to be able to read (e.g. [\"/example/parameter1\", \"/example/config/*\"])."
15-
nullable = false
16-
type = list(string)
17-
}
18-
1913
# ------------------------------------------------------------------------------
2014
# OPTIONAL PARAMETERS
2115
#
2216
# These parameters have reasonable defaults.
2317
# ------------------------------------------------------------------------------
18+
19+
variable "ssm_parameters" {
20+
default = []
21+
description = "The AWS SSM parameters that the IAM user needs to be able to read (e.g. [\"/example/parameter1\", \"/example/config/*\"])."
22+
nullable = false
23+
type = list(string)
24+
}

0 commit comments

Comments
 (0)