使用验证权限示例 Amazon CLI - Amazon Command Line Interface
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用验证权限示例 Amazon CLI

以下代码示例向您展示了如何使用 Amazon Command Line Interface 具有已验证权限的,来执行操作和实现常见场景。

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

场景是展示如何通过在同一服务中调用多个函数来完成特定任务任务的代码示例。

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

主题

操作

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

Amazon CLI

创建身份源

以下create-identity-source示例创建了一个身份源,允许您引用存储在指定 Amazon Cognito 用户池中的身份。这些身份作为实体类型在 “已验证权限” 中可用User

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

有关身份源的更多信息,请参阅《亚马逊验证权限用户指南》中的对身份提供商使用亚马逊验证权限

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

Amazon CLI

创建策略存储

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

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

有关策略存储的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略存储

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考CreatePolicyStore中的。

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

Amazon CLI

示例 1:创建策略模板

以下create-policy-template示例使用包含委托人占位符的语句创建策略模板。

aws verifiedpermissions create-policy-template \ --definition 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" }

有关策略模板的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略模板

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

Amazon 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" } }

有关政策的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限政策

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考CreatePolicy中的。

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

Amazon CLI

删除身份源

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

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

此命令不生成任何输出。

有关身份源的更多信息,请参阅《亚马逊验证权限用户指南》中的对身份提供商使用亚马逊验证权限

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

Amazon CLI

删除策略存储

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

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

此命令不生成任何输出。

有关策略存储的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略存储

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考DeletePolicyStore中的。

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

Amazon CLI

删除策略模板

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

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

此命令不生成任何输出。

有关策略模板的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略模板

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

Amazon CLI

删除静态或与模板关联的策略

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

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

此命令不生成任何输出。

有关政策的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限政策

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考DeletePolicy中的。

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

Amazon 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" }

有关身份源的更多信息,请参阅《亚马逊验证权限用户指南》中的对身份提供商使用亚马逊验证权限

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考GetIdentitySource中的。

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

Amazon 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" } }

有关策略存储的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略存储

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考GetPolicyStore中的。

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

Amazon 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);" }

有关策略模板的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略模板

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考GetPolicyTemplate中的。

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

Amazon 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" } }

有关政策的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限政策

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考GetPolicy中的。

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

Amazon 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 验证权限用户指南》中的策略存储架构

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考GetSchema中的。

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

Amazon 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 用户池中的身份的更多信息,请参阅《亚马逊验证权限用户指南》中的对身份提供商使用亚马逊验证权限

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

Amazon CLI

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

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

响应显示一个策略允许该请求。

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 验证权限用户指南》

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考IsAuthorized中的。

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

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

有关身份源的更多信息,请参阅《亚马逊验证权限用户指南》中的对身份提供商使用亚马逊验证权限

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

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

有关政策的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限政策

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListPolicies中的。

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

Amazon CLI

列出可用的策略存储库

以下list-policy-stores示例列出了该 Amazon 地区的所有策略存储。除了create-policy-storelist-policy-stores要求您指定要使用的策略存储的 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" } ] }

有关策略存储的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略存储

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考ListPolicyStores中的。

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

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

有关策略模板的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略模板

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

Amazon CLI

将架构保存到策略存储中

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

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

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

aws verifiedPermissions put-schema — 定义文件://schema.txt — psexampleabcdefg111111 policy-store-id

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 验证权限用户指南》中的策略存储架构

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考PutSchema中的。

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

Amazon 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" }

有关身份源的更多信息,请参阅《亚马逊验证权限用户指南》中的对身份提供商使用亚马逊验证权限

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

Amazon 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" }

有关策略存储的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略存储

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考UpdatePolicyStore中的。

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

Amazon 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" }

有关策略模板的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限策略模板

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

Amazon 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" } }

有关政策的更多信息,请参阅《亚马逊验证权限用户指南》中的亚马逊验证权限政策

  • 有关 API 的详细信息,请参阅Amazon CLI 命令参考UpdatePolicy中的。