Azure ARM (17) 基于角色的访问控制 (Role Based Access Control, RBAC) - 自定义Role

简介:

 《Windows Azure Platform 系列文章目录

  

  在上面一篇博客中,笔者介绍了如何在RBAC里面,设置默认的Role。

  这里笔者将介绍如何使用自定的Role。

  

  主要内容有:

  一.了解Role中的Action和NotAction

  二.通过PowerShell,查看相应的Action

  三.编辑json Template,自定义Role

  四.设置相应的Role

  五.删除自定义Role

 

  一.了解Role中的Action和NotAction

  比如SQL DB Contributor这个Role,权限如下

   

  允许的操作是Actions的操作,减去NotActions的操作。这个概念非常非常重要。

  允许的操作是Actions的操作,减去NotActions的操作。这个概念非常非常重要。

  允许的操作是Actions的操作,减去NotActions的操作。这个概念非常非常重要。

  The access granted by a custom role is computed by subtracting the NotActions operations from the Actions operations.

  https://docs.microsoft.com/en-us/azure/active-directory/role-based-access-control-custom-roles#notactions

 

  二.通过PowerShell,查看相应的Action

  我们知道在Azure ARM里面有非常多的服务,比如Azure Storage, Azure Virtual Machine, Azure SQL Database等。

  还有非常多的操作,比如Read, Delete, List等等。

  如果需要了解具体每一个服务和相应的操作步骤,我们需要查询相应的操作步骤Action。

  具体命令如下:

复制代码
#登录Azure China,以Admin身份登录
Add-AzureRmAccount -Environment AzureChinaCloud

#选择当前订阅
Select-AzureRmSubscription -SubscriptionName '[订阅名称]'

#获得所有对存储Storage的操作
Get-AzureRmProviderOperation Microsoft.Storage/*

#获得所有对虚拟机VM的只读操作
Get-AzureRmProviderOperation Microsoft.Compute/*/read
复制代码

  在输出的内容中,我们可以选择相应的Action。图略。

 

 

  三.编辑json Template,自定义Role

  1.通过上面的Get-AzureRmProviderOperation语句,我们就可以查看到具体的操作。

  在编辑json template之前,我们需要查看默认Role的Name和ID,防止自定义的Name和ID与默认的Role冲突。

  具体的命令如下:

复制代码
#登录Azure China,以Admin身份登录
Add-AzureRmAccount -Environment AzureChinaCloud

#选择当前订阅
Select-AzureRmSubscription -SubscriptionName '[订阅名称]'

#查看Azure已经存在的Role的Name,Id,IsCustom属性
Get-AzureRmRoleDefinition | Select Name,Id,IsCustom
复制代码

  执行结果如下图:

  

 

  2.然后我们就可以编辑json Template,模板如下:

复制代码
{
    //这里是自定义Role的名称,请不要与Azure默认的Name冲突
    "Name": "Cannot Delete Storage Account Role",
    //这里是Role的ID,请不要与Azure默认的Id冲突
    "Id": "11794e3b-eeeb-4e5c-a98b-27cc053a0b35",
    //因为是自定义设置,所以Value为true
    "IsCustom": true,
    //这里是简单的Role的描述
    "Description": "Cannot Delete Storage Account Role.",
    "Actions": [
    //这里是允许的操作
            //对Azure Storage进行只读操作
            "Microsoft.Storage/*/read",
            //查看Role
            "Microsoft.Authorization/*/read",
            //对Resource Group的只读操作
            "Microsoft.Resources/subscriptions/resourceGroups/read"
    ],
    "NotActions": [
    //请注意,这里不是拒绝的操作。
    //用户最终的权限,是Actions,减去NotActions的权限
    //The access granted by a custom role is computed by subtracting the NotActions operations from the Actions operations.
    //https://docs.microsoft.com/en-us/azure/active-directory/role-based-access-control-custom-roles#notactions
    
    ],
    "AssignableScopes": [
    //修改下面的subscription Id为用户Azure订阅ID
    "/subscriptions/11111111-2222-3333-4444-1e2900a4504b"
    ]
}
复制代码

  将上面的文件保存为json格式,放在D盘根目录下,路径为D:\cannotdeletestorage.json

 

  

  4.然后我们执行下面的Azure Powershell,把上面的cannotdeletestorage.json上传到Azure

复制代码
#登录Azure China,以Admin身份登录
Add-AzureRmAccount -Environment AzureChinaCloud

#选择当前订阅
Select-AzureRmSubscription -SubscriptionName '[订阅名称]'

#上传本地PC机器上的json template文件
New-AzureRmRoleDefinition -InputFile 'D:\cannotdeletestorage.json'
复制代码

  执行成功后如下图:

   

 

 

  四.设置相应的Role 

  1.打开Chrome浏览器,我们以服务管理员身份(Admin),登录Azure ARM Portal: https://portal.azure.cn

  2.创建1个存储账户,还有2个Azure SQL Database资源。如下图:

  可以看到一共有5个资源:

  

 

  3.点击Azure Active Directory,把readonly账户,设置为自定义Role:Cannot Delete Storage Account Role

  

  

  4.打开IE浏览器,以readonly账户登录Azure ARM Portal: https://portal.azure.cn,查看到的结果如下图:

  可以看到只有1个资源。

   

   这是因为我们在json template里面设置了Action

