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 -Name ‘[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 ARM (17) 基於角色的訪問控制 (Role Based Access Control, RBAC) - 自定義Role