Azure基於角色的用戶接入控制(RBAC)

RBAC是Role Based Access Control是基於角色的接入控制的簡稱。在Azure推出ARM之後,對Azure各類資源的管理粒度已經很是細緻,使得RBAC成爲可能。sql

經過RBAC能夠很是方便的給不一樣的用戶分配不一樣的資源的不一樣權限。nosql

本文將以一個最通用的例子來介紹如何給一個用戶分配相關的權限。ide

一 需求this

用戶vmops只能對資源組1的虛擬機和資源組2的特定虛擬機進行開、關機或重啓的動做。其餘全部操做權限都沒有。spa

二 實現rest

1 建立用戶code

在Azure的老Portal上建立建立用戶vmops@weiheng.partner.onmschina.cnblog

2 肯定能夠訪問的資源ip

vmops這個用戶能夠操做的資源有:資源

Subscription 2中的Resource Group 1

Subscription 2中的Resource Group 2中的VM 1

Subscription 1中全部的VM

能夠進行的操做有:

Start、restart、powerOff、Deallocate

3 經過PowerShell進行相關配置

A 首先須要獲得VM能夠操做的動做:

Get-AzureRMProviderOperation Microsoft.Compute/virtualMachines/* | ft operation
WARNING: The output object type of this cmdlet will be modified in a future release.
Operation
---------
Microsoft.Compute/virtualMachines/read
Microsoft.Compute/virtualMachines/write
Microsoft.Compute/virtualMachines/delete
Microsoft.Compute/virtualMachines/start/action
Microsoft.Compute/virtualMachines/powerOff/action
Microsoft.Compute/virtualMachines/redeploy/action
Microsoft.Compute/virtualMachines/restart/action
Microsoft.Compute/virtualMachines/deallocate/action
Microsoft.Compute/virtualMachines/generalize/action
Microsoft.Compute/virtualMachines/capture/action
Microsoft.Compute/virtualMachines/vmSizes/read
Microsoft.Compute/virtualMachines/instanceView/read
Microsoft.Compute/virtualMachines/extensions/read
Microsoft.Compute/virtualMachines/extensions/write
Microsoft.Compute/virtualMachines/extensions/delete

 

B 獲取三種資源的ID

1 subscription ID

Get-AzureRmSubscription | ft SubscriptionID
SubscriptionId
--------------
$Sub1
$Sub2

 

2 Subscription 2 中的Resouce Group ID

Get-AzureRmResourceGroup | ft ResourceId
ResourceId
----------
/subscriptions/$Sub1/resourceGroups/hwnosql
/subscriptions/$Sub1/resourceGroups/hwiscsi

3 hwiscsi中vm1的Id

get-azurermvm -ResourceGroupName hwiscsi | ft id
Id
/subscriptions/$Sub1/resourceGroups/hwiscsi/providers/Microsoft.Compute/virtualMachines/hwiscsi01
/subscriptions/$Sub1/resourceGroups/hwiscsi/providers/Microsoft.Compute/virtualMachines/hwiscsi02
/subscriptions/$Sub1/resourceGroups/hwiscsi/providers/Microsoft.Compute/virtualMachines/hwiscsiwin

 

C 定義新的Role

首先得到已有的Role的類型

Get-AzureRmRoleDefinition | ft name
Name
----
API Management Service Contributor
Application Insights Component Contributor
BizTalk Contributor
Classic Network Contributor
Classic Storage Account Contributor
Classic Virtual Machine Contributor
ClearDB MySQL DB Contributor
Contributor
Data Factory Contributor
DocumentDB Account Contributor
Intelligent Systems Account Contributor
Network Contributor
New Relic APM Account Contributor
Owner
Reader
Redis Cache Contributor
Scheduler Job Collections Contributor
Search Service Contributor
SQL DB Contributor
SQL Security Manager
SQL Server Contributor
Storage Account Contributor
User Access Administrator
Virtual Machine Contributor
Web Plan Contributor
Website Contributor

本方案經過Virtual Machine Contributor的模板修改。

#獲取"Virtual Machine Contributor"配置
$role = Get-AzureRmRoleDefinition "Virtual Machine Contributor" 
$role.Id = $null 
$role.Name = "Virtual Machine Operator" 
$role.Description = "Can monitor and start stop or restart virtual machines." 
$role.Actions.Clear() 

#添加周邊資源讀的權限
$role.Actions.Add("Microsoft.Storage/*/read") 
$role.Actions.Add("Microsoft.Network/*/read") 
$role.Actions.Add("Microsoft.Compute/*/read") 
$role.Actions.Add("Microsoft.Authorization/*/read") 
$role.Actions.Add("Microsoft.Resources/subscriptions/resourceGroups/read") 

#添加VM相關的操做權限
$role.Actions.Add("Microsoft.Compute/virtualMachines/start/action") 
$role.Actions.Add("Microsoft.Compute/virtualMachines/restart/action") 
$role.Actions.Add("Microsoft.Compute/virtualMachines/powerOff/action") 
$role.Actions.Add("Microsoft.Compute/virtualMachines/deallocate/action") 
$role.Actions.Add("Microsoft.Insights/alertRules/*") 

#把兩個Subscription加入到這個Role管理範圍中
$role.AssignableScopes.Clear() 
$role.AssignableScopes.Add("/subscriptions/$Sub1") 
$role.AssignableScopes.Add("/subscriptions/$Sub2") 

#添加角色
New-AzureRmRoleDefinition -Role $role
Name : Virtual Machine Operator
Id : 55aca895-61dc-4162-b7a6-fbab532d14a2
IsCustom : True
Description : Can monitor and start stop or restart virtual machines.
Actions : {Microsoft.Storage/*/read, Microsoft.Network/*/read, Microsoft.Compute/*/read, Microsoft.Compute/virtualMachines/start/action...}
NotActions : {}
AssignableScopes : {/subscriptions/$Sub1}
 

 

D 給用戶分配權限

New-AzureRmRoleAssignment -SignInName vmops@weiheng.partner.onmschina.cn -Scope /subscriptions/$Sub1/resourceGroups/hwnosql -RoleDefinitionName "Virtual Machine Operator" 
New-AzureRmRoleAssignment -SignInName vmops@weiheng.partner.onmschina.cn -Scope /subscriptions/$Sub1/resourceGroups/hwiscsi/providers/Microsoft.Compute/virtualMachines/hwiscsiwin -RoleDefinitionName "Virtual Machine Operator" 
New-AzureRmRoleAssignment -SignInName vmops@weiheng.partner.onmschina.cn -Scope /subscriptions/$Sub2 -RoleDefinitionName "Virtual Machine Operator" 

三 檢測

經過用戶登錄Portal後,能夠對定義過的資源進行管理,但對沒有權限的資源不能進行管理。

下圖顯示,ResourceGroup nosql中的VM能夠被管理,但只能操做定義過的權限,好比Stop VM

下圖顯示,虛擬機iscsiwin能夠被管理,但只能顯示定義過權限的操做:Start、Stop、Restart:

而admin的權限多了delete的菜單:

另外,對於VM之外的資源,好比建立StorageAccount,vmops這個用戶是沒有管理權限的:

建立VNET,一樣沒有權限:

四 總結:

經過Azure ARM中的資源的受權,能夠實現對不一樣用戶不一樣權限的控制。

受權的動做包括一些步驟:

  1. 建立角色Role,包括:能夠操做的動做、不能夠操做的動做
  2. 建立用戶
  3. 把用戶、角色、資源聯結起來
相關文章
相關標籤/搜索