Skip to content

Commit 9c40a07

Browse files
William ChrispJoshArmi
authored andcommitted
Add checkov to github workflows
1 parent 260fdcf commit 9c40a07

15 files changed

+420
-765
lines changed

.github/workflows/aws_cicd.yaml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ jobs:
2020
uses: actions/checkout@v3
2121

2222
- name: Setup Terraform
23-
uses: hashicorp/setup-terraform@v1
23+
uses: hashicorp/setup-terraform@v2
2424
with:
2525
terraform_wrapper: false
2626

2727
- name: Configure dev AWS credentials
28-
uses: aws-actions/configure-aws-credentials@v1
28+
uses: aws-actions/configure-aws-credentials@v2
2929
with:
3030
role-to-assume: arn:aws:iam::099267815798:role/github_oidc
3131
aws-region: ap-southeast-2
@@ -42,16 +42,23 @@ jobs:
4242
with:
4343
node-version: 18
4444
- name: Install cdktf
45-
run: npm install --global cdktf-cli@latest
45+
run: npm install --global cdktf-cli@0.15.5
4646

4747
- name: Install pip packages
4848
run: make install-dependencies
4949

5050
- name: Perform unittest
5151
run: make unittest
5252

53-
- name: Synthesize terraform configuration template
54-
run: make aws-synth
53+
- name: Plan terraform configuration
54+
run: make aws-plan-all
55+
56+
- name: Checkov
57+
uses: bridgecrewio/checkov-action@master
58+
with:
59+
directory: infrastructure/aws/cdktf.out/stacks/
60+
framework: terraform_plan
61+
skip_check: CKV_AWS_116,CKV_AWS_117,CKV_AWS_272
5562

5663
- name: Dev deployment
5764
run: make aws-deploy-all INFRA_ARGS=--auto-approve
@@ -64,7 +71,7 @@ jobs:
6471

6572
- name: Configure prod AWS credentials
6673
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
67-
uses: aws-actions/configure-aws-credentials@v1
74+
uses: aws-actions/configure-aws-credentials@v2
6875
with:
6976
role-to-assume: arn:aws:iam::103417687554:role/github_oidc
7077
aws-region: ap-southeast-2

.github/workflows/gcp_cicd.yaml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
uses: actions/checkout@v3
2121

2222
- name: Setup Terraform
23-
uses: hashicorp/setup-terraform@v1
23+
uses: hashicorp/setup-terraform@v2
2424
with:
2525
terraform_wrapper: false
2626

@@ -43,13 +43,19 @@ jobs:
4343
with:
4444
node-version: 18
4545
- name: Install cdktf
46-
run: npm install --global cdktf-cli@latest
46+
run: npm install --global cdktf-cli@0.15.5
4747

4848
- name: Install pip packages
4949
run: make install-dependencies
5050

51-
- name: Synthesize terraform configuration template
52-
run: make gcp-synth
51+
- name: Plan terraform configuration template
52+
run: make gcp-plan-all
53+
54+
- name: Checkov
55+
uses: bridgecrewio/checkov-action@master
56+
with:
57+
directory: infrastructure/gcp/cdktf.out/stacks/
58+
framework: terraform_plan
5359

5460
- name: Deploy base infrastructure
5561
run: make gcp-deploy-base INFRA_ARGS=--auto-approve

Makefile

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,24 @@ aws-synth: aws-build-dependencies
6464
@echo "\n\n---AWS-SYNTH---\n"
6565
cd infrastructure/aws;cdktf synth
6666

67-
aws-plan-core:
67+
aws-plan-core:
6868
@echo "\n\n---AWS-PLAN-CORE---\n"
6969
cd infrastructure/aws;cdktf plan aws_core
7070

7171
aws-plan-grafana:
7272
@echo "\n\n---AWS-PLAN-GRAFANA---\n"
7373
cd infrastructure/aws;cdktf plan aws_grafana_dashboard
7474

75-
aws-plan-all: aws-plan-core aws-plan-grafana
75+
aws-plan-all: aws-build-dependencies aws-plan-core aws-plan-grafana aws-plan-convert
76+
77+
aws-plan-convert:
78+
@echo "\n\n---Converting AWS plans file to json---\n"
79+
cd infrastructure/aws/cdktf.out/stacks; \
80+
find . -type f -name 'plan' -exec dirname {} \; | while read file; do \
81+
cd "$$file"; \
82+
terraform show -json plan > plan.json; \
83+
cd -; \
84+
done
7685

7786
aws-deploy-core:
7887
@echo "\n\n---AWS-DEPLOY-CORE---\n"
@@ -125,7 +134,16 @@ gcp-plan-grafana:
125134
@echo "\n\n---GCP-PLAN-GRAFANA---\n"
126135
cd infrastructure/gcp;cdktf plan gcp_grafana
127136

