Repository navigation
Expand file tree
/
Copy pathTaskfile.yml
More file actions
338 lines (336 loc) · 16.5 KB
/
Copy pathTaskfile.yml
File metadata and controls
338 lines (336 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
version: '3'
silent: true
tasks:
preflight:terraform:
internal: true
cmds:
- |
#!/bin/bash
set -euo pipefail
expected="$(tr -d '[:space:]' < .terraform-version)"
if command -v tfenv >/dev/null 2>&1; then
if ! tfenv install "$expected"; then
printf '%s\n' "WARNING: tfenv could not install Terraform $expected; checking the Terraform binary already on PATH." >&2
elif ! tfenv use "$expected"; then
printf '%s\n' "WARNING: tfenv could not select Terraform $expected; checking the Terraform binary already on PATH." >&2
fi
fi
if ! command -v terraform >/dev/null 2>&1; then
printf '%s\n' "Terraform $expected is required but was not found on PATH." >&2
if command -v tfenv >/dev/null 2>&1; then
printf '%s\n' "Install it with: tfenv install $expected && tfenv use $expected" >&2
else
printf '%s\n' "Install tfenv, or install Terraform $expected and add it to PATH." >&2
fi
exit 1
fi
actual="$(terraform version -json | jq -r '.terraform_version')"
if [ "$actual" != "$expected" ]; then
printf '%s\n' "Terraform version $actual is active; this repository requires $expected." >&2
if command -v tfenv >/dev/null 2>&1; then
printf '%s\n' "Run: tfenv install $expected && tfenv use $expected" >&2
else
printf '%s\n' "Install Terraform $expected or install tfenv, then run: tfenv install $expected && tfenv use $expected" >&2
fi
exit 1
fi
preflight:tofu:
internal: true
cmds:
- |
#!/bin/bash
set -euo pipefail
expected="$(tr -d '[:space:]' < .opentofu-version)"
if command -v tofuenv >/dev/null 2>&1; then
if ! tofuenv install "$expected"; then
printf '%s\n' "WARNING: tofuenv could not install OpenTofu $expected; checking the OpenTofu binary already on PATH." >&2
elif ! tofuenv use "$expected"; then
printf '%s\n' "WARNING: tofuenv could not select OpenTofu $expected; checking the OpenTofu binary already on PATH." >&2
fi
fi
if ! command -v tofu >/dev/null 2>&1; then
printf '%s\n' "OpenTofu $expected is required but was not found on PATH." >&2
if command -v tofuenv >/dev/null 2>&1; then
printf '%s\n' "Install it with: tofuenv install $expected && tofuenv use $expected" >&2
else
printf '%s\n' "Install tofuenv, or install OpenTofu $expected and add it to PATH." >&2
fi
exit 1
fi
actual="$(tofu version -json | jq -r '.terraform_version')"
if [ "$actual" != "$expected" ]; then
printf '%s\n' "OpenTofu version $actual is active; this repository requires $expected." >&2
if command -v tofuenv >/dev/null 2>&1; then
printf '%s\n' "Run: tofuenv install $expected && tofuenv use $expected" >&2
else
printf '%s\n' "Install OpenTofu $expected or install tofuenv, then run: tofuenv install $expected && tofuenv use $expected" >&2
fi
exit 1
fi
fmt:
desc: Formats root and in-scope Terraform modules
deps: [preflight:terraform]
cmds:
- |
#!/bin/bash
terraform fmt
for dir in aws_static aws_rbac aws_oidc azure_static azure_oidc gcp_oidc stackguardian_connector_cloud stackguardian_connector_vcs stackguardian_role stackguardian_role_assignment stackguardian_workflow_group; do
terraform -chdir="$dir" fmt
done
check:
desc: Checks Terraform formatting
deps: [preflight:terraform]
cmds:
- |
#!/bin/bash
set -euo pipefail
terraform fmt -check
for dir in aws_static aws_rbac aws_oidc azure_static azure_oidc gcp_oidc stackguardian_connector_cloud stackguardian_connector_vcs stackguardian_role stackguardian_role_assignment stackguardian_workflow_group; do
terraform -chdir="$dir" fmt -check
done
test:
desc: Runs isolated OpenTofu tests discovered under tests directories
deps: [preflight:tofu]
cmds:
- |
#!/bin/bash
set -euo pipefail
root="$(pwd)"
temp="$(mktemp -d)"
trap 'rm -rf "$temp"' EXIT
export AWS_ACCESS_KEY_ID="test"
export AWS_SECRET_ACCESS_KEY="test"
export AWS_SESSION_TOKEN="test"
export AWS_EC2_METADATA_DISABLED="true"
export ARM_CLIENT_ID="00000000-0000-0000-0000-000000000000"
export ARM_CLIENT_SECRET="test"
export ARM_TENANT_ID="00000000-0000-0000-0000-000000000000"
export ARM_SUBSCRIPTION_ID="00000000-0000-0000-0000-000000000000"
export AZUREAD_CLIENT_ID="$ARM_CLIENT_ID"
export AZUREAD_CLIENT_SECRET="$ARM_CLIENT_SECRET"
export AZUREAD_TENANT_ID="$ARM_TENANT_ID"
while IFS= read -r config_dir; do
if [ "$config_dir" = "$root" ]; then
target="$temp/root"
else
target="$temp/${config_dir#"$root"/}"
fi
mkdir -p "$target"
tar --exclude='.git' --exclude='.terraform' --exclude='.terraform.lock.hcl' -cf - -C "$config_dir" . | tar -xf - -C "$target"
if [ "$config_dir" = "$root" ]; then
: > "$target/provider.tf"
printf '%s\n' \
'terraform {' \
' required_version = "= 1.5.7"' \
'' \
' required_providers {' \
' stackguardian = {' \
' source = "StackGuardian/stackguardian"' \
' version = ">= 1.12.0, < 2.0.0"' \
' }' \
' }' \
'}' \
'' \
'provider "stackguardian" {' \
' api_key = var.stackguardian_api_key' \
' org_name = var.stackguardian_org_name' \
' api_uri = var.stackguardian_api_uri' \
'}' >> "$target/provider.tf"
fi
while IFS= read -r provider_file; do
perl -0pi -e 's/required_version = "= 1\.5\.7"/required_version = ">= 1.6.0"/' "$provider_file"
perl -0pi -e 's/source\s*=\s*"StackGuardian\/stackguardian"/source = "registry.terraform.io\/StackGuardian\/stackguardian"/' "$provider_file"
done < <(find "$target" -name provider.tf -type f)
tofu -chdir="$target" init -backend=false
tofu -chdir="$target" test
done < <(find "$root" \( -name .git -o -name .terraform \) -type d -prune -o -path '*/tests/*.tofutest.hcl' -type f -print | while IFS= read -r test_file; do dirname "$(dirname "$test_file")"; done | sort -u)
onboard:aws:
desc: Creates AWS OIDC resources and registers the connector with StackGuardian
deps: [preflight:terraform]
vars:
ONBOARD_VARS_FILE: '{{default "examples/aws-oidc.tfvars.example" .ONBOARD_VARS_FILE}}'
ONBOARD_CONNECTOR_NAME: '{{default "aws-oidc" .ONBOARD_CONNECTOR_NAME}}'
ONBOARD_PLAN_FILE: '{{default "onboard-aws.tfplan" .ONBOARD_PLAN_FILE}}'
ONBOARD_CONFIRM: '{{default "" .ONBOARD_CONFIRM}}'
cmds:
- |
#!/bin/bash
set -euo pipefail
if ! command -v aws >/dev/null 2>&1; then
printf '%s\n' "AWS CLI is required. Install it and authenticate with: aws sso login --profile <profile>" >&2
exit 1
fi
if ! aws sts get-caller-identity >/dev/null; then
printf '%s\n' "No usable AWS CLI credentials were found. Authenticate first, for example: aws sso login --profile <profile>" >&2
exit 1
fi
if [ -z "${TF_VAR_stackguardian_api_key:-}" ]; then
read -r -s -p "StackGuardian API token: " stackguardian_api_key
printf '\n'
export TF_VAR_stackguardian_api_key="$stackguardian_api_key"
fi
connector_name="{{.ONBOARD_CONNECTOR_NAME}}"
plan_file="{{.ONBOARD_PLAN_FILE}}"
confirmation="{{.ONBOARD_CONFIRM}}"
printf '%s\n' "AWS onboarding will create or reuse the account-level StackGuardian OIDC provider, create the IAM role and policy attachment for $connector_name, and register the StackGuardian connector."
printf '%s\n' "Variables file: {{.ONBOARD_VARS_FILE}}"
printf '%s\n' "Plan file: $plan_file"
if [ "$confirmation" != "$connector_name" ]; then
read -r -p "Type $connector_name to continue: " confirmation
fi
if [ "$confirmation" != "$connector_name" ]; then
printf '%s\n' "Confirmation did not match; no resources were created." >&2
exit 1
fi
oidc_provider_arn="arn:aws:iam::$(aws sts get-caller-identity --query Account --output text):oidc-provider/api.app.stackguardian.io"
oidc_provider_address="module.aws_oidc[\"$connector_name\"].aws_iam_openid_connect_provider.oidc_provider"
terraform init
if aws iam get-open-id-connect-provider --open-id-connect-provider-arn "$oidc_provider_arn" >/dev/null 2>&1 && ! terraform state list | grep -Fqx "$oidc_provider_address"; then
terraform import -input=false -var-file="{{.ONBOARD_VARS_FILE}}" "$oidc_provider_address" "$oidc_provider_arn"
fi
terraform plan -input=false -var-file="{{.ONBOARD_VARS_FILE}}" -out="$plan_file"
terraform apply -input=false "$plan_file"
onboard:azure:
desc: Creates Azure OIDC resources and registers the connector with StackGuardian
deps: [preflight:terraform]
vars:
ONBOARD_VARS_FILE: '{{default "examples/azure-oidc.tfvars.example" .ONBOARD_VARS_FILE}}'
ONBOARD_CONNECTOR_NAME: '{{default "azure-oidc" .ONBOARD_CONNECTOR_NAME}}'
ONBOARD_PLAN_FILE: '{{default "onboard-azure.tfplan" .ONBOARD_PLAN_FILE}}'
ONBOARD_CONFIRM: '{{default "" .ONBOARD_CONFIRM}}'
cmds:
- |
#!/bin/bash
set -euo pipefail
if ! command -v az >/dev/null 2>&1; then
printf '%s\n' "Azure CLI is required. Install it and authenticate with: az login --tenant <tenant-id>" >&2
exit 1
fi
subscription_id="$(az account show --query id --output tsv 2>/dev/null || true)"
tenant_id="$(az account show --query tenantId --output tsv 2>/dev/null || true)"
if [ -z "$subscription_id" ] || [ -z "$tenant_id" ]; then
printf '%s\n' "No active Azure CLI subscription was found. Authenticate first with: az login --tenant <tenant-id>" >&2
exit 1
fi
if [ -z "${TF_VAR_stackguardian_api_key:-}" ]; then
read -r -s -p "StackGuardian API token: " stackguardian_api_key
printf '\n'
export TF_VAR_stackguardian_api_key="$stackguardian_api_key"
fi
connector_name="{{.ONBOARD_CONNECTOR_NAME}}"
plan_file="{{.ONBOARD_PLAN_FILE}}"
confirmation="{{.ONBOARD_CONFIRM}}"
printf '%s\n' "Azure onboarding will create the Entra application, service principal, federated credential, subscription role assignment, and StackGuardian connector for $connector_name."
printf '%s\n' "Variables file: {{.ONBOARD_VARS_FILE}}"
printf '%s\n' "Plan file: $plan_file"
if [ "$confirmation" != "$connector_name" ]; then
read -r -p "Type $connector_name to continue: " confirmation
fi
if [ "$confirmation" != "$connector_name" ]; then
printf '%s\n' "Confirmation did not match; no resources were created." >&2
exit 1
fi
terraform init
export ARM_SUBSCRIPTION_ID="$subscription_id"
export ARM_TENANT_ID="$tenant_id"
export AZUREAD_TENANT_ID="$tenant_id"
terraform plan -input=false -var-file="{{.ONBOARD_VARS_FILE}}" -out="$plan_file"
terraform apply -input=false "$plan_file"
destroy:aws:
desc: Destroys an AWS OIDC role and StackGuardian connector, retaining any shared OIDC provider
deps: [preflight:terraform]
vars:
ONBOARD_VARS_FILE: '{{default "examples/aws-oidc.tfvars.example" .ONBOARD_VARS_FILE}}'
ONBOARD_CONNECTOR_NAME: '{{default "aws-oidc" .ONBOARD_CONNECTOR_NAME}}'
ONBOARD_DESTROY_CONFIRM: '{{default "" .ONBOARD_DESTROY_CONFIRM}}'
ONBOARD_PLAN_FILE: '{{default "destroy-aws.tfplan" .ONBOARD_PLAN_FILE}}'
cmds:
- |
#!/bin/bash
set -euo pipefail
if ! command -v aws >/dev/null 2>&1 || ! aws sts get-caller-identity >/dev/null; then
printf '%s\n' "A logged-in AWS CLI is required. Authenticate first, for example: aws sso login --profile <profile>" >&2
exit 1
fi
if [ -z "${TF_VAR_stackguardian_api_key:-}" ]; then
read -r -s -p "StackGuardian API token: " stackguardian_api_key
printf '\n'
export TF_VAR_stackguardian_api_key="$stackguardian_api_key"
fi
connector_name="{{.ONBOARD_CONNECTOR_NAME}}"
confirmation="{{.ONBOARD_DESTROY_CONFIRM}}"
if [ "$confirmation" != "$connector_name" ]; then
read -r -p "Type $connector_name to destroy its AWS OIDC role and StackGuardian connector: " confirmation
fi
if [ "$confirmation" != "$connector_name" ]; then
printf '%s\n' "Confirmation did not match; no resources were destroyed." >&2
exit 1
fi
plan_file="{{.ONBOARD_PLAN_FILE}}"
terraform init
terraform plan -destroy -input=false -var-file="{{.ONBOARD_VARS_FILE}}" -out="$plan_file"
terraform apply -input=false "$plan_file"
destroy:azure:
desc: Destroys an Azure OIDC identity and StackGuardian connector
deps: [preflight:terraform]
vars:
ONBOARD_VARS_FILE: '{{default "examples/azure-oidc.tfvars.example" .ONBOARD_VARS_FILE}}'
ONBOARD_CONNECTOR_NAME: '{{default "azure-oidc" .ONBOARD_CONNECTOR_NAME}}'
ONBOARD_DESTROY_CONFIRM: '{{default "" .ONBOARD_DESTROY_CONFIRM}}'
ONBOARD_PLAN_FILE: '{{default "destroy-azure.tfplan" .ONBOARD_PLAN_FILE}}'
cmds:
- |
#!/bin/bash
set -euo pipefail
if ! command -v az >/dev/null 2>&1; then
printf '%s\n' "Azure CLI is required. Install it and authenticate with: az login --tenant <tenant-id>" >&2
exit 1
fi
subscription_id="$(az account show --query id --output tsv 2>/dev/null || true)"
tenant_id="$(az account show --query tenantId --output tsv 2>/dev/null || true)"
if [ -z "$subscription_id" ] || [ -z "$tenant_id" ]; then
printf '%s\n' "No active Azure CLI subscription was found. Authenticate first with: az login --tenant <tenant-id>" >&2
exit 1
fi
if [ -z "${TF_VAR_stackguardian_api_key:-}" ]; then
read -r -s -p "StackGuardian API token: " stackguardian_api_key
printf '\n'
export TF_VAR_stackguardian_api_key="$stackguardian_api_key"
fi
connector_name="{{.ONBOARD_CONNECTOR_NAME}}"
confirmation="{{.ONBOARD_DESTROY_CONFIRM}}"
if [ "$confirmation" != "$connector_name" ]; then
read -r -p "Type $connector_name to destroy its Azure OIDC identity and StackGuardian connector: " confirmation
fi
if [ "$confirmation" != "$connector_name" ]; then
printf '%s\n' "Confirmation did not match; no resources were destroyed." >&2
exit 1
fi
plan_file="{{.ONBOARD_PLAN_FILE}}"
terraform init
export ARM_SUBSCRIPTION_ID="$subscription_id"
export ARM_TENANT_ID="$tenant_id"
export AZUREAD_TENANT_ID="$tenant_id"
terraform plan -destroy -input=false -var-file="{{.ONBOARD_VARS_FILE}}" -out="$plan_file"
terraform apply -input=false "$plan_file"
validate:
desc: Initializes and validates isolated copies of in-scope configurations
deps: [preflight:terraform]
cmds:
- |
#!/bin/bash
set -euo pipefail
root="$(pwd)"
temp="$(mktemp -d)"
trap 'rm -rf "$temp"' EXIT
for dir in . aws_static aws_rbac aws_oidc azure_static azure_oidc gcp_oidc stackguardian_connector_cloud stackguardian_connector_vcs stackguardian_role stackguardian_role_assignment stackguardian_workflow_group; do
target="$temp/${dir#./}"
mkdir -p "$target"
if [ "$dir" = "." ]; then
tar --exclude='.git' --exclude='.terraform' --exclude='.terraform.lock.hcl' --exclude='terraform.tfvars' --exclude='*.auto.tfvars' --exclude='role_response.json' --exclude='stackguardian_private_runner' -cf - -C "$root" . | tar -xf - -C "$target"
else
tar --exclude='.terraform' --exclude='.terraform.lock.hcl' -cf - -C "$root" "$dir" | tar -xf - -C "$temp"
fi
terraform -chdir="$target" init -backend=false
terraform -chdir="$target" validate
done