This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMakefile
198 lines (161 loc) · 6.22 KB
/
Makefile
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
NAME ?= cosign-ecs
KEY_ALIAS ?= ${NAME}-key
AWS_REGION ?= us-west-2
ACCOUNT_ID ?= $(shell aws sts get-caller-identity --query Account --output text)
EVENT ?= event.json
# These have the "signed" and "unsigned" tags, respectively, but we pin the digest.
IMAGE_URL_SIGNED ?= public.ecr.aws/d1r0p2a6/ecs-cosign-demo2@sha256:31267f66e1aeb1f4a301faa75a7222927fa5bf1382697667b32ff580f9c6bfac
IMAGE_URL_UNSIGNED ?= public.ecr.aws/d1r0p2a6/ecs-cosign-demo2@sha256:8648f155b13820cb73521d08ab0cc22a906735e5e932b0dc3e81a06008940e5c
AWS_DEFAULT_REGION = ${AWS_REGION}
STACK_NAME = ${NAME}-stack
SAM_TEMPLATE = template.yml
PACKAGED_TEMPLATE = packaged.yml
GO_SRCS := $(wildcard cosign-ecs-function/*.go)
export AWS_REGION AWS_DEFAULT_REGION
################################################################################
# Terraform
################################################################################
.PHONY: tf_clean tf_init tf_get tf_plan tf_apply tf_fmt tf_destroy tf_refresh
tf_clean:
cd terraform/ && \
rm -rf .terraform \
rm -rf plan.out
tf_init:
cd terraform/ && \
terraform init
tf_get:
cd terraform/ && \
terraform get
tf_plan:
cd terraform/ && \
terraform plan \
-var="name=${NAME}" \
-var="image_url_signed=${IMAGE_URL_SIGNED}" \
-var="image_url_unsigned=${IMAGE_URL_UNSIGNED}" \
-out=plan.out
tf_apply:
cd terraform/ && \
terraform apply \
-var="name=${NAME}" \
-var="image_url_signed=${IMAGE_URL_SIGNED}" \
-var="image_url_unsigned=${IMAGE_URL_UNSIGNED}" \
-auto-approve
tf_fmt:
cd terraform/ && \
terraform fmt
tf_destroy:
cd terraform/ && \
terraform destroy \
-var="name=${NAME}" \
-var="image_url_signed=${IMAGE_URL_SIGNED}" \
-var="image_url_unsigned=${IMAGE_URL_UNSIGNED}" \
-auto-approve
tf_refresh:
cd terraform/ && \
terraform refresh \
-var="name=${NAME}" \
-var="image_url_signed=${IMAGE_URL_SIGNED}" \
-var="image_url_unsigned=${IMAGE_URL_UNSIGNED}"
################################################################################
# SAM
################################################################################
.PHONY: sam_build sam_package sam_deploy sam_local sam_local_debug sam_delete
cosign-ecs-function/cosign-ecs-function: $(GO_SRCS) cosign-ecs-function/go.mod cosign-ecs-function/go.sum
cd ./cosign-ecs-function && go mod tidy && \
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o cosign-ecs-function .
go_build: cosign-ecs-function/cosign-ecs-function
# `sam build` will build the serverless binary itself
sam_build:
sam build --cached
sam_package: sam_build $(SAM_TEMPLATE)
sam package \
--template-file ${SAM_TEMPLATE} \
--output-template-file ${PACKAGED_TEMPLATE} \
--resolve-s3
sam_deploy: sam_package
KEY_PEM=$$(cat cosign.pub; echo .); var=${var%.}; \
sam deploy \
--template-file ${SAM_TEMPLATE} \
--resolve-s3 \
--capabilities CAPABILITY_IAM \
--stack-name ${STACK_NAME} \
--parameter-overrides \
"ParameterKey=KeyArn,ParameterValue=''" \
"ParameterKey=KeyPem,ParameterValue='$${KEY_PEM}'"
sam_local: sam_build
KEY_PEM=$$(cat cosign.pub; echo .); var=${var%.}; \
sam local invoke \
--event ${EVENT} \
--template ${SAM_TEMPLATE} \
--parameter-overrides \
"ParameterKey=KeyArn,ParameterValue=''" \
"ParameterKey=KeyPem,ParameterValue='$${KEY_PEM}'"
sam_local_debug: sam_build
KEY_PEM=$$(cat cosign.pub; echo .); var=${var%.}; \
sam local invoke \
--event ${EVENT} \
--template ${SAM_TEMPLATE} \
--parameter-overrides \
"ParameterKey=KeyArn,ParameterValue=''" \
"ParameterKey=KeyPem,ParameterValue='$${KEY_PEM}'" \
--debug
sam_delete:
sam delete \
--stack-name ${STACK_NAME} \
--region ${AWS_REGION} \
--no-prompts
# if --no-prompts, it ignores $AWS_REGION
################################################################################
# Test it out!
################################################################################
.PHONY: run_signed_task run_unsigned_task task_status
run_signed_task:
TASK_DEF_ARN=$$(cd terraform && terraform output -raw signed_task_arn) && \
CLUSTER_ARN=$$(cd terraform && terraform output -raw cluster_arn) && \
SUBNET_ID=$$(cd terraform && terraform output -raw subnet_id) && \
aws ecs run-task \
--task-definition $${TASK_DEF_ARN} \
--cluster $${CLUSTER_ARN} \
--network-configuration "awsvpcConfiguration={subnets=[$${SUBNET_ID}],assignPublicIp=ENABLED}" \
--launch-type FARGATE \
--no-cli-pager
run_unsigned_task:
TASK_DEF_ARN=$$(cd terraform && terraform output -raw unsigned_task_arn) && \
CLUSTER_ARN=$$(cd terraform && terraform output -raw cluster_arn) && \
SUBNET_ID=$$(cd terraform && terraform output -raw subnet_id) && \
aws ecs run-task \
--task-definition $${TASK_DEF_ARN} \
--cluster $${CLUSTER_ARN} \
--network-configuration "awsvpcConfiguration={subnets=[$${SUBNET_ID}],assignPublicIp=ENABLED}" \
--launch-type FARGATE \
--no-cli-pager
task_status:
CLUSTER_ARN=$$(cd terraform && terraform output -raw cluster_arn) && \
echo "STOPPED tasks" && \
aws ecs list-tasks --cluster $$CLUSTER_ARN --desired-status STOPPED && \
echo "RUNNING tasks" && \
aws ecs list-tasks --cluster $$CLUSTER_ARN --desired-status RUNNING
################################################################################
# Setup and cleanup
################################################################################
.PHONY: key_gen sign verify_signed verify_unsigned ecr_auth clean stop_tasks
key_gen:
cosign generate-key-pair --kms awskms:///alias/$(KEY_ALIAS)
sign: ecr_auth
cosign sign --key awskms:///alias/$(KEY_ALIAS) ${IMAGE_URL_SIGNED}
verify_signed: ecr_auth
cosign verify --key awskms:///alias/$(KEY_ALIAS) ${IMAGE_URL_SIGNED}
verify_unsigned: ecr_auth
cosign verify --key awskms:///alias/$(KEY_ALIAS) ${IMAGE_URL_UNSIGNED}
.SILENT: ecr_auth
ecr_auth:
REGISTRY_URL="$(ACCOUNT_ID).dkr.ecr.$(AWS_REGION).amazonaws.com"; \
aws ecr get-login-password | \
docker login --username AWS --password-stdin $$REGISTRY_URL
clean:
rm -f ./cosign-ecs-function/cosign-ecs-function ${PACKAGED_TEMPLATE}
stop_tasks:
CLUSTER_ARN=$$(cd terraform && terraform output -raw cluster_arn) && \
aws ecs list-tasks --cluster $$CLUSTER_ARN --desired-status RUNNING --output text --query taskArns | \
xargs --no-run-if-empty --max-args 1 \
aws ecs stop-task --no-cli-pager --cluster $$CLUSTER_ARN --task