zoukankan      html  css  js  c++  java
  • Azure ARM (17) 基于角色的访问控制 (Role Based Access Control, RBAC)

      《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"
      ]
    }
  • 相关阅读:
    OpenCvSharp 识别颜色
    树莓派 Raspberry Pi 4,.net core 3.0 ,Avalonia UI 开发
    iis频繁奔溃,分析dump
    prerender.io 搜索引擎优化 部署成windows服务 实现开机自动开启服务
    ionic3包还原使用yarn命令执行步骤(解决ionic3懒加载报找不到 module的错误)
    firebreath 在谷歌和火狐浏览器下的调试 以及打包
    ASP.NET Web API 2 对 CORS 的支持
    .net mvc通过ucenter和 discuz的整合,nopcommerce ucenter 插件的方式实现
    EF + Mysql
    Jenkins持续集成 & .NET
  • 原文地址:https://www.cnblogs.com/threestone/p/7569891.html
Copyright © 2011-2022 走看看