128-
gcp-plan-all: gcp-plan-base gcp-plan-core #gcp-plan-grafana
137+
gcp-plan-all: gcp-build-dependencies gcp-plan-base gcp-plan-core gcp-plan-convert
138+
139+
gcp-plan-convert:
140+
@echo "\n\n---Converting GCP plans file to json---\n"
141+
cd infrastructure/gcp/cdktf.out/stacks; \
142+
find . -type f -name 'plan' -exec dirname {} \; | while read file; do \
143+
cd "$$file"; \
144+
terraform show -json plan > plan.json; \
145+
cd -; \
146+
done
129147

130148
gcp-deploy-base:
131149
@echo "\n\n---GCP-DEPLOY-BASE---\n"

Pipfile.lock

Lines changed: 84 additions & 710 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ The approach to scaling a landing zone on AWS is [elaborated here](https://aws.a
1414
- [Flight Controller Event Catalog](https://legendary-spoon-f82c1640.pages.github.io): `docs/`
1515
- [README.md](docs/README.md)
1616
- Flight Controller Infrastructure Terraform CDK Code - `infrastructure/`
17-
- [README.md](infrastructure/README.md)
17+
- [AWS](infrastructure/aws/README.md)
18+
- [GCP](infrastructure/gcp/README.md)
1819
- Flight Controller Custom Publisher Code - `publisher/`
1920
- [README.md](publisher/README.md)
2021
- Flight Controller Code - `src/`

infrastructure/aws/dynamo_db_component.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from constructs import Construct
22

3-
from cdktf_cdktf_provider_aws import dynamodb_table
3+
from cdktf_cdktf_provider_aws import (
4+
dynamodb_table,
5+
kms_key
6+
)
47

58

69
class DynamoDBcomponent(Construct):
@@ -9,12 +12,27 @@ def __init__(self, scope: Construct, id: str, name: str):
912
self.partition_key = "aggregate_id"
1013
self.sort_key = "event_id"
1114

15+
# KMS Key
16+
self.key = kms_key.KmsKey(
17+
self,
18+
"flight_controller_core_dynamodb_key",
19+
description="Flight Controller Core DynamoDB KMS Key",
20+
enable_key_rotation=True,
21+
)
22+
1223
# create dynamoDB table
1324
self.table = dynamodb_table.DynamodbTable(
1425
self,
1526
"table",
1627
name=name,
1728
billing_mode="PAY_PER_REQUEST",
29+
point_in_time_recovery={
30+
"enabled": True
31+
},
32+
server_side_encryption={
33+
"enabled": True,
34+
"kms_key_arn": self.key.arn
35+
},
1836
hash_key=self.partition_key,
1937
range_key=self.sort_key,
2038
attribute=[

infrastructure/aws/grafana_lambda_with_permissions_component.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
lambda_function,
1717
grafana_workspace,
1818
lambda_permission,
19+
kms_key
1920
)
2021

2122

@@ -26,6 +27,7 @@ def __init__(
2627
id: str,
2728
name: str,
2829
grafanaWorkspace: grafana_workspace.GrafanaWorkspace,
30+
dynamo_db_key: str
2931
):
3032
super().__init__(scope, id)
3133

@@ -37,6 +39,15 @@ def __init__(
3739
asset_hash=dirhash(Path.join(os.getcwd(), "api_key_rotation"), "md5"),
3840
)
3941

42+
# KMS Key
43+
44+
key = kms_key.KmsKey(
45+
self,
46+
"flight_controller_grafana_rotation_lambda_key",
47+
description="Flight Controller Grafana Rotation Lambda KMS Key",
48+
enable_key_rotation=True
49+
)
50+
4051
# CREATE roles
4152

4253
lambda_iam_role = iam_role.IamRole(
@@ -73,6 +84,30 @@ def __init__(
7384
}
7485
),
7586
),
87+
iam_role.IamRoleInlinePolicy(
88+
name="AllowKMSDecrypt",
89+
policy=json.dumps(
90+
{
91+
"Version": "2012-10-17",
92+
"Statement": [
93+
{
94+
"Action": [
95+
"kms:Decrypt",
96+
"kms:Encrypt",
97+
"kms:CreateGrant",
98+
],
99+
"Resource": dynamo_db_key,
100+
"Effect": "Allow",
101+
},
102+
{
103+
"Action": "kms:ListAliases",
104+
"Resource": "*",
105+
"Effect": "Allow",
106+
}
107+
]
108+
}
109+
),
110+
)
76111
],
77112
)
78113

@@ -97,6 +132,7 @@ def __init__(
97132
function_name=name,
98133
handler="main.lambda_handler",
99134
runtime="python3.9",
135+
kms_key_arn=key.arn,
100136
role=lambda_iam_role.arn,
101137
filename=asset.path,
102138
)

infrastructure/aws/lambda_with_permissions_component.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
lambda_function,
1616
dynamodb_table,
1717
timestreamwrite_table,
18+
kms_key
1819
)
1920

