

# Resource-based Amazon Bedrock 的政策 AgentCore
<a name="resource-based-policies"></a>

Resource-based Amazon Bedrock 中的策略 AgentCore 允许您控制哪些委托人（AWS 账户、IAM 用户或 IAM 角色）可以调用和管理您的 Amazon Bedrock AgentCore 资源（目前支持运行时、网关和内存）。您可以将 IAM-style 策略直接附加到您的资源，以定义有关谁可以启动运行时会话、调用网关、访问内存或执行其他管理和调用操作的规则。

Resource-based 策略与基于身份的 IAM 策略配合使用，可为您的 Amazon Bed AgentCore rock 资源提供访问控制。虽然基于身份的策略附加到 IAM 身份并指定他们可以执行的操作，但基于资源的策略直接附加到资源并指定谁可以访问这些资源。

**Topics**
+ [支持的资源](#resource-based-policies-supported-resources)
+ [基于资源的政策是如何运作的](#resource-based-policies-how-they-work)
+ [策略结构](#resource-based-policies-structure)
+ [支持的操作](#resource-based-policies-supported-actions)
+ [条件键](#resource-based-policies-condition-keys)
+ [常见用例和示例](#resource-based-policies-examples)
+ [管理资源策略](#resource-based-policies-managing)
+ [安全最佳实践](#resource-based-policies-best-practices)
+ [问题排查](#resource-based-policies-troubleshooting)

## 支持的资源
<a name="resource-based-policies-supported-resources"></a>

Amazon Bedrock AgentCore 支持针对以下资源的基于资源的策略：
+  **代理运行时和代理端点**-控制对代理调用和管理操作的访问权限
+  **网关**-控制对网关调用操作的访问权限
+  **内存**-控制对内存操作的访问

## 基于资源的政策是如何运作的
<a name="resource-based-policies-how-they-work"></a>

### Identity-based 与基于资源的策略
<a name="resource-based-policies-vs-identity-based"></a>


| 方面 | Identity-Based 政策 | Resource-Based 政策 | 
| --- | --- | --- | 
| 附件 | 附加到 IAM 用户、角色或群组 | 直接附加到 Amazon Bedrock 资源 AgentCore  | 
| 管理 | 通过 AWS IAM 进行管理 | 通过 Amazon Bedrock API AgentCore 进行管理 | 
| 指定 | 行动和资源（主体是隐含的） | 委托人、操作和条件（资源是隐式的） | 
| 使用场景 | 定义身份能做什么 | 定义谁可以访问资源 | 

### 策略评估
<a name="resource-based-policies-evaluation"></a>

向 Amazon Bedrock AgentCore 资源提出请求时， AWS 会评估基于身份和基于资源的策略。下表显示了不同的策略组合如何影响访问权限：


| IAM 策略 | 资源策略 | 结果 | 
| --- | --- | --- | 
| 授予访问权限 | 无提示 | 允许 | 
| 授予访问权限 | 授予访问权限 | 允许 | 
| 授予访问权限 | 拒绝访问 | 已拒绝 | 
| 无提示 | 无提示 | 已拒绝 | 
| 无提示 | 授予访问权限 | 允许 | 
| 无提示 | 拒绝访问 | 已拒绝 | 
| 拒绝访问 | 无提示 | 已拒绝 | 
| 拒绝访问 | 允许访问 | 已拒绝 | 
| 拒绝访问 | 拒绝访问 | 已拒绝 | 

关键原则：
+  **显式拒绝永远获胜**：如果任何策略明确拒绝该操作，则无论采用何种其他策略，都将拒绝访问权限
+  **任一策略均可允许**：如果基于身份或基于资源的策略允许该操作（且没有策略拒绝该操作），则授予访问权限
+  **默认拒绝**：如果没有策略明确允许某项操作，则访问将被拒绝

### 代理运行时和端点的分层授权
<a name="resource-based-policies-hierarchical-authorization"></a>

代理端点是代理运行时特定版本的可寻址接入点。每个端点都指向运行时配置的特定版本，DEFAULT 端点会自动路由到最新版本。在授权运行时 API 操作（如`InvokeAgentRuntime`和）时`InvokeAgentRuntimeCommand`， AWS 会评估代理运行时和正在调用的代理端点的基于身份和基于资源的策略。

要使请求获得授权，必须满足以下条件：
+ 附加到调用主体的基于身份的策略必须允许对代理运行时资源和代理端点资源执行操作
+ 代理运行时上基于资源的策略必须允许该操作（如果存在策略）
+ 代理端点上的基于资源的策略必须允许该操作（如果存在策略）

**重要**  
**要向委托人提供跨账户访问权限，您必须创建基于资源的策略，授予代理运行时和代理端点的访问权限。**如果任一资源拒绝访问或缺少明确的 allow 语句，则请求将被拒绝。

示例：授予跨账户访问权限需要对这两个资源采取策略：

```
// Policy for Agent Runtime (attached to
// arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID)
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/CrossAccountRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID"
        }
    ]
}

// Policy for Agent Endpoint (attached to
// arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID/endpoint/ENDPOINTID)
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/CrossAccountRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID/endpoint/ENDPOINTID"
        }
    ]
}
```

### 身份验证类型注意事项
<a name="resource-based-policies-authentication-types"></a>

编写基于资源的策略的方式取决于为代理运行时或网关配置的身份验证类型：

Sigv4 身份验证  
在`Principal`元素中使用特定的 AWS 委托人（IAM 用户、角色或账户）。例如：`"Principal": {"AWS": "arn:aws:iam::123456789012:role/MyRole"}`。该策略与调用者的 IAM 权限一起评估。有关将运行时限制为只能由网关调用的示例，请参阅[限制对您的 AgentCore 网关进行 IAM (Sigv4) 入站调用](runtime-oauth.md#runtime-restrict-iam-gateway)。

OAuth 身份验证  
在政策声明中必须使用通配符主体（“主体”：“\*”）。OAuth 令牌在策略评估之前由 AWS 身份服务验证。只有拥有来自注册身份提供商 (IdP) 的有效 JWT 令牌的经过身份验证的 OAuth 用户才能调用该资源。在策略评估之前，匿名或未经身份验证的请求会被拒绝。使用条件键限制访问权限（例如`aws:SourceVpc`，`aws:SourceVpce`）。

**重要**  
代理运行时或网关只能在创建时使用 Sigv4 或 OAuth 身份验证进行配置，不能同时配置两者。这意味着一个基于资源的策略仅适用于一种身份验证类型。

## 策略结构
<a name="resource-based-policies-structure"></a>

基于资源的策略是具有以下结构的 JSON 文档：

```
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "StatementId",
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::account-id:role/role-name"
            },
            "Action": "bedrock-agentcore:ActionName",
            "Resource": "arn:aws:bedrock-agentcore:region:account-id:resource-type/resource-id",
            "Condition": {
                "ConditionOperator": {
                    "ConditionKey": "ConditionValue"
                }
            }
        }
    ]
}
```

**重要**  
策略文档中的`Resource`字段必须包含策略所关联资源的确切 ARN。使用 “资源”：不支持 “\*”，这将导致验证错误。

## 支持的操作
<a name="resource-based-policies-supported-actions"></a>

### 代理运行时操作
<a name="resource-based-policies-runtime-actions"></a>
+  `bedrock-agentcore:InvokeAgentRuntime`-调用代理运行时
+  `bedrock-agentcore:InvokeAgentRuntimeForUser`-调用带有 X-Amzn-Bedrock-AgentCore-Runtime-User-Id 标头的代理运行时端点
+  `bedrock-agentcore:InvokeAgentRuntimeCommand`-在活动的运行时会话中执行 shell 命令
+  `bedrock-agentcore:InvokeAgentRuntimeCommandShell`-在活动运行时会话中打开交互式 WebSocket shell 会话
+  `bedrock-agentcore:InvokeAgentRuntimeWithWebSocketStream`-使用 WebSocket 流调用代理运行时
+  `bedrock-agentcore:InvokeAgentRuntimeWithWebSocketStreamForUser`-使用带 X-Amzn-Bedrock-AgentCore-Runtime-User-Id 标头的 WebSocket 流调用代理运行时
+  `bedrock-agentcore:StopRuntimeSession`-停止处于活动状态的运行时会话
+  `bedrock-agentcore:GetAgentCard`-检索代理卡信息

### 网关操作
<a name="resource-based-policies-gateway-actions"></a>
+  `bedrock-agentcore:InvokeGateway`-调用网关

### 记忆动作
<a name="resource-based-policies-memory-actions"></a>
+  `bedrock-agentcore:GetMemory`-检索内存资源
+  `bedrock-agentcore:UpdateMemory`-更新内存资源
+  `bedrock-agentcore:DeleteMemory`-删除内存资源
+  `bedrock-agentcore:CreateEvent`-在内存资源中创建事件
+  `bedrock-agentcore:GetEvent`-从内存资源中检索事件
+  `bedrock-agentcore:DeleteEvent`-从内存资源中删除事件
+  `bedrock-agentcore:ListEvents`-列出来自内存资源的事件
+  `bedrock-agentcore:ListActors`-列出内存资源中的演员
+  `bedrock-agentcore:ListSessions`-列出来自内存资源的会话
+  `bedrock-agentcore:GetMemoryRecord`-从内存资源中获取内存记录
+  `bedrock-agentcore:ListMemoryRecords`-列出内存资源中的内存记录
+  `bedrock-agentcore:RetrieveMemoryRecords`-从内存资源中搜索内存记录
+  `bedrock-agentcore:DeleteMemoryRecord`-从内存资源中删除内存记录
+  `bedrock-agentcore:BatchCreateMemoryRecords`-在内存资源中批量创建内存记录
+  `bedrock-agentcore:BatchUpdateMemoryRecords`-批量更新内存资源中的内存记录
+  `bedrock-agentcore:BatchDeleteMemoryRecords`-批量删除内存资源中的内存记录
+  `bedrock-agentcore:StartMemoryExtractionJob`-在内存资源中启动提取作业
+  `bedrock-agentcore:ListMemoryExtractionJobs`-列出内存资源中的提取任务

## 条件键
<a name="resource-based-policies-condition-keys"></a>

您可以使用条件密钥来进一步完善策略中的访问控制。有关可用条件键的完整列表，请参阅 B [edrock AgentCore 条件键](https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonbedrockagentcore.html)和[AWS 全局条件上下文键](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)。

## 常见用例和示例
<a name="resource-based-policies-examples"></a>

本节提供了常见场景中基于资源的策略的实际示例。每个示例中的`Resource`字段都必须包含策略所关联资源的确切 ARN。将示例 ARN 替换为实际资源 ARN。

### 允许在其他角色中扮演角色 AWS 账户
<a name="resource-based-policies-cross-account-example"></a>

向不同 AWS 账户中的特定角色授予 API 访问权限：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": [
                    "arn:aws:iam::123456789012:role/DeveloperRole",
                    "arn:aws:iam::123456789012:role/AdminRole"
                ]
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID"
        }
    ]
}
```

### 根据源 IP 地址拒绝流量
<a name="resource-based-policies-ip-restriction-example"></a>

阻止来自特定 IP 地址范围的传入流量：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/ApplicationRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID"
        },
        {
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/ApplicationRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID",
            "Condition": {
                "IpAddress": {
                    "aws:SourceIp": [
                        "192.0.2.0/24",
                        "198.51.100.0/24"
                    ]
                }
            }
        }
    ]
}
```

### 仅允许来自特定 VPC 的流量
<a name="resource-based-policies-vpc-restriction-example"></a>

限制对来自特定 VPC 的请求的访问：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/ApplicationRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID"
        },
        {
            "Effect": "Deny",
            "Principal": {
                "AWS": "arn:aws:iam::123456789012:role/ApplicationRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID",
            "Condition": {
                "StringNotEquals": {
                    "aws:SourceVpc": "vpc-1a2b3c4d"
                }
            }
        }
    ]
}
```

### 具有 VPC 限制的 OAuth 身份验证
<a name="resource-based-policies-oauth-example"></a>

如果您的代理运行时或网关配置了 OAuth 身份验证，则必须使用通配符主体。此示例将 OAuth-authenticated 请求限制到特定 VPC：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowOAuthFromVPC",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID",
            "Condition": {
                "StringEquals": {
                    "aws:SourceVpc": "vpc-1a2b3c4d"
                }
            }
        }
    ]
}
```

**重要**  
OAuth 身份验证**需要**使用通配符主体（“主体”：“\*”）。OAuth 令牌在策略评估之前由 AWS 身份服务验证。只有拥有您注册的身份提供商提供的有效 JWT 令牌的用户才能访问该资源。匿名或未经身份验证的请求在达到策略评估之前会被拒绝。使用条件键（例如`aws:SourceVpc`，`aws:SourceVpce`）进一步限制访问权限

## 管理资源策略
<a name="resource-based-policies-managing"></a>

选择以下方法之一：

**Example**  

1. ====== 创建或更新资源策略

   使用 `put-resource-policy` 命令：

   ```
   aws bedrock-agentcore-control put-resource-policy \
       --resource-arn arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID \
       --policy file://policy.json
   ```

    **获取资源政策** 

   使用 `get-resource-policy` 命令：

   ```
   aws bedrock-agentcore-control get-resource-policy \
       --resource-arn arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
   ```

    **删除资源策略** 

   使用 `delete-resource-policy` 命令：

   ```
   aws bedrock-agentcore-control delete-resource-policy \
       --resource-arn arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
   ```

1. 以下示例展示了如何使用 AWS Python 软件开发工具包 (Boto3) 管理资源策略：

   ```
   import boto3
   import json
   
   client = boto3.client('bedrock-agentcore-control', region_name='us-west-2')
   
   # Define the resource ARN
   resource_arn = 'arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID'
   
   # Put resource policy
   # Note: The Resource field must match the resource ARN to which the policy is attached
   policy = {
   "Version": "2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": {"AWS": "arn:aws:iam::123456789012:role/MyRole"},
               "Action": "bedrock-agentcore:InvokeAgentRuntime",
               "Resource": resource_arn
           }
       ]
   }
   
   response = client.put_resource_policy(
       resourceArn=resource_arn,
       policy=json.dumps(policy)
   )
   
   # Get resource policy
   response = client.get_resource_policy(
       resourceArn='arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID'
   )
   print(response['policy'])
   
   # Delete resource policy
   response = client.delete_resource_policy(
       resourceArn='arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID'
   )
   ```

## 安全最佳实践
<a name="resource-based-policies-best-practices"></a>

### 授予最低权限
<a name="resource-based-policies-least-privilege"></a>

仅授予您的用例所需的最低权限：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::111122223333:role/ApplicationRole"
            },
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID"
        }
    ]
}
```

### 防止副手感到困惑
<a name="resource-based-policies-confused-deputy"></a>

在授予 AWS 服务访问权限时，请务必使用条件键：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:gateway/GATEWAYID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "Service": "lambda.amazonaws.com"
            },
            "Action": "bedrock-agentcore:InvokeGateway",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:gateway/GATEWAYID",
            "Condition": {
                "StringEquals": {
                    "aws:SourceAccount": "111122223333"
                },
                "ArnEquals": {
                    "aws:SourceArn": "arn:aws:lambda:us-west-2:111122223333:function/SpecificFunction"
                }
            }
        }
    ]
}
```

