Skip to content

Commit 325ac67

Browse files
authored
Merge pull request #198716 from juliakm/users/jukullam/adding-oidc-bicep
Adding OIDC authentication method to Bicep article
2 parents bae4517 + 3ddd95e commit 325ac67

File tree

1 file changed

+110
-3
lines changed

1 file changed

+110
-3
lines changed

articles/azure-resource-manager/bicep/deploy-github-actions.md

Lines changed: 110 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: In this quickstart, you learn how to deploy Bicep files by using Gi
44
author: mumian
55
ms.author: jgao
66
ms.topic: conceptual
7-
ms.date: 11/16/2021
7+
ms.date: 05/19/2022
88
ms.custom: github-actions-azure
99
---
1010

@@ -32,7 +32,9 @@ az group create -n exampleRG -l westus
3232

3333
## Generate deployment credentials
3434

35-
Your GitHub Actions runs under an identity. Use the [az ad sp create-for-rbac](/cli/azure/ad/sp#az-ad-sp-create-for-rbac) command to create a [service principal](../../active-directory/develop/app-objects-and-service-principals.md#service-principal-object) for the identity.
35+
# [Service principal](#tab/userlevel)
36+
37+
Your GitHub Actions run under an identity. Use the [az ad sp create-for-rbac](/cli/azure/ad/sp#az-ad-sp-create-for-rbac) command to create a [service principal](../../active-directory/develop/app-objects-and-service-principals.md#service-principal-object) for the identity.
3638

3739
Replace the placeholder `myApp` with the name of your application. Replace `{subscription-id}` with your subscription ID.
3840

@@ -54,9 +56,57 @@ The output is a JSON object with the role assignment credentials that provide ac
5456
(...)
5557
}
5658
```
59+
# [Open ID Connect](#tab/openid)
60+
61+
62+
Open ID Connect is an authentication method that uses short-lived tokens. Setting up [OpenID Connect with GitHub Actions](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect) is more complex process that offers hardened security.
63+
64+
1. If you do not have an existing application, register a [new Active Directory application and service principal that can access resources](../../active-directory/develop/howto-create-service-principal-portal.md). Create the Active Directory application.
65+
66+
```azurecli-interactive
67+
az ad app create --display-name myApp
68+
```
69+
70+
This command will output JSON with an `appId` that is your `client-id`. Save the value to use as the `AZURE_CLIENT_ID` GitHub secret later.
71+
72+
You'll use the `objectId` value when creating federated credentials with Graph API and reference it as the `APPLICATION-OBJECT-ID`.
73+
74+
1. Create a service principal. Replace the `$appID` with the appId from your JSON output.
75+
76+
This command generates JSON output with a different `objectId` and will be used in the next step. The new `objectId` is the `assignee-object-id`.
77+
78+
Copy the `appOwnerTenantId` to use as a GitHub secret for `AZURE_TENANT_ID` later.
79+
80+
```azurecli-interactive
81+
az ad sp create --id $appId
82+
```
83+
84+
1. Create a new role assignment by subscription and object. By default, the role assignment will be tied to your default subscription. Replace `$subscriptionId` with your subscription ID, `$resourceGroupName` with your resource group name, and `$assigneeObjectId` with the generated `assignee-object-id`. Learn [how to manage Azure subscriptions with the Azure CLI](/cli/azure/manage-azure-subscriptions-azure-cli).
85+
86+
```azurecli-interactive
87+
az role assignment create --role contributor --subscription $subscriptionId --assignee-object-id $assigneeObjectId --assignee-principal-type ServicePrincipal --scopes /subscriptions/$subscriptionId/resourceGroups/$resourceGroupName/providers/Microsoft.Web/sites/
88+
```
5789
90+
1. Run the following command to [create a new federated identity credential](/graph/api/application-post-federatedidentitycredentials?view=graph-rest-beta&preserve-view=true) for your active directory application.
91+
92+
* Replace `APPLICATION-OBJECT-ID` with the **objectId (generated while creating app)** for your Active Directory application.
93+
* Set a value for `CREDENTIAL-NAME` to reference later.
94+
* Set the `subject`. The value of this is defined by GitHub depending on your workflow:
95+
* Jobs in your GitHub Actions environment: `repo:< Organization/Repository >:environment:< Name >`
96+
* For Jobs not tied to an environment, include the ref path for branch/tag based on the ref path used for triggering the workflow: `repo:< Organization/Repository >:ref:< ref path>`. For example, `repo:n-username/ node_express:ref:refs/heads/my-branch` or `repo:n-username/ node_express:ref:refs/tags/my-tag`.
97+
* For workflows triggered by a pull request event: `repo:< Organization/Repository >:pull_request`.
98+
99+
```azurecli
100+
az rest --method POST --uri 'https://graph.microsoft.com/beta/applications/<APPLICATION-OBJECT-ID>/federatedIdentityCredentials' --body '{"name":"<CREDENTIAL-NAME>","issuer":"https://token.actions.githubusercontent.com","subject":"repo:organization/repository:ref:refs/heads/main","description":"Testing","audiences":["api://AzureADTokenExchange"]}'
101+
```
102+
103+
To learn how to create a Create an active directory application, service principal, and federated credentials in Azure portal, see [Connect GitHub and Azure](/azure/developer/github/connect-from-azure#use-the-azure-login-action-with-openid-connect).
104+
105+
---
58106
## Configure the GitHub secrets
59107
108+
# [Service principal](#tab/userlevel)
109+
60110
Create secrets for your Azure credentials, resource group, and subscriptions.
61111
62112
1. In [GitHub](https://github.com/), navigate to your repository.
@@ -69,6 +119,26 @@ Create secrets for your Azure credentials, resource group, and subscriptions.
69119
70120
1. Create another secret named `AZURE_SUBSCRIPTION`. Add your subscription ID to the secret's value field (example: `90fd3f9d-4c61-432d-99ba-1273f236afa2`).
71121
122+
# [Open ID Connect](#tab/openid)
123+
124+
You need to provide your application's **Client ID**, **Tenant ID**, and **Subscription ID** to the login action. These values can either be provided directly in the workflow or can be stored in GitHub secrets and referenced in your workflow. Saving the values as GitHub secrets is the more secure option.
125+
126+
1. Open your GitHub repository and go to **Settings**.
127+
128+
1. Select **Settings > Secrets > New secret**.
129+
130+
1. Create secrets for `AZURE_CLIENT_ID`, `AZURE_TENANT_ID`, and `AZURE_SUBSCRIPTION_ID`. Use these values from your Active Directory application for your GitHub secrets:
131+
132+
|GitHub Secret | Active Directory Application |
133+
|---------|---------|
134+
|AZURE_CLIENT_ID | Application (client) ID |
135+
|AZURE_TENANT_ID | Directory (tenant) ID |
136+
|AZURE_SUBSCRIPTION_ID | Subscription ID |
137+
138+
1. Save each secret by selecting **Add secret**.
139+
140+
---
141+
72142
## Add a Bicep file
73143
74144
Add a Bicep file to your GitHub repository. The following Bicep file creates a storage account:
@@ -91,6 +161,8 @@ To create a workflow, take the following steps:
91161
1. Rename the workflow file if you prefer a different name other than **main.yml**. For example: **deployBicepFile.yml**.
92162
1. Replace the content of the yml file with the following code:
93163
164+
# [Service principal](#tab/userlevel)
165+
94166
```yml
95167
on: [push]
96168
name: Azure ARM
@@ -117,7 +189,7 @@ To create a workflow, take the following steps:
117189
parameters: storagePrefix=mystore
118190
failOnStdErr: false
119191
```
120-
192+
121193
Replace `mystore` with your own storage account name prefix.
122194
123195
> [!NOTE]
@@ -127,6 +199,41 @@ To create a workflow, take the following steps:
127199
128200
- **name**: The name of the workflow.
129201
- **on**: The name of the GitHub events that triggers the workflow. The workflow is triggered when there's a push event on the main branch.
202+
203+
# [OpenID Connect](#tab/openid)
204+
205+
```yml
206+
on: [push]
207+
name: Azure ARM
208+
jobs:
209+
build-and-deploy:
210+
runs-on: ubuntu-latest
211+
steps:
212+
213+
# Checkout code
214+
- uses: actions/checkout@main
215+
216+
# Log into Azure
217+
- uses: azure/login@v1
218+
with:
219+
client-id: ${{ secrets.AZURE_CLIENT_ID }}
220+
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
221+
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
222+
223+
# Deploy Bicep file
224+
- name: deploy
225+
uses: azure/arm-deploy@v1
226+
with:
227+
subscriptionId: ${{ secrets.AZURE_SUBSCRIPTION }}
228+
resourceGroupName: ${{ secrets.AZURE_RG }}
229+
template: ./main.bicep
230+
parameters: storagePrefix=mystore
231+
failOnStdErr: false
232+
```
233+
234+
---
235+
236+
130237
131238
1. Select **Start commit**.
132239
1. Select **Commit directly to the main branch**.

0 commit comments

Comments
 (0)