-
Notifications
You must be signed in to change notification settings - Fork 505
Update doc for service principal certificate authentication #4756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ ms.date: 09/02/2024 | |
ms.topic: concept-article | ||
ms.service: azure-cli | ||
ms.custom: devx-track-azurecli | ||
#customer intent: As an app developer, I need to security automate authentication to Azure using a service principal. | ||
#customer intent: As an app developer, I need to security automate authentication to Azure using a service principal. | ||
--- | ||
|
||
# Sign into Azure with a service principal using the Azure CLI | ||
# Sign into Azure with a service principal using the Azure CLI | ||
|
||
Service principals are accounts not tied to any particular user, which can have permissions on them assigned through | ||
predefined roles. Authenticating with a service principal is the best way to write secure scripts or programs, | ||
|
@@ -18,32 +18,40 @@ about service principals, see [Work with Azure service principals using the Azur | |
To sign in with a service principal, you need: | ||
|
||
* The URL or name associated with the service principal | ||
* The service principal password, or the X509 certificate used to create the service principal in PEM format | ||
* The tenant associated with the service principal, as either an `.onmicrosoft.com` domain or Azure object ID | ||
* The service principal client secret, or the X509 certificate used to create the service principal in PEM format | ||
* The tenant associated with the service principal, as either an `.onmicrosoft.com` domain or Microsoft Entra tenant ID | ||
|
||
Note two important facts when working with service principals and the Azure CLI: | ||
|
||
* A **CERTIFICATE** must be appended to the **PRIVATE KEY** within a PEM file. For an example of a PEM file format, see [Certificate-based authentication](./azure-cli-sp-tutorial-3.md). | ||
|
||
* If your service principal uses a certificate that is stored in Key Vault, that certificate's private key must be available without signing in to Azure. To retrieve the certificate for `az login`, see [Retrieve certificate from Key Vault](./azure-cli-sp-tutorial-3.md#work-with-azure-key-vault). | ||
|
||
Log in with client secret: | ||
|
||
```azurecli-interactive | ||
az login --service-principal --username APP_ID --password CLIENT_SECRET --tenant TENANT_ID | ||
``` | ||
|
||
Log in with certificate: | ||
|
||
```azurecli-interactive | ||
az login --service-principal -u <app-id> -p <password-or-cert> --tenant <tenant> | ||
az login --service-principal --username APP_ID --certificate /path/to/cert.pem --tenant TENANT_ID | ||
``` | ||
|
||
> [!IMPORTANT] | ||
> If you want to avoid displaying your password on console and are using `az login` interactively, | ||
> use the `read -s` command under `bash`. | ||
> | ||
> ```bash | ||
> read -sp "Azure password: " AZ_PASS && echo && az login --service-principal -u <app-id> -p $AZ_PASS --tenant <tenant> | ||
> read -sp "Azure password: " AZ_PASS && echo && az login --service-principal --username <app-id> --password $AZ_PASS --tenant <tenant> | ||
> ``` | ||
> | ||
> Under PowerShell, use the `Get-Credential` cmdlet. | ||
> | ||
> ```powershell | ||
> $AzCred = Get-Credential -UserName <app-id> | ||
> az login --service-principal -u $AzCred.UserName -p $AzCred.GetNetworkCredential().Password --tenant <tenant> | ||
> az login --service-principal --username $AzCred.UserName --password $AzCred.GetNetworkCredential().Password --tenant <tenant> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mikefrobbins any thoughts on using |
||
> ``` | ||
|
||
## See also | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ keywords: azure service principal, create service principal azure, create servic | |
|
||
# Use an Azure service principal with certificate-based authentication | ||
|
||
When creating a service principal, you choose the type of sign-in authentication it uses. There are two types of authentication available for Azure service principals: **password-based authentication** and **certificate-based authentication**. | ||
When creating a service principal, you choose the type of sign-in authentication it uses. There are two types of authentication available for Azure service principals: **password-based authentication** and **certificate-based authentication**. | ||
|
||
We recommend using certificate-based authentication due to the security restrictions of password-based authentication. Certificate-based authentication enables you to adopt a phishing resistant authentication by using [conditional access policies](/azure/active-directory/conditional-access/overview), which better protects Azure resources. To learn more about why certificate-based authentication is more secure, see [Microsoft Entra certificate-based authentication](/azure/active-directory/authentication/concept-certificate-based-authentication). | ||
|
||
|
@@ -102,8 +102,6 @@ az keyvault secret download --file /path/to/cert.pfx \ | |
--name CertName \ | ||
--encoding base64 | ||
openssl pkcs12 -in cert.pfx -passin pass: -passout pass: -out cert.pem -nodes | ||
|
||
az login --service-principal -u "<myAppClientID>" -p cert.pem --tenant "<myTenantID>" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to mention |
||
``` | ||
|
||
## Convert an existing PKCS12 file | ||
|
@@ -142,10 +140,7 @@ The output includes credentials that you must protect. Be sure that you do not i | |
To sign in with a certificate, the certificate must be available locally as a PEM or DER file in ASCII format. PKCS#12 files (.p12/.pfx) don't work. When you use a PEM file, the **PRIVATE KEY** and **CERTIFICATE** must be appended together within the file. You don't need to prefix the path with an `@` like you do with other az commands. | ||
|
||
```azurecli-interactive | ||
az login --service-principal \ | ||
--username myServicePrincipalID \ | ||
--tenant myOwnerOrganizationId \ | ||
--password /path/to/cert | ||
az login --service-principal --username APP_ID --certificate /path/to/cert.pem --tenant TENANT_ID | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a very long command, so no need to break it into several lines. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the placeholders are changed to align with those in |
||
``` | ||
|
||
## Next Steps | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Full argument names should be preferred.