Skip to content

Commit 9b78b81

Browse files
authored
ci(test): replace the service principal with federated managed identity to access AKV (#213)
CI: - removed service principal. - added federated credential with user assigned managed identity. - added `actions/github-script@v6` action to get federated token file for container test cases as `az` CLI is not available in container. - the E2E test uses `E2E` Github environment, which needs manually approval from maintainers. This can improve the security. --------- Signed-off-by: Junjie Gao <[email protected]>
1 parent 501e0e6 commit 9b78b81

File tree

2 files changed

+51
-28
lines changed

2 files changed

+51
-28
lines changed

.github/workflows/test.yml

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ on:
1212
- main
1313
- release-*
1414

15+
permissions:
16+
id-token: write # Require write permission to Fetch an OIDC token.
17+
contents: read
18+
1519
jobs:
1620
lint:
1721
name: Lint Code Base
@@ -36,6 +40,7 @@ jobs:
3640
VALIDATE_CHECKOV: false
3741
VALIDATE_MARKDOWN: false
3842
VALIDATE_JSCPD: false
43+
VALIDATE_SHELL_SHFMT: false
3944
test:
4045
name: Unit Testing and Build
4146
runs-on: ubuntu-latest
@@ -62,7 +67,6 @@ jobs:
6267
# the binary will be used in E2E test
6368
python3 ./scripts/build.py v0.0.1 linux-x64 --enable-aot
6469
- name: Upload Linux artifact
65-
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
6670
uses: actions/upload-artifact@v4
6771
with:
6872
name: linux-amd64-binary
@@ -73,7 +77,6 @@ jobs:
7377
# the binary will be used in E2E test
7478
python3 ./scripts/build.py v0.0.1 osx-x64
7579
- name: Upload macOS artifact
76-
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
7780
uses: actions/upload-artifact@v4
7881
with:
7982
name: darwin-amd64-binary
@@ -82,8 +85,8 @@ jobs:
8285
e2e-mariner-container:
8386
name: E2E testing for Mariner container
8487
runs-on: ubuntu-latest
88+
environment: E2E
8589
needs: test
86-
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
8790
steps:
8891
- name: Check out code into the project directory
8992
uses: actions/checkout@v4
@@ -101,15 +104,33 @@ jobs:
101104
docker push localhost:5000/hello-world:v1
102105
- name: Build notation-akv:v1 image
103106
run: docker build -t notation-akv:v1 -f ./test/e2e/containerized/Dockerfile.mariner .
107+
- name: Install OIDC Client from Core Package
108+
run: npm install @actions/[email protected] @actions/http-client
109+
- name: Write Id Token
110+
uses: actions/github-script@v6
111+
id: idtoken
112+
with:
113+
script: |
114+
try {
115+
const core = require('@actions/core')
116+
const fs = require('fs')
117+
let id_token = await core.getIDToken('api://AzureADTokenExchange')
118+
fs.writeFileSync("./federated_token", id_token);
119+
console.log(`Federated Token written to ./federated_token`);
120+
} catch (error) {
121+
core.setFailed(`Action failed with error: ${error.message}`);
122+
}
104123
- name: Run e2e
105124
run: bash ./test/e2e/containerized/test.sh
106125
env:
107-
AZURE_CREDENTIALS: ${{ secrets.AZURE_CREDENTIALS }}
126+
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
127+
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
128+
AZURE_FEDERATED_TOKEN_FILE: ./federated_token
108129
e2e-linux:
109130
name: E2E testing on Linux
110131
runs-on: ubuntu-latest
132+
environment: E2E
111133
needs: test
112-
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
113134
steps:
114135
- name: Check out code into the project directory
115136
uses: actions/checkout@v4
@@ -137,7 +158,9 @@ jobs:
137158
- name: Azure login
138159
uses: azure/login@v2
139160
with:
140-
creds: ${{ secrets.AZURE_CREDENTIALS }}
161+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
162+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
163+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
141164
- name: E2E testing
142165
uses: ./test/e2e
143166
with:
@@ -146,8 +169,8 @@ jobs:
146169
e2e-windows:
147170
name: E2E testing on Windows
148171
runs-on: windows-latest
172+
environment: E2E
149173
needs: test
150-
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
151174
steps:
152175
- name: Check out code into the project directory
153176
uses: actions/checkout@v4
@@ -180,7 +203,9 @@ jobs:
180203
- name: Azure login
181204
uses: azure/login@v2
182205
with:
183-
creds: ${{ secrets.AZURE_CREDENTIALS }}
206+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
207+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
208+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
184209
- name: E2E testing
185210
uses: ./test/e2e
186211
with:
@@ -189,8 +214,8 @@ jobs:
189214
e2e-macos:
190215
name: E2E testing on macOS
191216
runs-on: macos-13
217+
environment: E2E
192218
needs: test
193-
if: github.event.pull_request.head.repo.full_name == github.repository || github.event_name == 'push'
194219
steps:
195220
- name: Check out code into the project directory
196221
uses: actions/checkout@v4
@@ -223,7 +248,9 @@ jobs:
223248
- name: Azure login
224249
uses: azure/login@v2
225250
with:
226-
creds: ${{ secrets.AZURE_CREDENTIALS }}
251+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
252+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
253+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
227254
- name: E2E testing
228255
uses: ./test/e2e
229256
with:

test/e2e/containerized/test.sh

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,41 @@
11
#!/bin/bash
22
#
33
# containerized e2e test for azure-kv plugin
4-
# prerequisite:
4+
# prerequisite:
55
# - notation-akv:v1 image
6-
# - AZURE_CREDENTIALS environment variable
6+
# - Environment variables:
7+
# - AZURE_CLIENT_ID
8+
# - AZURE_TENANT_ID
9+
# - AZURE_FEDERATED_TOKEN_FILE
710

811
set -e
12+
echo "which az"
13+
which az
914

10-
# setup credentials
11-
if [ -z "$AZURE_CREDENTIALS" ]; then
12-
echo "AZURE_CREDENTIALS is not set"
13-
exit 1
14-
fi
15-
16-
AZURE_TENANT_ID=$(echo "$AZURE_CREDENTIALS" | jq -r .tenantId)
17-
AZURE_CLIENT_ID=$(echo "$AZURE_CREDENTIALS" | jq -r .clientId)
18-
AZURE_CLIENT_SECRET=$(echo "$AZURE_CREDENTIALS" | jq -r .clientSecret)
19-
20-
function testSign(){
15+
function testSign() {
2116
# print all the arguments
2217
echo "notation sign --signature-format cose localhost:5000/hello-world:v1 --plugin azure-kv" "$@"
2318
docker run \
2419
-v "$(pwd)"/test/:/test \
25-
-e AZURE_CLIENT_SECRET="$AZURE_CLIENT_SECRET" \
2620
-e AZURE_CLIENT_ID="$AZURE_CLIENT_ID" \
2721
-e AZURE_TENANT_ID="$AZURE_TENANT_ID" \
22+
-e AZURE_FEDERATED_TOKEN_FILE=/root/federated-token \
23+
--mount type=bind,source="$AZURE_FEDERATED_TOKEN_FILE",target=/root/federated-token,readonly \
2824
--network host notation-akv:v1 \
2925
notation sign --signature-format cose localhost:5000/hello-world:v1 --plugin azure-kv "$@"
3026
local result=$?
3127
echo ""
3228
return $result
3329
}
3430

35-
function assertSucceeded(){
31+
function assertSucceeded() {
3632
if [ $? -ne 0 ]; then
3733
echo "test failed"
3834
exit 1
3935
fi
4036
}
4137

42-
function assertFailed(){
38+
function assertFailed() {
4339
if [ $? -eq 0 ]; then
4440
echo "test failed"
4541
exit 1
@@ -58,7 +54,7 @@ testSign --id https://acrci-test-kv.vault.azure.net/keys/imported-ca-issued-pem-
5854
assertSucceeded
5955
testSign --id https://acrci-test-kv.vault.azure.net/keys/imported-ca-issued-pkcs12/20548a2bcaba42308f609df2d79682b5
6056
assertSucceeded
61-
testSign --id https://acrci-test-kv.vault.azure.net/keys/imported-ca-issued-pkcs12-unordered/b4fdf86062e44839b666ce8ff3f3a470
57+
testSign --id https://acrci-test-kv.vault.azure.net/keys/imported-ca-issued-pkcs12-unordered/b4fdf86062e44839b666ce8ff3f3a470
6258
assertSucceeded
6359
testSign --id https://acrci-test-kv.vault.azure.net/keys/csr-ca-issued-pem-chain/09cd1aeaaa894e60b0ef83f062604863
6460
assertSucceeded
@@ -76,4 +72,4 @@ assertFailed
7672
testSign --id https://acrci-test-kv.vault.azure.net/keys/imported-ca-issued-pem/5a768b6209564c3cb30ecc30d800dc43 --plugin-config self_signed=true
7773
assertFailed
7874
testSign --id https://acrci-test-kv.vault.azure.net/keys/imported-ca-issued-pem/5a768b6209564c3cb30ecc30d800dc43 --plugin-config self_signed=true --plugin-config ca_certs=./test/e2e/certs/cert-bundle.pem
79-
assertFailed
75+
assertFailed

0 commit comments

Comments
 (0)