Skip to content

Commit 1aedeec

Browse files
committed
Incorporated OIDC for authenticating towards Azure in GH Action
1 parent 72dec1b commit 1aedeec

File tree

6 files changed

+34
-16
lines changed

6 files changed

+34
-16
lines changed

.github/workflows/deploy_to_azure.yml

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ jobs:
4848
environment:
4949
name: 'Production'
5050
url: ${{ steps.deploy-to-function.outputs.webapp-url }}
51+
permissions:
52+
id-token: write
53+
contents: read
5154
steps:
5255
- name: Download artifact from build job
5356
uses: actions/download-artifact@v4
@@ -57,10 +60,12 @@ jobs:
5760
- name: Unzip artifact for deployment
5861
run: unzip release.zip
5962

60-
- name: Login to Azure using Service Principal
61-
uses: azure/login@v1
63+
- name: Login to Azure with OIDC
64+
uses: azure/login@v2
6265
with:
63-
creds: ${{ secrets.AZURE_CREDENTIALS }}
66+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
67+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
68+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
6469

6570
- name: Deploy to Azure Functions
6671
uses: Azure/functions-action@v1
@@ -103,6 +108,9 @@ jobs:
103108
deploy-react:
104109
runs-on: ubuntu-latest
105110
needs: build-react
111+
permissions:
112+
id-token: write
113+
contents: read
106114
steps:
107115
- name: Download React build artifact
108116
uses: actions/download-artifact@v4
@@ -112,10 +120,12 @@ jobs:
112120
- name: Unzip React build
113121
run: unzip build.zip
114122

115-
- name: Login to Azure using service principal
116-
uses: azure/login@v1
123+
- name: Login to Azure with OIDC
124+
uses: azure/login@v2
117125
with:
118-
creds: ${{ secrets.AZURE_CREDENTIALS }}
126+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
127+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
128+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
119129

120130

121131
- name: Deploy React build to Azure Static Website

README.md

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ This [static web app](client/src/App.js), built with the React framework, enable
44
Once uploaded, a blob-triggered Azure Function processes the files to calculate correlations between various variables, such as experience, state, gender, and income. The computed statistics are then stored in a separate storage blob (**out**).
55
These functions are implemented in the python script [function_app.py](hvalfangst_function/function_app.py); which is the main entrypoint of our Azure Function App instance.
66

7-
A branch-triggered pipeline has been set up to deploy the function app and the static web app to Azure using a GitHub Actions Workflow [script](.github/workflows/deploy_to_azure.yml). A service principal has been created as part of the resource provisioning script, which is used
8-
to authenticate our requests in said pipeline script. It is therefore important to set the associated GitHub secrets in the repository settings.
7+
A branch-triggered pipeline has been set up to deploy the function app and the static web app to Azure using a GitHub Actions Workflow [script](.github/workflows/deploy_to_azure.yml).
8+
A service principal assigned to a federated credential has been created as part of the [resource provisioning script](infra/allocate_resources.sh), which is used to authenticate our requests in said pipeline script.
9+
It is therefore important to set the associated GitHub secrets in the repository settings (more on this in below).
910

1011

1112

@@ -43,10 +44,6 @@ graph TD
4344
```
4445

4546
## GitHub secrets
46-
As touched upon earlier, the GitHub secret **AZURE_CREDENTIALS** must be set in the repository settings. This secret comprises a JSON object containing the service principal credentials.
47-
It is generated by the Azure CLI command `az ad sp create-for-rbac`, which
48-
was executed as part of our [resource provisioning script](infra/allocate_resources.sh). The resulting terminal output of said command needs to be copy/pasted to the secret field.
49-
Again, it is used to authenticate our requests in the [GitHub Actions Workflow script](.github/workflows/deploy_to_azure.yml) as contributor access to the resource group is necessary
50-
in order to deploy our function and static web app. There are many ways to do this, but this is obviously a simple example.
51-
52-
![img_2.png](img_2.png)
47+
When inspecting the **Login to Azure with OIDC** step in our **deploy** stage associated with our [GitHub Actions Workflow script](.github/workflows/deploy_to_azure.yml), it is evident
48+
that three secrets are required. These are the **AZURE_CLIENT_ID**, **AZURE_TENANT_ID**, and **AZURE_SUBSCRIPTION_ID** and
49+
must be set in the repository settings.

img.png

-13.3 KB
Binary file not shown.

img_1.png

-53.6 KB
Binary file not shown.

img_2.png

-63.3 KB
Binary file not shown.

infra/allocate_resources.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,23 @@ fi
5959

6060
# Create service principal used by GitHub Actions, the returned JSON is stored as secret in the GitHub repository
6161
echo -e "${YELLOW}Creating service principal...${RESET}"
62-
az ad sp create-for-rbac --name hvalfangst-github-actions-sp --role contributor --scopes /subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP}
62+
SP_APP_ID=$(az ad sp create-for-rbac --name hvalfangst-github-actions-sp --role contributor --scopes /subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP} --query "appId" -o tsv)
6363
if [ $? -ne 0 ]; then
6464
echo -e "${RED}Failed to create service principal.${RESET}"
6565
exit 1
6666
fi
6767

68+
# Add federated credential to Azure AD application
69+
echo -e "${YELLOW}Adding federated credential to Azure AD application...${RESET}"
70+
az ad app federated-credential create --id ${SP_APP_ID} --parameters '{
71+
"name": "GitHubActionsFederatedCred",
72+
"issuer": "https://token.actions.githubusercontent.com",
73+
"subject": "repo:hvalfangst/azure-static-react-website-triggering-functions:ref:refs/heads/main",
74+
"audiences": [
75+
"api://AzureADTokenExchange"
76+
]
77+
}'
78+
6879
# Set up our storage container to serve static website with default index and 404 page
6980
echo -e "${YELLOW}Setting up static website...${RESET}"
7081
az storage blob service-properties update \

0 commit comments

Comments
 (0)