自動核准和拒絕存取政策的陳述式結構和內建運算子 - AWS Systems Manager

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

自動核准和拒絕存取政策的陳述式結構和內建運算子

下表顯示自動核准和拒絕存取政策的結構。

元件 語法
效用

permit | forbid

scope

(principal, action, resource)

條件子句
when { principal or resource has attribute name };

政策元件

自動核准或拒絕存取政策包含下列元件:

  • 效果permit(允許) 或 forbid(拒絕) 存取。

  • 範圍 – 套用效果的主體、動作和資源。您可以透過無法識別特定委託人、動作或資源,讓 Cedar 中的範圍保持未定義。在此情況下,政策適用於所有可能的委託人、動作和資源。對於just-in-time節點存取, 一律actionAWS::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" }

如需額外的範例,請參閱範例政策陳述式

Just-in-time節點存取結構描述

以下是just-in-time節點存取的 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 } }; }

內建運算子

使用各種條件建立自動核准或拒絕存取政策的內容時,您可以使用 &&運算子來新增其他條件。您也可以使用許多其他內建運算子,為您的政策條件新增額外的表達式能力。下表包含所有內建運算子以供參考。

運算子 類型和過載 描述

!

布林值 → 布林值

邏輯不是。

==

任何 → 任何

平等。適用於任何類型的引數,即使類型不相符。不同類型的值永遠不會彼此相等。

!= 任何 → 任何

不等式;完全相同的反向 (請參閱上述)。

<

(長、長) → 布林值

小於 的長整數。

<=

(長、長) → 布林值

less-than-or-equal-to的長整數。

>

(長、長) → 布林值

大於 的長整數。

>=

(長、長) → 布林值

greater-than-or-equal-to。

in (實體、實體) → 布林值 階層成員資格 (反射:A 中的 A 一律為 true)。
(實體,set(實體)) → 布林值 階層成員資格:如果 (A 和 B) || (C 中的 A) || ... 如果集合包含非實體,則 【B、C、...】 中的 A 為 true。
&& (布林值、布林值) → 布林值

邏輯 和 (短路)。

|| (布林值、布林值) → 布林值

邏輯 或 (短路)。

.exists() 實體 → 布林值 實體存在。
具有 (實體、屬性) → 布林值 Infix Operator。 會e has f測試記錄或實體是否e具有屬性 的繫結ffalse 如果 e 不存在或 e存在但 沒有 屬性 ,則傳回 f。屬性可以表示為識別符或字串常值。
like (字串、字串) → 布林值 Infix Operator。 會t like p檢查文字是否t與模式 相符p,其中可能包含*符合 0 或多個任何字元的萬用字元。為了符合 中的常值星星字元t,您可以使用 \*中的特殊逸出字元序列p
.hasTag() (實體、字串) → 布林值 檢查實體是否已套用指定的標籤。
.getTag() (實體、字串) → 布林值 傳回指定標籤索引鍵的值。
.contains() (集合,任何) → 布林值 設定成員資格 (B 為 A 元素)。
.containsAll() (設定、設定) → 布林值 測試集合 A 是否包含集合 B 中的所有元素。
.containsAny() (設定、設定) → 布林值 測試集合 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" };