This guide describes how to set up your Azure DevOps (ADO) and Azure environment to leverage workflow identity federation, enabling you to use
AzureDevOpsSubscriptionProvider
provided in this section. See the README for more details.
Create a new service principal on which you will assign the necessary permissions. In this example, we use an app registration:
-
Navigate to the App Registrations page on the Azure portal
-
Click on
New Registration
-
Assign any name
-
Make sure to select the first option for the account type (
Accounts in this organization directory only (Microsoft only - Single tenant)
) -
Leave the Redirect URI and Service Tree ID fields empty
-
Click on
Register
Create a new ADO service connection under your organization's project. In this example, we create it under the DevDiv project:
-
Navigate to the organization's (DevDiv) ADO page
-
Navigate to the settings page by clicking on the gear icon on the bottom left
-
Select the "service connections" blade from the panel on the left
-
Create a new service connection by clicking on the
New service connection
button
- Select
Azure Resource Manager
as the type - Select
Workload Identity federation (manual)
for the authentication type - Provide a new name for your new service connection
- Click on
Next
- This will create a new draft service connection, with the
issuer
andsubject identifier
fields already filled in. - Leave this window open while you finish the next step, which will require those
issuer
andsubject identifier
fields, then you will return to this window to finish creating the service principal
Create a new "federated credential" on your service principal to connect it to your new service connection:
- Navigate back to the Azure Portal page for your service connection (app registration) from step 1
- Navigate to the
Certificates & secrets
blade - Navigate to the
Federated credentials
tab - Click on the
Add credential
button
-
For the scenario, select
Other issuer
-
For the
issuer
andsubject identifier
fields, fill in with the details of your draft service connection from the previous step -
Select a new name for your new federated credential
-
Click on
Add
This step is not required for running your tests, but is required to finish creating the service connection. This should be revoked after successful creation of the service connection and only necessary roles applied to the service principal.
-
On the Azure Portal, navigate to the page for the subscription you want the service principal to have access to.
-
Navigate to the
Access control (IAM)
blade -
Navigate to the
Roles
tab -
Click on the
+ Add
button, and chooseAdd role assignment
- Choose
Reader
and clickNext
- Choose
User, group, or service principal
, then click on+ Select members
- Select your service principal from step 1
- Click on
Review and assign
Finish creating the draft service connection you created in step 2.
- Navigate back to your draft service connection from step 2
- For Environment, select
Azure Cloud
- For Scope Level, choose
Subscription
- Under
Subscription Id
, andSubscription Name
, write the subscription ID and name (must provide both) for the desired subscription - For
Service Principal Id
, provide theApplication (client) ID
of your app registration from step 1 (can be found in theOverview
blade) - For the
Tenant ID
, provide theDirectory (tenant) ID
of your app registration from step 1 (can be found inOverview
blade) - Click on
Verify and save
Revoke the Reader
role on the subscription for the service connection after it is created. This is no longer necessary.
- Navigate to
Access control (IAM)
blade. - Under the
Role assignments
tab, find the role assignment corresponding to the App registered on step 1 - Click on
Remove
thenYes
- You can then assign the required roles to specific resources only if required, instead of assigning
Reader
role to the entire subscription.
A dummy Key vault step is required to propagate the necessary environment variables in the context of the pipeline.
-
Create a new Key Vault resource in the subscription you want to test on
-
Give it a new name as appropriate. You can keep the default settings
- Navigate to
Access control (IAM)
blade on your newly created dummy key vault
- Navigate to the
Roles
tab - Click on the
+ Add
button, and chooseAdd role assignment
- Choose
Key Vault Reader
(NOTReader
) and clickNext
- Choose
User, group, or service principal
, then click on+ Select members
- Select your app registration from step 1
- Click on
Review and assign
To ensure that the appropriate env variables are propagated in the context of running the pipeline, a dummy Key Vault step is required in that pipeline:
-
In the desired pipeline's
.yml
file, add a step as below. TheazureSubscription
field should correspond to the name of your service connection from step 2, while thekeyVaultName
field should correspond to the dummy key vault created in step 7:# This gives the TestServiceConnection service connection access to this pipeline. - task: AzureKeyVault@1 displayName: 'Authorize TestServiceConnection service connection' inputs: azureSubscription: 'TestServiceConnection' KeyVaultName: 'TestDummyKeyVault'
-
In the step which runs your code (e.g., the npm test step), make sure that the
$(System.AccessToken)
variable is manually propagated as aSYSTEM_ACCESSTOKEN
environment variable. All other required environment variables should be propagated automatically:- task: Npm@1 displayName: "Test" inputs: command: custom customCommand: test env: SYSTEM_ACCESSTOKEN: $(System.AccessToken) ```
The constructor for AzureDevOpsSubscriptionProvider
expects three arguments in an initializer object in order to identify your service connection you setup in step 5.
These are:
-
serviceConnectionId
: The resource ID of the service connection created in step 2, which can be found on theresourceId
field of the URL at the address bar, when viewing the service connection in the Azure DevOps portal -
domain
: TheTenant ID
field of the service connection properties, which can be accessed by clicking "Edit" on the service connection page -
clientId
: TheService Principal Id
field of the service connection properties, which can be accessed by clicking "Edit" on the service connection page
Make sure you pass an object containing these variables for the new AzureDevOpsServiceProvider()
constructor. These values are not secrets, so they can be set as environment variables, assigned as pipeline variables in ADO, accessed and assigned using an Azure Key Vault step, or even manually hardcoded in code (not recommended).