ローカル開発環境で Azure SDK for Python を利用するための認証設定
Azure SDK for Python を利用するためにサービスプリンシパルを登録する手順を解説します。
サービスプリンシパルを作成する
アプリの新しいサービス プリンシパルを作成します。
az ad sp create-for-rbac^
--name service-principal_app01
下記のような出 力が得られるので、コピーして保存します。
{
"appId": "00000000-0000-0000-0000-000000000000",
"displayName": "{service-principal-name}",
"password": "abcdefghijklmnopqrstuvwxyz",
"tenant": "11111111-1111-1111-1111-111111111111"
}
グループを作成し、サービスプリンシパルをメンバー に入れる
グループの作成
az ad group create^
--display-name SampleGroup^
--mail-nickname SampleGroup
下記のような出力が得られるので、"id": "0000-0000-0000-0000-0000"
の箇所がグループIDなのでメモしておきます。
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups/$entity",
"classification": null,
"createdDateTime": "2023-11-24T01:41:20Z",
"creationOptions": [],
"deletedDateTime": null,
"description": null,
"displayName": "SampleGroup",
"expirationDateTime": null,
"groupTypes": [],
"id": "0000-0000-0000-0000-0000",
"isAssignableToRole": null,
"mail": null,
"mailEnabled": false,
"mailNickname": "SampleGroup",
"membershipRule": null,
・・・・
}
サービスプリンシパルのIDを取得
az ad sp list^
--filter "startswith(displayName, 'service-principal_app01')"^
--output table
下記のような出力が得られるので、Id
の位置に出力されたサービスプリンシパルのIDをメモします。
DisplayName Id AppId CreatedDateTime
----------------------- ------------------------------------ ------------------------------------ --------------------
service-principal_app01 00000000-0000-0000-0000-000000000000 ***********-******-******-****** 2023-11-24T01:50:07Z
グループメンバーに追加する
az ad group member add^
--group SampleGroup^
--member-id "00000000-0000-0000-0000-000000000000"
グループにロールを割り当てる
ロール一覧を取得します。
az role definition list --output table
下記のような出力が得られます。
Name Type Description
---------------------------------------------------------- --------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
AcrPush Microsoft.Authorization/roleDefinitions acr push
API Management Service Contributor Microsoft.Authorization/roleDefinitions Can manage service and the APIs
AcrPull Microsoft.Authorization/roleDefinitions acr pull
・・・・
Owner Microsoft.Authorization/roleDefinitions Grants full access to manage all resources, including the ability to assign roles in Azure RBAC.
Reader Microsoft.Authorization/roleDefinitions View all resources, but does not allow you to make any changes.
グループに対してリソースグループのフルアクセス権限を割り当てます。
assigneeには上記でメモしたグループIDを設定します。
az role assignment create^
--assignee "00000000-0000-0000-0000-000000000000"^
--role "Owner"^
--resource-group "resource_group_for_python"
グループに対してリソースへのアクセス権限を割り当てます。(下記はVM Imageに権限を割り当てる例)
assigneeには上記でメモしたグループIDを設定します。
az role assignment create^
--assignee "00000000-0000-0000-0000-000000000000"^
--role "Owner"^
--scope "/subscriptions/*****-*****-*****-*****/resourceGroups/resource_group_for_python/providers/Microsoft.Compute/galleries/shared_image_gallery_01/images/vm-image_01"
ロールの割り当て状況を確認する
az role assignment list^
--scope "/subscriptions/*****-*****-*****-*****/resourceGroups/resource_group_for_python/providers/Microsoft.Compute/galleries/shared_image_gallery_01/images/vm-image_01"
az role assignment list^
--all^
--assignee "00000000-0000-0000-0000-000000000000"
ローカル開発の環境変数を設定する
環境変数を設定します。
- AZURE_CLIENT_ID:上記で取得したappId
- AZURE_TENANT_ID:上記で取得したtenant
- AZURE_CLIENT_SECRET:上記で取得したpassword
👇例
AZURE_CLIENT_ID=00000000-0000-0000-0000-000000000000
AZURE_TENANT_ID=11111111-1111-1111-1111-111111111111
AZURE_CLIENT_SECRET=abcdefghijklmnopqrstuvwxyz
👇関連記事
- ローカル開発環境で Azure SDK for Python を利用する
- Azure Blob Storage CLI及びPython経由ファイルアップロード/ダウンロード操作手順
- Azure Functions でPython関数を作成してデプロイする
👇参考URL
- サービス プリンシパルを使用したローカル開発時に Azure サービスに対して Python アプリを認証する
- Azure CLI を使用して Azure ロールを割り当てる
- az role assignment
- az ad group
[keywords]
Azure SDK python 認証
ローカル開発環境でAzure SDK for Pythonを利用するための認証設定
更新日:2023/11/07