使用 AWS CLI 的 Verified Permissions 示例 - AWS Command Line Interface

本文档仅适用于 AWS CLI 版本 1。有关 AWS CLI 版本 2 的相关文档,请参阅版本 2 用户指南

使用 AWS CLI 的 Verified Permissions 示例

以下代码示例演示了如何通过将 AWS Command Line Interface与 Verified Permissions 结合使用,来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示如何使用 create-identity-source

AWS CLI

创建身份源

以下 create-identity-source 示例创建一个身份源,以便您能够引用存储在指定 Amazon Cognito 用户池中的身份。这些身份作为 User 实体类型在 Verified Permissions 中提供。

aws verifiedpermissions create-identity-source \ --configuration file://config.txt \ --principal-entity-type "User" \ --policy-store-id PSEXAMPLEabcdefg111111

config.txt 的内容:

{ "cognitoUserPoolConfiguration": { "userPoolArn": "arn:aws:cognito-idp:us-west-2:123456789012:userpool/us-west-2_1a2b3c4d5", "clientIds":["a1b2c3d4e5f6g7h8i9j0kalbmc"] } }

输出:

{ "createdDate": "2023-05-19T20:30:28.214829+00:00", "identitySourceId": "ISEXAMPLEabcdefg111111", "lastUpdatedDate": "2023-05-19T20:30:28.214829+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111" }

有关身份源的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的结合使用 Amazon Verified Permissions 与身份提供者

以下代码示例演示如何使用 create-policy-store

AWS CLI

创建策略存储

以下 create-policy-store 示例在当前 AWS 区域中创建策略存储。

aws verifiedpermissions create-policy-store \ --validation-settings "mode=STRICT"

输出:

{ "arn": "arn:aws:verifiedpermissions::123456789012:policy-store/PSEXAMPLEabcdefg111111", "createdDate": "2023-05-16T17:41:29.103459+00:00", "lastUpdatedDate": "2023-05-16T17:41:29.103459+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111" }

有关策略存储的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略存储

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 CreatePolicyStore

以下代码示例演示如何使用 create-policy-template

AWS CLI

创建策略模板

以下 create-policy-template 示例使用包含主体占位符的声明创建策略模板。

aws verifiedpermissions create-policy-template \ --statement file://template1.txt \ --policy-store-id PSEXAMPLEabcdefg111111

template1.txt 的内容:

permit( principal in ?principal, action == Action::"view", resource == Photo::"VacationPhoto94.jpg" );

输出:

{ "createdDate": "2023-06-12T20:47:42.804511+00:00", "lastUpdatedDate": "2023-06-12T20:47:42.804511+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyTemplateId": "PTEXAMPLEabcdefg111111" }

有关更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略模板

以下代码示例演示如何使用 create-policy

AWS CLI

示例 1:创建静态策略

以下 create-policy 示例创建一个静态策略,其策略范围指定了主体和资源。

aws verifiedpermissions create-policy \ --definition file://definition1.txt \ --policy-store-id PSEXAMPLEabcdefg111111

definition1.txt 文件的内容:

{ "static": { "description": "Grant everyone of janeFriends UserGroup access to the vacationFolder Album", "statement": "permit(principal in UserGroup::\"janeFriends\", action, resource in Album::\"vacationFolder\" );" } }

输出:

{ "createdDate": "2023-06-12T20:33:37.382907+00:00", "lastUpdatedDate": "2023-06-12T20:33:37.382907+00:00", "policyId": "SPEXAMPLEabcdefg111111", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "STATIC", "principal": { "entityId": "janeFriends", "entityType": "UserGroup" }, "resource": { "entityId": "vacationFolder", "entityType": "Album" } }

示例 2:创建向所有人授予资源访问权限的静态策略

以下 create-policy 示例创建一个静态策略,其策略范围仅指定了资源。

aws verifiedpermissions create-policy \ --definition file://definition2.txt \ --policy-store-id PSEXAMPLEabcdefg111111

definition2.txt 文件的内容:

{ "static": { "description": "Grant everyone access to the publicFolder Album", "statement": "permit(principal, action, resource in Album::\"publicFolder\");" } }

输出:

