2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ユーザー割り当てマネージドIDによるAzureリソースへのアクセス権限付与

Last updated at Posted at 2024-01-27

ユーザー割り当てマネージドIDを作成しAzureリソースへのアクセス権限を付与する手順を解説します。
作成したユーザー割り当てマネージドIDは、Azure VMAzure コンテナー インスタンスへ適用することができます。
これらの手順は関連記事で解説します。

ユーザー割り当てマネージドIDの作成

👇ユーザー割り当てマネージドIDを作成します。

az identity create^
 --resource-group sampleResourceGroup^
 --name user_managed_id_01

上記コマンドを発行すると下記の出力が得られます。
出力の中のid(ユーザー割り当てマネージドID)とprincipalId(プリンシパルID)を後で利用しますのでメモしておきます。

{
  "clientId": "00000-00000-00000-00000",
  "id": "/subscriptions/99999-99999-99999-99999/resourcegroups/sampleResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/user_managed_id_01",
  "location": "japaneast",
  "name": "user_managed_id_01",
  "principalId": "*****-*****-*****-*****-*****",
  "resourceGroup": "sampleResourceGroup",
    .....
}

グループの作成

👇Azureリソースへのアクセス権限付与のため、グループを作成します。

az ad group create^
 --display-name SampleRoleGroup^
 --mail-nickname SampleRoleGroup

下記出力が得られます。
出力の中のid(グループID)を後で利用しますのでメモしておきます。

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#groups/$entity",
  "classification": null,
  "createdDateTime": "2024-01-26T09:17:58Z",
  "creationOptions": [],
  "deletedDateTime": null,
  "description": null,
  "displayName": "SampleRoleGroup",
  "expirationDateTime": null,
  "groupTypes": [],
  "id": "00000000-0000-0000-0000-000000000000",
  "isAssignableToRole": null,
  "mail": null,
  .....

グループメンバーにユーザー割り当てマネージドIDを追加

👇上記手順でメモしておいたプリンシパルIDを使ってグループメンバーにユーザー割り当てマネージドIDを追加します。

az ad group member add^
 --group ResourceControlGroup^
 --member-id "*****-*****-*****-*****-*****"

グループへの権限付与

例1)
グループに対してリソースグループのフルアクセス権限を割り当てます。
assigneeには上記でメモしたグループIDを設定します。

az role assignment create^
 --assignee "00000000-0000-0000-0000-000000000000"^
 --role "Owner"^
 --resource-group "resource_group_sample"

