自动审批和拒绝访问策略的语句结构和内置运算符
下表展示了自动审批和拒绝访问策略的结构。
组件 | 语法 |
---|---|
效果 |
|
范围 |
|
条件子句 |
|
策略组件
自动审批或拒绝访问策略包含以下部分:
-
效果 –
permit
(允许)或forbid
(拒绝)访问。 -
范围 – 指定效果适用于哪些主体、操作和资源。您可以通过不标识特定主体、操作或资源来使 Cedar 中的范围保持未定义状态。在这种情况下,策略适用于所有可能的主体、操作和资源。对于即时访问节点,
action
始终为AWS::SSM::Action::"getTokenForInstanceAccess"
。 -
条件子句 – 应用效果的上下文。
评论
您可以在 策略中包含注释。注释被定义为以 //
开头、以换行符结尾的一行。
以下示例显示了策略中的注释。
// Allows users in the Engineering group from the Platform org to automatically connect to nodes tagged with Engineering and Production keys.
permit (
principal in AWS::IdentityStore::Group::"d4q81745-r081-7079-d789-14da1EXAMPLE",
action == AWS::SSM::Action::"getTokenForInstanceAccess",
resource
)
when {
principal has organization && resource.hasTag("Engineering") && resource.hasTag("Production") && principal.organization == "Platform"
};
多子句
您可以利用 &&
运算符,在一个策略声明中使用多个条件子句。
// Allow access if node has tag where the tag key is Environment
// & tag value is Development
permit(principal, action == AWS::SSM::getTokenForInstanceAccess, resource)
when {
resource.hasTag("Environment") &&
resource.getTag("Environment") == "Development"
};
预留字符
以下示例说明当上下文属性使用 :
(分号)时如何编写策略,该符号是策略语言中的保留字符。
permit (
principal,
action == AWS::SSM::Action::"getTokenForInstanceAccess",
resource
)
when {
principal has employeeNumber && principal.employeeNumber like "E-1*" && resource.hasTag("Purpose") && resource.getTag("Purpose") == "Testing"
}
有关其他示例,请参阅 示例策略语句。
即时节点访问架构
以下是即时节点访问的 Cedar 架构。
namespace AWS::EC2 { entity Instance tags String; } namespace AWS::IdentityStore { entity Group; entity User in [Group] { employeeNumber?: String, costCenter?: String, organization?: String, division?: String, }; } namespace AWS::IAM { entity Role; type AuthorizationContext = { principalTags: PrincipalTags, }; entity PrincipalTags tags String; } namespace AWS::SSM { entity ManagedInstance tags String; action "getTokenForInstanceAccess" appliesTo { principal: [AWS::IdentityStore::User], resource: [AWS::EC2::Instance, AWS::SSM::ManagedInstance], context: { "iam": AWS::IAM::AuthorizationContext } }; }
内置运算符
在使用各种条件创建自动审批或拒绝访问策略的上下文时,您可以使用 &&
运算符来添加其他条件。您还可以使用许多其他内置运算符来为您的策略条件添加更多的表达能力。下表包含所有内置运算符,以供参考。
运算符 | 类型和重载 | 描述 |
---|---|---|
! |
Boolean → Boolean |
逻辑非。 |
== |
any → any |
等于。适用于任何类型的参数,即使类型不匹配。不同类型的值永远不会彼此相等。 |
!= | any → any |
不等于;与等于完全相反(见上文)。 |
< |
(long, long) → Boolean |
长整数小于。 |
<= |
(long, long) → Boolean |
长整数小于或等于。 |
> |
(long, long) → Boolean |
长整数大于。 |
>= |
(long, long) → Boolean |
长整数大于或等于。 |
in | (entity, entity) → Boolean | 层次结构隶属(自反:A in A 始终为真)。 |
(entity, set(entity)) → Boolean | 层次结构隶属:A in [B, C, ...] 为真,如果 (A and B) || (A in C) || … 错误,如果集合包含非实体。 | |
&& | (Boolean, Boolean) → Boolean |
逻辑与(短路)。 |
|| | (Boolean, Boolean) → Boolean |
逻辑或(短路)。 |
.exists() | entity → Boolean | 实体存在。 |
has | (entity, attribute) → Boolean | 中缀运算符。e has f 测试记录或实体 e 是否具有属性 f 的绑定。如果 e 不存在或者 e 存在但没有属性 f ,则返回 false 。属性可以表示为标识符或字符串文字。 |
like | (string, string) → Boolean | 中缀运算符。t like p 检查文本 t 是否与模式 p 匹配,其中可能包含与 0 个或多个任意字符匹配的通配符 * 。为了匹配 t 中的文字星形字符,可以在 p 中使用特殊的转义字符序列 \* 。 |
.hasTag() | (entity, string) → Boolean | 检查实体是否应用了指定的标签。 |
.getTag() | (entity, string) → Boolean | 返回指定标签键的值。 |
.contains() | (set, any) → Boolean | 设置隶属关系(B 是 A 的元素吗)。 |
.containsAll() | (set, set) → Boolean | 测试集合 A 是否包含集合 B 中的所有元素。 |
.containsAny() | (set, set) → Boolean | 测试集合 A 是否包含集合 B 中的任意元素。 |
示例策略语句
以下是策略语句示例。
// Users assuming IAM roles with a principal tag of "Elevated" can automatically access nodes tagged with the "Environment" key when the value equals "prod"
permit(principal, action == AWS::SSM::getTokenForInstanceAccess, resource)
when {
// Verify IAM role principal tag
context.iam.principalTags.getTag("AccessLevel") == "Elevated" &&
// Verify the node has a tag with "Environment" tag key and a tag value of "prod"
resource.hasTag("Environment") &&
resource.getTag("Environment") == "prod"
};
// Identity Center users in the "Contractor" division can automatically access nodes tagged with the "Environment" key when the value equals "dev"
permit(principal, action == AWS::SSM::getTokenForInstanceAccess, resource)
when {
// Verify that the user is part of the "Contractor" division
principal.division == "Contractor" &&
// Verify the node has a tag with "Environment" tag key and a tag value of "dev"
resource.hasTag("Environment") &&
resource.getTag("Environment") == "dev"
};
// Identity Center users in a specified group can automatically access nodes tagged with the "Environment" key when the value equals "Production"
permit(principal in AWS::IdentityStore::Group::"d4q81745-r081-7079-d789-14da1EXAMPLE",
action == AWS::SSM::getTokenForInstanceAccess,
resource)
when {
resource.hasTag("Environment") &&
resource.getTag("Environment") == "Production"
};