{ "createdDate": "2023-06-12T20:39:44.975897+00:00", "lastUpdatedDate": "2023-06-12T20:39:44.975897+00:00", "policyId": "PbfR73F8oh5MMfr9uRtFDB", "policyStoreId": "PSEXAMPLEabcdefg222222", "policyType": "STATIC", "resource": { "entityId": "publicFolder", "entityType": "Album" } }

示例 3:创建与指定模板关联的模板链接策略

以下 create-policy 示例使用指定策略模板创建模板关联策略,并将要使用的指定主体与新的模板关联策略相关联。

aws verifiedpermissions create-policy \ --definition file://definition.txt \ --policy-store-id PSEXAMPLEabcdefg111111

definition.txt 的内容:

{ "templateLinked": { "policyTemplateId": "PTEXAMPLEabcdefg111111", "principal": { "entityType": "User", "entityId": "alice" } } }

输出:

{ "createdDate": "2023-06-12T20:49:51.490211+00:00", "lastUpdatedDate": "2023-06-12T20:49:51.490211+00:00", "policyId": "TPEXAMPLEabcdefg111111", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "TEMPLATE_LINKED", "principal": { "entityId": "alice", "entityType": "User" }, "resource": { "entityId": "VacationPhoto94.jpg", "entityType": "Photo" } }

有关策略的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 CreatePolicy

以下代码示例演示如何使用 delete-identity-source

AWS CLI

删除身份源

以下 delete-identity-source 示例删除具有指定 ID 的身份源。

aws verifiedpermissions delete-identity-source \ --identity-source-id ISEXAMPLEabcdefg111111 \ --policy-store-id PSEXAMPLEabcdefg111111

此命令不生成任何输出。

有关身份源的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的结合使用 Amazon Verified Permissions 与身份提供者

以下代码示例演示如何使用 delete-policy-store

AWS CLI

删除策略存储

以下 delete-policy-store 示例删除具有指定 ID 的策略存储。

aws verifiedpermissions delete-policy-store \ --policy-store-id PSEXAMPLEabcdefg111111

此命令不生成任何输出。

有关策略存储的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略存储

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DeletePolicyStore

以下代码示例演示如何使用 delete-policy-template

AWS CLI

删除策略模板

以下 delete-policy-template 示例删除具有指定 ID 的策略模板。

aws verifiedpermissions delete-policy \ --policy-template-id PTEXAMPLEabcdefg111111 \ --policy-store-id PSEXAMPLEabcdefg111111

此命令不生成任何输出。

有关更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略模板

以下代码示例演示如何使用 delete-policy

AWS CLI

删除静态或模板链接策略

以下 delete-policy 示例删除具有指定 ID 的策略。

aws verifiedpermissions delete-policy \ --policy-id SPEXAMPLEabcdefg111111 \ --policy-store-id PSEXAMPLEabcdefg111111

此命令不生成任何输出。

有关策略的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DeletePolicy

以下代码示例演示如何使用 get-identity-source

AWS CLI

检索有关身份源的详细信息

以下 get-identity-source 示例显示具有指定 ID 的身份源的详细信息。

aws verifiedpermissions get-identity-source \ --identity-source ISEXAMPLEabcdefg111111 \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "createdDate": "2023-06-12T22:27:49.150035+00:00", "details": { "clientIds": [ "a1b2c3d4e5f6g7h8i9j0kalbmc" ], "discoveryUrl": "https://cognito-idp.us-west-2.amazonaws.com/us-west-2_1a2b3c4d5", "openIdIssuer": "COGNITO", "userPoolArn": "arn:aws:cognito-idp:us-west-2:123456789012:userpool/us-west-2_1a2b3c4d5" }, "identitySourceId": "ISEXAMPLEabcdefg111111", "lastUpdatedDate": "2023-06-12T22:27:49.150035+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "principalEntityType": "User" }

有关身份源的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的结合使用 Amazon Verified Permissions 与身份提供者

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetIdentitySource

以下代码示例演示如何使用 get-policy-store

AWS CLI

检索有关策略存储的详细信息

以下 get-policy-store 示例显示具有指定 ID 的策略存储的详细信息。