例2)
グループに対してリソースへのアクセス権限を割り当てます。(下記は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"

ユーザー割り当てマネージドIDをVMに適用(CLI)

👇vm起動時に--assign-identityパラメータに上記ユーザー割り当てマネージドIDを指定することでユーザー割り当てマネージドIDに割り当てた権限をVMに付与することができます。
Azure VM起動については、関連記事参照下さい。
Azure CLI で Ubuntu のVMを起動/削除する

az vm create --location japaneast^
 --resource-group sampleResourceGroup^
 --name ubuntu-vm^
 --image Canonical:0001-com-ubuntu-server-jammy:22_04-lts-gen2:latest^
 --size Standard_B1ms^
 --public-ip-address public_ip_01^
 --public-ip-sku Standard^
 --data-disk-delete-option Delete^
 --os-disk-delete-option Delete^
 --nic-delete-option Delete^
 --nsg nsg_01^
 --vnet-name vnet_01^
 --priority Regular^
 --nsg-rule SSH^
 --admin-username admin01^
 --admin-password SampleVM!123^
 --assign-identity /subscriptions/99999-99999-99999-99999/resourcegroups/sampleResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/user_managed_id_01

ユーザー割り当てマネージドIDをAzure コンテナー インスタンスに適用(CLI)

👇Azure コンテナー インスタンス作成時に--assign-identityパラメータに上記ユーザー割り当てマネージドIDを指定することでユーザー割り当てマネージドIDに割り当てた権限をAzure コンテナー インスタンスに付与することができます。
Azure コンテナー インスタンスについては、関連記事参照下さい。
Azure コンテナー インスタンスでコンテナー レジストリ に登録したDockerイメージを起動する

az container create^
 --resource-group sampleResourceGroup^
 --name webservercontainer^
 --image containerregistory01.azurecr.io/webserver:latest^
 --assign-identity /subscriptions/99999-99999-99999-99999/resourcegroups/sampleResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/user_managed_id_01^
 --dns-name-label webserver^
 --ports 80^
 --registry-username containerregistory01^
 --registry-password **********

ユーザー割り当てマネージドIDを Azure SDK for Python から利用するための準備

以下内容は、ローカル開発環境でAzure SDK for Pythonを利用するための認証設定を前提とします。
ユーザー割り当てマネージドIDを Azure SDK for Python から利用するためにはユーザー割り当てマネージドID自体への権限付与が必要です。

az role assignment create^
 --assignee "00000000-0000-0000-0000-000000000000"^
 --role "Owner"^
 --scope "/subscriptions/*****-*****-*****-*****/resourcegroups/sampleResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/user_managed_id_01"

ユーザー割り当てマネージドIDをVMに適用(Azure SDK for Python)

user_assigned_identitiesのところで設定しています。

from azure.identity import DefaultAzureCredential
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient

from azure.mgmt.network.models import SecurityRule
from azure.mgmt.network.models import NetworkSecurityGroup

# credential object
credential = DefaultAzureCredential()

SUBSCRIPTION_ID = "****-****-****-****-****"
RESOURCE_GROUP_NAME = "vm_resource_group"
LOCATION = "japaneast"

VNET_NAME = "python-example-vnet"
SUBNET_NAME = "python-example-subnet"
IP_NAME = "python-example-ip"
IP_CONFIG_NAME = "python-example-ip-config"
NIC_NAME = "python-example-nic"

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.networkmanagementclient?view=azure-python
network_client = NetworkManagementClient(credential, SUBSCRIPTION_ID)

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.operations.virtualnetworksoperations?view=azure-python
poller = network_client.virtual_networks.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    VNET_NAME,
    {
        "location": LOCATION,
        "address_space": {"address_prefixes": ["10.0.0.0/16"]},
    },
)
vnet_result = poller.result()
print(f"Provisioned virtual network {vnet_result}")

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.operations.subnetsoperations?view=azure-python
poller = network_client.subnets.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    VNET_NAME,
    SUBNET_NAME,
    {"address_prefix": "10.0.0.0/24"},
)
subnet_result = poller.result()
print(f"Provisioned virtual subnet {subnet_result} ")

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.operations.publicipaddressesoperations?view=azure-python
poller = network_client.public_ip_addresses.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    IP_NAME,
    {
        "location": LOCATION,
        "sku": {"name": "Standard"},
        "public_ip_allocation_method": "Static",
        "public_ip_address_version": "IPV4",
    },
)
ip_address_result = poller.result()
print(f"Provisioned public IP address {ip_address_result} ")
print(f"ip address : {ip_address_result.ip_address}")

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.models.securityrule?view=azure-python
nsg_rule = SecurityRule(
    name="AllowSSH",
    access="Allow",
    protocol="Tcp",
    destination_port_range="22",
    destination_address_prefix="*",
    direction="Inbound",
    source_port_range="*",
    source_address_prefix="*",
    priority=100
)

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.models.networksecuritygroup?view=azure-python
nsg = NetworkSecurityGroup(
    location=LOCATION,
    security_rules=[nsg_rule],
)

NSG_NAME = "python-example-nsg"

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.operations.networksecuritygroupsoperations?view=azure-python#azure-mgmt-network-operations-networksecuritygroupsoperations-begin-create-or-update
poller = network_client.network_security_groups.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    NSG_NAME,
    nsg,    
)
nsg_result = poller.result()
print(f"Provisioned network security group {nsg_result} ")

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-network/azure.mgmt.network.operations.networkinterfacesoperations?view=azure-python
poller = network_client.network_interfaces.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    NIC_NAME,
    {
        "location": LOCATION,
        "network_security_group": nsg_result,
        "ip_configurations": [
            {
                "name": IP_CONFIG_NAME,
                "subnet": {"id": subnet_result.id},
                "public_ip_address": {"id": ip_address_result.id},
            }
        ],
    },
)
nic_result = poller.result()
print(f"Provisioned network interface client {nic_result}")

