常見政策模式
這些範例示範常用的 Cedar 政策模式。模式可同時使用 OAuth 和 IAM 身分驗證 - 為您的 AgentCore Gateway 組態選擇適當的委託人類型。如需主體屬性的詳細資訊,請參閱主體屬性。
無論身分驗證類型為何,這些模式都適用。
緊急關機
停用整個閘道的所有工具呼叫:
forbid( principal, action, resource );
使用案例:緊急關機、維護模式或事件回應。
效果:由於禁止wins 語意而覆寫所有允許政策。
停用特定工具
停用特定工具,同時讓其他工具保持運作:
forbid( principal, action == AgentCore::Action::"RefundTool___process_refund", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/refund-gateway" );
使用案例:暫時停用有問題的工具,而不會影響其他功能。
封鎖使用者存取
防止特定使用者或帳戶執行任何動作:
OAuth:封鎖特定使用者
透過比對使用者的使用者名稱標籤來封鎖使用者:
forbid( principal is AgentCore::OAuthUser, action, resource ) when { principal.hasTag("username") && principal.getTag("username") == "suspended-user" };
使用案例:立即撤銷遭入侵或暫停使用者帳戶的存取權。
IAM:封鎖特定帳戶
封鎖來自特定 AWS 帳戶的呼叫者:
forbid( principal is AgentCore::IamEntity, action, resource ) when { principal.id like "*:444455556666:*" };
使用案例:封鎖測試或未經授權的帳戶存取生產工具。模式 :444455556666: 符合包含該帳戶 ID 的任何 ARN 格式 (擔任角色、IAM 使用者或 IAM 角色)。
角色類型存取控制
根據角色限制存取。OAuth 使用角色標籤;IAM 使用角色 ARN 模式。
OAuth:使用角色標籤
僅允許具有特定角色的使用者存取:
permit( principal is AgentCore::OAuthUser, action == AgentCore::Action::"AdminAPI___delete_resource", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/admin" ) when { principal.hasTag("role") && (principal.getTag("role") == "admin" || principal.getTag("role") == "manager") };
使用案例:僅允許具有管理員或管理員角色的使用者執行管理操作。
IAM:使用 IAM 角色 ARNs
僅允許使用特定 IAM 角色的來電者存取。您可以使用完全principal ==相符或principal.id like模式相符:
// Exact match (recommended for single-role policies) permit( principal == AgentCore::IamEntity::"arn:aws:sts::123456789012:assumed-role/AdminRole", action == AgentCore::Action::"AdminAPI___delete_resource", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/admin" );
使用案例:僅允許擔任 AdminRole IAM 角色的呼叫者執行管理操作。擔任角色的 Cedar 實體 ID 使用 格式 arn:aws:sts::<account>:assumed-role/<role-name>。
使用模式比對的變化:
// Match a specific role from any account principal.id like "arn:aws:sts::*:assumed-role/AdminRole" // Match any role in a specific account principal.id like "arn:aws:sts::123456789012:assumed-role/*"
資料類型操作
Cedar 支援條件中的各種資料類型。這些範例使用 OAuth 主體 AgentCore::OAuthUser ()。對於 IAM 驗證的閘道,請AgentCore::IamEntity改用 - 輸入驗證邏輯保持不變。
整數 (長)
// Check if passenger count is exactly 2 permit( principal is AgentCore::OAuthUser, action == AgentCore::Action::"TravelAPI___search_flights", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/travel" ) when { context.input.passengers == 2 };
Strings
// Check if payment method is credit card permit( principal is AgentCore::OAuthUser, action == AgentCore::Action::"PaymentAPI___process_payment", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/payment" ) when { context.input.paymentMethod == "credit-card" };
清單 (集合)
// Check if country is in allowed list permit( principal is AgentCore::OAuthUser, action == AgentCore::Action::"ShippingAPI___calculate_rate", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/shipping" ) when { ["US", "CA", "MX"].contains(context.input.country) };
檢查選用欄位
// Require optional field to be present permit( principal is AgentCore::OAuthUser, action == AgentCore::Action::"OrderAPI___create_order", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/order" ) when { context.input has shippingAddress };