

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

# 策略生成：按策略验证
<a name="policy-generation-validation"></a>

使用策略创作服务从自然语言生成策略时，会在生成**过程中对**每个策略进行验证和分析。

## 工作原理
<a name="policy-generation-validation-how"></a>

1. 自然语言被转换为 Cedar 政策

1. 生成的每个策略都要根据网关架构进行验证

1. 分别对每项策略进行分析

1. 结果可在生成响应中找到

## 示例：生成并验证策略
<a name="policy-generation-validation-example"></a>

检索生成结果分为两个步骤：首先检查生成状态，然后列出生成的资产以查看策略及其验证结果。

开始策略生成：

```
aws bedrock-agentcore-control start-policy-generation \
  --policy-engine-id MyEngine-abc123 \
  --name RefundPolicy \
  --content '{
    "rawText": "Allow customer service agents to process refunds up to 500 dollars for orders placed within the last 30 days"
  }' \
  --resource '{
    "arn": "arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/MyGateway-xyz789"
  }'
```

响应包括策略生成 ID 和状态：

```
{
  "policyGenerationId": "RefundPolicy-def456",
  "policyEngineId": "MyEngine-abc123",
  "status": "GENERATING"
}
```

使用`get-policy-generation`以下命令检查生成状态：

```
aws bedrock-agentcore-control get-policy-generation \
  --policy-engine-id MyEngine-abc123 \
  --policy-generation-id RefundPolicy-def456
```

响应显示了总体生成状态：

```
{
  "policyGenerationId": "RefundPolicy-def456",
  "status": "GENERATED",
  "statusReasons": []
}
```

状态变为后`GENERATED`，列出生成的资产以检索策略及其每项政策的验证结果：

```
aws bedrock-agentcore-control list-policy-generation-assets \
  --policy-engine-id MyEngine-abc123 \
  --policy-generation-id RefundPolicy-def456
```

回应包括每项生成的政策及其Cedar定义和验证结果：

```
{
  "policyGenerationAssets": [
    {
      "policyGenerationAssetId": "asset-1",
      "definition": {
        "cedar": {
          "statement": "permit(\n  principal is AgentCore::OAuthUser,\n  action == AgentCore::Action::\"RefundTool___process_refund\",\n  resource == AgentCore::Gateway::\"arn:aws:bedrock-agentcore:us-east-1:123456789012:gateway/MyGateway-xyz789\"\n) when {\n  context.input.amount <= 500\n};"
        }
      },
      "findings": [
        {
          "type": "VALID"
        }
      ],
      "rawTextFragment": "Allow customer service agents to process refunds up to 500 dollars"
    },
    {
      "policyGenerationAssetId": "asset-2",
      "definition": {
        "cedar": {
          "statement": "permit(\n  principal,\n  action == AgentCore::Action::\"RefundTool___view_order_history\",\n  resource\n);"
        }
      },
      "findings": [
        {
          "type": "ALLOW_ALL",
          "description": "Overly Permissive: Policy Engine will allow every request for the specified principal (AgentCore::OAuthUser), action (RefundTool___view_order_history) and resource (gateway/*) combination if the policy is added or updated"
        }
      ],
      "rawTextFragment": "Allow customer service agents to view order history"
    }
  ]
}
```

## 每项策略的验证结果
<a name="policy-generation-findings"></a>

生成的每个策略资产都包含一个`Finding`对象`findings`数组，每个对象都有一个`type`和`description`。以下示例显示了不同的查找类型：

通过验证和分析的策略：

```
{
  "findings": [
    {
      "type": "VALID"
    }
  ]
}
```

一项被标记为过于宽松的政策：

```
{
  "findings": [
    {
      "type": "ALLOW_ALL",
      "description": "Overly Permissive: Policy Engine will allow every request for the specified principal (AgentCore::OAuthUser), action (RefundTool___view_order_history) and resource (gateway/*) combination if the policy is added or updated"
    }
  ]
}
```

无法通过自然语言输入生成的策略：

```
{
  "findings": [
    {
      "type": "NOT_TRANSLATABLE",
      "description": "Unsupported Condition Error: The request includes conditions that rely on data or attributes currently not supported."
    }
  ]
}
```

## 生成的策略的常见发现
<a name="policy-common-findings"></a>

下表描述了生成的策略可以返回的查找类型：


| 调查发现类型 | 严重性 | 说明 | 推荐操作 | 
| --- | --- | --- | --- | 
|  `VALID`  | 成功 | Cedar 的政策是有效的，没有发现任何结果。未返回该发现类型的描述。 | 无需操作。该政策已准备就绪，可供使用。 | 
|  `INVALID`  | 错误 | 生成的 Cedar 策略包含语法错误或不符合网关架构。 | 查看生成的策略中是否存在架构违规或语法问题。改写自然语言输入并重新生成。 | 
|  `NOT_TRANSLATABLE`  | 错误 | 自然语言无法转换为有效的 Cedar。该请求可能包含依赖于不支持的数据或属性的条件。 | 仔细检查目标网关资源以了解工具定义。 | 
|  `ALLOW_ALL`  | 警告 | 许可政策适用于所有主体、行动和资源组合。 | 确认无限制访问是有意的。如果没有，请添加条件以限制范围。 | 
|  `ALLOW_NONE`  | 警告 | 许可政策不是决定性的，因为它不允许任何东西。 | 查看政策条件。该政策可能包含矛盾或无法实现的条件。 | 
|  `DENY_ALL`  | 警告 | 政策拒绝所有委托人采取所有行动。 | 确认完全拒绝是有意的。由于禁止覆盖-许可语义，这会覆盖所有许可策略。 | 
|  `DENY_NONE`  | 警告 | 禁止政策不是决定性的，因为它不否认任何内容。 | 查看政策条件。禁令政策可能包含矛盾或无法实现的条件。 | 