

# 了解 Cedar 政策
<a name="policy-understanding-cedar"></a>

AgentCore 中的政策使用 Cedar 政策來控制對 AgentCore Gateway 工具的存取。本節說明 Cedar 政策結構、評估語意和關鍵概念。

**Topics**
+ [範例 政策](#policy-example)
+ [政策結構](#policy-structure)
+ [政策效果](#policy-effects)
+ [預設拒絕](#policy-default-deny)
+ [授權評估](#policy-authorization-evaluation)
+ [政策獨立性](#policy-independence)
+ [政策評估演算法](#policy-evaluation-algorithm)

## 範例 政策
<a name="policy-example"></a>

請考慮具有以下要求的退款處理工具：
+ 只有使用者 "John" 可以處理退款
+ 退款上限為 500 美元或更少

強制執行這些要求的 Cedar 政策：

```
permit(
  principal is AgentCore::OAuthUser,
  action == AgentCore::Action::"RefundTool___process_refund",
  resource == AgentCore::Gateway::"arn:aws:bedrock-agentcore:your-region:your-account-id:gateway/refund-gateway"
)
when {
  principal.hasTag("username") &&
  principal.getTag("username") == "John" &&
  context.input.amount < 500
};
```

此政策僅在使用者為「John」且退款金額低於 500 美元時，才允許退款處理。

## 政策結構
<a name="policy-structure"></a>

Cedar 政策包含三個主要元件：

1.  **效果** - 決定是否允許或拒絕存取 ( `permit`或 `forbid` )

1.  **範圍** - 指定政策套用的委託人、動作和資源

1.  **條件** - 定義必須滿足的其他邏輯 ( `when`或 `unless` )，並可參考工具參數 （透過內容） 和 OAuth 字符 （透過標籤）

## 政策效果
<a name="policy-effects"></a>

Cedar 政策使用兩種效果來控制存取：
+  `permit` - 允許動作繼續
+  `forbid` - 拒絕動作

## 預設拒絕
<a name="policy-default-deny"></a>

預設會拒絕所有動作。如果沒有政策符合請求，Cedar 會傳回 DENY。您必須明確撰寫允許政策，以允許 動作。

## 授權評估
<a name="policy-authorization-evaluation"></a>

Cedar forbid-overrides-permit評估模型：

1. Cedar 會評估套用至請求的所有政策

1. 如果任何禁止政策相符，則結果為 DENY

1. 如果至少一個許可政策相符且沒有禁止政策相符，則結果為允許

1. 如果沒有相符的政策，則結果為 DENY （預設拒絕）

## 政策獨立性
<a name="policy-independence"></a>

每個 Cedar 政策都會獨立評估。政策的評估僅取決於：
+ 範圍 （委託人、動作、資源）
+ 內容和標籤

政策不參考或依賴其他政策。

## 政策評估演算法
<a name="policy-evaluation-algorithm"></a>

評估請求時，政策引擎會使用下列演算法來決定授權決策：

1. 如果任何`forbid`政策符合請求，則決策為 DENY。

1. 如果沒有`forbid`政策符合請求，且至少有一個`permit`政策符合，則決策為允許。

1. 如果 `forbid`或 `permit`政策都不符合請求，則決策為 DENY。

此評估模型會強制執行**預設拒絕**狀態。

`forbid` 政策永遠不會導致 ALLOW 決策。`forbid` 政策上的 `unless`子句指定政策`forbid`不適用的條件；它**不會**授予許可，也不會覆寫相符`permit`的政策。