Skip to content

Commit 98fca6f

Browse files
authored
Fix running aws cli on GH Actions/Azure, get modules dynamically (#309)
* Prevent aws cli using GH Actions Azure Metadata service * Get modules dynamically * Fix broken tests
1 parent e68091b commit 98fca6f

File tree

11 files changed

+123
-142
lines changed

11 files changed

+123
-142
lines changed

.github/workflows/ci.yml

+17-66
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1+
# To avoid https://github.com/aws/aws-cli/issues/5262
2+
env:
3+
AWS_EC2_METADATA_DISABLED: true
4+
15
jobs:
6+
get-modules:
7+
runs-on: ubuntu-20.04
8+
outputs:
9+
matrix: ${{steps.list_dirs.outputs.matrix}}
10+
# Modified from https://stackoverflow.com/a/67180216 to filter just directory names
11+
# and to filter out scripts dir and to disables bless-ca
12+
steps:
13+
- uses: actions/checkout@v2
14+
- id: list_dirs
15+
run: echo "::set-output name=matrix::$(ls -d */|sed -e 's/\///'|grep -v 'bless-ca\|scripts'|jq -cnR '[inputs | select(length>0)]')"
16+
217
check-mod:
318
name: check-mod
419
runs-on: ubuntu-latest
@@ -26,75 +41,11 @@ jobs:
2641
test:
2742
name: test ${{ matrix.module }}
2843
runs-on: ubuntu-latest
44+
needs: get-modules
2945
strategy:
3046
fail-fast: false
3147
matrix:
32-
module:
33-
- aws-acm-cert
34-
- aws-aurora
35-
- aws-aurora-mysql
36-
- aws-aurora-postgres
37-
- aws-cloudfront-domain-redirect
38-
- aws-cloudfront-logs-bucket
39-
- aws-cloudwatch-log-group
40-
- aws-cloudwatch-log-retention-manager
41-
- aws-default-vpc-security
42-
- aws-ecs-job
43-
- aws-ecs-job-fargate
44-
- aws-ecs-service
45-
- aws-ecs-service-fargate
46-
- aws-efs-volume
47-
- aws-iam-ecs-task-role
48-
- aws-iam-group-assume-role
49-
- aws-iam-group-console-login
50-
- aws-iam-instance-profile
51-
- aws-iam-password-policy
52-
- aws-iam-policy-cwlogs
53-
- aws-iam-role
54-
- aws-iam-role-bless
55-
- aws-iam-role-cloudfront-poweruser
56-
- aws-iam-role-crossacct
57-
- aws-iam-role-ec2-poweruser
58-
- aws-iam-role-ecs-poweruser
59-
- aws-iam-role-infraci
60-
- aws-iam-role-poweruser
61-
- aws-iam-role-readonly
62-
- aws-iam-role-route53domains-poweruser
63-
- aws-iam-role-security-audit
64-
- aws-iam-secrets-reader-policy
65-
- aws-lambda-edge-add-security-headers
66-
- aws-lambda-function
67-
- aws-param
68-
- aws-params-reader-policy
69-
- aws-params-secrets-setup
70-
- aws-params-writer
71-
- aws-redis-node
72-
- aws-redis-replication-group
73-
- aws-s3-private-bucket
74-
- aws-s3-public-bucket
75-
- aws-single-page-static-site
76-
- aws-sns-lambda
77-
- aws-ssm-params
78-
- aws-ssm-params-writer
79-
# - bless-ca/test
80-
- github-webhooks-to-s3
81-
- module-template
82-
- snowflake-account-grant-all
83-
- snowflake-database-grant-all
84-
- snowflake-external-table-grant-all
85-
- snowflake-file-format-grant-all
86-
- snowflake-function-grant-all
87-
- snowflake-integration-grant-all
88-
- snowflake-materialized-view-grant-all
89-
- snowflake-procedure-grant-all
90-
- snowflake-resource-monitor-grant-all
91-
- snowflake-schema-grant-all
92-
- snowflake-sequence-grant-all
93-
- snowflake-stage-grant-all
94-
- snowflake-stream-grant-all
95-
- snowflake-table-grant-all
96-
- snowflake-view-grant-all
97-
- snowflake-warehouse-grant-all
48+
module: ${{fromJson(needs.get-modules.outputs.matrix)}}
9849
steps:
9950
- uses: actions/checkout@v2
10051
- id: filter

aws-default-vpc-security/module_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ import (
1010
func TestAWSDefaultVPCSecurity(t *testing.T) {
1111
test := tftest.Test{
1212
Setup: func(t *testing.T) *terraform.Options {
13-
return tftest.Options(
14-
tftest.DefaultRegion,
15-
map[string]interface{}{},
16-
)
13+
// Not using tftest.Options because module does not take standard arguments
14+
return &terraform.Options{
15+
TerraformDir: ".",
16+
17+
EnvVars: map[string]string{
18+
"AWS_DEFAULT_REGION": tftest.DefaultRegion,
19+
},
20+
}
1721
},
1822

1923
Mode: tftest.Plan,

aws-iam-group-console-login/module_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ import (
1212
func TestAWSIAMGroupConsoleLogin(t *testing.T) {
1313
test := tftest.Test{
1414
Setup: func(t *testing.T) *terraform.Options {
15-
return tftest.Options(
16-
tftest.IAMRegion,
15+
// Not using tftest.Options because module does not take standard arguments
16+
return &terraform.Options{
17+
TerraformDir: ".",
1718

18-
map[string]interface{}{
19+
EnvVars: map[string]string{
20+
"AWS_DEFAULT_REGION": tftest.IAMRegion,
21+
},
22+
23+
Vars: map[string]interface{}{
1924
"group_name": random.UniqueId(),
2025
"iam_path": fmt.Sprintf("/%s/", random.UniqueId()),
2126
},
22-
)
27+
}
2328
},
2429
Validate: func(t *testing.T, options *terraform.Options) {},
2530
}

aws-iam-instance-profile/module_test.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@ import (
1111
func TestAWSIAMInstanceProfile(t *testing.T) {
1212
test := tftest.Test{
1313
Setup: func(t *testing.T) *terraform.Options {
14-
return tftest.Options(
15-
tftest.IAMRegion,
16-
map[string]interface{}{
14+
// Not using tftest.Options because module does not take standard arguments
15+
return &terraform.Options{
16+
TerraformDir: ".",
17+
18+
EnvVars: map[string]string{
19+
"AWS_DEFAULT_REGION": tftest.IAMRegion,
20+
},
21+
22+
Vars: map[string]interface{}{
1723
"name_prefix": random.UniqueId(),
1824
"iam_path": "/foo/",
1925
"role_description": random.UniqueId(),
2026
},
21-
)
27+
}
2228
},
2329
Validate: func(t *testing.T, options *terraform.Options) {},
2430
}

aws-iam-policy-cwlogs/module_test.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@ import (
66

77
"github.com/chanzuckerberg/go-misc/tftest"
88
"github.com/gruntwork-io/terratest/modules/random"
9+
"github.com/gruntwork-io/terratest/modules/terraform"
910
)
1011

1112
func TestAWSIAMPolicyCwlogs(t *testing.T) {
1213
roleName := tftest.CreateRole(t)
1314
defer tftest.DeleteRole(t, roleName) //nolint
1415

15-
terraformOptions := tftest.Options(
16-
tftest.IAMRegion,
17-
map[string]interface{}{
16+
terraformOptions := &terraform.Options{
17+
TerraformDir: ".",
18+
19+
EnvVars: map[string]string{
20+
"AWS_DEFAULT_REGION": tftest.IAMRegion,
21+
},
22+
Vars: map[string]interface{}{
1823
"role_name": roleName,
1924
"iam_path": fmt.Sprintf("/%s/", random.UniqueId()),
2025
},
21-
)
26+
}
2227

2328
defer tftest.Cleanup(t, terraformOptions)
2429

aws-iam-secrets-reader-policy/module_test.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package test
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/aws/aws-sdk-go/service/secretsmanager"
78
"github.com/chanzuckerberg/go-misc/tftest"
@@ -14,18 +15,22 @@ func TestDefaults(t *testing.T) {
1415
test := tftest.Test{
1516
Setup: func(t *testing.T) *terraform.Options {
1617
// vars are all encoded in the test terraform files
17-
opt := tftest.Options(
18-
tftest.DefaultRegion,
19-
map[string]interface{}{},
20-
)
21-
opt.TerraformDir = "./test"
22-
return opt
18+
return &terraform.Options{
19+
TerraformDir: "./test",
20+
21+
EnvVars: map[string]string{
22+
"AWS_DEFAULT_REGION": tftest.DefaultRegion,
23+
},
24+
}
2325
},
2426

2527
Validate: func(t *testing.T, options *terraform.Options) {
2628
r := require.New(t)
2729
secret := terraform.Output(t, options, "secret")
2830
notSecret := terraform.Output(t, options, "not_secret")
31+
// Need sleep to allow IAM time to catch up and recognize that
32+
// test user is allowed to assume our roles.
33+
time.Sleep(10 * time.Second)
2934

3035
{
3136
roleArn := terraform.Output(t, options, "role")

aws-iam-secrets-reader-policy/test/main.tf

+13-24
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,25 @@ resource "random_string" "not" {
1010

1111
data "aws_caller_identity" "cur" {}
1212

13+
data "aws_iam_policy_document" "assume" {
14+
statement {
15+
actions = ["sts:AssumeRole"]
16+
17+
principals {
18+
type = "AWS"
19+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.cur.account_id}:root"]
20+
}
21+
}
22+
}
23+
1324
resource "aws_iam_role" "role" {
1425
name = random_string.name.result
15-
assume_role_policy = <<JSON
16-
{
17-
"Version": "2012-10-17",
18-
"Statement": {
19-
"Effect": "Allow",
20-
"Principal": {
21-
"AWS": "arn:aws:iam::${data.aws_caller_identity.cur.account_id}:root"
22-
},
23-
"Action": "sts:AssumeRole"
24-
}
25-
}
26-
JSON
26+
assume_role_policy = data.aws_iam_policy_document.assume.json
2727
}
2828

2929
resource "aws_iam_role" "not" {
3030
name = random_string.not.result
31-
assume_role_policy = <<JSON
32-
{
33-
"Version": "2012-10-17",
34-
"Statement": {
35-
"Effect": "Allow",
36-
"Principal": {
37-
"AWS": "arn:aws:iam::${data.aws_caller_identity.cur.account_id}:root"
38-
},
39-
"Action": "sts:AssumeRole"
40-
}
41-
}
42-
JSON
31+
assume_role_policy = data.aws_iam_policy_document.assume.json
4332
}
4433

4534
resource "aws_secretsmanager_secret" "secret" {

aws-params-reader-policy/module_test.go

+6-11
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,15 @@ func TestAWSParamsSecretReaderPolicy(t *testing.T) {
1919

2020
log.Debug("SETUP ROLE")
2121

22-
setupTerraformOptions := &terraform.Options{
23-
TerraformDir: "../aws-iam-role-crossacct",
24-
25-
Vars: map[string]interface{}{
22+
setupTerraformOptions := tftest.Options(
23+
tftest.IAMRegion,
24+
map[string]interface{}{
2625
"role_name": random.UniqueId(),
2726
"iam_path": fmt.Sprintf("/%s/", random.UniqueId()),
2827
"source_account_id": curAcct,
2928
},
30-
EnvVars: map[string]string{
31-
"AWS_DEFAULT_REGION": tftest.IAMRegion,
32-
},
33-
}
29+
)
30+
setupTerraformOptions.TerraformDir = "../aws-iam-role-crossacct"
3431

3532
defer tftest.Cleanup(t, setupTerraformOptions)
3633

@@ -62,13 +59,11 @@ func TestAWSParamsSecretReaderPolicy(t *testing.T) {
6259
terraformOptions := tftest.Options(
6360
tftest.IAMRegion,
6461
map[string]interface{}{
65-
"project": random.UniqueId(),
66-
"env": random.UniqueId(),
67-
"service": random.UniqueId(),
6862
"role_name": roleName,
6963
"parameter_store_key_alias": keyAlias,
7064
},
7165
)
66+
delete(terraformOptions.Vars, "owner")
7267

7368
defer tftest.Cleanup(t, terraformOptions)
7469

aws-s3-account-public-access-block/module_test.go

+32-10
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ package test
33
import (
44
"testing"
55

6-
"github.com/chanzuckerberg/go-misc/tftest"
76
"github.com/gruntwork-io/terratest/modules/terraform"
87
"github.com/stretchr/testify/require"
98
)
109

1110
func TestAll(t *testing.T) {
1211
r := require.New(t)
1312

14-
terraformOptions := tftest.Options("us-east-1", map[string]interface{}{
15-
"restrict": "all",
16-
})
13+
terraformOptions := &terraform.Options{
14+
TerraformDir: ".",
15+
16+
EnvVars: map[string]string{
17+
"AWS_DEFAULT_REGION": "us-east-1",
18+
},
19+
20+
Vars: map[string]interface{}{
21+
"restrict": "all",
22+
},
23+
}
1724

1825
defer terraform.Destroy(t, terraformOptions)
1926
terraform.InitAndApply(t, terraformOptions)
@@ -32,10 +39,17 @@ func TestAll(t *testing.T) {
3239
func TestNone(t *testing.T) {
3340
r := require.New(t)
3441

35-
terraformOptions := tftest.Options("us-east-1", map[string]interface{}{
36-
"restrict": "none",
37-
})
42+
terraformOptions := &terraform.Options{
43+
TerraformDir: ".",
44+
45+
EnvVars: map[string]string{
46+
"AWS_DEFAULT_REGION": "us-east-1",
47+
},
3848

49+
Vars: map[string]interface{}{
50+
"restrict": "none",
51+
},
52+
}
3953
defer terraform.Destroy(t, terraformOptions)
4054
terraform.InitAndApply(t, terraformOptions)
4155

@@ -51,9 +65,17 @@ func TestNone(t *testing.T) {
5165
func TestNew(t *testing.T) {
5266
r := require.New(t)
5367

54-
terraformOptions := tftest.Options("us-east-1", map[string]interface{}{
55-
"restrict": "new",
56-
})
68+
terraformOptions := &terraform.Options{
69+
TerraformDir: ".",
70+
71+
EnvVars: map[string]string{
72+
"AWS_DEFAULT_REGION": "us-east-1",
73+
},
74+
75+
Vars: map[string]interface{}{
76+
"restrict": "new",
77+
},
78+
}
5779

5880
defer terraform.Destroy(t, terraformOptions)
5981
terraform.InitAndApply(t, terraformOptions)

aws-single-page-static-site/module_test.go

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ func TestAwsSinglePageStaticSite(t *testing.T) {
1111
t.Parallel()
1212

1313
test := tftest.Test{
14-
SkipDestroy: true,
1514
Setup: func(t *testing.T) *terraform.Options {
1615
subdomain := tftest.UniqueID()
1716
route53ZoneID := tftest.EnvVar(tftest.EnvRoute53ZoneID)

0 commit comments

Comments
 (0)