使用 Azure Agent Grid 時,您在 Azure 容器執行個體 中註冊的每個無伺服器沙箱都會獲得 SPIFFE 工作負載識別碼,系統預設會拒絕來自沙盒的外站流量,除非綁定到網格的政策群組允許。 您可以對網格中的所有沙箱套用預設政策,並在沙盒執行期間為個別沙盒新增政策。
在本教學中,您將 AI 代理群組與 Agent Grid 資源 (Microsoft.NetworkSecurity/agentFabrics) 建立關聯,套用允許所有網格成員使用 *.bing.com 的預設政策,確認其他目的地是否被封鎖,然後新增一項允許某個沙盒的 SPIFFE 身分使用 *.google.com 的政策。
在這個教學中,你會學到如何:
- 建立預設的出站原則群組並將其繫結至 Agent Grid
- 建立與 Agent Grid 相關聯的 AI 代理程式群組
- 建立已登錄於代理程式網格的沙盒並測試外寄規則
- 取得沙盒的 SPIFFE 身份並套用每個沙盒原則
Prerequisites
Azure 訂用帳戶。 如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
Azure CLI(版本 2.60 或更新版本)已登入
az login,或使用其他工具登入,以向 Azure Resource Manager 傳送經過驗證的 REST 請求。在您的訂用帳戶中註冊的
Microsoft.ContainerInstance資源提供者。az provider register --namespace Microsoft.ContainerInstance --wait參與者、Azure Deployment Stack Owner 和 Storage Blob Data Contributor 角色已指派給您訂閱中的 計算沙盒群組 服務主體,以便該服務能配置您訂閱中的受控資源群組和快照儲存體帳戶。 要指派角色,您需要有權限在訂閱上建立角色指派,例如 擁有者 或 使用者存取管理員 角色:
subscriptionId=$(az account show --query id --output tsv) spObjectId=$(az ad sp show --id 1eeacc2b-ec4a-4b1e-977a-2b5fef2131c6 --query id --output tsv 2>/dev/null || \ az ad sp create --id 1eeacc2b-ec4a-4b1e-977a-2b5fef2131c6 --query id --output tsv) for role in "Contributor" "Azure Deployment Stack Owner" "Storage Blob Data Contributor"; do az role assignment create --assignee-object-id $spObjectId --assignee-principal-type ServicePrincipal \ --role "$role" --scope /subscriptions/$subscriptionId done角色指派可能需要最多 15 分鐘才能生效。
在您的訂閱中為 Azure 容器執行個體 中無伺服器沙盒配置的配額:
在 Azure 入口網站開啟新的支援請求(問題類型設為服務與訂閱限制(配額),配額類型設為 容器執行個體),選擇您的訂閱,然後前往「附加詳細資料」標籤。
選擇「輸入詳細資料」以開啟配額詳細窗格,選擇您的位置,為配額類型選擇AIAgentsGroups - StandardCores (Preview),並填寫網路類型、容器類型、總 AIAgentsGroups 群組限制及總 AIAgentsGroups 核心限制:
提交支援申請。 在您的配額申請獲核准後,請重新註冊
Microsoft.ContainerInstance資源提供者(az provider register --namespace Microsoft.ContainerInstance --wait),以便您的訂閱能接收新啟用的功能。
在建立 AI 代理群組和沙箱後(請參閱 快速入門:建立您的第一個無伺服器沙箱)後,若要連接正在執行中的沙箱,請使用由沙箱閘道 protobuf 檔案
sandboxgateway.proto產生的 gRPC 用戶端。 需要 gRPC 用戶端,因為沙箱閘道會公開一個 gRPC 服務(aci.sandboxgateway.SandboxGatewayService),用於沙盒內的操作,例如等待準備就緒、執行指令、傳輸檔案及讀取日誌。 若要使用本文件中的跨平台範例 C# CLI(支援 Linux、macOS 和 Windows),請安裝 .NET 8 SDK,並編譯 CLI。Note
如果你透過 Azure Kubernetes Service (AKS) 排程和管理無伺服器沙盒,則不需要 gRPC 用戶端。
-
Azure Agent Grid
Microsoft.NetworkSecurity/agentFabrics資源,位於您建立 AI 代理程式群組的同一訂閱及區域內,且位於 Agent Grid 可用的區域。 有關支援的區域,請參閱 Agent Grid 區域可用性。
將預設的外寄政策套用至 Agent Grid
政策群組(Microsoft.NetworkSecurity/policyGroups)定義符合來源 SPIFFE 工作負載識別模式的 FqdnFiltering 規則,並允許對指定 FQDN 和連接埠進行外寄流量通訊。 萬用字模式 spiffe://microsoft.agentfabric/ns/*/sa/* 與網格中所有沙盒相符。
建立預設政策群組,允許所有網格成員對
*.bing.com的連接埠80和443進行外寄流量:PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/policyGroups/default-egress-policy?api-version=2026-07-21-preview Content-Type: application/json { "location": "centralus", "properties": { "displayName": "Default corporate egress", "description": "Allow Bing subdomains for all current and future grid members.", "priority": 100, "networkSecurityPolicies": [ { "type": "FqdnFiltering", "name": "allow-approved-corporate-egress", "priority": 10, "direction": "Outbound", "ports": [80, 443], "source": { "type": "WorkloadIdPatterns", "patterns": [ "spiffe://microsoft.agentfabric/ns/*/sa/*" ] }, "destination": { "type": "Fqdns", "fqdns": [ "*.bing.com" ] } } ] } }在
properties.defaultPolicyGroups中將政策群組綁定至您的代理程式網格資源:PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/agentFabrics/myAgentGrid?api-version=2026-07-21-preview Content-Type: application/json { "location": "centralus", "properties": { "defaultPolicyGroups": [ "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/policyGroups/default-egress-policy" ] } }
建立與 Agent Grid 相關聯的 AI 代理程式群組
建立一個 AI 代理程式群組,並將
properties.agentFabricResourceId設為 Agent Grid 資源的資源 ID。 您必須在建立群組時設定agentFabricResourceId,且無法將其與networkProfile搭配使用:PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myGridAgentsGroup?api-version=2026-08-01-preview Content-Type: application/json { "location": "centralus", "properties": { "agentFabricResourceId": "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/agentFabrics/myAgentGrid" } }檢查 AI 代理群組直到
properties.provisioningState是Succeeded,然後撥打connect來取得它的{endpoint}和{accessToken}:GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myGridAgentsGroup?api-version=2026-08-01-previewPOST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myGridAgentsGroup/connect?api-version=2026-08-01-preview
建立已註冊至 Agent Grid 的沙箱
在 AI 代理程式群組中建立一個沙盒,設定 properties.attachToAgentFabric 為 true。 你建立的沙盒若未使用它,則不會加入網格,且在建立後無法將 attachToAgentFabric 從 true 變更為 false,也無法將其與 機密沙盒結合:
PUT {endpoint}/sandboxes/grid-sandbox1?api-version=2026-08-01-preview
Authorization: Bearer {accessToken}
Content-Type: application/json
{
"location": "centralus",
"properties": {
"image": "mcr.microsoft.com/mirror/docker/library/alpine:3.16",
"attachToAgentFabric": true,
"resources": {
"cpu": 1,
"memory": 2
}
}
}
等到properties.provisioningState為Succeeded。 接著從回覆中複製 properties.connectionString,並等待來賓準備就緒 (WaitForReady)。 透過使用 範例 C# CLI:
dotnet run -- -cs "<grid-sandbox1-connection-string>" wait
驗證預設的輸出原則
從內部 grid-sandbox1 測試對 www.bing.com、www.google.com 和 example.com 的外寄 HTTP 請求。 請使用範例 C# CLI:
dotnet run -- -cs "<grid-sandbox1-connection-string>" exec /bin/sh -c '
for url in http://www.bing.com http://www.google.com http://example.com; do
out=$(if command -v curl >/dev/null 2>&1; then curl -sSD - -o /dev/null --max-time 15 "$url" 2>&1; else wget -S -O /dev/null -T 15 -t 1 "$url" 2>&1; fi)
code=$(printf "%s\n" "$out" | sed -n "s/^[[:space:]]*HTTP\/[^[:space:]]*[[:space:]]*\([0-9][0-9][0-9]\).*/\1/p" | head -n 1)
echo "$url -> HTTP ${code:-BLOCKED}"
done'
由於預設政策群組只 *.bing.com 允許 ,請求 www.bing.com 會以 HTTP 200 狀態碼成功,而對 www.google.com 和 example.com 的請求則被阻擋:
http://www.bing.com -> HTTP 200
http://www.google.com -> HTTP BLOCKED
http://example.com -> HTTP BLOCKED
使用沙箱的 SPIFFE ID 套用每個沙盒原則
當沙盒附加到代理網格時,網格會將其註冊為成員,並為其指派唯一的 SPIFFE 工作負載識別碼(properties.spiffeId)。 您可以在政策群組中鎖定該 SPIFFE ID,使只有該沙箱能抵達額外的目的地。
列出代理網格資源的成員,並找出其中
properties.workload.type為ACI、properties.workload.workloadId為grid-sandbox1,且properties.workload.resourceId為 AI 代理程式群組資源 ID 的項目:GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/agentFabrics/myAgentGrid/members?api-version=2026-07-21-preview從回應中複製
properties.spiffeId的grid-sandbox1值(例如,spiffe://microsoft.agentfabric/ns/<namespace>/sa/<service-account>)。建立原則群組,僅允許該沙箱的
spiffeId存取*.google.com:PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/policyGroups/agent-specific-egress-policy?api-version=2026-07-21-preview Content-Type: application/json { "location": "centralus", "properties": { "displayName": "Agent-specific Google egress", "description": "Allow this sandbox member to access Google subdomains.", "priority": 90, "networkSecurityPolicies": [ { "type": "FqdnFiltering", "name": "allow-google-egress", "priority": 10, "direction": "Outbound", "ports": [80, 443], "source": { "type": "WorkloadIdPatterns", "patterns": [ "<sandbox-spiffe-id>" ] }, "destination": { "type": "Fqdns", "fqdns": [ "*.google.com" ] } } ] } }將新的政策群組與代理程式網格資源上的預設政策群組綁定:
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/agentFabrics/myAgentGrid?api-version=2026-07-21-preview Content-Type: application/json { "location": "centralus", "properties": { "defaultPolicyGroups": [ "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/policyGroups/default-egress-policy", "/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.NetworkSecurity/policyGroups/agent-specific-egress-policy" ] } }政策更新完成後(可能需要幾分鐘),請在執行中的沙箱內再次執行外送測試,且不重新啟動或重新建立沙箱。 請使用範例 C# CLI:
dotnet run -- -cs "<grid-sandbox1-connection-string>" exec /bin/sh -c ' for url in http://www.bing.com http://www.google.com http://example.com; do out=$(if command -v curl >/dev/null 2>&1; then curl -sSD - -o /dev/null --max-time 15 "$url" 2>&1; else wget -S -O /dev/null -T 15 -t 1 "$url" 2>&1; fi) code=$(printf "%s\n" "$out" | sed -n "s/^[[:space:]]*HTTP\/[^[:space:]]*[[:space:]]*\([0-9][0-9][0-9]\).*/\1/p" | head -n 1) echo "$url -> HTTP ${code:-BLOCKED}" done'現在
www.bing.com和www.google.com都允許grid-sandbox1存取,而example.com仍遭封鎖:http://www.bing.com -> HTTP 200 http://www.google.com -> HTTP 200 http://example.com -> HTTP BLOCKED
清理資源
刪除沙盒,或若不再需要 AI 代理群組,請刪除它:
DELETE {endpoint}/sandboxes/grid-sandbox1?api-version=2026-08-01-preview
Authorization: Bearer {accessToken}
DELETE https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/myResourceGroup/providers/Microsoft.ContainerInstance/AIAgentsGroups/myGridAgentsGroup?api-version=2026-08-01-preview
如需更多關於在 Azure 入口網站中設定政策、TLS 檢查、MCP 政策及流程記錄的資訊,請參閱《設定 Azure 代理網格》。