使用 Amazon Q 建立just-in-time節點存取的核准政策 - AWS Systems Manager

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

使用 Amazon Q 建立just-in-time節點存取的核准政策

將 Amazon Q Developer 用於命令列,可在軟體開發的各個層面提供指導和支援。對於just-in-time節點存取,Amazon Q 透過產生和更新政策的程式碼、分析政策陳述式等,協助您建立核准政策。以下資訊說明如何使用 Amazon Q for 命令列建立核准政策。

識別您的使用案例

建立核准政策的第一步是清楚定義您的使用案例。例如,在您的組織中,您可能想要使用 Environment:Testing標籤自動核准對節點的存取請求。如果員工 ID 以 開頭,您可能也想要明確拒絕對具有 Environment:Production標籤的節點進行自動核准TEMP。對於具有Tier:Database標籤的節點,您可能需要兩個層級的手動核准。

在任何特定案例中,您可能偏好一個政策或條件,而不是另一個政策或條件。因此,我們建議您明確定義要決定哪些陳述式最適合您的使用案例和偏好設定的政策行為。

設定開發環境

為您要開發核准政策的命令列安裝 Amazon Q。如需為命令列安裝 Amazon Q 的詳細資訊,請參閱《Amazon Q 開發人員使用者指南》中的為命令列安裝 Amazon Q

我們也建議您為 AWS 文件安裝 MCP 伺服器。此 MCP 伺服器會將 Amazon Q for 命令列連線至最新的文件資源。如需有關在命令列使用 MCP 搭配 Amazon Q 的資訊,請參閱《Amazon Q 開發人員使用者指南》中的搭配使用 MCP 搭配 Amazon Q 開發人員

如需 AWS 文件 MCP 伺服器的詳細資訊,請參閱 AWS 文件 MCP 伺服器

如果您尚未安裝和設定 AWS CLI。如需詳細資訊,請參閱安裝或更新最新版本的 AWS CLI。

開發核准政策內容

識別您的使用案例並設定環境後,您就可以開發政策的內容。您的使用案例和偏好設定將主要決定您使用的核准政策和陳述式類型。

如果您不確定如何使用特定政策,或需要有關政策結構描述的詳細資訊,請參閱 為您的節點建立核准政策和後續主題。這些主題詳細說明如何評估政策,並提供有效的範例陳述式。

下列程序說明如何使用 Amazon Q for 命令列建立核准政策。

注意

Amazon Q Developer 使用生成式 AI。您可能需要驗證回應。請參閱AWS 負責任的 AI 政策

使用 Amazon Q for 命令列建立核准政策
  1. 開啟終端機並使用 Amazon Q 啟動工作階段。

    $ q chat
  2. 輸入提示並描述您要建立哪種政策陳述式。例如,針對核准政策使用先前的案例,您可以輸入下列提示:

    Create a Cedar policy statement that allows a principal to use the “getTokenForInstanceAccess" action for a resource 
    that has a tag key “Environment” and tag value of “Testing”. Use the following example statement for reference to verify the syntax.
    
    permit (
        principal in AWS::IdentityStore::Group::"e8c17310-e011-7089-d989-10da1EXAMPLE",
        action == AWS::SSM::Action::"getTokenForInstanceAccess",
        resource
    )
    when {
        principal has organization && resource.hasTag("Engineering") && resource.hasTag("Production") && principal.organization == "Platform"
    };
    
    The Cedar policy statement you create must be valid. Validate the Cedar statement using the AWS documentation provided for just-in-time node access policy statement structure and make sure you abide by the schema.
  3. 輸入此範例提示會產生下列輸出:

    Based on the AWS documentation for Cedar policy statements and the examples provided, I'll create a valid Cedar policy 
    statement that allows a principal to use the "getTokenForInstanceAccess" action for resources with the tag key "Environment" and tag value "Testing".
                                
    Here's the Cedar policy statement:
    
    permit (
        principal,
        action == AWS::SSM::Action::"getTokenForInstanceAccess",
        resource
    )
    when {
        resource.hasTag("Environment") &&
        resource.getTag("Environment") == "Testing"
    };
    
    
    This policy statement:
    1. Uses the permit effect to allow access
    2. Applies to any principal (since no specific principal is defined)
    3. Specifically targets the "getTokenForInstanceAccess" action
    4. Applies to resources that have a tag with key "Environment" and value "Testing"
    5. Uses the proper syntax for the condition clause with the hasTag() and getTag() operators
    
    The statement follows the Cedar policy language structure as documented in the AWS Systems Manager documentation for just-in-time node access policies.
  4. 將政策陳述式貼到新的 Cedar 檔案中,以驗證語法。您也可以使用 Cedar 遊樂場驗證陳述式。

  5. 驗證陳述式之後,請儲存 Cedar 檔案。

  6. 從終端機執行下列 AWS CLI 命令,以使用您的政策內容建立 SSM 文件。在生產環境中使用核准政策之前,請使用 AWS 帳戶 和 中的身分和節點子集來測試核准政策 AWS 區域。

    注意

    對於自動核准政策,文件名稱必須是 SSM-JustInTimeAccessAutoApprovalPolicy。每個 AWS 帳戶 和 只能有一個自動核准政策 AWS 區域。對於拒絕存取政策,文件名稱必須是 SSM-JustInTimeAccessDenyAccessOrgPolicy。每個 AWS Organizations 組織只能有一個拒絕存取政策,且必須在 Systems Manager 的委派管理員帳戶中建立政策。手動核准政策的命名限制與其他 SSM 文件相同。如需詳細資訊,請參閱 CreateDocument

    Linux & macOS
    aws ssm create-document \ --content file://path/to/file/policyContent.cedar \ --name "SSM-JustInTimeAccessAutoApprovalPolicy" \ --document-type "AutoApproval"
    Windows
    aws ssm create-document ^ --content file://C:\path\to\file\policyContent.cedar ^ --name "SSM-JustInTimeAccessAutoApprovalPolicy" ^ --document-type "AutoApproval"
    PowerShell
    $cedar = Get-Content -Path "C:\path\to\file\policyContent.cedar" | Out-String New-SSMDocument ` -Content $cedar ` -Name "SSM-JustInTimeAccessAutoApprovalPolicy" ` -DocumentType "AutoApproval"