2021
from dirhash import dirhash
@@ -28,6 +29,7 @@ def __init__(
2829
name: str,
2930
dynamoDbTable: dynamodb_table.DynamodbTable,
3031
timestream_table: timestreamwrite_table,
32+
dynamo_db_key: str
3133
):
3234
super().__init__(scope, id)
3335

@@ -43,6 +45,15 @@ def __init__(
4345
asset_hash=dirhash(Path.join(os.getcwd(), "controller_core"), "md5"),
4446
)
4547

48+
# KMS Key
49+
50+
key = kms_key.KmsKey(
51+
self,
52+
"flight_controller_core_lambda_key",
53+
description="Flight Controller Core Lambda KMS Key",
54+
enable_key_rotation=True,
55+
)
56+
4657
# CREATE roles
4758

4859
lambda_iam_role = iam_role.IamRole(
@@ -99,6 +110,30 @@ def __init__(
99110
}
100111
),
101112
),
113+
iam_role.IamRoleInlinePolicy(
114+
name="AllowKMSDecrypt",
115+
policy=json.dumps(
116+
{
117+
"Version": "2012-10-17",
118+
"Statement": [
119+
{
120+
"Action": [
121+
"kms:Decrypt",
122+
"kms:Encrypt",
123+
"kms:CreateGrant",
124+
],
125+
"Resource": dynamo_db_key,
126+
"Effect": "Allow",
127+
},
128+
{
129+
"Action": "kms:ListAliases",
130+
"Resource": "*",
131+
"Effect": "Allow",
132+
}
133+
]
134+
}
135+
),
136+
)
102137
],
103138
)
104139

@@ -109,13 +144,14 @@ def __init__(
109144
role=lambda_iam_role.name,
110145
)
111146

112-
# # Create lambda function from asset
147+
# Create lambda function from asset
113148
self.lambda_func = lambda_function.LambdaFunction(
114149
self,
115150
"lambda_flight_control",
116151
function_name=name,
117152
handler="src/entrypoints/aws_lambda.lambda_handler",
118153
runtime="python3.9",
154+
kms_key_arn=key.arn,
119155
role=lambda_iam_role.arn,
120156
filename=asset.path,
121157
environment=lambda_function.LambdaFunctionEnvironment(

infrastructure/aws/main.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ def __init__(
4848
self.lambda_name,
4949
dynamoDBcomponent.table,
5050
timeStreamComponent.timestream_table,
51+
dynamoDBcomponent.key.arn
5152
)
5253
# create event bridge
5354
eventBridgeComponent = EventBridgeComponent(
@@ -61,9 +62,11 @@ def __init__(
6162
self.grafana_workspace_name,
6263
)
6364

65+
self.grafana_workspace_id = grafanaWorkspace.grafana_workspace.id
66+
6467
# Create lambda function to rotate Grafana key
6568
grafanaLambdaComponent = GrafanaLambdaComponent(
66-
self, "grafana_function", self.grafana_lambda_name, grafanaWorkspace.grafana_workspace,
69+
self, "grafana_function", self.grafana_lambda_name, grafanaWorkspace.grafana_workspace, dynamoDBcomponent.key.arn
6770
)
6871

6972
# Create rotation rules to trigger every 29 days
@@ -72,7 +75,7 @@ def __init__(
7275
)
7376

7477
class AwsGrafana(TerraformStack):
75-
def __init__(self, scope: Construct, id: str):
78+
def __init__(self, scope: Construct, id: str, workspace_id: str):
7679
super().__init__(scope, id)
7780

7881
AwsProvider(self, "AWS")
@@ -82,19 +85,13 @@ def __init__(self, scope: Construct, id: str):
8285
"api_key",
8386
secret_id="flight-controller/grafana-api-key", # Secret name stored in AWS Secrets Manager
8487
)
85-
86-
workspace_id = data_aws_secretsmanager_secret_version.DataAwsSecretsmanagerSecretVersion(
87-
self,
88-
"workspace_id",
89-
secret_id="flight-controller/grafana-workspace-id", # Secret name stored in AWS Secrets Manager
90-
)
9188

9289
GrafanaProvider(
9390
self,
9491
"Grafana",
9592
auth=api_key.secret_string,
9693
url="https://"
97-
+ workspace_id.secret_string
94+
+ workspace_id
9895
+ ".grafana-workspace.ap-southeast-2.amazonaws.com/",
9996
)
10097

@@ -109,7 +106,7 @@ def __init__(self, scope: Construct, id: str):
109106

110107
core_stack = AwsCore(app, "aws_core")
111108

112-
grafana_dashboard_stack = AwsGrafana(app, "aws_grafana_dashboard")
109+
grafana_dashboard_stack = AwsGrafana(app, "aws_grafana_dashboard", core_stack.grafana_workspace_id)
113110

114111
account_id = boto3.client("sts").get_caller_identity()["Account"]
115112

0 commit comments

Comments
 (0)