Skip to content

Commit e59d627

Browse files
committed
Merge remote-tracking branch 'skeleton/develop' into lineage/skeleton
2 parents 4435a21 + 26819a9 commit e59d627

File tree

9 files changed

+213
-57
lines changed

9 files changed

+213
-57
lines changed

.github/workflows/build.yml

+9-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ jobs:
170170
uses: mxschmitt/action-tmate@v3
171171
if: env.RUN_TMATE
172172
test:
173+
# TODO: Figure out a plan to test in all environments, not just dev-a.
174+
# See cisagov/skeleton-ansible-role-with-test-user#183 for more details.
175+
environment: dev-a
173176
name: >-
174177
test (${{ matrix.scenario }}) -
175178
${{ matrix.platform }}-${{ matrix.architecture }}
@@ -275,7 +278,12 @@ jobs:
275278
sudo apt-get install apparmor-utils
276279
sudo aa-disable /usr/sbin/unix_chkpwd
277280
if: ${{ startsWith(matrix.platform, 'fedora') }}
278-
- name: Run molecule tests
281+
- # This is an example of how to pass GHA secrets to Molecule
282+
# via an environment variable. See also
283+
# molecule/default/converge.yml in this repository.
284+
# env:
285+
# THIRD_PARTY_BUCKET: ${{ secrets.THIRD_PARTY_BUCKET }}
286+
name: Run molecule tests
279287
run: >-
280288
molecule test
281289
--platform-name ${{ matrix.platform }}-${{ matrix.architecture }}

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ __pycache__
1010
## Terraform ##
1111
.terraform
1212
.terraform.lock.hcl
13+
*.tfconfig
14+
*.tfvars

README.md

+79-35
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,90 @@ An Ansible role for installing
88

99
## Pre-requisites (Ignore Until the COOL Migration) ##
1010

