Skip to content

Commit f00e273

Browse files
authored
Introduce elastio-iam-policies terraform module (#101)
This PR introduces the `elastio-iam-policies` module with the initial IAM policy code generated via the TypeScript code. The first policy that I added lists the IAM permissions required to deploy the Elastio Asset Account stack. I did the codegen here for several reasons: - I expect that we'll extend the set of IAM policies with permission boundaries in the future and it'll be very important to have shared code that handles them by taking subsets of the golden `ElastioCloudConnector` role policy. Plus we may need to support CFN variant of the same stack in the future. - I found an [amazing `npm` package](https://github.com/JuroOravec/aws-iam-policy-types/tree/main) that adds type definitions for IAM actions making it impossible to fuck up the IAM actions in IAM policy definitions
1 parent 474af6c commit f00e273

File tree

27 files changed

+1239
-52
lines changed

27 files changed

+1239
-52
lines changed

.editorconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[*.ts]
2+
indent_size = 2

.githooks/pre-commit

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ if command_exists terraform-docs; then
4242
done
4343
fi
4444

45+
if command_exists npm; then
46+
npm run codegen
47+
files="$files $script_dir/../iam-policies/terraform/policies"
48+
fi
49+
4550
if command_exists ./node_modules/.bin/prettier; then
4651
echo "$files" | xargs ./node_modules/.bin/prettier --ignore-unknown --write
4752
fi
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Collect metadata about the repository
2+
description: >
3+
Looks for modules and examples in the repository and outputs their paths.
4+
5+
outputs:
6+
tf-modules:
7+
description: Paths to the Terraform modules found in the repository
8+
value: ${{ steps.find-modules.outputs.tf-modules }}
9+
10+
tf-examples:
11+
description: Paths to the Terraform examples found in the repository
12+
value: ${{ steps.find-examples.outputs.tf-examples }}
13+
14+
runs:
15+
using: composite
16+
steps:
17+
- name: Find modules
18+
id: find-modules
19+
run: |
20+
tf_modules=$(./.github/scripts/collect-modules.sh | jq -cnR '[inputs]')
21+
echo "tf-modules=$tf_modules" > "$GITHUB_OUTPUT"
22+
shell: bash
23+
24+
- name: Find examples
25+
id: find-examples
26+
run: |
27+
tf_examples=$(./.github/scripts/collect-examples.sh | jq -cnR '[inputs]')
28+
echo "tf-examples=$tf_examples" > "$GITHUB_OUTPUT"
29+
shell: bash

.github/actions/collect-modules/action.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
git ls-files --cached --others --exclude-standard \
6+
| grep '/examples/' \
7+
| xargs -I{} dirname {} \
8+
| sort -u

.github/scripts/collect-modules.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ while IFS= read -r -d '' module_cfg; do
1010
exit 1
1111
;;
1212
terraform)
13-
echo "Found Terraform module in $module_cfg" >&2
1413
tf_modules+=("$(dirname "$module_cfg")")
1514
;;
1615
esac

.github/workflows/ci.yml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ on:
66
pull_request:
77

88
jobs:
9-
collect-modules:
9+
meta:
1010
runs-on: ubuntu-latest
1111
outputs:
12-
tf-modules: ${{ steps.collect-modules.outputs.tf-modules }}
12+
tf-modules: ${{ steps.meta.outputs.tf-modules }}
13+
tf-examples: ${{ steps.meta.outputs.tf-examples }}
1314

1415
steps:
1516
- uses: actions/checkout@v4
16-
17-
- uses: ./.github/actions/collect-modules
18-
id: collect-modules
17+
- uses: ./.github/actions/collect-meta
18+
id: meta
1919

2020
typos:
2121
runs-on: ubuntu-latest
@@ -37,13 +37,11 @@ jobs:
3737

3838
terraform-validate:
3939
runs-on: ubuntu-latest
40+
needs: [meta]
4041

4142
strategy:
4243
matrix:
43-
project:
44-
- asset-account/terraform/stack-set/examples/self-managed
45-
- asset-account/terraform/stack-set/examples/service-managed
46-
- connector/terraform/examples/basic
44+
terraform_example: ${{fromJson(needs.meta.outputs.tf-modules)}}
4745

4846
steps:
4947
- uses: actions/checkout@v4
@@ -54,19 +52,18 @@ jobs:
5452
terraform_wrapper: false
5553

5654
- run: terraform init -input=false
57-
working-directory: ${{ matrix.project }}
55+
working-directory: ${{ matrix.terraform_example }}
5856

5957
- run: terraform validate
60-
working-directory: ${{ matrix.project }}
58+
working-directory: ${{ matrix.terraform_example }}
6159

6260
terraform-docs:
6361
runs-on: ubuntu-latest
64-
needs:
65-
- collect-modules
62+
needs: [meta]
6663

6764
strategy:
6865
matrix:
69-
terraform_module: ${{fromJson(needs.collect-modules.outputs.tf-modules)}}
66+
terraform_module: ${{fromJson(needs.meta.outputs.tf-modules)}}
7067

7168
steps:
7269
- uses: actions/checkout@v4
@@ -129,3 +126,21 @@ jobs:
129126
cache: "npm"
130127
- run: npm ci --ignore-scripts
131128
- run: npx prettier --check .
129+
130+
codegen-freshness:
131+
runs-on: ubuntu-latest
132+
133+
steps:
134+
- uses: actions/checkout@v4
135+
- uses: actions/setup-node@v4
136+
with:
137+
node-version: "22"
138+
cache: "npm"
139+
140+
- run: npm ci --ignore-scripts
141+
- run: cd codegen && npm ci --ignore-scripts
142+
- run: npm run codegen
143+
144+
- run: >-
145+
git diff --exit-code --color=always || ( echo "Generated code is
146+
out-of-date. See the diff above." && exit 1 )

.github/workflows/release.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,26 @@ on:
44
workflow_dispatch:
55

66
jobs:
7-
collect-modules:
7+
meta:
88
runs-on: ubuntu-latest
99
outputs:
10-
tf-modules: ${{ steps.collect-modules.outputs.tf-modules }}
10+
tf-modules: ${{ steps.meta.outputs.tf-modules }}
11+
tf-examples: ${{ steps.meta.outputs.tf-examples }}
1112

1213
steps:
1314
- uses: actions/checkout@v4
14-
15-
- uses: ./.github/actions/collect-modules
16-
id: collect-modules
15+
- uses: ./.github/actions/collect-meta
16+
id: meta
1717

1818
release-module:
1919
runs-on: ubuntu-latest
20-
needs: ["collect-modules"]
20+
needs: [meta]
2121
permissions:
2222
contents: write
2323
actions: write
2424
strategy:
2525
matrix:
26-
module: ${{fromJson(needs.collect-modules.outputs.tf-modules)}}
26+
module: ${{fromJson(needs.meta.outputs.tf-modules)}}
2727
fail-fast: false
2828

2929
steps:

asset-account/terraform/stack-set/examples/self-managed/admin.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module "elastio_asset_account" {
77

88
depends_on = [
99
# Needs to wait for the execution role in the asset account to be fully created
10-
aws_iam_role_policy.execution_deployment,
10+
aws_iam_role_policy_attachment.execution_deployment,
1111

1212
# Needs to wait for the admin role in the admin account to be fully created
1313
aws_iam_role_policy.admin_execution,

asset-account/terraform/stack-set/examples/self-managed/asset.tf

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,16 @@ data "aws_iam_policy_document" "execution_trust" {
1818
}
1919

2020
# Specifies the set of permissions required for the deployment of the Cloudfomation stack
21-
data "aws_iam_policy_document" "execution_deployment" {
22-
statement {
23-
actions = ["*"]
24-
effect = "Allow"
25-
resources = ["*"]
26-
}
21+
module "elastio_policies" {
22+
# Use this module from the Cloudsmith registry via the URL in real code:
23+
# source = "terraform.cloudsmith.io/public/elastio-iam-policies/aws"
24+
source = "../../../../../iam-policies/terraform"
25+
policies = ["ElastioAssetAccountDeployer"]
2726
}
2827

29-
resource "aws_iam_role_policy" "execution_deployment" {
28+
resource "aws_iam_role_policy_attachment" "execution_deployment" {
3029
provider = aws.asset
3130

32-
name = "Deployment"
33-
policy = data.aws_iam_policy_document.execution_deployment.json
34-
role = aws_iam_role.execution.name
31+
policy_arn = module.elastio_policies.policies.ElastioAssetAccountDeployer.arn
32+
role = aws_iam_role.execution.name
3533
}

0 commit comments

Comments
 (0)