|
| 1 | +# Guide for migrating to azure-identity from azure-common |
| 2 | + |
| 3 | +## JSON- and file-based authentication |
| 4 | + |
| 5 | +To encourage best security practices, `azure-identity` does not support JSON- and file-based authentication in the same |
| 6 | +way as `azure-common`. `azure-common` provided factory methods like [`get_client_from_json_dict`][client_from_json] and |
| 7 | +[`get_client_from_auth_file`][client_from_auth_file] that are no longer available in `azure-identity`. |
| 8 | + |
| 9 | +In `azure-common` you could provide credentials in a JSON dictionary, or from a JSON file: |
| 10 | +```python |
| 11 | +from azure.common.client_factory import get_client_from_json_dict, get_client_from_auth_file |
| 12 | +from azure.mgmt.keyvault import KeyVaultManagementClient |
| 13 | +# Provide credentials in JSON: |
| 14 | +json_dict = { |
| 15 | + "clientId": "...", |
| 16 | + "clientSecret": "...", |
| 17 | + "subscriptionId": "...", |
| 18 | + "tenantId": "...", |
| 19 | + "activeDirectoryEndpointUrl": "https://login.microsoftonline.com", |
| 20 | + "resourceManagerEndpointUrl": "https://management.azure.com" |
| 21 | +} |
| 22 | +client = get_client_from_json_dict(KeyVaultManagementClient, json_dict) |
| 23 | +# Or, provide credentials from a JSON file: |
| 24 | +client = get_client_from_auth_file(KeyVaultManagementClient, "credentials.json") |
| 25 | +``` |
| 26 | + |
| 27 | +If it's not possible to immediately migrate from file-based authentication, you can still use `azure-identity`. With a |
| 28 | +JSON file containing your credentials, you can use [`json.load`][json] to authenticate a service principal with a |
| 29 | +[`ClientSecretCredential`][client_secret_cred]: |
| 30 | +```python |
| 31 | +import json |
| 32 | +from azure.identity import ClientSecretCredential |
| 33 | +from azure.mgmt.keyvault import KeyVaultManagementClient |
| 34 | + |
| 35 | +with open("credentials.json") as json_file: |
| 36 | + json_dict = json.load(json_file) |
| 37 | + |
| 38 | +credential = ClientSecretCredential( |
| 39 | + tenant_id=json_dict["tenantId"], |
| 40 | + client_id=json_dict["clientId"], |
| 41 | + client_secret=json_dict["clientSecret"], |
| 42 | + authority=json_dict["activeDirectoryEndpointUrl"] |
| 43 | +) |
| 44 | +client = KeyVaultManagementClient( |
| 45 | + credential, |
| 46 | + json_dict["subscriptionId"], |
| 47 | + base_url=json_dict["resourceManagerEndpointUrl"], |
| 48 | + credential_scopes=["{}/.default".format(json_dict["resourceManagerEndpointUrl"])] |
| 49 | +) |
| 50 | +``` |
| 51 | + |
| 52 | +If storing credentials in a file, be sure to protect access to this file. Make certain that it's excluded by version |
| 53 | +control -- for example, by adding the credential file name to your project's `.gitignore` file. |
| 54 | + |
| 55 | +The global documentation for authenticating Python apps on Azure is available [here][authenticate_docs]. |
| 56 | + |
| 57 | +[authenticate_docs]: https://docs.microsoft.com/azure/developer/python/azure-sdk-authenticate?tabs=cmd |
| 58 | +[client_from_json]: https://docs.microsoft.com/python/api/azure-common/azure.common.client_factory?view=azure-python#get-client-from-json-dict-client-class--config-dict----kwargs- |
| 59 | +[client_from_auth_file]: https://docs.microsoft.com/python/api/azure-common/azure.common.client_factory?view=azure-python#get-client-from-auth-file-client-class--auth-path-none----kwargs- |
| 60 | +[client_secret_cred]: https://docs.microsoft.com/python/api/azure-identity/azure.identity.clientsecretcredential?view=azure-python |
| 61 | +[json]: https://docs.python.org/3/library/json.html#json.load |
0 commit comments