本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
示例 2:具有经过验证的权限和 Cedar 的基本 RBAC
此示例使用已验证的权限和 Cedar 来演示基本的 RBAC。如前所述,Cedar的基本结构是一个实体。开发人员定义自己的实体,并且可以选择在实体之间创建关系。以下示例包括三种类型的实体:Users
Roles
、和Problems
。 Students
并且Teachers
可以被视为该类型的实体Role,
,每个实体User
都可以与零或任何一个相关联Roles
。

在 Cedar 中,这些关系是通过将链接Role
Student
到User
Bob
作为其父项来表达的。这种关联在逻辑上将所有学生用户分组到一个组中。有关在 Cedar 中进行分组的更多信息,请参阅 Cedar 文档
以下策略评估与该类型的逻辑组相关联的所有委托Students
人的操作submitProblem,
决策ALLOW
。Role
permit ( principal in ElearningApp::Role::"Students", action == ElearningApp::Action::"submitProblem", resource );
以下策略根据操作的决策进行评估answerProblem
,submitProblem
或者ALLOW
针对与该类型的逻辑组相关联的所有委托Teachers
人进行评估。Role
permit ( principal in ElearningApp::Role::"Teachers", action in [ ElearningApp::Action::"submitProblem", ElearningApp::Action::"answerProblem" ], resource );
为了评估使用这些策略的请求,评估引擎需要知道授权请求中引用的委托人是否为相应组的成员。因此,应用程序必须将相关的群组成员资格信息作为授权请求的一部分传递给评估引擎。这是通过属性完成的,它使您能够向 Cedar 评估引擎提供授权调用中涉及的委托人和资源的属性和组成员身份数据。entities
在以下代码中,组成员资格通过定义User::"Bob"
为调用父代来表示Role::"Students"
。
{ "policyStoreId": "ELEARNING_POLICYSTOREID", "principal": { "entityType": "ElearningApp::User", "entityId": "Bob" }, "action": { "actionType": "ElearningApp::Action", "actionId": "answerProblem" }, "resource": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "entities": { "entityList": [ { "identifier": { "entityType": "ElearningApp::User", "entityId": "Bob" }, "attributes": {}, "parents": [ { "entityType": "ElearningApp::Role", "entityId": "Students" } ] }, { "identifier": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "attributes": {}, "parents": [] } ] } }
在此示例中,Bob 是answerProblem
发出请求的登录用户。因此,Bob 是委托人,而实体属于该类型User
。Bob 想要执行的操作是answerProblem
。为了评估 Bob 能否执行answerProblem
操作,您需要提供一个实体结构,该结构将该实体User
与值关联起来,Bob
并通过将父实体列为Role::"Students"
,来分配其组成员资格。由于用户组Role::"Students"
中的实体只能执行操作,因此此授权请求的计算结果为。submitProblem
DENY
另一方面,如果值为Alice
且属于该组的类型User
Role::"Teachers"
尝试执行操作,则授权请求的计算结果为ALLOW
,因为该策略规定允许组中的委托人对Role::"Teachers"
所有资源执行操作。answerProblem
answerProblem
以下代码显示了评估结果为的此类授权请求。ALLOW
{ "policyStoreId": "ELEARNING_POLICYSTOREID", "principal": { "entityType": "ElearningApp::User", "entityId": "Alice" }, "action": { "actionType": "ElearningApp::Action", "actionId": "answerProblem" }, "resource": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "entities": { "entityList": [ { "identifier": { "entityType": "ElearningApp::User", "entityId": "Alice" }, "attributes": {}, "parents": [ { "entityType": "ElearningApp::Role", "entityId": "Teachers" } ] }, { "identifier": { "entityType": "ElearningApp::Problem", "entityId": "SomeProblem" }, "attributes": {}, "parents": [] } ] } }