🎯 Mục Tiêu Lab
Xác định đúng action Microsoft.Compute/virtualMachines/restart/action cần cho restart VM
Tạo file JSON định nghĩa custom role với Actions, NotActions, AssignableScopes đúng chuẩn
Deploy custom role bằng az role definition create
Gán custom role cho az500-helpdesk01 và kiểm tra quyền thực tế
Xác minh helpdesk restart được VM nhưng không xóa, không thay đổi config được
Cập nhật và xóa custom role đúng quy trình
🏗️ Kịch Bản
Đội helpdesk cần quyền restart VM khi nhận ticket từ user — nhưng không được xóa VM hay thay đổi cấu hình. Không có built-in role nào phù hợp. Giải pháp: tạo Custom Role "VM Restart Operator" với đúng 2 action cần thiết: xem VM (để biết VM nào) và restart VM. Đây là ví dụ điển hình cho nguyên tắc least privilege.
📋 Chuẩn Bị
- Azure subscription — quyền Owner để tạo custom role
- User
az500-helpdesk01đã tạo (Lab 02) - Ít nhất 1 VM trong subscription để test restart
- Azure CLI 2.50+ hoặc Azure Cloud Shell
- Custom role cần tối đa 5 phút để propagate sau khi tạo
- Mỗi subscription giới hạn 5.000 custom role definitions
- AssignableScopes phải chứa scope nơi bạn sẽ assign role
- Không thể xóa custom role khi đang có active assignment
🧪 Các Bước Thực Hiện
Xác định Actions cần thiết cho Restart VM
Trước khi tạo custom role, cần biết chính xác tên action. Dùng CLI để tìm.
# Tìm tất cả operations liên quan đến VM restart
az provider operation show \
--namespace Microsoft.Compute \
--query "resourceTypes[?name=='virtualMachines'].operations[].name" \
-o tsv | grep -i restart
# Xem chi tiết operations cần thiết
az provider operation show \
--namespace Microsoft.Compute \
--query "resourceTypes[?name=='virtualMachines'].operations[?contains(name,'restart') || contains(name,'read')]" \
-o json
# Actions cần thiết:
# Microsoft.Compute/virtualMachines/restart/action — restart VM
# Microsoft.Compute/virtualMachines/read — xem danh sách VM
# Microsoft.Compute/virtualMachines/instanceView/read — xem trạng thái VM
Microsoft.Compute/virtualMachines/restart/action tồn tại và đúng tên.
Tạo file JSON định nghĩa Custom Role
Tạo file vm-restart-operator.json với cấu trúc chuẩn Azure RBAC custom role definition.
{
"Name": "VM Restart Operator",
"IsCustom": true,
"Description": "Chỉ cho phép restart virtual machine. Không tạo, xóa hoặc thay đổi cấu hình VM.",
"Actions": [
"Microsoft.Compute/virtualMachines/restart/action",
"Microsoft.Compute/virtualMachines/read",
"Microsoft.Compute/virtualMachines/instanceView/read",
"Microsoft.Resources/subscriptions/resourceGroups/read"
],
"NotActions": [],
"DataActions": [],
"NotDataActions": [],
"AssignableScopes": [
"/subscriptions/YOUR_SUBSCRIPTION_ID"
]
}
# Lấy subscription ID hiện tại
SUB_ID=$(az account show --query id -o tsv)
echo "Subscription ID: $SUB_ID"
# Tạo file JSON (Bash/Cloud Shell)
cat > vm-restart-operator.json << EOF
{
"Name": "VM Restart Operator",
"IsCustom": true,
"Description": "Chỉ cho phép restart virtual machine. Không tạo, xóa hoặc thay đổi cấu hình VM.",
"Actions": [
"Microsoft.Compute/virtualMachines/restart/action",
"Microsoft.Compute/virtualMachines/read",
"Microsoft.Compute/virtualMachines/instanceView/read",
"Microsoft.Resources/subscriptions/resourceGroups/read"
],
"NotActions": [],
"DataActions": [],
"NotDataActions": [],
"AssignableScopes": [
"/subscriptions/$SUB_ID"
]
}
EOF
cat vm-restart-operator.json
Deploy Custom Role bằng az role definition create
- 3.1Portal → Subscriptions → chọn subscription → Access control (IAM)
- 3.2Tab Roles → click + Add → Add custom role
- 3.3Custom role name:
VM Restart Operator - 3.4Tab JSON → click Edit → dán nội dung JSON ở Bước 2 → Save
- 3.5Click Review + create → Create → chờ 2–3 phút để propagate
# Tạo custom role từ file JSON
az role definition create \
--role-definition vm-restart-operator.json
# Xác nhận role đã được tạo
az role definition list \
--custom-role-only true \
--output table
# Xem chi tiết custom role vừa tạo
az role definition list \
--name "VM Restart Operator" \
--output json
# Kết quả mong đợi: IsCustom=true, roleName="VM Restart Operator"
Name Type Description
-------------------- --------------------------- ----------------------------------------------------
VM Restart Operator CustomRole Chỉ cho phép restart virtual machine...
Gán Custom Role cho helpdesk và kiểm tra
- 4.1Mở VM hoặc resource group chứa VM → Access control (IAM)
- 4.2+ Add → Add role assignment → tìm VM Restart Operator
- 4.3Members: chọn az500-helpdesk01 → Assign
- 4.4Incognito browser → đăng nhập az500-helpdesk01 → vào VM → thấy nút Restart hoạt động
- 4.5Thử click Delete → bị từ chối (không có quyền)
# Gán custom role cho helpdesk tại scope resource group
HELPDESK_ID=$(az ad user show \
--id [email protected] \
--query id -o tsv)
az role assignment create \
--assignee $HELPDESK_ID \
--role "VM Restart Operator" \
--scope /subscriptions/$SUB_ID/resourceGroups/rg-az500-rbac
# --- Đăng nhập bằng helpdesk01 để test ---
az login --username [email protected]
# Restart VM (sẽ thành công)
az vm restart \
--resource-group rg-az500-rbac \
--name YOUR_VM_NAME
# Thử xóa VM (sẽ bị từ chối)
az vm delete \
--resource-group rg-az500-rbac \
--name YOUR_VM_NAME \
--yes
# → AuthorizationFailed: does not have authorization to perform action delete
Cập nhật Custom Role (thêm Start/Stop)
Thực hành update custom role để thêm quyền Start và Stop VM — mô phỏng quy trình mở rộng role trong thực tế.
# Lấy role definition hiện tại để update
ROLE_ID=$(az role definition list \
--name "VM Restart Operator" \
--query "[0].name" -o tsv)
# Tạo file JSON update với thêm start/stop
cat > vm-restart-operator-v2.json << EOF
{
"Name": "VM Restart Operator",
"Id": "$ROLE_ID",
"IsCustom": true,
"Description": "Cho phép restart, start và stop VM. Không tạo, xóa VM.",
"Actions": [
"Microsoft.Compute/virtualMachines/restart/action",
"Microsoft.Compute/virtualMachines/start/action",
"Microsoft.Compute/virtualMachines/deallocate/action",
"Microsoft.Compute/virtualMachines/read",
"Microsoft.Compute/virtualMachines/instanceView/read",
"Microsoft.Resources/subscriptions/resourceGroups/read"
],
"NotActions": [],
"DataActions": [],
"NotDataActions": [],
"AssignableScopes": [
"/subscriptions/$SUB_ID"
]
}
EOF
# Update custom role
az role definition update \
--role-definition vm-restart-operator-v2.json
🧹 Dọn Dẹp Tài Nguyên
# Xóa role assignment trước (bắt buộc trước khi xóa definition)
az role assignment delete \
--assignee $HELPDESK_ID \
--role "VM Restart Operator"
# Xóa custom role definition
az role definition delete \
--name "VM Restart Operator"
# Xác nhận đã xóa
az role definition list \
--custom-role-only true \
--output table
📊 Kết Quả Đầu Ra Lab 11
IsCustom: true, 4 Actions đúng, AssignableScopes đúng subscription
az vm restart hoàn thành không có lỗi, VM restarted
AuthorizationFailed khi az vm delete — least privilege hoạt động đúng
az role definition update cập nhật thêm start/stop không lỗi
❓ Câu Hỏi Ôn Tập
1. Thuộc tính nào trong JSON custom role quyết định scope nào role có thể được gán?
Gợi ý: AssignableScopes — có thể là subscription, resource group hoặc cả management group.
2. Khác biệt giữa Actions và DataActions trong custom role definition?
Gợi ý: Actions kiểm soát management operations (ARM API); DataActions kiểm soát truy cập data plane (đọc blob, queue message...).
3. Tại sao phải xóa tất cả role assignments trước khi xóa custom role definition?
Gợi ý: Không thể xóa definition khi còn active assignments — tránh orphaned permissions.
4. Lệnh CLI nào dùng để tạo custom role từ file JSON?
Gợi ý: az role definition create --role-definition filename.json
5. Khi nào nên tạo custom role thay vì dùng built-in role?
Gợi ý: Khi built-in role cấp quá nhiều quyền hơn cần thiết hoặc khi cần kết hợp quyền từ nhiều service mà không có built-in phù hợp.