aws verifiedpermissions get-policy-store \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "arn": "arn:aws:verifiedpermissions::123456789012:policy-store/PSEXAMPLEabcdefg111111", "createdDate": "2023-06-05T20:16:46.225598+00:00", "lastUpdatedDate": "2023-06-08T20:40:23.173691+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "validationSettings": { "mode": "OFF" } }

有关策略存储的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略存储

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetPolicyStore

以下代码示例演示如何使用 get-policy-template

AWS CLI

检索有关策略模板的详细信息

以下 get-policy-template 示例显示具有指定 ID 的策略模板的详细信息。

aws verifiedpermissions get-policy-template \ --policy-template-id PTEXAMPLEabcdefg111111 \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "createdDate": "2023-06-12T20:47:42.804511+00:00", "lastUpdatedDate": "2023-06-12T20:47:42.804511+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyTemplateId": "PTEXAMPLEabcdefg111111", "statement": "permit(\n principal in ?principal,\n action == Action::\"view\",\n resource == Photo::\"VacationPhoto94.jpg\"\n);" }

有关更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略模板

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetPolicyTemplate

以下代码示例演示如何使用 get-policy

AWS CLI

检索有关策略的详细信息

以下 get-policy 示例显示具有指定 ID 的策略的详细信息。

aws verifiedpermissions get-policy \ --policy-id PSEXAMPLEabcdefg111111 \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "createdDate": "2023-06-12T20:33:37.382907+00:00", "definition": { "static": { "description": "Grant everyone of janeFriends UserGroup access to the vacationFolder Album", "statement": "permit(principal in UserGroup::\"janeFriends\", action, resource in Album::\"vacationFolder\" );" } }, "lastUpdatedDate": "2023-06-12T20:33:37.382907+00:00", "policyId": "SPEXAMPLEabcdefg111111", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "STATIC", "principal": { "entityId": "janeFriends", "entityType": "UserGroup" }, "resource": { "entityId": "vacationFolder", "entityType": "Album" } }

有关策略的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetPolicy

以下代码示例演示如何使用 get-schema

AWS CLI

检索策略存储中的架构

以下 get-schema 示例显示指定策略存储中架构的详细信息。

aws verifiedpermissions get-schema \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "policyStoreId": "PSEXAMPLEabcdefg111111", "schema": "{\"MySampleNamespace\":{\"entityTypes\":{\"Employee\":{\"shape\":{\"attributes\":{\"jobLevel\":{\"type\":\"Long\"},\"name\":{\"type\":\"String\"}},\"type\":\"Record\"}}},\"actions\":{\"remoteAccess\":{\"appliesTo\":{\"principalTypes\":[\"Employee\"]}}}}}", "createdDate": "2023-06-14T17:47:13.999885+00:00", "lastUpdatedDate": "2023-06-14T17:47:13.999885+00:00" }

有关架构的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的策略存储架构

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetSchema

以下代码示例演示如何使用 is-authorized-with-token

AWS CLI

示例 1:请求对用户请求做出授权决定(允许)

以下 is-authorized-with-token 示例请求对已通过 Amazon Cognito 身份验证的用户做出授权决定。该请求使用 Cognito 提供的身份令牌,而不是访问令牌。在此示例中,将指定信息存储配置为将主体作为 CognitoUser 实体类型返回。

aws verifiedpermissions is-authorized-with-token \ --action actionId="View",actionType="Action" \ --resource entityId="vacationPhoto94.jpg",entityType="Photo" \ --policy-store-id PSEXAMPLEabcdefg111111 \ --identity-token "AbCdE12345...long.string...54321EdCbA"

策略存储包含一个带以下声明的策略,该声明接受来自指定 Cognito 用户池和应用程序 ID 的身份。

permit( principal == CognitoUser::"us-east-1_1a2b3c4d5|a1b2c3d4e5f6g7h8i9j0kalbmc", action, resource == Photo::"VacationPhoto94.jpg" );

输出:

{ "decision":"Allow", "determiningPolicies":[ { "determiningPolicyId":"SPEXAMPLEabcdefg111111" } ], "errors":[] }

有关使用 Cognito 用户池中身份的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的结合使用 Amazon Verified Permissions 与身份提供者

以下代码示例演示如何使用 is-authorized

AWS CLI

示例 1:请求对用户请求做出授权决定(允许)

