0

我想以编程方式为 Azure VM 赋予贡献者角色,以修改其他资源(如路由表、存储帐户)中的内容。

https://docs.microsoft.com/en-us/azure/active-directory/managed-service-identity/howto-assign-access-cli

以上 msft 文档解释了如何使用 Azure CLI 为启用 MSI 的 VM 赋予 Azure 存储帐户的贡献者角色。有人可以使用 Azure Python SDK 而不是 Azure CLI 来实现相同的目标吗?不启用微星也能达到同样的目的吗?

4

1 回答 1

1

如果您为您的 VM 创建一个服务主体,并以某种方式在 VM 上推送凭据,您可以避免 MSI。但是创建 MSI 是为了避免这种情况,因为在 VM 中推送凭据并不是一个简单的过程,也不是安全的。

要将角色分配给 Active Directory ID(无论使用 MSI 还是专用 ServicePrincipal),您可以使用此代码分配角色(使用azure-mgmt-authorization包)。

https://github.com/Azure-Samples/compute-python-msi-vm#role-assignement-to-the-msi-credentials

# Get "Contributor" built-in role as a RoleDefinition object
role_name = 'Contributor'
roles = list(authorization_client.role_definitions.list(
    resource_group.id,
    filter="roleName eq '{}'".format(role_name)
))
assert len(roles) == 1
contributor_role = roles[0]

# Add RG scope to the AD id
# This assumes "sp_id" is either a MSI id or a SP id
role_assignment = authorization_client.role_assignments.create(
    resource_group.id,
    uuid.uuid4(), # Role assignment random name
    {
        'role_definition_id': contributor_role.id,
        'principal_id': sp_id
    }
)

然后这个 AD id 将只能作用于该角色,仅此而已。

于 2018-07-18T23:47:27.013 回答