Skip to content

Commit 9d2265d

Browse files
authored
Use AWS Secrets Manager to store secrets (#213)
Cloudformation template and CI actions adapted from CBMC viewer.
1 parent aa7aba0 commit 9d2265d

File tree

6 files changed

+191
-33
lines changed

6 files changed

+191
-33
lines changed

.github/cloudformation/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
These are
2+
[AWS CloudFormation templates](https://aws.amazon.com/cloudformation/resources/templates/)
3+
for maintaining pypi and homebrew credentials used to publish the CBMC starter kit.

.github/cloudformation/oidc.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Description:
2+
Register the GitHub identity provider with the AWS security token service.
3+
4+
Resources:
5+
GithubIdentityProvider:
6+
Type: AWS::IAM::OIDCProvider
7+
Properties:
8+
Url:
9+
# The GitHub identity provider supporting OIDC
10+
https://token.actions.githubusercontent.com
11+
ThumbprintList:
12+
# The GitHub certification authority (the signature of its certificate)
13+
- 6938fd4d98bab03faadb97b34396831e3780aea1
14+
ClientIdList:
15+
# The AWS security token service
16+
- sts.amazonaws.com
17+
18+
19+
Outputs:
20+
GithubIdentityProvider:
21+
Value: !Ref GithubIdentityProvider
22+
Export:
23+
Name: GithubIdentityProvider

.github/cloudformation/token.yaml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Description: >
2+
Enable storage of access tokens in AWS Secrets Manager and access to the PAT
3+
from the GitHub workflows in model-checking/cbmc-starter-kit.
4+
5+
Parameters:
6+
GithubRepoOrganization:
7+
Type: String
8+
Description: GitHub organization for the CBMC starter kit
9+
Default: model-checking
10+
CbmcStarterKitRepoName:
11+
Type: String
12+
Description: GitHub repository for CBMC starter kit
13+
Default: cbmc-starter-kit
14+
CbmcStarterKitPublicationTag:
15+
Type: String
16+
Description: GitHub tag for CBMC starter kit triggering the GitHub publication workflow
17+
Default: starterkit-*
18+
19+
Resources:
20+
21+
BrewBotEmail:
22+
Type: AWS::SecretsManager::Secret
23+
Properties:
24+
Name: BOT_EMAIL
25+
Description: >
26+
The email address to use with Homebrew commits.
27+
28+
BrewToken:
29+
Type: AWS::SecretsManager::Secret
30+
Properties:
31+
Name: RELEASE_CI_ACCESS_TOKEN
32+
Description: >
33+
GitHub access token.
34+
35+
PypiToken:
36+
Type: AWS::SecretsManager::Secret
37+
Properties:
38+
Name: PYPI_ACCESS_TOKEN
39+
Description: >
40+
Pypi access token.
41+
42+
PublisherTokenReader:
43+
Type: AWS::IAM::Role
44+
Properties:
45+
RoleName: PublisherTokenReader
46+
Description: >
47+
This role can retrieve the personal access token for the model
48+
checking publisher in the Microsoft Marketplace.
49+
50+
AssumeRolePolicyDocument:
51+
Version: "2012-10-17"
52+
Statement:
53+
- Effect: Allow
54+
Principal:
55+
Federated: !ImportValue GithubIdentityProvider
56+
Action: sts:AssumeRoleWithWebIdentity
57+
Condition:
58+
StringEquals:
59+
token.actions.githubusercontent.com:aud: sts.amazonaws.com
60+
StringLike:
61+
token.actions.githubusercontent.com:sub:
62+
!Sub repo:${GithubRepoOrganization}/${CbmcStarterKitRepoName}:ref:refs/tags/${CbmcStarterKitPublicationTag}
63+
64+
Policies:
65+
- PolicyName: PublisherTokenAccess
66+
PolicyDocument:
67+
Version: "2012-10-17"
68+
Statement:
69+
- Effect: Allow
70+
Action: secretsmanager:GetSecretValue
71+
Resource: !Ref BrewBotEmail
72+
- Effect: Allow
73+
Action: secretsmanager:GetSecretValue
74+
Resource: !Ref BrewToken
75+
- Effect: Allow
76+
Action: secretsmanager:GetSecretValue
77+
Resource: !Ref PypiToken

.github/workflows/release-brew.yaml

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,29 @@ env:
4646
FORMULA: cbmc-starter-kit
4747
TAP: aws/tap
4848
BOT_USER: cbmc-starter-kit-release-ci
49-
BOT_EMAIL: ${{ secrets.BOT_EMAIL }}
50-
BOT_TOKEN: ${{ secrets.RELEASE_CI_ACCESS_TOKEN }}
5149
RELEASE_TAG: ${GITHUB_REF/refs\/tags\/} # GITHUB_REF = refs/tags/STRING-MAJOR.MINOR
5250
VERSION: $(echo $GITHUB_REF | cut -d "/" -f 3 | cut -d "-" -f 2)
53-
FORK_REPO: https://[email protected]/$BOT_USER/homebrew-$(echo $TAP |cut -d "/" -f 2).git
51+
AWS_ROLE: arn:aws:iam::${{secrets.AWS_ACCOUNT}}:role/PublisherTokenReader
52+
AWS_REGION: us-west-2
5453

5554
jobs:
5655
homebrew-pr:
5756
name: Homebrew Bump Formula PR
5857
runs-on: macos-latest
58+
permissions:
59+
id-token: write
5960
steps:
61+
- name: Authenticate GitHub workflow to AWS
62+
uses: aws-actions/configure-aws-credentials@v4
63+
with:
64+
role-to-assume: ${{ env.AWS_ROLE }}
65+
aws-region: ${{ env.AWS_REGION }}
66+
67+
- name: Fetch secrets
68+
run: |
69+
echo "BOT_EMAIL=$(aws secretsmanager get-secret-value --secret-id BOT_EMAIL | jq -r '.SecretString')" >> $GITHUB_ENV
70+
echo "HOMEBREW_GITHUB_API_TOKEN=$(aws secretsmanager get-secret-value --secret-id RELEASE_CI_ACCESS_TOKEN | jq -r '.SecretString')" >> $GITHUB_ENV
71+
6072
- name: Configure git user name and email
6173
run: |
6274
git config --global user.name ${{ env.BOT_USER }}
@@ -66,21 +78,33 @@ jobs:
6678
run: |
6779
brew tap ${{ env.TAP }}
6880
brew update-reset
69-
brew bump-formula-pr --tag "${{ env.RELEASE_TAG }}" --revision "$GITHUB_SHA" ${{ env.TAP }}/${{ env.FORMULA }} --force
70-
env:
71-
HOMEBREW_GITHUB_API_TOKEN: ${{ env.BOT_TOKEN }}
81+
brew bump-formula-pr --tag "${{ env.RELEASE_TAG }}" --revision "${{ github.sha }}" ${{ env.TAP }}/${{ env.FORMULA }} --force
7282
7383
build-bottle:
7484
needs: homebrew-pr
7585
strategy:
7686
matrix:
7787
os: [ubuntu-latest, macos-latest]
7888
runs-on: ${{ matrix.os }}
89+
permissions:
90+
id-token: write
91+
contents: write
7992
steps:
8093
- name: Set up Homebrew
8194
id: set-up-homebrew
8295
uses: Homebrew/actions/setup-homebrew@master
8396

97+
- name: Authenticate GitHub workflow to AWS
98+
uses: aws-actions/configure-aws-credentials@v4
99+
with:
100+
role-to-assume: ${{ env.AWS_ROLE }}
101+
aws-region: ${{ env.AWS_REGION }}
102+
103+
- name: Fetch secrets
104+
run: |
105+
echo "FORK_REPO=https://$(aws secretsmanager get-secret-value --secret-id RELEASE_CI_ACCESS_TOKEN | jq -r '.SecretString')@github.com/${{ env.BOT_USER }}/homebrew-$(echo ${{ env.TAP }} |cut -d / -f 2).git" >> $GITHUB_ENV
106+
echo "GITHUB_TOKEN=$(aws secretsmanager get-secret-value --secret-id RELEASE_CI_ACCESS_TOKEN | jq -r '.SecretString')" >> $GITHUB_ENV
107+
84108
- name: Set up Python
85109
# The GitHub action actions/setup-python@v4 installs CPython 3.10
86110
run: |
@@ -102,43 +126,55 @@ jobs:
102126
103127
- name: Build bottle
104128
run: |
105-
brew test-bot --tap ${{ env.TAP }} --testing-formulae ${{ env.TAP }}/${{ env.FORMULA }} --only-formulae --root-url=https://github.com/$GITHUB_REPOSITORY/releases/download/${{ env.RELEASE_TAG }}
129+
brew test-bot --tap ${{ env.TAP }} --testing-formulae ${{ env.TAP }}/${{ env.FORMULA }} --only-formulae --root-url=https://github.com/${{ github.repository }}/releases/download/${{ env.RELEASE_TAG }}
106130
107131
- name: Get Package Path
108132
id: get_package_path
109133
run: |
110-
echo "bottle=$(ls *.tar.gz)" >> $GITHUB_ENV
134+
echo "bottle_name=$(ls *.tar.gz)" >> $GITHUB_OUTPUT
111135
112136
- name: Get File Name
113137
id: get_file_name
114138
run: |
115-
brew install jq
116-
echo "file_name=$(jq -r '."${{ env.TAP }}/${{ env.FORMULA }}".bottle.tags[].filename' *.json)" >> $GITHUB_ENV
139+
file_name="$(cat *.json | jq -r '."${{ env.TAP }}/${{ env.FORMULA }}".bottle.tags[].filename')"
140+
echo "file_name=$file_name" >> $GITHUB_OUTPUT
117141
118142
- name: Upload bottles as artifact
119-
uses: actions/upload-artifact@v3
143+
uses: actions/upload-artifact@v4
120144
with:
121-
name: bottles
145+
name: bottle-${{ matrix.os }}
122146
path: '*.bottle.*'
123147

124148
- name: Upload release binary
125149
# this github action is deprecated, but there is no obvious replacement
126150
uses: actions/[email protected]
127-
env:
128-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
129151
with:
130152
upload_url: ${{ github.event.release.upload_url }}
131-
asset_path: ${{ env.bottle }}
132-
asset_name: ${{ env.file_name }}
153+
asset_path: ${{ steps.get_package_path.outputs.bottle_name }}
154+
asset_name: ${{ steps.get_file_name.outputs.file_name }}
133155
asset_content_type: application/x-gzip
134156

135157
update-pr:
136158
needs: build-bottle
137159
runs-on: macos-latest
160+
permissions:
161+
id-token: write
162+
contents: write
138163
steps:
139-
- uses: actions/download-artifact@v3
164+
- uses: actions/download-artifact@v4
165+
with:
166+
pattern: bottle-*
167+
168+
- name: Authenticate GitHub workflow to AWS
169+
uses: aws-actions/configure-aws-credentials@v4
140170
with:
141-
name: bottles
171+
role-to-assume: ${{ env.AWS_ROLE }}
172+
aws-region: ${{ env.AWS_REGION }}
173+
174+
- name: Fetch secrets
175+
run: |
176+
echo "BOT_EMAIL=$(aws secretsmanager get-secret-value --secret-id BOT_EMAIL | jq -r '.SecretString')" >> $GITHUB_ENV
177+
echo "FORK_REPO=https://$(aws secretsmanager get-secret-value --secret-id RELEASE_CI_ACCESS_TOKEN | jq -r '.SecretString')@github.com/${{ env.BOT_USER }}/homebrew-$(echo ${{ env.TAP }} |cut -d / -f 2).git" >> $GITHUB_ENV
142178
143179
- name: Configure git user name and email
144180
run: |
@@ -156,6 +192,6 @@ jobs:
156192
157193
- name: Generate and merge bottle DSL
158194
run: |
159-
brew bottle --merge --write $(ls *.json)
195+
brew bottle --merge --write $(ls bottle-*/*.json)
160196
cd $(brew --repo ${{ env.TAP }})
161197
git push fork-repo bump-${{ env.FORMULA }}-${{ env.VERSION }}

.github/workflows/release-pypi.yaml

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,45 @@ on:
33
release:
44
types: [created]
55

6+
env:
7+
AWS_ROLE: arn:aws:iam::${{secrets.AWS_ACCOUNT}}:role/PublisherTokenReader
8+
AWS_REGION: us-west-2
9+
610
jobs:
711
upload-to-pypi:
812
name: Upload to PyPi
913
runs-on: ubuntu-20.04
14+
permissions:
15+
id-token: write
16+
contents: write
1017
steps:
1118
- uses: actions/checkout@v3
1219
- name: Install dependencies
1320
run: python3 -m pip install --upgrade pip build setuptools wheel twine
1421
- name: Build pip package
1522
run: python3 -m build
23+
- name: Authenticate GitHub workflow to AWS
24+
uses: aws-actions/configure-aws-credentials@v4
25+
with:
26+
role-to-assume: ${{ env.AWS_ROLE }}
27+
aws-region: ${{ env.AWS_REGION }}
28+
- name: Fetch secrets
29+
run: |
30+
echo "GITHUB_TOKEN=$(aws secretsmanager get-secret-value --secret-id RELEASE_CI_ACCESS_TOKEN | jq -r '.SecretString')" >> $GITHUB_ENV
31+
echo "TWINE_PASSWORD=$(aws secretsmanager get-secret-value --secret-id PYPI_ACCESS_TOKEN | jq -r '.SecretString')" >> $GITHUB_ENV
1632
- name: Get Package Name
1733
id: get_package_name
1834
run: |
19-
echo "package_name=$(ls dist/*.whl | cut -d "/" -f 2)" >> $GITHUB_ENV
35+
echo "package_name=$(ls dist/*.whl | cut -d / -f 2)" >> $GITHUB_OUTPUT
2036
- name: Upload release binary
2137
# this github action is deprecated, but there is no obvious replacement
2238
uses: actions/[email protected]
23-
env:
24-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2539
with:
2640
upload_url: ${{ github.event.release.upload_url }}
27-
asset_path: dist/${{ env.package_name }}
28-
asset_name: ${{ env.package_name }}
41+
asset_path: dist/${{ steps.get_package_name.outputs.package_name }}
42+
asset_name: ${{ steps.get_package_name.outputs.package_name }}
2943
asset_content_type: application/zip
3044
- name: Upload to PyPi
3145
env:
3246
TWINE_USERNAME: __token__
33-
TWINE_PASSWORD: ${{ secrets.PYPI_ACCESS_TOKEN }}
3447
run: python3 -m twine upload dist/*

.github/workflows/release.yaml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ on:
33
push:
44
tags:
55
- starterkit-*
6+
env:
7+
AWS_ROLE: arn:aws:iam::${{secrets.AWS_ACCOUNT}}:role/PublisherTokenReader
8+
AWS_REGION: us-west-2
69

710
jobs:
811
Release:
912
name: CBMC starter kit release
1013
runs-on: ubuntu-20.04
11-
env:
12-
GITHUB_TOKEN: ${{ secrets.RELEASE_CI_ACCESS_TOKEN }}
14+
permissions:
15+
id-token: write
16+
contents: write
1317
steps:
1418
- name: Checkout code
1519
uses: actions/checkout@v3
@@ -25,13 +29,15 @@ jobs:
2529
echo "Setup and source versions ${{env.SETUP_VERSION}} and ${{env.SOURCE_VERSION}} did not match tag version ${{env.TAG_VERSION}}"
2630
exit 1
2731
fi
32+
- name: Authenticate GitHub workflow to AWS
33+
uses: aws-actions/configure-aws-credentials@v4
34+
with:
35+
role-to-assume: ${{ env.AWS_ROLE }}
36+
aws-region: ${{ env.AWS_REGION }}
37+
- name: Fetch secrets
38+
run: |
39+
echo "GITHUB_TOKEN=$(aws secretsmanager get-secret-value --secret-id RELEASE_CI_ACCESS_TOKEN | jq -r '.SecretString')" >> $GITHUB_ENV
2840
- name: Create release
29-
# The GitHub action actions/create-release is deprecated:
30-
# https://github.com/actions/create-release
31-
# GitHub recommends ncipollo/release-action@v1, but that
32-
# action fails to trigger the release workflows for brew and
33-
# pypi, perhaps because it does not trigger the expected kind
34-
# of release event.
3541
uses: actions/create-release@v1
3642
with:
3743
tag_name: starterkit-${{ env.TAG_VERSION }}

0 commit comments

Comments
 (0)