复制代码
"Actions": [
         //这里是允许的操作
            //对Azure Storage进行只读操作
            "Microsoft.Storage/*/read",
            //查看Role
            "Microsoft.Authorization/*/read",
            //对Resource Group的只读操作
            "Microsoft.Resources/subscriptions/resourceGroups/read"
    ],
复制代码

  对Storage存储账户是只读操作的,对Azure SQL Database不进行任何操作。所以readonly这个账户无法看到Azure SQL Database相应的资源。

 

  5.因为readonly账户对Storage存储账户是只读操作的,所以无法删除存储账户。结果如下图:

  

   

 

  五.删除自定义Role

  1.如果用户不希望继续使用自定义Role,可以按照以下步骤操作。

  2.打开Chrome浏览器,我们以服务管理员身份(Admin),登录Azure ARM Portal: https://portal.azure.cn

  把readonly账户删除自定义Role。如下图:

  

 

  3.在Azure PowerShell里面执行以下命令:

复制代码
#登录Azure China,以Admin身份登录
Add-AzureRmAccount -Environment AzureChinaCloud

#选择当前订阅
Select-AzureRmSubscription -SubscriptionName '[订阅名称]'

#可以根据自定义Role的Name进行删除
Remove-AzureRmRoleDefinition -Name 'Cannot Delete Storage Account Role'

#或者根据自定义Role的ID,进行删除
Remove-AzureRmRoleDefinition -Id '[RoleID]'
复制代码

  执行结果:

 

 

 

 

 

 

 

  最后如果大家有兴趣的话,可以查看下面这个自定义Role所拥有的权限

复制代码
{
  "Name": "Virtual Machine Operator",
  "Id": "cadb4a5a-4e7a-47be-84db-05cad13b6769",
  "IsCustom": true,
  "Description": "Can monitor and restart virtual machines.",
  "Actions": [
    "Microsoft.Storage/*/read",
    "Microsoft.Network/*/read",
    "Microsoft.Compute/*/read",
    "Microsoft.Compute/virtualMachines/start/action",
    "Microsoft.Compute/virtualMachines/restart/action",
    "Microsoft.Authorization/*/read",
    "Microsoft.Resources/subscriptions/resourceGroups/read",
    "Microsoft.Insights/alertRules/*",
    "Microsoft.Insights/diagnosticSettings/*",
    "Microsoft.Support/*"
  ],
  "NotActions": [

  ],
  "AssignableScopes": [
    "/subscriptions/c276fc76-9cd4-44c9-99a7-4fd71546436e",
    "/subscriptions/e91d47c4-76f3-4271-a796-21b4ecfe3624",
    "/subscriptions/34370e90-ac4a-4bf9-821f-85eeedeae1a2"
  ]
}
复制代码

 


本文转自Azure Lei Zhang博客园博客,原文链接:http://www.cnblogs.com/threestone/p/7569891.html,如需转载请自行联系原作者


目录
相关文章
|
JavaScript 前端开发 API
【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)
【Azure Developer】use @azure/arm-monitor sdk 遇见 ManagedIdentityCredential authentication failed.(status code 500)
152 1
|
存储 网络协议 安全
【Azure 环境】ARM部署模板大于4MB的解决方案及Linked Template遇见存储账号防火墙无法访问
【Azure 环境】ARM部署模板大于4MB的解决方案及Linked Template遇见存储账号防火墙无法访问
218 0
|
存储 网络协议 网络安全
【Azure 环境】部署ARM Linked Template时候 Blob SAS Token不能正常工作
Unable to retrieve url https://<stroage account name>.blob.core.chinacloudapi.cn/arm/azuredeploy.json?sp=r 'st' is not recognized as an internal or external command, operable program or batch file. 'se' is not recognized as an internal or external command, operable program or batch file. 'spr' is no
225 1
|
安全 Linux 数据库
|
安全 数据安全/隐私保护 开发者
【Azure 服务总线】Azure门户获取ARM模板,修改Service Bus的TLS版本
【Azure 服务总线】Azure门户获取ARM模板,修改Service Bus的TLS版本
194 0
|
存储
【Azure Developer】Github Action部署资源(ARM模板)到Azure中国区时,遇见登录问题的解决办法
【Azure Developer】Github Action部署资源(ARM模板)到Azure中国区时,遇见登录问题的解决办法
204 0
|
安全 测试技术 微服务
【Azure 微服务】Service Fabric, 使用ARM Template方式来更新SF集群的证书(Renew SF Certificate)
【Azure 微服务】Service Fabric, 使用ARM Template方式来更新SF集群的证书(Renew SF Certificate)
210 0