

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 内存策略示例
<a name="memory-gateway-fgac-policy-examples"></a>

本页提供特定于 Amazon Bedrock M AgentCore emory 的 Cedar 策略模式——将呼叫者与自己的操作者、自己的命名空间和一组选定的内存操作隔离开来。有关这些策略引用的内存操作 ID 和`context.input`字段，请参阅[内存操作和请求属性](memory-gateway-fgac.md#memory-gateway-fgac-reference)。有关通用 Cedar 模型和更广泛的模式库（OAuth 范围和角色检查、IAM 委托人匹配以及输入验证），请参阅[策略范围](policy-scope.md)和[策略编写指南](policy-authoring-guide.md)。`forbid` `unless`

您可以通过[CreatePolicy](https://docs.amazonaws.cn/bedrock-agentcore-control/latest/APIReference/API_CreatePolicy.html)操作将每个 Cedar 策略添加到网关的策略引擎中，将 Cedar 语句作为策略传递`definition`。有关设置顺序，请参阅[为内存设置精细访问控制。](memory-gateway-fgac.md#memory-gateway-fgac-setup)

在每个 Cedar 示例中，`<target-name>`替换为连接器目标`<gw-arn>`名称和网关 ARN。

## Per-user 隔离 — actorID 必须等于 JWT 主体
<a name="memory-gateway-fgac-example-user-isolation"></a>

仅当请求`actorId`等于其 JWT 声明时，才允许 OAuth 用户访问会话。`sub`这是内存的核心每用户隔离模式：每个用户只能访问自己的 actor 数据。

```
permit(
  principal is AgentCore::OAuthUser,
  action == AgentCore::Action::"<target-name>___POST:/memories/{memoryId}/actor/{actorId}/sessions/{sessionId}",
  resource == AgentCore::Gateway::"<gw-arn>"
) when {
  principal.hasTag("sub") &&
  context has input && context.input has actorId &&
  context.input.actorId == principal.getTag("sub")
};
```

## 命名空间隔离 — 调用者只能检索自己的命名空间下的记录
<a name="memory-gateway-fgac-example-namespace-isolation"></a>

此策略允许 OAuth 调用者仅在请求与令牌声明中包含的调用者自己的命名空间路径`namespacePath`匹配时检索内存记录。

Cedar 不支持字符串连接——该`+`运算符仅适用于整数操作数，并且`like`通配符模式必须是字符串文字。因此，您无法根据策略内的声明构建命名空间值（例如，`("/actors/" + principal.getTag("sub") + "/")`Cedar 无效）。取而代之的是，让您的身份提供商发布已包含调用者完整命名空间路径的声明，将其映射到主体标签，然后与该标签进行比`namespacePath`较。在此示例中，令牌提供了包含调用者命名空间路径的`namespace`声明（例如`/actors/<sub>/`）。

```
permit(
  principal is AgentCore::OAuthUser,
  action == AgentCore::Action::"<target-name>___POST:/memories/{memoryId}/retrieve",
  resource == AgentCore::Gateway::"<gw-arn>"
) when {
  principal.hasTag("namespace") &&
  context has input && context.input has namespacePath &&
  context.input.namespacePath == principal.getTag("namespace")
};
```

## 操作范围-仅允许一组内存操作
<a name="memory-gateway-fgac-example-action-scoping"></a>

授予固定的内存操作列表，例如，允许列出事件和检索记录，但不允许其他操作。将多个内存操作分组为`action in […​]`；不支持 action-id 通配符。

```
permit(
  principal,
  action in [
    AgentCore::Action::"<target-name>___POST:/memories/{memoryId}/actor/{actorId}/sessions/{sessionId}",
    AgentCore::Action::"<target-name>___POST:/memories/{memoryId}/retrieve"
  ],
  resource == AgentCore::Gateway::"<gw-arn>"
);
```

为确保只能通过网关访问内存，而不能由主体直接调用内存数据平面访问，请参阅[限制对内存的直接访问](memory-gateway-restrict-access.md)。