Azure SDK for Go - Authentication methods - Environmental credential
In the last part of this series, you learned how to use different credential types in a local development environment. Another method you can use within local development and in a hosted/deployed service is providing credentials through environment variables. This is done using the NewEnvironmentCredential()
method in the azidentity
package.
|
|
This credential type supports multiple types of authentication in the following order.
Service principal with client secret
In this method, you can export the tenant ID, client ID, and client secret of an Azure AD application as environment variables. These variable values get retrieved using NewEnvironmentCredential(),
and the client will be authenticated using these values.
To use this method, you need an Azure AD application with a client secret, a necessary role assignment for the user, and admin consent should be granted.
Once you complete the appropriate Azure AD application registration and configuration, you can set the environment variables for this authentication method.
|
|
You will receive an appropriate error message if these values are invalid. You will receive the following error if the application does not have the right permissions or role assignment.
|
|
Service principal with a certificate
To use a client certificate instead of a client secret, you can use the AZURE_CLIENT_CERTIFICATE_PATH
environment variable. If you set both the client secret and certificate variables, the client secret gets precedence.
|
|
The prerequisites for using this method are the same as the earlier one. Instead of creating a client secret, you must create a client certificate. If the certificate you created requires a password, you can specify that using the optionalAZURE_CLIENT_CERTIFICATE_PASSWORD
variable.
With username and password
This method is not recommended, but if you prefer username and password-based authentication, you can set the AZURE_CLIENT_ID, AZURE_USERNAME, and AZURE_PASSWORD environment variables.
|
|
To specify a tenant to authenticate, use the optional AZURE_TENANT_ID
environment variable. The authentication methods you saw in the previous article support optional options parameter to customize the behavior. With these options, you can specify additional tenants allowed for multitenant authentication. When using environment credentials, this can be specified using an optional environment variable called AZURE_ADDITIONALLY_ALLOWED_TENANTS
. You can specify a comma-separated list of tenant IDs as a value. If you specify ‘*’ as the value of this variable, you enable requesting tokens from any tenant.
If all three methods are set as environment variables, the client secret takes precedence over the others. The Go code for using any of these methods is the same as shown at the beginning of this article. The environment credential method can be used in development and production (hosted/deployed) service scenarios.
In the next part of this series, you will learn how to use managed identity credentials within your Go code.
Share on: