Skip to content

Commit 15d847b

Browse files
authored
Merge pull request #61 from cisagov/improvement/modern-env
Create Molecule users for a single environment
2 parents 2d00280 + acab884 commit 15d847b

7 files changed

Lines changed: 46 additions & 115 deletions

File tree

README.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,39 @@
22

33
[![GitHub Build Status](https://github.com/cisagov/molecule-iam-user-tf-module/workflows/build/badge.svg)](https://github.com/cisagov/molecule-iam-user-tf-module/actions)
44

5-
A Terraform module for creating an IAM user suitable for use in molecule
6-
testing of an Ansible role.
5+
A Terraform module for creating an IAM user suitable for use in [Molecule
6+
testing](https://ansible.readthedocs.io/projects/molecule/) of an
7+
[Ansible](https://www.redhat.com/en/ansible-collaborative) role.
78

89
## Usage ##
910

10-
### Multi-Account Usage ###
11+
### Multi-provider usage ###
1112

1213
```hcl
1314
module "example" {
1415
source = "github.com/cisagov/molecule-iam-user-tf-module"
1516
1617
providers = {
17-
aws = aws
18-
aws.images-production-provisionaccount = aws.images-production-provisionaccount
19-
aws.images-staging-provisionaccount = aws.images-staging-provisionaccount
20-
aws.images-production-ssm = aws.images-production-ssm
21-
aws.images-staging-ssm = aws.images-staging-ssm
18+
aws = aws
19+
aws.images-provisionaccount = aws.images-provisionaccount
20+
aws.images-ssm = aws.images-ssm
2221
}
2322
2423
entity = "my-repo"
2524
ssm_parameters = ["/example/parameter1", "/example/config/*"]
2625
}
2726
```
2827

29-
### Single Account Usage ###
28+
### Single provider usage ###
3029

3130
```hcl
3231
module "example" {
3332
source = "github.com/cisagov/molecule-iam-user-tf-module"
3433
3534
providers = {
36-
aws = aws
37-
aws.images-production-provisionaccount = aws
38-
aws.images-staging-provisionaccount = aws
39-
aws.images-production-ssm = aws
40-
aws.images-staging-ssm = aws
35+
aws = aws
36+
aws.images-provisionaccount = aws
37+
aws.images-ssm = aws
4138
}
4239
4340
entity = "my-repo"
@@ -62,23 +59,20 @@ module "example" {
6259
| Name | Version |
6360
|------|---------|
6461
| aws | >= 4.9 |
65-
| aws.images-production-provisionaccount | >= 4.9 |
66-
| aws.images-staging-provisionaccount | >= 4.9 |
62+
| aws.images-provisionaccount | >= 4.9 |
6763

6864
## Modules ##
6965

7066
| Name | Source | Version |
7167
|------|--------|---------|
7268
| ci\_user | github.com/cisagov/ci-iam-user-tf-module | n/a |
73-
| parameterstorereadonly\_role\_production | github.com/cisagov/ssm-read-role-tf-module | n/a |
74-
| parameterstorereadonly\_role\_staging | github.com/cisagov/ssm-read-role-tf-module | n/a |
69+
| parameterstorereadonly\_role | github.com/cisagov/ssm-read-role-tf-module | n/a |
7570

7671
## Resources ##
7772

7873
| Name | Type |
7974
|------|------|
80-
| [aws_iam_role_policy_attachment.ssm_production_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
81-
| [aws_iam_role_policy_attachment.ssm_staging_attachment](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
75+
| [aws_iam_role_policy_attachment.ssm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
8276
| [aws_iam_user_policy.assume_parameterstorereadonly](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy) | resource |
8377
| [aws_caller_identity.users](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
8478
| [aws_iam_policy_document.assume_parameterstorereadonly_role_doc](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
@@ -95,8 +89,7 @@ module "example" {
9589
| Name | Description |
9690
|------|-------------|
9791
| access\_key | The IAM access key associated with the CI IAM user created by this module. |
98-
| production\_role | The IAM role that the CI user can assume to read SSM parameters in the production account. |
99-
| staging\_role | The IAM role that the CI user can assume to read SSM parameters in the staging account. |
92+
| role | The IAM role that the CI user can assume to read SSM parameters. |
10093
| user | The CI IAM user created by this module. |
10194
<!-- END_TF_DOCS -->
10295

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# IAM policy document that allows assumption of the ParameterStoreReadOnly
2-
# role in the Images accounts (Production and Staging) for this user
2+
# role in the Images account for this user
33
data "aws_iam_policy_document" "assume_parameterstorereadonly_role_doc" {
44
statement {
55
actions = [
@@ -8,16 +8,15 @@ data "aws_iam_policy_document" "assume_parameterstorereadonly_role_doc" {
88
]
99
effect = "Allow"
1010
resources = [
11-
module.parameterstorereadonly_role_production.role.arn,
12-
module.parameterstorereadonly_role_staging.role.arn
11+
module.parameterstorereadonly_role.role.arn
1312
]
1413
}
1514
}
1615

1716
# The IAM policy allowing this user to assume their custom
18-
# ParameterStoreReadOnly role in the Images accounts (Production and Staging)
17+
# ParameterStoreReadOnly role in the Images account
1918
resource "aws_iam_user_policy" "assume_parameterstorereadonly" {
20-
name = "Images-Assume${module.parameterstorereadonly_role_production.role.name}"
19+
name = "Images-Assume${module.parameterstorereadonly_role.role.name}"
2120
policy = data.aws_iam_policy_document.assume_parameterstorereadonly_role_doc.json
2221
user = module.ci_user.user.name
2322
}

examples/basic_usage/main.tf

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,57 +15,34 @@ provider "aws" {
1515
region = "us-east-1"
1616
}
1717

18-
# ProvisionAccount AWS provider for the Images (Production) account
18+
# ProvisionAccount AWS provider for the Images account
1919
provider "aws" {
20-
alias = "images-production-provisionaccount"
20+
alias = "images-provisionaccount"
2121
default_tags {
2222
tags = local.tags
2323
}
24-
profile = "cool-images-production-provisionaccount"
24+
profile = "cool-images-provisionaccount"
2525
region = "us-east-1"
2626
}
2727

28-
# ProvisionAccount AWS provider for the Images (Staging) account
28+
# ProvisionParameterStoreReadRoles AWS provider for the Images account
2929
provider "aws" {
30-
alias = "images-staging-provisionaccount"
30+
alias = "images-ssm"
3131
default_tags {
3232
tags = local.tags
3333
}
34-
profile = "cool-images-staging-provisionaccount"
34+
profile = "cool-images-provisionparameterstorereadroles"
3535
region = "us-east-1"
3636
}
3737

38-
# ProvisionParameterStoreReadRoles AWS provider for the
39-
# Images (Production) account
40-
provider "aws" {
41-
alias = "images-production-ssm"
42-
default_tags {
43-
tags = local.tags
44-
}
45-
profile = "cool-images-production-provisionparameterstorereadroles"
46-
region = "us-east-1"
47-
}
48-
49-
# ProvisionParameterStoreReadRoles AWS provider for the
50-
# Images (Staging) account
51-
provider "aws" {
52-
alias = "images-staging-ssm"
53-
default_tags {
54-
tags = local.tags
55-
}
56-
profile = "cool-images-staging-provisionparameterstorereadroles"
57-
region = "us-east-1"
58-
}
5938

6039
module "iam_user" {
6140
source = "../.."
6241

6342
providers = {
64-
aws = aws
65-
aws.images-production-provisionaccount = aws.images-production-provisionaccount
66-
aws.images-staging-provisionaccount = aws.images-staging-provisionaccount
67-
aws.images-production-ssm = aws.images-production-ssm
68-
aws.images-staging-ssm = aws.images-staging-ssm
43+
aws = aws
44+
aws.images-provisionaccount = aws.images-provisionaccount
45+
aws.images-ssm = aws.images-ssm
6946
}
7047

7148
entity = "molecule-iam-user-tf-module"

outputs.tf

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,9 @@ output "access_key" {
44
value = module.ci_user.access_key
55
}
66

7-
output "production_role" {
8-
description = "The IAM role that the CI user can assume to read SSM parameters in the production account."
9-
value = module.ci_user.production_role
10-
}
11-
12-
output "staging_role" {
13-
description = "The IAM role that the CI user can assume to read SSM parameters in the staging account."
14-
value = module.ci_user.staging_role
7+
output "role" {
8+
description = "The IAM role that the CI user can assume to read SSM parameters."
9+
value = module.ci_user.role
1510
}
1611

1712
output "user" {

parameterstorereadonly_role.tf

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
# ------------------------------------------------------------------------------
2-
# Create the IAM roles that allow read-only access to the specified SSM
3-
# Parameter Store parameters in the Images accounts (Production and Staging).
2+
# Create the IAM role that allows read-only access to the specified SSM
3+
# Parameter Store parameters in the Images account.
44
# ------------------------------------------------------------------------------
55

66
# Get the default caller identity, which corresponds to the Users account.
77
# This is needed to determine the Users account ID.
88
data "aws_caller_identity" "users" {
99
}
1010

11-
module "parameterstorereadonly_role_production" {
11+
module "parameterstorereadonly_role" {
1212
source = "github.com/cisagov/ssm-read-role-tf-module"
1313

1414
providers = {
15-
aws = aws.images-production-ssm
15+
aws = aws.images-ssm
1616
}
1717

1818
account_ids = [data.aws_caller_identity.users.account_id]
1919
entity_name = var.entity
2020
iam_usernames = [module.ci_user.user.name]
21-
role_name = "ParameterStoreReadOnly-%s-Production"
22-
ssm_names = var.ssm_parameters
23-
}
24-
25-
module "parameterstorereadonly_role_staging" {
26-
source = "github.com/cisagov/ssm-read-role-tf-module"
27-
28-
providers = {
29-
aws = aws.images-staging-ssm
30-
}
31-
32-
account_ids = [data.aws_caller_identity.users.account_id]
33-
entity_name = var.entity
34-
iam_usernames = [module.ci_user.user.name]
35-
role_name = "ParameterStoreReadOnly-%s-Staging"
21+
role_name = "ParameterStoreReadOnly-%s"
3622
ssm_names = var.ssm_parameters
3723
}

providers.tf

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
# This is the provider that is used to create the role that can be
22
# assumed to perform CI functions.
33
provider "aws" {
4-
alias = "images-production-provisionaccount"
4+
alias = "images-provisionaccount"
55
}
66

7-
# This is the provider that is used to create the role that can be
8-
# assumed to perform CI functions.
9-
provider "aws" {
10-
alias = "images-staging-provisionaccount"
11-
}
12-
13-
# This is the provider that is used to create the role and policy that can
14-
# read Parameter Store parameters inside the Images Production account
15-
provider "aws" {
16-
alias = "images-production-ssm"
17-
}
187

198
# This is the provider that is used to create the role and policy that can
20-
# read Parameter Store parameters inside the Images Staging account
9+
# read Parameter Store parameters inside the Images account
2110
provider "aws" {
22-
alias = "images-staging-ssm"
11+
alias = "images-ssm"
2312
}

user.tf

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,19 @@ module "ci_user" {
22
source = "github.com/cisagov/ci-iam-user-tf-module"
33

44
providers = {
5-
aws = aws
6-
aws.production = aws.images-production-provisionaccount
7-
aws.staging = aws.images-staging-provisionaccount
5+
aws = aws
6+
aws.ci = aws.images-provisionaccount
87
}
98

109
role_description = local.role_description
1110
role_name = local.role_name
1211
user_name = local.user_name
1312
}
1413

15-
# Attach the AWS SSM Parameter Store read role policies to the CI
16-
# production and staging roles
17-
resource "aws_iam_role_policy_attachment" "ssm_staging_attachment" {
18-
provider = aws.images-staging-provisionaccount
14+
# Attach the AWS SSM Parameter Store read role policy to the CI role
15+
resource "aws_iam_role_policy_attachment" "ssm" {
16+
provider = aws.images-provisionaccount
1917

20-
policy_arn = module.parameterstorereadonly_role_staging.policy.arn
21-
role = module.ci_user.staging_role.name
22-
}
23-
resource "aws_iam_role_policy_attachment" "ssm_production_attachment" {
24-
provider = aws.images-production-provisionaccount
25-
26-
policy_arn = module.parameterstorereadonly_role_production.policy.arn
27-
role = module.ci_user.production_role.name
18+
policy_arn = module.parameterstorereadonly_role.policy.arn
19+
role = module.ci_user.role.name
2820
}

0 commit comments

Comments
 (0)