以下 is-authorized 示例请求对名为 Alice 的主体类型 User 做出授权决定,该实体想要对名为 VacationPhoto94.jpg 的资源类型 Photo 执行 updatePhoto 操作。

响应表明某项策略允许该请求。

aws verifiedpermissions is-authorized \ --principal entityType=User,entityId=alice \ --action actionType=Action,actionId=view \ --resource entityType=Photo,entityId=VactionPhoto94.jpg \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "decision": "ALLOW", "determiningPolicies": [ { "policyId": "SPEXAMPLEabcdefg111111" } ], "errors": [] }

示例 2:请求对用户请求做出授权决定(拒绝)

除了主体是 User::"Bob" 之外,以下示例与前面的示例相同。策略存储不包含任何允许该用户访问 Album::"alice_folder" 的策略。

输出表明 Deny 是隐式的,因为列表 DeterminingPolicies 为空。

aws verifiedpermissions create-policy \ --definition file://definition2.txt \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "decision": "DENY", "determiningPolicies": [], "errors": [] }

有关更多信息,请参阅 Amazon Verified Permissions 用户指南

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 IsAuthorized

以下代码示例演示如何使用 list-identity-sources

AWS CLI

列出可用身份源

以下 list-identity-sources 示例列出了指定策略存储中的所有身份源。

aws verifiedpermissions list-identity-sources \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "identitySources": [ { "createdDate": "2023-06-12T22:27:49.150035+00:00", "details": { "clientIds": [ "a1b2c3d4e5f6g7h8i9j0kalbmc" ], "discoveryUrl": "https://cognito-idp.us-west-2.amazonaws.com/us-west-2_1a2b3c4d5", "openIdIssuer": "COGNITO", "userPoolArn": "arn:aws:cognito-idp:us-west-2:123456789012:userpool/us-west-2_1a2b3c4d5" }, "identitySourceId": "ISEXAMPLEabcdefg111111", "lastUpdatedDate": "2023-06-12T22:27:49.150035+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "principalEntityType": "User" } ] }

有关身份源的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的结合使用 Amazon Verified Permissions 与身份提供者

以下代码示例演示如何使用 list-policies

AWS CLI

列出可用策略

以下 list-policies 示例列出了指定策略存储中的所有策略。

aws verifiedpermissions list-policies \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "policies": [ { "createdDate": "2023-06-12T20:33:37.382907+00:00", "definition": { "static": { "description": "Grant everyone of janeFriends UserGroup access to the vacationFolder Album" } }, "lastUpdatedDate": "2023-06-12T20:33:37.382907+00:00", "policyId": "SPEXAMPLEabcdefg111111", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "STATIC", "principal": { "entityId": "janeFriends", "entityType": "UserGroup" }, "resource": { "entityId": "vacationFolder", "entityType": "Album" } }, { "createdDate": "2023-06-12T20:39:44.975897+00:00", "definition": { "static": { "description": "Grant everyone access to the publicFolder Album" } }, "lastUpdatedDate": "2023-06-12T20:39:44.975897+00:00", "policyId": "SPEXAMPLEabcdefg222222", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "STATIC", "resource": { "entityId": "publicFolder", "entityType": "Album" } }, { "createdDate": "2023-06-12T20:49:51.490211+00:00", "definition": { "templateLinked": { "policyTemplateId": "PTEXAMPLEabcdefg111111" } }, "lastUpdatedDate": "2023-06-12T20:49:51.490211+00:00", "policyId": "SPEXAMPLEabcdefg333333", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "TEMPLATE_LINKED", "principal": { "entityId": "alice", "entityType": "User" }, "resource": { "entityId": "VacationPhoto94.jpg", "entityType": "Photo" } } ] }

有关策略的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListPolicies

以下代码示例演示如何使用 list-policy-stores

AWS CLI

列出可用策略存储

以下 list-policy-stores 示例列出 AWS 区域中的所有策略存储。除了 create-policy-storelist-policy-stores 之外,所有 Verified Permissions 命令都要求您指定您要使用的策略存储的 ID。

aws verifiedpermissions list-policy-stores

输出:

{ "policyStores": [ { "arn": "arn:aws:verifiedpermissions::123456789012:policy-store/PSEXAMPLEabcdefg111111", "createdDate": "2023-06-05T20:16:46.225598+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111" }, { "arn": "arn:aws:verifiedpermissions::123456789012:policy-store/PSEXAMPLEabcdefg222222", "createdDate": "2023-06-08T18:09:37.364356+00:00", "policyStoreId": "PSEXAMPLEabcdefg222222" }, { "arn": "arn:aws:verifiedpermissions::123456789012:policy-store/PSEXAMPLEabcdefg333333", "createdDate": "2023-06-08T18:09:46.920600+00:00", "policyStoreId": "PSEXAMPLEabcdefg333333" } ] }

有关策略存储的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略存储

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListPolicyStores

以下代码示例演示如何使用 list-policy-templates

AWS CLI

列出可用策略模板

以下 list-policy-templates 示例列出了指定策略存储中的所有策略模板。

aws verifiedpermissions list-policy-templates \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "policyTemplates": [ { "createdDate": "2023-06-12T20:47:42.804511+00:00", "lastUpdatedDate": "2023-06-12T20:47:42.804511+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyTemplateId": "PTEXAMPLEabcdefg111111" } ] }

