LAB 11 ~60 phút Chương 3 AZ-500 Custom Role

Custom Role Chỉ Restart VM

Tạo Azure RBAC Custom Role với quyền tối thiểu — chỉ cho phép restart VM bằng JSON definition. Gán cho az500-helpdesk01, xác minh user có thể restart nhưng không xóa được VM. Nguyên tắc least privilege trong thực tế.

🎯 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ị

Yêu cầu:
  • 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
Lưu ý:
  • 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

1

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.

Cách 2 — Azure CLI
Azure CLI— Tìm actions của Microsoft.Compute
# 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
Kết quả: Xác nhận action Microsoft.Compute/virtualMachines/restart/action tồn tại và đúng tên.
2

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.

JSON — vm-restart-operator.json
JSON— vm-restart-operator.json (thay YOUR_SUBSCRIPTION_ID)
{
  "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"
  ]
}
Cách 2 — Azure CLI
Azure CLI— Tạo file JSON tự động
# 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
3

Deploy Custom Role bằng az role definition create

Cách 1 — Portal
  1. 3.1Portal → Subscriptions → chọn subscription → Access control (IAM)
  2. 3.2Tab Roles → click + AddAdd custom role
  3. 3.3Custom role name: VM Restart Operator
  4. 3.4Tab JSON → click Edit → dán nội dung JSON ở Bước 2 → Save
  5. 3.5Click Review + createCreate → chờ 2–3 phút để propagate
Cách 2 — Azure CLI
Azure CLI— az role definition create từ file JSON
# 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"
Kết quả (Output)— az role definition list --custom-role-only
Name                  Type                         Description
--------------------  ---------------------------  ----------------------------------------------------
VM Restart Operator   CustomRole                   Chỉ cho phép restart virtual machine...
4

Gán Custom Role cho helpdesk và kiểm tra

Cách 1 — Portal
  1. 4.1Mở VM hoặc resource group chứa VM → Access control (IAM)
  2. 4.2+ AddAdd role assignment → tìm VM Restart Operator
  3. 4.3Members: chọn az500-helpdesk01 → Assign
  4. 4.4Incognito browser → đăng nhập az500-helpdesk01 → vào VM → thấy nút Restart hoạt động
  5. 4.5Thử click Delete → bị từ chối (không có quyền)
Cách 2 — Azure CLI
Azure CLI— Gán custom role và test restart VM
# 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
5

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ế.

Cách 2 — Azure CLI
Azure CLI— Update custom role definition
# 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

Azure CLI— Xóa assignment trước, rồi xóa role definition
# 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

Custom role "VM Restart Operator" tạo thành công

IsCustom: true, 4 Actions đúng, AssignableScopes đúng subscription

helpdesk01 restart VM thành công

az vm restart hoàn thành không có lỗi, VM restarted

helpdesk01 bị từ chối delete VM

AuthorizationFailed khi az vm delete — least privilege hoạt động đúng

Update role thành cô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.

Lab 10: RBAC Resource Group Thư viện Labs Lab 12: PIM Eligible Assignment
Zalo