### 对关键控制使用显式拒绝
<a name="resource-based-policies-explicit-deny"></a>

对安全关键限制使用显式拒绝声明：

```
// Policy attached to arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID
{
"Version": "2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "DenyAllExceptVPC",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "bedrock-agentcore:InvokeAgentRuntime",
            "Resource": "arn:aws:bedrock-agentcore:us-west-2:111122223333:runtime/AGENTID",
            "Condition": {
                "StringNotEquals": {
                    "aws:SourceVpc": "vpc-12345678"
                },
                "Bool": {
                    "aws:ViaAWSService": "false"
                }
            }
        }
    ]
}
```

## 问题排查
<a name="resource-based-policies-troubleshooting"></a>

### 访问被拒绝错误
<a name="resource-based-policies-access-denied"></a>

如果您收到 “访问被拒绝” 错误：
+  **同时检查这两个策略**：验证基于身份和基于资源的策略
+  **寻找显式拒绝**：任何策略中的明确拒绝都会覆盖所有允许的内容
+  **验证委托人 ARN**：确保策略中的委托人 ARN 与调用方匹配
+  **检查条件**：验证所有条件键的计算结果均为 true
+  **审查 SCP**：组织服务控制策略可以优先于资源策略

### 策略验证错误
<a name="resource-based-policies-validation-errors"></a>

常见的策略验证错误：
+  **JSON 无效**：确保您的策略是有效的 JSON
+  **ARN 格式无效**：验证所有 ARN 的格式是否正确
+  **不支持的操作**：检查资源类型是否支持所有操作
+  **缺少必需元素**：确保存在版本、陈述、效果、主体和操作