有关更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略模板

以下代码示例演示如何使用 put-schema

AWS CLI

将架构保存到策略存储

以下 put-schema 示例创建或替换指定策略存储中的架构。

输入文件中的 cedarJson 参数采用 JSON 对象的字符串表示形式。它在最外面的引号对中包含嵌入式引号(")。这要求您将 JSON 转换为字符串,方法是在所有嵌入的引号前面加上反斜杠字符("),然后将所有行合并为一个不带换行符的文本行。

为了便于阅读,可以在此处将字符串示例分成多行显示,但该操作要求将参数作为单行字符串提交。

aws verifiedpermissions put-schema --definition file://schema.txt --policy-store-id PSEXAMPLEabcdefg111111

schema.txt 的内容:

{ "cedarJson": "{\"MySampleNamespace\": {\"actions\": {\"remoteAccess\": { \"appliesTo\": {\"principalTypes\": [\"Employee\"]}}},\"entityTypes\": { \"Employee\": {\"shape\": {\"attributes\": {\"jobLevel\": {\"type\": \"Long\"},\"name\": {\"type\": \"String\"}},\"type\": \"Record\"}}}}}" }

输出:

{ "policyStoreId": "PSEXAMPLEabcdefg111111", "namespaces": [ "MySampleNamespace" ], "createdDate": "2023-06-14T17:47:13.999885+00:00", "lastUpdatedDate": "2023-06-14T17:47:13.999885+00:00" }

有关架构的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的策略存储架构

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PutSchema

以下代码示例演示如何使用 update-identity-source

AWS CLI

更新身份源

以下 update-identity-source 示例通过提供新的 Cognito 用户池配置并更改身份源返回的实体类型,来修改指定的身份源。

aws verifiedpermissions update-identity-source --identity-source-id ISEXAMPLEabcdefg111111 \ --update-configuration file://config.txt \ --principal-entity-type "Employee" \ --policy-store-id PSEXAMPLEabcdefg111111

config.txt 的内容:

{ "cognitoUserPoolConfiguration": { "userPoolArn": "arn:aws:cognito-idp:us-west-2:123456789012:userpool/us-west-2_1a2b3c4d5", "clientIds":["a1b2c3d4e5f6g7h8i9j0kalbmc"] } }

输出:

{ "createdDate": "2023-05-19T20:30:28.214829+00:00", "identitySourceId": "ISEXAMPLEabcdefg111111", "lastUpdatedDate": "2023-05-19T20:30:28.214829+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111" }

有关身份源的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的结合使用 Amazon Verified Permissions 与身份提供者

以下代码示例演示如何使用 update-policy-store

AWS CLI

更新策略存储

以下 update-policy-store 示例通过更改其验证设置来修改策略存储。

aws verifiedpermissions update-policy-store \ --validation-settings "mode=STRICT" \ --policy-store-id PSEXAMPLEabcdefg111111

输出:

{ "arn": "arn:aws:verifiedpermissions::123456789012:policy-store/PSEXAMPLEabcdefg111111", "createdDate": "2023-05-16T17:41:29.103459+00:00", "lastUpdatedDate": "2023-05-16T17:41:29.103459+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111" }

