author | description | ms.author | ms.date | ms.service | ms.subservice | ms.topic | title | uid |
---|---|---|---|---|---|---|---|---|
anraman |
This document provides instructions on how to authenticate using a service principal. |
anraman |
02/01/2021 |
azure-quantum |
core |
how-to |
Using a service principal to authenticate |
microsoft.quantum.optimization.authenticate-service-principal |
Sometimes it is unsuitable to use interactive authentication or to authenticate as a user account. These cases may arise when you want to submit jobs from a web service, another worker role, or an automated system. In this case you typically want to authenticate using a Service Principal.
To authenticate as a service principal, you must first create a service principal.
To create a service principal, assign access, and generate a credential:
-
Create an Azure AAD application:
[!NOTE] You do not need to set a redirect URI
- Once created, write down the Application (client) ID and the Directory (tenant) ID.
-
Create a credential to login as the application:
- In the settings for your application, select Certificates & secrets.
- Under Client Secrets, select Create New Secret.
- Provide a description and duration, then select Add.
- Copy the value of the secret to a safe place immediately - you won't be able to see it again!
-
Give your service principal permissions to access your workspace:
- Open the Azure Portal.
- In the search bar, enter the name of the resource group you created your workspace in. Select the resource group when it comes up in the results.
- On the resource group overview, select Access control (IAM).
- Click Add Role Assignment.
- Search for and select the service principal.
- Assign either the Contributor or Owner role.
Step 1: Install the azure-common
python package:
pip3 install azure-common
Step 2: Before you call workspace.login()
, instantiate your service
principal and provide it to the workspace:
from azure.common.credentials import ServicePrincipalCredentials
workspace.credentials = ServicePrincipalCredentials(
tenant = "", # From service principal creation, your Directory (tenant) ID
client_id = "", # From service principal creation, your Application (client) ID
secret = "", # From service principal creation, your secret
resource = "https://quantum.microsoft.com" # Do not change! This is the resource you want to authenticate against - the Azure Quantum service
)
That's it! Make sure you call workspace.login()
after setting up the service
principal and you should be able to create jobs as usual.
Note
Calling workspace.login(refresh=True)
will clear the workspace.credentials property and force a new Interactive Device Authentication.
Whatever credentials were set in the workspace.credentials will be lost, including ServicePrincipalCredentials.