Amazon Q を使用してジャストインタイムノードアクセスの承認ポリシーを作成する - AWS Systems Manager

Amazon Q を使用してジャストインタイムノードアクセスの承認ポリシーを作成する

コマンドライン用の Amazon Q Developer を使用すると、ソフトウェア開発のさまざまな側面でガイダンスとサポートが提供されます。ジャストインタイムノードアクセスの場合、Amazon Q は、ポリシーのコードの生成および更新や、ポリシーステートメントの分析などにより、承認ポリシーを作成する際に役立ちます。以下の情報は、コマンドライン用の Amazon Q を使用して承認ポリシーを作成する方法を示しています。

ユースケースの特定

承認ポリシーを作成する最初のステップは、ユースケースを明確に定義することです。例えば、組織で Environment:Testing タグを持つノードへのアクセスリクエストを自動的に承認できます。また、従業員 ID が TEMP で始まる場合に、Environment:Production タグを持つノードへの自動承認を明示的に拒否することもできます。Tier:Database タグを持つノードについて、2 つのレベルの手動承認を必要とすることもできます。

特定のシナリオで、あるポリシーまたは条件を別のポリシーまたは条件よりも優先できます。そのため、ユースケースと設定に最適なステートメントを決定するために必要なポリシーの動作を明確に定義することをお勧めします。

開発環境をセットアップする

承認ポリシーを作成するコマンドライン用の Amazon Q をインストールします。コマンドライン用の Amazon Q のインストールについては、「Amazon Q Developer ユーザーガイド」の「Installing Amazon Q for command line」を参照してください。

また、AWS ドキュメントの MCP サーバーをインストールすることをお勧めします。この MCP サーバーは、コマンドライン用の Amazon Q を最新のドキュメントリソースに接続します。コマンドライン用の Amazon Q との MCP の使用の詳細については、「Amazon Q Developer ユーザーガイド」の「Using MCP with Amazon Q Developer」を参照してください。

AWS ドキュメント MCP サーバーの詳細については、「AWS Documentation MCP Server」を参照してください。

まだ AWS CLI をインストールして設定していない場合は、インストールして設定します。詳細については、「AWS CLI の最新バージョンのインストールまたは更新」を参照してください。

承認ポリシーの内容を開発する

ユースケースを特定して環境をセットアップしたら、ポリシーの内容を開発できます。主にユースケースと設定によって、使用する承認ポリシーとステートメントのタイプが決まります。

特定のポリシーの使用方法が不明な場合や、ポリシーのスキーマに関する詳細情報が必要な場合は、「ノードの承認ポリシーを作成する」および以降のトピックを参照してください。これらのトピックでは、ポリシーの評価方法を詳しく説明し、実用的なステートメントの例を示しています。

以下の手順は、コマンドライン用の Amazon Q を使用して承認ポリシーを作成する方法を示しています。

注記

Amazon Q Developer は生成 AI を使用します。レスポンスの検証が必要になる場合があります。「AWS 責任ある AI ポリシー」を参照してください。

コマンドライン用の Amazon Q を使用して承認ポリシーを作成するには
  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 リージョンの ID とノードのサブセットを使用して承認ポリシーをテストします。

    注記

    自動承認ポリシーの場合、ドキュメント名は SSM-JustInTimeAccessAutoApprovalPolicy である必要があります。AWS アカウントおよび AWS リージョンごとに 1 つの自動承認ポリシーのみが存在できます。アクセス拒否ポリシーの場合、ドキュメント名は SSM-JustInTimeAccessDenyAccessOrgPolicy である必要があります。AWS Organizations 組織ごとに 1 つのアクセス拒否ポリシーのみが存在できます。このポリシーは、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"