11-
In order to execute the Molecule tests for this Ansible role in GitHub
12-
Actions, a build user must exist in AWS. The accompanying Terraform
13-
code will create the user with the appropriate name and
14-
permissions. This only needs to be run once per project, per AWS
15-
account. This user can also be used to run the Molecule tests on your
16-
local machine.
17-
18-
Before the build user can be created, you will need a profile in your
19-
AWS credentials file that allows you to read and write your remote
20-
Terraform state. (You almost certainly do not want to use local
21-
Terraform state for this long-lived build user.) If the build user is
22-
to be created in the CISA COOL environment, for example, then you will
23-
need the `cool-terraform-backend` profile.
24-
25-
The easiest way to set up the Terraform remote state profile is to
26-
make use of our
27-
[`aws-profile-sync`](https://github.com/cisagov/aws-profile-sync)
28-
utility. Follow the usage instructions in that repository before
29-
continuing with the next steps, and note that you will need to know
30-
where your team stores their remote profile data in order to use
11+
In order to execute the Molecule tests for this Ansible role in GitHub Actions,
12+
a test user must exist in AWS. The accompanying Terraform code will create the
13+
user with the appropriate name and permissions. This only needs to be run once
14+
per project, per AWS account. This user can also be used to run the Molecule
15+
tests on your local machine.
16+
17+
Before the test user can be created, you will need a profile in your AWS
18+
credentials file that allows you to read and write your remote Terraform state.
19+
(You almost certainly do not want to use local Terraform state for this
20+
long-lived test user.) If the test user is to be created in the CISA COOL
21+
environment, for example, then you will need the `cool-terraform-backend`
22+
profile.
23+
24+
The easiest way to set up the Terraform remote state profile is to make use of
25+
our [`aws-profile-sync`](https://github.com/cisagov/aws-profile-sync) utility.
26+
Follow the usage instructions in that repository before continuing with the next
27+
steps, and note that you will need to know where your team stores their remote
28+
profile data in order to use
3129
[`aws-profile-sync`](https://github.com/cisagov/aws-profile-sync).
3230

33-
To create the build user, follow these instructions:
31+
### Creating a test user ###
3432

35-
```console
36-
cd terraform
37-
terraform init --upgrade=true
38-
terraform apply
39-
```
33+
You will need to create a test user for each environment that you use. The
34+
following steps show how to create a test user for an environment named "dev".
35+
You will need to repeat this process for any additional environments.
36+
37+
1. Change into the `terraform` directory:
38+
39+
```console
40+
cd terraform
41+
```
42+
43+
1. Create a backend configuration file named `dev.tfconfig` containing the
44+
name of the bucket where "dev" environment Terraform state is stored - this file
45+
is required to initialize the Terraform backend in each environment:
46+
47+
```hcl
48+
bucket = "my-dev-terraform-state-bucket"
49+
```
50+
51+
1. Initialize the Terraform backend for the "dev" environment using your backend
52+
configuration file:
53+
54+
```console
55+
terraform init -backend-config=dev.tfconfig
56+
```
57+
58+
> [!NOTE]
59+
> When performing this step for additional environments (i.e. not your first
60+
> environment), use the `-reconfigure` flag:
61+
>
62+
> ```console
63+
> terraform init -backend-config=other-env.tfconfig -reconfigure
64+
> ```
65+
66+
1. Create a Terraform variables file named `dev.tfvars` containing all
67+
required variables (currently only `terraform_state_bucket`):
68+
69+
```hcl
70+
terraform_state_bucket = "my-dev-terraform-state-bucket"
71+
```
72+
73+
1. Create a Terraform workspace for the "dev" environment:
74+
75+
```console
76+
terraform workspace new dev
77+
```
78+
79+
1. Initialize and upgrade the Terraform workspace, then apply the configuration
80+
to create the test user in the "dev" environment:
81+
82+
```console
83+
terraform init -upgrade=true
84+
terraform apply -var-file=dev.tfvars
85+
```
4086

41-
Once the user is created you will need to update the [repository's
42-
secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets)
43-
with the new encrypted environment variables. This should be done
44-
using the
87+
Once the test user is created you will need to update the
88+
[repository's secrets](https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets)
89+
with the new encrypted environment variables. This should be done using the
4590
[`terraform-to-secrets`](https://github.com/cisagov/development-guide/tree/develop/project_setup#terraform-iam-credentials-to-github-secrets-)
46-
tool available in the [development
47-
guide](https://github.com/cisagov/development-guide). Instructions for
48-
how to use this tool can be found in the ["Terraform IAM Credentials
49-
to GitHub Secrets"
50-
section](https://github.com/cisagov/development-guide/tree/develop/project_setup#terraform-iam-credentials-to-github-secrets-).
91+
tool available in the
92+
[development guide](https://github.com/cisagov/development-guide). Instructions
93+
for how to use this tool can be found in the
94+
["Terraform IAM Credentials to GitHub Secrets" section](https://github.com/cisagov/development-guide/tree/develop/project_setup#terraform-iam-credentials-to-github-secrets-).
5195
of the Project Setup README.
5296

5397
If you have appropriate permissions for the repository you can view

terraform/backend.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
terraform {
22
backend "s3" {
3-
bucket = "ncats-terraform-state-storage"
3+
# Use a partial configuration to avoid hardcoding the bucket name. This
4+
# allows the bucket name to be set on a per-environment basis via the
5+
# -backend-config command line option or other methods. For details, see:
6+
# https://developer.hashicorp.com/terraform/language/backend#partial-configuration
7+
bucket = ""
48
dynamodb_table = "terraform-state-lock"
59
encrypt = true
610
key = "ansible-role-ncats-webd/terraform.tfstate"
11+
profile = "cool-terraform-backend"
712
region = "us-east-1"
813
}
914
}

terraform/outputs.tf

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
output "access_key" {
2-
value = module.user.access_key
32
description = "The IAM access key associated with the CI IAM user created by this module."
43
sensitive = true
4+
value = module.user.access_key
55
}
66

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

1712
output "user" {
18-
value = module.user.user
1913
description = "The CI IAM user created by this module."
14+
value = module.user.user
2015
}

terraform/providers.tf

+41
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,44 @@ provider "aws" {
88
}
99
region = var.aws_region
1010
}
11+
12+
# The provider used to create the role that can be assumed to do
13+
# everything the CI user needs to do in the Images account.
14+
provider "aws" {
15+
alias = "images_provisionaccount"
16+
assume_role {
17+
role_arn = data.terraform_remote_state.images.outputs.provisionaccount_role.arn
18+
session_name = local.caller_user_name
19+
}
20+
default_tags {
21+
tags = var.tags
22+
}
23+
region = var.aws_region
24+
}
25+
26+
# The provider used to create policies and roles that can read
27+
# parameters from AWS SSM Parameter Store in the Images account.
28+
provider "aws" {
29+
alias = "images_ssm"
30+
assume_role {
31+
role_arn = data.terraform_remote_state.images_ssm.outputs.provisionparameterstorereadroles_role.arn
32+
session_name = local.caller_user_name
33+
}
34+
default_tags {
35+
tags = var.tags
36+
}
37+
region = var.aws_region
38+
}
39+
40+
# The provider used to create the test user
41+
provider "aws" {
42+
alias = "users"
43+
assume_role {
44+
role_arn = data.terraform_remote_state.users.outputs.provisionaccount_role.arn
45+
session_name = local.caller_user_name
46+
}
47+
default_tags {
48+
tags = var.tags
49+
}
50+
region = var.aws_region
51+
}

terraform/remote_states.tf

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# ------------------------------------------------------------------------------
2+
# Retrieve state data from a Terraform backend. This allows use of the
3+
# root-level outputs of one or more Terraform configurations as input
4+
# data for this configuration.
5+
# ------------------------------------------------------------------------------
6+
7+
data "terraform_remote_state" "images" {
8+
backend = "s3"
9+
10+
config = {
11+
bucket = var.terraform_state_bucket
12+
dynamodb_table = "terraform-state-lock"
13+
encrypt = true
14+
key = "cool-accounts/images.tfstate"
15+
profile = "cool-terraform-readstate"
16+
region = "us-east-1"
17+
}
18+
19+
workspace = terraform.workspace
20+
}
21+
22+
data "terraform_remote_state" "images_ssm" {
23+
backend = "s3"
24+
25+
config = {
26+
bucket = var.terraform_state_bucket
27+
dynamodb_table = "terraform-state-lock"
28+
encrypt = true
29+
key = "cool-images-parameterstore/terraform.tfstate"
30+
profile = "cool-terraform-readstate"
31+
region = "us-east-1"
32+
}
33+
34+
workspace = terraform.workspace
35+
}
36+
37+
data "terraform_remote_state" "users" {
38+
backend = "s3"
39+
40+
config = {
41+
bucket = var.terraform_state_bucket
42+
dynamodb_table = "terraform-state-lock"
43+
encrypt = true
44+
key = "cool-accounts/users.tfstate"
45+
profile = "cool-terraform-readstate"
46+
region = "us-east-1"
47+
}
48+
49+
workspace = terraform.workspace
50+
}

terraform/user.tf

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ module "user" {
33
source = "github.com/cisagov/molecule-iam-user-tf-module"
44

55
providers = {
6-
aws = aws
7-
aws.images-production-provisionaccount = aws
8-
aws.images-staging-provisionaccount = aws
9-
aws.images-production-ssm = aws
10-
aws.images-staging-ssm = aws
6+
aws = aws.users
7+
aws.images-provisionaccount = aws.images_provisionaccount
8+
aws.images-ssm = aws.images_ssm
119
}
1210

1311
entity = "ansible-role-ncats-webd"

terraform/variables.tf

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1+
# ------------------------------------------------------------------------------
2+
# Required parameters
3+
#
4+
# You must provide a value for each of these parameters.
5+
# ------------------------------------------------------------------------------
6+
7+
variable "terraform_state_bucket" {
8+
description = "The name of the S3 bucket where Terraform state is stored."
9+
nullable = false
10+
type = string
11+
}
12+
113
# ------------------------------------------------------------------------------
214
# Optional parameters
315
#
416
# These parameters have reasonable defaults.
517
# ------------------------------------------------------------------------------
618

719
variable "aws_region" {
8-
type = string
9-
description = "The AWS region to deploy into (e.g. us-east-1)."
1020
default = "us-east-1"
21+
description = "The AWS region to deploy into (e.g. us-east-1)."
22+
nullable = false
23+
type = string
1124
}
1225

1326
variable "tags" {
14-
type = map(string)
15-
description = "Tags to apply to all AWS resources created"
16-
1727
default = {
1828
Team = "VM Fusion - Development"
1929
Application = "ansible-role-ncats-webd testing"
2030
}
31+
description = "Tags to apply to all AWS resources created"
32+
nullable = false
33+
type = map(string)
2134
}

0 commit comments

Comments
 (0)