VM_NAME = "ExampleVM"
USERNAME = "azureuser"
PASSWORD = "admin!12345"
DISK_NAME = "python-example-disk"

USER_MANAGED_ID = "/subscriptions/99999-99999-99999-99999/resourcegroups/sampleResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/user_managed_id_01"

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-compute/azure.mgmt.compute.computemanagementclient?view=azure-python
compute_client = ComputeManagementClient(credential, SUBSCRIPTION_ID)

# https://learn.microsoft.com/ja-jp/python/api/azure-mgmt-compute/azure.mgmt.compute.v2023_07_01.operations.virtualmachinesoperations?view=azure-python
poller = compute_client.virtual_machines.begin_create_or_update(
    RESOURCE_GROUP_NAME,
    VM_NAME,
    {
        "location": LOCATION,
        "storage_profile": {
            "image_reference": {
                "publisher": "Canonical",
                "offer": "0001-com-ubuntu-server-jammy",
                "sku": "22_04-lts-gen2",
                "version": "latest",
            },
            "os_disk": {
                "create_option": "FromImage",
                "name": DISK_NAME
            }
        },
        "hardware_profile": {"vm_size": "Standard_B1ms"},
        "os_profile": {
            "computer_name": VM_NAME,
            "admin_username": USERNAME,
            "admin_password": PASSWORD,
        },
        "network_profile": {
            "network_interfaces": [
                {
                    "id": nic_result.id,
                }
            ],
        },
        "identity":{
            "type": "UserAssigned",
            "user_assigned_identities":{
                USER_MANAGED_ID: {}
            }
        }

    },
)
vm_result = poller.result()
print(f"Provisioned virtual machine {vm_result}")

ユーザー割り当てマネージドIDをAzure コンテナー インスタンスに適用(Azure SDK for Python)

user_assigned_identitiesのところで設定しています。

from azure.identity import DefaultAzureCredential
from azure.mgmt.containerinstance import ContainerInstanceManagementClient


SUBSCRIPTION_ID = "****-****-****-****-****"
RESOURCE_GROUP_NAME = "container_resource_group"
LOCATION = "japaneast"

CONTAINER_GROUP_NAME = "webservercontainer"

USER_MANAGED_ID = "/subscriptions/99999-99999-99999-99999/resourcegroups/sampleResourceGroup/providers/Microsoft.ManagedIdentity/userAssignedIdentities/user_managed_id_01"
IMAGE = "containerregistory01.azurecr.io/skypesample:latest"
COMMAND_LINE = ["nginx", "-g", "daemon off;"]

REGISTORY_SERVER = "containerregistory01.azurecr.io"
REGISTORY_USER_NAME = "containerregistory01"
REGISTORY_USER_PASSWORD = "************"

client = ContainerInstanceManagementClient(credential=DefaultAzureCredential(), subscription_id=SUBSCRIPTION_ID)

poller = client.container_groups.begin_create_or_update(resource_group_name=RESOURCE_GROUP_NAME \
    ,container_group_name=CONTAINER_GROUP_NAME \
    ,container_group={
        "identity":{
            "type": "UserAssigned",
            "user_assigned_identities":{
                USER_MANAGED_ID: {}
            }
        },
        "containers":[{
            "name":CONTAINER_GROUP_NAME,
            "image":IMAGE,
            "command":COMMAND_LINE,
            "resources":{
                "requests":{
                    "memory_in_gb":1.5,
                    "cpu":1,
                }
            }

        }],
        "image_registry_credentials":[{
            "server":REGISTORY_SERVER,
            "username":REGISTORY_USER_NAME,
            "password":REGISTORY_USER_PASSWORD
        }],
        "restart_policy":"Never",
        "location":LOCATION,
        "os_type": "Linux",
    })

container_result = poller.result()
print(f"Provisioned container {container_result}")

👇関連記事

👇参考URL

[keywords]
Azure CLI ユーザー割り当てマネージドID アクセス権限 ロール

ユーザー割り当てマネージドIDによるAzureリソースへのアクセス権限付与

更新日:2024年01月29日

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?