有关策略存储的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略存储

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UpdatePolicyStore

以下代码示例演示如何使用 update-policy-template

AWS CLI

示例 1:更新策略模板

以下 update-policy-template 示例修改指定的模板链接策略以替换其策略声明。

aws verifiedpermissions update-policy-template \ --policy-template-id PTEXAMPLEabcdefg111111 \ --statement file://template1.txt \ --policy-store-id PSEXAMPLEabcdefg111111

template1.txt 文件的内容:

permit( principal in ?principal, action == Action::"view", resource == Photo::"VacationPhoto94.jpg" );

输出:

{ "createdDate": "2023-06-12T20:47:42.804511+00:00", "lastUpdatedDate": "2023-06-12T20:47:42.804511+00:00", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyTemplateId": "PTEXAMPLEabcdefg111111" }

有关更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略模板

以下代码示例演示如何使用 update-policy

AWS CLI

示例 1:创建静态策略

以下 create-policy 示例创建一个静态策略,其策略范围指定了主体和资源。

aws verifiedpermissions create-policy \ --definition file://definition.txt \ --policy-store-id PSEXAMPLEabcdefg111111

statement 参数采用 JSON 对象的字符串表示形式。它在最外面的引号对中包含嵌入式引号(")。这要求您将 JSON 转换为字符串,方法是在所有嵌入的引号前面加上反斜杠字符("),然后将所有行合并为一个不带换行符的文本行。

为了便于阅读,可以在此处将字符串示例分成多行显示,但该操作要求将参数作为单行字符串提交。

definition.txt 文件的内容:

{ "static": { "description": "Grant everyone of janeFriends UserGroup access to the vacationFolder Album", "statement": "permit(principal in UserGroup::\"janeFriends\", action, resource in Album::\"vacationFolder\" );" } }

输出:

{ "createdDate": "2023-06-12T20:33:37.382907+00:00", "lastUpdatedDate": "2023-06-12T20:33:37.382907+00:00", "policyId": "SPEXAMPLEabcdefg111111", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "STATIC", "principal": { "entityId": "janeFriends", "entityType": "UserGroup" }, "resource": { "entityId": "vacationFolder", "entityType": "Album" } }

示例 2:创建向所有人授予资源访问权限的静态策略

以下 create-policy 示例创建一个静态策略,其策略范围仅指定了资源。

aws verifiedpermissions create-policy \ --definition file://definition2.txt \ --policy-store-id PSEXAMPLEabcdefg111111

definition2.txt 文件的内容:

{ "static": { "description": "Grant everyone access to the publicFolder Album", "statement": "permit(principal, action, resource in Album::\"publicFolder\");" } }

输出:

{ "createdDate": "2023-06-12T20:39:44.975897+00:00", "lastUpdatedDate": "2023-06-12T20:39:44.975897+00:00", "policyId": "PbfR73F8oh5MMfr9uRtFDB", "policyStoreId": "PSEXAMPLEabcdefg222222", "policyType": "STATIC", "resource": { "entityId": "publicFolder", "entityType": "Album" } }

示例 3:创建与指定模板关联的模板链接策略

以下 create-policy 示例使用指定策略模板创建模板关联策略,并将要使用的指定主体与新的模板关联策略相关联。

aws verifiedpermissions create-policy \ --definition file://definition2.txt \ --policy-store-id PSEXAMPLEabcdefg111111

definition3.txt 的内容:

{ "templateLinked": { "policyTemplateId": "PTEXAMPLEabcdefg111111", "principal": { "entityType": "User", "entityId": "alice" } } }

输出:

{ "createdDate": "2023-06-12T20:49:51.490211+00:00", "lastUpdatedDate": "2023-06-12T20:49:51.490211+00:00", "policyId": "TPEXAMPLEabcdefg111111", "policyStoreId": "PSEXAMPLEabcdefg111111", "policyType": "TEMPLATE_LINKED", "principal": { "entityId": "alice", "entityType": "User" }, "resource": { "entityId": "VacationPhoto94.jpg", "entityType": "Photo" } }

有关策略的更多信息,请参阅《Amazon Verified Permissions 用户指南》中的 Amazon Verified Permissions 策略

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UpdatePolicy