如何使用Azure Python SDK向存储帐户中的内置角色添加权限?

时间:2018-10-12 03:45:00

标签: python azure azure-storage-blobs azure-rbac

我有一个存储帐户,我想以“存储帐户密钥操作员服务角色”授予我的一个应用程序服务的权限。 类似于Azure门户中的以下操作。

Azure Portal Add Permission

2 个答案:

答案 0 :(得分:1)

  

也欢迎任何好的解决方法。

这里有一些解决方法。

1。使用powershell,请参阅此link

New-AzureRmRoleAssignment -ObjectId <ObjectId> -RoleDefinitionName "Storage Account Key Operator Service Role" -Scope "<your storage account resourceID>"

2。使用Azure CLI,请参阅此link

az role assignment create --role "Storage Account Key Operator Service Role" --assignee-object-id "<object-id>" --scope "<your storage account resourceID>"

3。使用Rest API,请参阅此link

PUT https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleAssignments/{roleAssignmentName}?api-version=2015-07-01

4。使用ARM模板,请参阅此link

答案 1 :(得分:0)

花费了很多时间后,我才能够使用python来授权应用服务。这就是我遵循的方法

您使用的凭据应属于订阅所有者,因为不允许贡献者进行访问更改。

这是需要安装的python软件包

azure-mgmt-authorization==0.50.0
azure-graphrbac==0.51.0

这是代码段

subscription_id = config['SUBSCRIPTION_ID']
credentials = ServicePrincipalCredentials(
    client_id=config['AZURE_CLIENT_ID'],
    secret=config['AZURE_CLIENT_SECRET'],
    tenant=config['AZURE_TENANT_ID']
)
graph_credentials = ServicePrincipalCredentials(
    client_id=config['AZURE_CLIENT_ID'],
    secret=config['AZURE_CLIENT_SECRET'],
    tenant=config['AZURE_TENANT_ID'],
    resource="https://graph.windows.net"
)


def get_object_id(full_app_name, resource_name_prefix, resource_type="Microsoft.Web/sites"):

    gcli = GraphRbacManagementClient(graph_credentials, config['AZURE_TENANT_ID'])
    sp = gcli.service_principals.list(filter="displayName eq '%s'" % full_app_name)
    sp = next(sp, False)
    if sp:
        print("Found Service Principal %s" % sp.display_name)
        return sp.object_id
    else:
        raise Exception("Service Principal not found")


def delete_keylistrole_appservice(resource_group_name, storage_name, role_assignment_name):

    resource_provider = "Microsoft.Storage"
    resource_type = "storageAccounts"
    scope = '/subscriptions/%s/resourceGroups/%s/providers/%s/%s/%s' % (
        subscription_id, resource_group_name, resource_provider, resource_type, storage_name)
    auth_cli = AuthorizationManagementClient(credentials, subscription_id, api_version="2015-07-01")
    resp = auth_cli.role_assignments.delete(scope, role_assignment_name)
    print("%s App Service access revoked %s Storage account" % (role_assignment_name, storage_name))


def assign_keylistrole_appservice(resource_group_name, storage_name, app_service_name):

    resource_provider = "Microsoft.Storage"
    resource_type = "storageAccounts"
    scope = '/subscriptions/%s/resourceGroups/%s/providers/%s/%s/%s' % (
        subscription_id, resource_group_name, resource_provider, resource_type, storage_name)
    role_assignment_name = str(uuid.uuid4())

    role_id = "/subscriptions/%s/providers/Microsoft.Authorization/roleDefinitions/%s" % (subscription_id, "81a9662b-bebf-436f-a333-f67b29880f12")
    principal_id = get_object_id(app_service_name)
    props = RoleAssignmentProperties(role_definition_id=role_id, principal_id=principal_id)

    auth_cli = AuthorizationManagementClient(credentials, subscription_id, api_version="2015-07-01")
    resp = auth_cli.role_assignments.create(scope, role_assignment_name, properties=props)
    print("%s App Service authorized to access %s Storage account" % (app_service_name, storage_name))
    return role_assignment_name

请注意graph_credentials与凭据不同,因为它们需要resource =“ https://graph.windows.net”