View a markdown version of this page

Resource-based 亚马逊 Bedrock 政策 AgentCore - 亚马逊基岩 AgentCore

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

Resource-based 亚马逊 Bedrock 政策 AgentCore

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

Resource-based 策略与基于身份的 IAM 策略配合使用,为您的亚马逊 Bedro AgentCore ck 资源提供访问控制。虽然基于身份的策略附加到 IAM 身份并指定其可以执行的操作,但基于资源的策略直接附加到资源并指定谁可以访问它们。

支持的资源

Amazon Bedrock AgentCore 支持针对以下资源的基于资源的政策:

  • 代理运行时和代理端点 -控制对代理调用和管理操作的访问权限

  • 网关 -控制对网关调用操作的访问权限

  • 内存 -控制对内存操作的访问权限

基于资源的策略如何运作

Identity-based 与基于资源的策略对比

方面 Identity-Based 政策 Resource-Based 政策

附件

附加到 IAM 用户、角色或群组

直接附加到亚马逊 Bedrock 资源 AgentCore

管理

通过 AWS IAM 管理

通过亚马逊 Bedrock API AgentCore 进行管理

指定

行动和资源(主体是隐含的)

主体、行动和条件(资源是隐含的)

使用场景

定义身份能做什么

定义谁可以访问资源

策略评估

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

IAM 策略 资源策略 结果

授予访问权限

无提示

允许

授予访问权限

授予访问权限

允许

授予访问权限

拒绝访问

已拒绝

无提示

无提示

已拒绝

无提示

授予访问权限

允许

无提示

拒绝访问

已拒绝

拒绝访问

无提示

已拒绝

拒绝访问

允许访问

已拒绝

拒绝访问

拒绝访问

已拒绝

关键原则:

  • 明确拒绝永远是赢家:如果任何策略明确拒绝该操作,则无论其他策略如何,访问都将被拒绝

  • 任一策略均可允许:如果基于身份或基于资源的策略允许该操作(且没有任何策略拒绝),则授予访问权限

  • 默认拒绝:如果没有任何策略明确允许某项操作,则访问将被拒绝

代理运行时和端点的分层授权

代理端点是代理运行时特定版本的可寻址接入点。每个端点都指向运行时配置的特定版本,默认端点会自动路由到最新版本。授权运行时 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" } ] }

身份验证类型注意事项

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

SigV4 身份验证

在Principal元素中使用特定的 AWS 委托人(IAM 用户、角色或账户)。例如:"Principal": {"AWS": "arn:aws:iam::123456789012:role/MyRole"}。该政策是与调用者的 IAM 权限一起评估的。有关限制运行时只能由网关调用的示例,请参阅限制 IAM (SigV4) 对 AgentCore 网关的入站调用。

OAuth 身份验证

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

重要

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

策略结构

基于资源的策略是具有以下结构的 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。不支持使用 “资源”: “*”,这将导致验证错误。

支持的操作

代理运行时操作

  • 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 stream 调用代理运行时

  • bedrock-agentcore:InvokeAgentRuntimeWithWebSocketStreamForUser-使用带 X-Amzn-Bedrock-AgentCore-Runtime-User-Id 标头的 WebSocket 流调用代理运行时

  • bedrock-agentcore:StopRuntimeSession-停止活动的运行时会话

  • bedrock-agentcore:GetAgentCard-检索代理卡信息

网关操作

  • bedrock-agentcore:InvokeGateway-调用网关

记忆动作

  • 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-列出内存资源中的提取作业

条件键

您可以使用条件密钥进一步完善策略中的访问控制。有关可用条件键的完整列表,请参阅基岩 AgentCore 条件键和AWS 全局条件上下文键。

常见用例和示例

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

允许角色出现在另一个角色中 AWS 账户

向不同 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 地址拒绝流量

阻止来自特定 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 的流量

限制访问来自特定 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 身份验证

当您的代理运行时或网关配置为 OAuth 身份验证时,必须使用通配符主体。此示例限制了对特定 VPC 的 OAuth-authenticated 请求:

// 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)进一步限制访问权限

管理资源策略

选择以下方法之一:

例
AWS CLI
  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
Python (Boto3)
  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' )

安全最佳实践

授予最低权限

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

// 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" } ] }

防止副手感到困惑

授予 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" } } } ] }

对关键控制使用显式拒绝

对安全关键限制使用明确的拒绝语句:

// 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" } } } ] }

问题排查

访问被拒绝错误

如果您收到 “访问被拒绝” 错误:

  • 检查两个策略:验证基于身份的策略和基于资源的策略

  • 查找明确拒绝:任何策略中的明确拒绝都会覆盖所有允许

  • 验证委托人 ARN:确保策略中的主体 ARN 与呼叫者匹配

  • 检查条件:验证所有条件键的计算结果为真

  • 查看 SCP:组织服务控制策略可以取代资源策略

策略验证错误

常见的策略验证错误:

  • JSON 无效:确保您的政策是有效的 JSON

  • ARN 格式无效:验证所有 ARN 都遵循正确的格式

  • 不支持的操作:检查该资源类型是否支持所有操作

  • 缺少必填元素:确保版本、声明、效果、主体和行动都存在