本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
編寫暫時政策
您使用 Dogwood 政策語言撰寫暫時政策,並將其新增至政策引擎,就像在 AgentCore 中為政策建立任何其他政策一樣。暫時政策是 permit或 forbid規則,其工作階段感知條件會放置在temporal區塊中;規則套用的主體、動作和資源會使用標準(principal, action, resource)範圍撰寫,與任何其他政策相同。下列各節說明如何建立暫時政策,並逐步解說您可以表達的常見模式。
建立時間政策
您可以使用 create-policy操作、與其他政策相同的操作來建立暫時政策,並將其連接至政策引擎。時間政策的陳述式在定義policy中低於 ,而不是在 下cedar作為無狀態 Cedar 政策。
下列 AWS CLI 範例會在政策引擎上建立時間政策:
aws bedrock-agentcore-control create-policy \ --policy-engine-id my-policy-engine-id \ --name TransferToLookedUpAccount \ --validation-mode FAIL_ON_ANY_FINDINGS \ --definition '{ "policy": { "statement": "permit (principal, action == AgentCore::Action::\"FundsTarget___transfer_funds\", resource == AgentCore::Gateway::\"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway\") when temporal { formerly within 1h AgentCore::Action::\"FundsTarget___get_account_balance\"::response{ eventResource: resource, output.accountId: context.input.toAccount } };" } }'
您也可以使用自然語言描述時間政策,而不是自行撰寫 Dogwood 陳述式,來建立時間政策。
事件結構描述:您可以參考的欄位
temporal { } 區塊內的條件使用暫時事件述詞來比對目前在工作階段中記錄的特定事件 (最多包含目前正在授權的動作)。述詞會命名時段、動作和事件類型,以及相符事件的一組欄位限制。上一節中create-policy的範例使用一個述詞 formerly within 1h AgentCore::Action::"FundsTarget___get_account_balance"::response{ eventResource: resource, output.accountId: context.input.toAccount },這符合上一個小時內get_account_balanceresponse記錄的 ,其output.accountId等於目前請求的 toAccount。
若要撰寫述詞,您需要知道動作產生的事件以及每個事件攜帶的欄位,因為這些是述詞可以限制和關聯的欄位。本節說明該事件結構描述。
每個動作會產生最多三種類型的事件,以述詞 (::request、::response、) 中的事件類型命名::error:
-
request— 記錄每個授權請求。執行動作的輸入欄位。 -
response— 在工具成功傳回時記錄。執行動作的輸入和輸出欄位。 -
error— 在請求遭拒或工具傳回錯誤時記錄。執行動作的輸入欄位。此事件僅限歷史記錄。
時間事件結構描述會為每個動作 定義這些事件A。 …inputs(A)並…outputs(A)擴展至動作宣告的輸入和輸出欄位:
// Recorded for each authorized request. decision event <A>::request { ...inputs(A), eventPrincipal: principalType(A), eventResource: resourceType(A), requestId: String, pin sessionId: String = context.sessionId, } // Recorded when the tool returns successfully; carries inputs and outputs. event <A>::response { ...inputs(A), ...outputs(A), eventPrincipal: principalType(A), eventResource: resourceType(A), requestId: String, pin sessionId: String = context.sessionId, } // Recorded when the request is denied or the tool returns an error; history-only. event <A>::error { ...inputs(A), eventPrincipal: principalType(A), eventResource: resourceType(A), requestId: String, pin sessionId: String = context.sessionId, }
在述詞內文中,您可以參考相符事件的下列欄位:
| 欄位 | 說明 |
|---|---|
|
|
動作的輸入欄位。適用於 |
|
|
動作的輸出欄位。僅適用於 |
|
|
提出記錄請求的委託人。 |
|
|
一律將此設定為 |
若要將記錄的事件與目前請求建立關聯,請將其中一個欄位與目前請求的值進行比較,例如 context.input.<name>。
使用案例
以下是幾個時間政策的範例。
可用的工具
本節中的範例使用名為 的閘道目標FundsTarget,公開三個工具。在政策中,此處列出的輸入和輸出欄位會參考每個工具的動作名稱 FundsTarget___<tool-name>和 。
-
FundsTarget___get_account_balance -
擷取客戶的目前帳戶餘額。
-
輸入:
customerId(字串,必要)。 -
輸出:
status(字串)、customerId(字串)、accountId(字串)、balance(整數)。
-
-
FundsTarget___transfer_funds -
在帳戶之間轉移資金。
-
輸入:
fromAccount(字串、必要)、toAccount(字串、必要)、amount(整數、必要)。 -
輸出:
status(字串)、fromAccount(字串)、toAccount(字串)、amount(整數)。
-
-
FundsTarget___get_transaction_history -
擷取 帳戶的交易歷史記錄。
-
輸入:
accountId(字串、必要)、startDate(字串、選用)、endDate(字串、選用)。 -
輸出:
status(字串)、accountId(字串)。
-
範例:output-to-input完整性
此範例只允許客服人員將資金轉移到先前在相同工作階段中查詢的帳戶,以防止其轉移到其建立的帳戶。transfer_funds 只有在工作階段中稍早的get_account_balance回應傳回相同的帳戶時,政策才允許:
permit ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 1h AgentCore::Action::"FundsTarget___get_account_balance"::response{ eventResource: resource, output.accountId: context.input.toAccount } };
::response 述詞符合先前 的記錄回應get_account_balance。 output.accountId 是工具傳回的欄位,context.input.toAccount也是目前transfer_funds請求的目的地帳戶;要求它們將傳輸與先前查詢相等。
由於政策引擎預設拒絕,而且因為response只有在允許時才將 動作記錄為 ,因此您也授予 permit的純值,get_account_balance以便在工作階段中允許查詢並記錄為回應:
permit ( principal, action == AgentCore::Action::"FundsTarget___get_account_balance", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" );
在這兩個政策都就緒的情況下,工作階段中的請求決定如下:
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
拒絕 |
|
|
允許 |
|
|
拒絕 |
範例:工具排序
此範例僅在相同工作階段中較早執行先決條件動作之後,才允許 動作。下列政策get_account_balance僅在transfer_funds請求在過去五分鐘內發生時允許:
permit ( principal, action == AgentCore::Action::"FundsTarget___get_account_balance", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 5m AgentCore::Action::"FundsTarget___transfer_funds"::request{ eventResource: resource } };
::request 述詞符合工作階段中的先前transfer_funds請求。將此與 permit 的 配對,transfer_funds以便允許並記錄動作。在這兩個政策都就緒的情況下, get_account_balance 會被拒絕,直到 已在工作階段中執行transfer_funds:
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
拒絕 |
|
|
允許 |
範例:資料新鮮度
此範例僅在先決條件在緊密的時段內成功完成時,才允許 動作,讓過時的結果使許可過期。get_account_balance 只有在過去五分鐘內transfer_funds完成時,才允許:
permit ( principal, action == AgentCore::Action::"FundsTarget___get_account_balance", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 5m AgentCore::Action::"FundsTarget___transfer_funds"::response{ eventResource: resource } };
比對::response而非 ::request是與工具排序的差異:只有在動作成功完成時才會記錄response事件,因此此政策需要最近成功完成,而不只是先前的請求。視窗長度會設定完成的新鮮程度;一旦視窗通過,許可就會失效,直到先決條件再次執行為止。
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
拒絕 |
|
|
允許 |
|
|
拒絕 |
範例:以工作階段為基礎的速率限制
此範例以工作階段內固定數量的呼叫為工具上限。下列政策禁止在工作階段的五分鐘內transfer_funds呼叫超過三次:
forbid ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { exists (n: Long). (count for (t: Timepoint). where (formerly within 5m (AgentCore::Action::"FundsTarget___transfer_funds"::request{ eventResource: resource } && tp(t)))) == n && n > 3 };
count 表達式會計算過去五分鐘內在工作階段中記錄的transfer_funds請求,包括目前的請求;當該計數超過三個時,即適用禁止。將其與 permit 的 配對transfer_funds,以便允許最多呼叫限制。在這兩個政策都就緒的情況下,允許任何五分鐘時段的前三個transfer_funds呼叫,並且拒絕該時段的第四個 (或更新版本) 呼叫。
重要
此限制僅適用於單一工作階段,因此它不是針對決定呼叫者的安全控制。由於發起人提供工作階段 ID,他們可以透過啟動新的工作階段來重設計數。使用此模式來塑造合作工作階段中的行為,而不是對控制自己的工作階段 ID 的發起人強制執行硬性限制。如需詳細資訊,請參閱安全性考量。
範例:one-time-use
此範例讓每個核准都適合單次使用。只有在工作階段中自上次 get_account_balance(核准) 以來transfer_funds沒有完成時,transfer_funds才允許 。一旦轉移完成,就會耗用核准,並拒絕下一次轉移,直到發生新的核准為止:
permit ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { !AgentCore::Action::"FundsTarget___transfer_funds"::response{ eventResource: resource } since within 1h AgentCore::Action::"FundsTarget___get_account_balance"::response{ eventResource: resource } };
當完成 get_account_balance(核准) 前一個小時內發生,而且該核准之後沒有完成時,此since條件transfer_funds會保留。比對::response至關重要:只有在成功之後,傳輸才會視為已完成,因此授權的請求不會封鎖本身。將此與 permit 的 配對,get_account_balance以便記錄核准。
工作階段中的請求決定如下:
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
允許 |
|
|
拒絕 |
|
新的 |
允許 |
注意
工具response的事件會在通話完成後不久記錄。等待 get_account_balance(核准) 請求完成並加以response記錄,再發出下一個 transfer_funds,而不是發回。如需詳細資訊,請參閱依先前回應排序動作。
範例:累積預算
此範例會限制視窗內動作的總值。下列政策禁止過去五分鐘內工作階段傳輸的amount輸入transfer_funds總和達到 3000:
forbid ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { exists (total: Long). (sum amt for (amt: Long), (t: Timepoint). where (formerly within 5m (AgentCore::Action::"FundsTarget___transfer_funds"::request{ eventResource: resource, input.amount: amt } && tp(t)))) == total && total >= 3000 };
sum 表達式會加總視窗中相符transfer_funds請求的amount輸入欄位,包括目前的請求;當總計達到閾值時,即套用禁止。加總欄位是 動作的輸入欄位。將政策與適用於 permit的 配對transfer_funds。例如,如果閾值為 3000 且傳輸量為 1000,則允許前兩者,而第三個會達到 3000,則會遭到拒絕。
與速率限制一樣,總和的範圍為目前工作階段,不會跨工作階段彙總。
範例:冷卻
此範例會強制執行冷卻時間:動作無法在上次完成的固定期間內重複。它禁止transfer_funds在最後一分鐘內transfer_funds完成 :
forbid ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 1m AgentCore::Action::"FundsTarget___transfer_funds"::response{ eventResource: resource } };
此條件為自我參考:符合授權的相同動作。比對::response是它運作的原因,因為授權的請求尚未產生回應,因此它本身並不相符。::request 此處的比對會使目前的請求符合自己的事件,並且將永久禁止該動作。視窗經過而沒有新的完成之後,就會再次允許 動作。
| 工作階段中的請求序列 | 決策 |
|---|---|
|
首先 |
允許 |
|
|
拒絕 |
|
|
允許 |
範例:連續先決條件
此範例僅在先決條件保留時允許 動作:最近發生正面確認,且之後沒有任何動作使它失效。transfer_funds 只有在 get_account_balance(確認) 在過去五分鐘內完成,但未完成 get_transaction_history(失效) 之後,才允許:
permit ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { !AgentCore::Action::"FundsTarget___get_transaction_history"::response{ eventResource: resource } since within 5m AgentCore::Action::"FundsTarget___get_account_balance"::response{ eventResource: resource } };
此since條件會保留過去五分鐘內get_account_balance完成且之後未get_transaction_history完成的時間。已完成 會get_account_balance確認先決條件,並要求 get_transaction_history 沒有發生,因為 之後不會使它失效。授予 get_account_balance和 的許可,get_transaction_history以便記錄它們。
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
拒絕 |
|
|
允許 |
|
|
拒絕 |
|
新的 |
允許 |
範例:多跳轉鏈
您可以編寫數個排序政策來要求動作鏈,每個動作只允許在上一個動作鏈完成後。此範例需要使用transfer_funds兩個政策的鏈結 get_account_balance → get_transaction_history → (每個連結一個):
// Link 1: permit get_transaction_history only after get_account_balance completed permit ( principal, action == AgentCore::Action::"FundsTarget___get_transaction_history", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 5m AgentCore::Action::"FundsTarget___get_account_balance"::response{ eventResource: resource } }; // Link 2: permit transfer_funds only after get_transaction_history completed permit ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 5m AgentCore::Action::"FundsTarget___get_transaction_history"::response{ eventResource: resource } };
每個政策都會強制執行一個連結,而鏈會從其合成中出現: transfer_funds需要 get_transaction_history,而 需要 get_account_balance。permit 為鏈結中的第一個動作授予 ,使其可以開始。在先決條件完成之前,會拒絕嘗試不按順序執行的步驟。
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
拒絕 |
|
|
每個步驟允許 |
範例:交互排除
此範例會讓兩個動作在視窗中互斥:其中任何一個執行會先封鎖另一個執行。它使用兩個對稱禁止政策,因此排除會保留兩個方向。在這裡, transfer_funds和 get_transaction_history不能在兩分鐘內發生:
// Forbid get_transaction_history if a transfer_funds was requested within 2m forbid ( principal, action == AgentCore::Action::"FundsTarget___get_transaction_history", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 2m AgentCore::Action::"FundsTarget___transfer_funds"::request{ eventResource: resource } }; // Forbid transfer_funds if a get_transaction_history was requested within 2m forbid ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 2m AgentCore::Action::"FundsTarget___get_transaction_history"::request{ eventResource: resource } };
由於每個政策在 上都相符::request,即使請求一個動作也會封鎖另一個動作,因此區塊不會等待第一個動作完成。您需要兩個對稱forbid政策,每個方向一個:一個在transfer_funds請求get_transaction_history後禁止,另一個在get_transaction_history請求transfer_funds後禁止。單一禁止只會封鎖一個訂單。將兩者與兩個動作的允許配對。
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
transfer ALLOW、歷程記錄 DENY |
|
|
歷史記錄允許、轉移 DENY |
範例:結合暫時、護欄和雪松條件
單一政策可以將時間條件與護欄和標準 Cedar 條件結合;所有條件都必須滿足才能套用政策。此範例transfer_funds僅在累積轉移金額保持在上限 (暫時) 之下、請求不包含敏感資訊 (護欄),且發起人不在封鎖的群組 (Cedar) 中時允許:
permit ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { exists (total: Long). (sum amt for (amt: Long), (t: Timepoint). where (formerly within 24h (AgentCore::Action::"FundsTarget___transfer_funds"::request{ eventResource: resource, input.amount: amt } && tp(t)))) == total && total < 60000 } when { BedrockGuardrails::SensitiveInformation(["ACCOUNT_NUMBER"], [context.input.body]).count() == 0 } unless { principal in Group::"blocked_users" };
暫時區塊會強制執行累積上限,護欄區塊會封鎖包含所列敏感資訊的請求,而 Cedar unless區塊會排除封鎖的主體。每個條件類型都會獨立評估,且只有在所有條件類型都保留時才套用許可。如需護欄條件語法,請參閱 政策中的護欄;暫時區塊的行為如上述範例所述。
範例:平行先決條件
此範例需要兩個先決條件,才能以任何順序完成動作。get_account_balance 只有在 transfer_funds和 都在過去一小時內get_transaction_history完成時,才會允許 ,並結合兩個formerly條件與 &&:
permit ( principal, action == AgentCore::Action::"FundsTarget___get_account_balance", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 1h AgentCore::Action::"FundsTarget___transfer_funds"::response{ eventResource: resource } && formerly within 1h AgentCore::Action::"FundsTarget___get_transaction_history"::response{ eventResource: resource } };
這兩個先決條件都必須在 視窗中完成 (::response),而且順序並不重要。授予兩個先決條件動作的許可,以便記錄這些動作。僅完成一個動作會使動作遭拒,直到另一個動作也完成為止。
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
拒絕 |
|
只完成一個先決條件,然後 |
拒絕 |
|
兩個先決條件都已完成,然後 |
允許 |
範例:核准閾值
此範例僅在限定事件的閾值數量之後,才允許 動作。只有在至少transfer_funds完成兩個客戶帳戶,並將轉移的 toAccount與餘額請求的 相關聯時,才get_account_balance允許客戶使用 customerId:
permit ( principal, action == AgentCore::Action::"FundsTarget___get_account_balance", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { exists (n: Long). (count for (t: Timepoint). where (formerly within 5m (AgentCore::Action::"FundsTarget___transfer_funds"::response{ eventResource: resource, input.toAccount: context.input.customerId } && tp(t)))) == n && n >= 2 };
count 表達式會計算視窗中相符的已完成事件,並在計數達到閾值時允許 動作。
注意
count 計算相符事件,而不是不同的主體。它無法強制執行事件來自不同的發起人,因此它表示「N 事件」閾值,而不是 N 不同方的多方核准。
| 比對已完成的 帳戶轉移 |
get_account_balance
|
|---|---|
|
少於 2 |
拒絕 |
|
2 或以上 |
允許 |
範例:在先前的拒絕之後封鎖動作
當相同工作階段中較早的工具呼叫遭拒時,此範例會封鎖敏感動作。拒絕的請求會記錄為error事件,而且::error述詞符合這類事件。下列政策禁止transfer_funds在過去三分鐘內拒絕工作階段get_account_balance中的 :
forbid ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" ) when temporal { formerly within 3m AgentCore::Action::"FundsTarget___get_account_balance"::error{ eventResource: resource } };
forbid 規則會覆寫任何 permit,因此將其與transfer_funds在正常情況下允許 permit 的 配對:
permit ( principal, action == AgentCore::Action::"FundsTarget___transfer_funds", resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:us-west-2:123456789012:gateway/my-gateway" );
在這兩個政策都就緒的情況下,工作階段中的請求決定如下:
| 工作階段中的請求序列 | 決策 |
|---|---|
|
|
允許 |
|
|
拒絕 |