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,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值