View a markdown version of this page

添加上下文 - Amazon Verified Permissions
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

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

添加上下文

背景是与政策决策相关的信息,但不是您的主体、行动或资源身份的一部分。访问令牌声明是上下文。您可能希望仅允许来自一组源 IP 地址的操作,或者仅在您的用户使用 MFA 登录时才允许执行操作。您的应用程序有权访问这些上下文会话数据,并且必须将其填充到授权请求中。已验证权限授权请求中的上下文数据必须 JSON-formatted 位于contextMap元素中。

说明此内容的示例来自示例策略存储库。要继续操作,请在测试环境中创建DigitalPetStore示例策略存储。

以下上下文对象基于示例DigitalPetStore策略存储为应用程序声明每种 Cedar 数据类型之一。

"context": { "contextMap": { "AccountCodes": { "set": [ { "long": 111122223333 }, { "long": 444455556666 }, { "long": 123456789012 } ] }, "approvedBy": { "entityIdentifier": { "entityId": "Bob", "entityType": "DigitalPetStore::User" } }, "MfaAuthorized": { "boolean": true }, "NetworkInfo": { "record": { "IPAddress": { "string": "192.0.2.178" }, "Country": { "string": "United States of America" }, "SSL": { "boolean": true } } }, "RequestedOrderCount": { "long": 4 }, "UserAgent": { "string": "My UserAgent 1.12" } } }
授权环境中的数据类型
布尔值

二进制true或false值。在示例中,f true or 的布尔值MfaAuthenticated表示客户在请求查看订单之前已执行多因素身份验证。

设置

上下文元素的集合。集合成员可以是所有相同的类型,如本示例所示,也可以是不同的类型,包括嵌套集合。在示例中,客户与 3 个不同的账户相关联。

字符串

用"字符括起来的字母、数字或符号序列。在示例中,该UserAgent字符串表示客户请求查看其订单时使用的浏览器。

长整型

一个整数。在示例中,RequestedOrderCount表示该请求是由于客户要求查看其过去的四个订单而产生的批次请求的一部分。

记录

属性的集合。你必须在请求上下文中声明这些属性。具有架构的策略存储必须在架构中包含此实体和该实体的属性。在示例中,NetworkInfo记录包含有关用户的来源 IP、由客户端确定的该 IP 的地理位置以及传输中的加密的信息。

EntityIdentifier

对请求entities元素中声明的实体和属性的引用。在示例中,用户的订单已获得员工的批准Bob。

要在示例DigitalPetStore应用程序中测试此示例上下文,您必须使用描述客户角色-获取订单来更新请求entities、策略存储架构和静态策略。

修改 DigitalPetStore 以接受授权上下文

最初,DigitalPetStore它不是一个非常复杂的策略存储。它不包含任何预先配置的策略或上下文属性,以支持我们提供的上下文。要使用此上下文信息评估授权请求示例,请对您的策略存储和授权请求进行以下修改。有关以访问令牌信息为上下文的上下文示例,请参阅映射 Amazon Cognito 访问令牌和映射 OIDC 访问令牌。

Schema

将以下更新应用到您的策略存储架构以支持新的上下文属性。按actions如下GetOrder方式更新。

"GetOrder": { "memberOf": [], "appliesTo": { "resourceTypes": [ "Order" ], "context": { "type": "Record", "attributes": { "AccountCodes": { "type": "Set", "required": true, "element": { "type": "Long" } }, "approvedBy": { "name": "User", "required": true, "type": "Entity" }, "MfaAuthorized": { "type": "Boolean", "required": true }, "NetworkInfo": { "type": "NetworkInfo", "required": true }, "RequestedOrderCount": { "type": "Long", "required": true }, "UserAgent": { "required": true, "type": "String" } } }, "principalTypes": [ "User" ] } }

要引用请求上下文NetworkInfo中命名的record数据类型,请先actions在架构中添加以下内容,在架构中创建 CommonType 构造。commonType构造是一组共享的属性,您可以将其应用于不同的实体。

"commonTypes": { "NetworkInfo": { "attributes": { "IPAddress": { "type": "String", "required": true }, "SSL": { "required": true, "type": "Boolean" }, "Country": { "required": true, "type": "String" } }, "type": "Record" } },
Policy

以下策略设置了每个提供的上下文元素必须满足的条件。它建立在现有静态策略的基础上,描述为 “客户角色-获取订单” 。该政策最初仅要求提出请求的主体是资源的所有者。

permit ( principal in DigitalPetStore::Role::"Customer", action in [DigitalPetStore::Action::"GetOrder"], resource ) when { principal == resource.owner && context.AccountCodes.contains(111122223333) && context.approvedBy in DigitalPetStore::Role::"Employee" && context.MfaAuthorized == true && context.NetworkInfo.Country like "*United States*" && context.NetworkInfo.IPAddress like "192.0.2.*" && context.NetworkInfo.SSL == true && context.RequestedOrderCount <= 4 && context.UserAgent like "*My UserAgent*" };

现在,我们要求检索订单的请求必须满足我们在请求中添加的其他上下文条件。

  1. 用户必须使用 MFA 登录。

  2. 用户的 Web 浏览器User-Agent必须包含该字符串My UserAgent。

  3. 用户必须请求查看 4 个或更少的订单。

  4. 用户的账户代码之一必须是111122223333。

  5. 用户的 IP 地址必须来自美国,必须处于加密会话中,并且其 IP 地址必须以192.0.2.开头。

  6. 员工必须已批准他们的订单。在授权请求的entities元素中,我们将声明一个角色为的Bob用户Employee。

Request body

在使用相应的架构和策略配置策略存储库后,您可以向已验证的权限 API 操作提交此授权请求IsAuthorized。请注意,该entities区段包含角色为的Bob用户的定义Employee。

{ "principal": { "entityType": "DigitalPetStore::User", "entityId": "Alice" }, "action": { "actionType": "DigitalPetStore::Action", "actionId": "GetOrder" }, "resource": { "entityType": "DigitalPetStore::Order", "entityId": "1234" }, "context": { "contextMap": { "AccountCodes": { "set": [ {"long": 111122223333}, {"long": 444455556666}, {"long": 123456789012} ] }, "approvedBy": { "entityIdentifier": { "entityId": "Bob", "entityType": "DigitalPetStore::User" } }, "MfaAuthorized": { "boolean": true }, "NetworkInfo": { "record": { "Country": {"string": "United States of America"}, "IPAddress": {"string": "192.0.2.178"}, "SSL": {"boolean": true} } }, "RequestedOrderCount":{ "long": 4 }, "UserAgent": { "string": "My UserAgent 1.12" } } }, "entities": { "entityList": [ { "identifier": { "entityType": "DigitalPetStore::User", "entityId": "Alice" }, "attributes": { "memberId": { "string": "801b87f2-1a5c-40b3-b580-eacad506d4e6" } }, "parents": [ { "entityType": "DigitalPetStore::Role", "entityId": "Customer" } ] }, { "identifier": { "entityType": "DigitalPetStore::User", "entityId": "Bob" }, "attributes": { "memberId": { "string": "49d9b81e-735d-429c-989d-93bec0bcfd8b" } }, "parents": [ { "entityType": "DigitalPetStore::Role", "entityId": "Employee" } ] }, { "identifier": { "entityType": "DigitalPetStore::Order", "entityId": "1234" }, "attributes": { "owner": { "entityIdentifier": { "entityType": "DigitalPetStore::User", "entityId": "Alice" } } }, "parents": [] } ] }, "policyStoreId": "PSEXAMPLEabcdefg111111" }