-
Notifications
You must be signed in to change notification settings - Fork 19
171 lines (156 loc) · 5.91 KB
/
pipeline-hardened.yaml
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
name: Pipeline
on:
workflow_dispatch:
push:
branches:
- vuln-api-hardened
env:
PIPELINE_USER_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
PIPELINE_USER_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
SAM_TEMPLATE: books-api/template.yaml
STACK_NAME: books-api-hardened
PIPELINE_EXECUTION_ROLE: ${{ vars.PIPELINE_EXECUTION_ROLE }}
CLOUDFORMATION_EXECUTION_ROLE: ${{ vars.CLOUDFORMATION_EXECUTION_ROLE }}
ARTIFACTS_BUCKET: ${{ vars.ARTIFACTS_BUCKET }}
# change region to your preferred region
AWS_REGION: ${{ vars.AWS_REGION }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v2
with:
dotnet-version: '6.0.x'
- run: |
dotnet test books-api/test/Bookstore.Test/Bookstore.Tests.csproj
build-and-package:
needs: [test]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- uses: actions/setup-dotnet@v2
with:
dotnet-version: '6.0.x'
- uses: aws-actions/setup-sam@v2
- name: Build resources
run: sam build --template ${SAM_TEMPLATE}
- name: Assume the pipeline user role
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ env.PIPELINE_USER_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.PIPELINE_USER_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.PIPELINE_EXECUTION_ROLE }}
role-session-name: lambda-packaging
role-duration-seconds: 3600
role-skip-session-tagging: true
- name: Upload artifacts to artifact buckets
run: |
sam package \
--s3-bucket ${ARTIFACTS_BUCKET} \
--region ${AWS_REGION} \
--output-template-file packaged-lambda.yaml
- uses: actions/upload-artifact@v2
with:
name: packaged-lambda.yaml
path: packaged-lambda.yaml
lint-api:
name: linting-api
needs: [build-and-package]
runs-on: ubuntu-latest
steps:
# Check out the repository
- uses: actions/checkout@v2
- uses: actions/setup-node@v3
- run: |
npm install --save -D @stoplight/spectral-owasp-ruleset@1.4.3
npm install --save -D @stoplight/spectral-cli
# Run Spectral
- uses: stoplightio/spectral-action@latest
with:
file_glob: 'API-Definitions/*.yaml'
spectral_ruleset: 'owasp.spectral.yaml'
deploy:
needs: [lint-api]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- uses: actions/setup-dotnet@v2
with:
dotnet-version: '6.0.x'
- uses: aws-actions/setup-sam@v2
- uses: actions/download-artifact@v2
with:
name: packaged-lambda.yaml
- name: Assume the pipeline user role
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ env.PIPELINE_USER_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.PIPELINE_USER_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.PIPELINE_EXECUTION_ROLE }}
role-session-name: lambda-deployment
role-duration-seconds: 3600
role-skip-session-tagging: true
- name: Deploy to account
run: |
sam deploy --stack-name ${STACK_NAME} \
--template packaged-lambda.yaml \
--capabilities CAPABILITY_IAM \
--region ${AWS_REGION} \
--s3-bucket ${ARTIFACTS_BUCKET} \
--no-fail-on-empty-changeset \
--role-arn ${CLOUDFORMATION_EXECUTION_ROLE}
integration-test:
needs: [deploy]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v2
with:
dotnet-version: '6.0.x'
- name: Assume the pipeline user role
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ env.PIPELINE_USER_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.PIPELINE_USER_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.PIPELINE_EXECUTION_ROLE }}
role-session-name: testing-deployment
role-duration-seconds: 3600
role-skip-session-tagging: true
- run: |
dotnet test books-api/test/Bookstore.IntegrationTest/Bookstore.IntegrationTest.csproj
env:
STACK_NAME: ${{ env.STACK_NAME}}
AWS_REGION: ${{ env.AWS_REGION }}
output-endpoint:
needs: [integration-test]
runs-on: ubuntu-latest
steps:
- name: Assume the pipeline user role
uses: aws-actions/configure-aws-credentials@v1-node16
with:
aws-access-key-id: ${{ env.PIPELINE_USER_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ env.PIPELINE_USER_SECRET_ACCESS_KEY }}
aws-region: ${{ env.AWS_REGION }}
role-to-assume: ${{ env.PIPELINE_EXECUTION_ROLE }}
role-session-name: get-cloud-formation-output
role-duration-seconds: 3600
role-skip-session-tagging: true
- run: |
echo "AWS_API_ENDPOINT=$( aws cloudformation \
--region ${AWS_REGION} describe-stacks \
--stack-name ${STACK_NAME} \
--query "Stacks[0].Outputs[0].OutputValue" --output text)" >> $GITHUB_OUTPUT
id: get-outputs
- name: Write summary
run: |
echo "AWS_API_Gateway_Endpoint: ${{steps.get-outputs.outputs.AWS_API_ENDPOINT}}" >> $GITHUB_STEP_SUMMARY