claude-skills/

Anthropic公式スキル・プラグインの日本語ディレクトリ

last sync 22h ago
スキルOfficialdevelopment

🔐aws-secrets-manager

プラグイン
aws-core

説明

AWS Secrets Manager のシークレット安全管理、認証情報、APIキー、トークン、パスワードの保護に対応するスキル。 AIエージェントがシークレットの値を直接取得することを防ぎ、`asm-exec` を使ったランタイム動的参照の手法を提示することで、平文がLLMのコンテキストウィンドウに入り込まないようにします。

原文を表示

Secret safety for AWS Secrets Manager, secret management, credentials, API keys, tokens, and passwords. Prevents AI agents from directly fetching secret values and teaches runtime dynamic references with asm-exec so plaintext never enters the LLM context window.

ユースケース

  • AWS Secrets Manager のシークレットを安全に管理したい
  • 認証情報やAPIキーを保護する必要がある
  • パスワードやトークンの漏洩を防ぎたい
  • LLMコンテキストに平文が入り込まないようしたい

本文(日本語訳)

Agentでシークレットを安全に扱う

概要

AIエージェントがシェルやAWS APIへのアクセス権を持つ状態で、シークレット・認証情報・APIキー・トークン・パスワードを扱う場合、aws secretsmanager get-secret-value を呼び出してコンテキストウィンドウ内に平文の値を受け取ることができます。これにはリスクが伴います。シークレットがログ・会話履歴・下流のツール呼び出しに漏洩する可能性があります。

このスキルでは、より安全なパターンを学習します。動的参照(dynamic references) をラッパースクリプト(asm-exec)が実行時に解決することで、Agentがシークレットの値を直接参照することなく処理を行います。

ベストエフォートの防御であり、セキュリティ境界ではありません。 最も一般的な漏洩経路を防ぐものですが、すべての回避手段を防ぐことはできません。 IAM最小権限・CloudTrailモニタリング・VPCエンドポイントポリシーと組み合わせて使用してください。


ルール

シークレットを扱う際は、以下のルールを必ず遵守してください。

  1. get-secret-value または batch-get-secret-value を呼び出してはなりません。 AWS CLI・SDK・MCPツール・curl、その他いかなる手段によるものも含みます。
  2. Secrets Manager Agent(SMA)デーモン(localhost:2773 またはそのループバック変形)から直接シークレット値を読み取ろうとしてはなりません。
  3. {{resolve:secretsmanager:...}} 参照を使用しなければなりません。 これらは asm-exec によって実行時に解決され、Agentに値が公開されることはありません。

{{resolve:...}} 構文

{{resolve:secretsmanager:<secret-id>:<field-type>:<json-key>:<version-stage>}}
コンポーネント 必須 デフォルト
secret-id 必須 -- prod/db-creds またはフルARN
field-type 任意 SecretString SecretString
json-key 任意 (値全体) password
version-stage 任意 AWSCURRENT AWSPENDING

asm-exec の使い方

asm-exec は、コマンド引数および環境変数内の {{resolve:...}} 参照を解決し、対象コマンドを exec するラッパーです。シークレットの値は子プロセス内にのみ存在し、Agentのコンテキストには一切渡りません。

使用例

# データベースパスワードを psql に渡す(値は外部に露出しない)
asm-exec -- psql \
  "host=mydb.example.com \
   user={{resolve:secretsmanager:prod/db-creds:SecretString:username}} \
   password={{resolve:secretsmanager:prod/db-creds:SecretString:password}}" \
  -c "SELECT * FROM users LIMIT 10"

# field-type のデフォルト(SecretString)と値全体(json-key なし)を使用
asm-exec -- curl -H "Authorization: Bearer {{resolve:secretsmanager:prod/api-token}}" \
  https://api.example.com/data

# 1つのコマンドで複数のシークレットを使用
asm-exec -- mysql \
  -h {{resolve:secretsmanager:prod/mysql:SecretString:host}} \
  -u {{resolve:secretsmanager:prod/mysql:SecretString:username}} \
  -p{{resolve:secretsmanager:prod/mysql:SecretString:password}} \
  -e "SHOW TABLES"

動作の仕組み

  1. すべてのコマンド引数から {{resolve:...}} パターンをスキャンします。
  2. 各参照を、利用可能な最初のバックエンドを通じて順番に解決します。
    1. AWS Secrets Manager Agent(SMA) — localhost:2773(ゼロレイテンシ・キャッシュ済み)
    2. AWS MCPエンドポイントhttps://aws-mcp.us-east-1.api.aws/mcp) — SigV4署名済みリクエストで aws___call_aws ツールを呼び出す
    3. ARNのリージョンセグメント、または AWS_REGION / AWS_DEFAULT_REGION からシークレットのリージョンを判定し、リゾルバに渡します。
  3. re.sub とcallableを使って解決済みの値を置換します(シングルパス処理により、シークレット値に {{resolve:...}} が含まれていてもリスキャンインジェクションを防止)。
  4. subprocess.run でターゲットコマンドを実行します。シークレットの値は asm-exec プロセス内にのみ存在し、Agentのコンテキストウィンドウには一切渡りません。

解決にローカルのAWS CLIフォールバックは使用しません。 asm-exec は参照を解決するために aws secretsmanager get-secret-value をシェルアウト実行しません。 解決はSMAまたはMCPエンドポイントのみを通じて行われるため、平文の値がローカルプロセスの標準出力に書き出されてキャプチャされることはありません。

SigV4署名

MCPエンドポイントはすべてのツール呼び出しをAWS SigV4で認証します。asm-exec はPython標準ライブラリ(hashlib / hmac)のみを使用して自前でリクエストに署名します。botocoreへの依存も mcp-proxy-for-aws プロキシの起動も不要であり、ラッパーを軽量なエフェメラルプロセスとして保ちます。署名に使用するサービスとリージョンはエンドポイントのホスト名から推定されます(例: aws-mcp.us-east-1.api.aws → サービス aws-mcp、リージョン us-east-1)。この署名リージョンは、シークレット自体のリージョンとは独立しており、後者はサーバーサイドのCLIコマンドに --region として渡されます。

署名に使用するクレデンシャルは以下の順で解決されます: 環境変数(AWS_ACCESS_KEY_ID 等)→ aws configure export-credentials(AWS CLI v2)→ aws configure get(AWS CLI v1)。

前提条件

以下いずれかのバックエンドが secretsmanager:GetSecretValue 権限を持つクレデンシャルで到達可能である必要があります。

  • AWS Secrets Manager Agent(SMA) が localhost:2773 で起動していること、または
  • MCPエンドポイントのSigV4署名に使用できるAWSクレデンシャルが解決可能であること(上記参照)。 クロスリージョンのシークレットの場合は、正しいリージョンを指定するために AWS_REGION を設定するか、フルARNを使用してください。

SMAセットアップガイド も参照してください。


よく使うパターン

データベース接続

asm-exec -- psql "postgresql://{{resolve:secretsmanager:prod/db:SecretString:username}}:{{resolve:secretsmanager:prod/db:SecretString:password}}@db.example.com:5432/mydb"

Dockerでシークレットを使用する

asm-exec -- docker run -e "DB_PASSWORD={{resolve:secretsmanager:prod/db:SecretString:password}}" myapp:latest

設定ファイルのテンプレート生成

# 解決済みのシークレットを含む設定ファイルを生成してファイルに書き出す
asm-exec -- sh -c 'echo "password={{resolve:secretsmanager:app/db:SecretString:password}}" > /tmp/app.conf'

構造的な強制(Pluginフック)

aws-core pluginが有効な場合、PreToolUse フックがAWS CLI・MCPツール・直接SMAアクセスを問わず、get-secret-value または batch-get-secret-value の呼び出し試行を自動的にブロックします。手動での設定は不要です。

フックは plugins/aws-core/hooks/hooks.json に定義されており、pluginのインストール時に自動で有効化されます。


トラブルシューティング

「Secret not found」エラー

シークレットが存在すること、およびIAMロールに secretsmanager:GetSecretValue 権限があることを確認してください。シークレット名が完全一致していること(大文字・小文字を区別)を確認してください。

SMA接続拒否

Secrets Manager Agentが起動していない可能性があります。これは致命的ではなく、asm-exec はSigV4署名済みのMCPエンドポイントにフォールバックします。そのバックエンドが認証できるよう、AWSクレデンシャルが解決可能であることを確認してください(上記「SigV4署名」参照)。

「Failed to resolve」エラー

両方のバックエンドに到達できないか、値が返されませんでした。以下を確認してください。

  • SMAが起動しているか、またはAWSクレデンシャルが有効であること(aws sts get-caller-identity で確認)
  • シークレットのリージョンが正しいこと(AWS_REGION を設定するか、フルARNを使用)
  • 対象のシークレットに対して secretsmanager:GetSecretValue 権限があること

MCPエンドポイントからの 401 はシークレットが存在しないのではなく、SigV4署名またはクレデンシャルの問題を示しています。

解決結果が空文字列になる

シークレット値にそのJSONキーが存在しない可能性があります。AWSコンソールでシークレットの構造を確認するか、シークレットの管理者に利用可能なキーを確認してください。

原文(English)を表示

Using Secrets Safely with Agents

Overview

When AI agents handle secrets, credentials, API keys, tokens, or passwords with shell or AWS API access, they can call aws secretsmanager get-secret-value and receive plaintext values in their context window. This creates risk: secrets may leak into logs, conversation history, or downstream tool calls.

This skill teaches a safer pattern: dynamic references resolved at runtime by a wrapper script (asm-exec), so the agent never sees the secret value.

Best-effort defense, not a security boundary. This prevents the most common leakage path but cannot stop all evasion vectors. Combine with IAM least-privilege, CloudTrail monitoring, and VPC endpoint policies.

Rules

You MUST follow these rules when working with secrets:

  1. MUST NOT call get-secret-value or batch-get-secret-value -- not via AWS CLI, SDK, MCP tools, curl, or any other mechanism.
  2. MUST NOT attempt to read secret values from the Secrets Manager Agent (SMA) daemon directly (localhost:2773 or any loopback variant).
  3. MUST use {{resolve:secretsmanager:...}} references -- these are resolved at runtime by asm-exec without exposing values to you.

The {{resolve:...}} Syntax

{{resolve:secretsmanager:<secret-id>:<field-type>:<json-key>:<version-stage>}}
Component Required Default Example
secret-id Yes -- prod/db-creds or full ARN
field-type No SecretString SecretString
json-key No (full value) password
version-stage No AWSCURRENT AWSPENDING

Using asm-exec

asm-exec is a wrapper that resolves {{resolve:...}} references in command arguments and environment variables, then execs the target command. The secret value exists only in the child process -- never in the agent's context.

Usage

# Pass a database password to psql without exposing it
asm-exec -- psql \
  "host=mydb.example.com \
   user={{resolve:secretsmanager:prod/db-creds:SecretString:username}} \
   password={{resolve:secretsmanager:prod/db-creds:SecretString:password}}" \
  -c "SELECT * FROM users LIMIT 10"

# Use default field-type (SecretString) and full value (no json-key)
asm-exec -- curl -H "Authorization: Bearer {{resolve:secretsmanager:prod/api-token}}" \
  https://api.example.com/data

# Multiple secrets in one command
asm-exec -- mysql \
  -h {{resolve:secretsmanager:prod/mysql:SecretString:host}} \
  -u {{resolve:secretsmanager:prod/mysql:SecretString:username}} \
  -p{{resolve:secretsmanager:prod/mysql:SecretString:password}} \
  -e "SHOW TABLES"

How It Works

  1. Scans all command arguments for {{resolve:...}} patterns
  2. Resolves each reference through the first available backend, in order:
    1. AWS Secrets Manager Agent (SMA) on localhost:2773 (zero-latency, cached)
    2. AWS MCP endpoint (https://aws-mcp.us-east-1.api.aws/mcp), calling the aws___call_aws tool over a SigV4-signed request
    3. Determines the secret's region from an ARN's region segment, or from AWS_REGION / AWS_DEFAULT_REGION, and passes it to the resolver
  3. Substitutes resolved values using re.sub with a callable (single-pass -- prevents re-scan injection if a secret value contains {{resolve:...}})
  4. Runs the target command via subprocess.run -- secret values exist only in the asm-exec process, never in the agent's context window

No local AWS CLI fallback for resolution. asm-exec does not shell out to aws secretsmanager get-secret-value to resolve references. Resolution happens only through SMA or the MCP endpoint, so the plaintext value is never written to a local process's stdout where it could be captured.

SigV4 signing

The MCP endpoint authenticates every tool call with AWS SigV4. asm-exec signs requests itself using only the Python standard library (hashlib/hmac) -- it does not depend on botocore or spin up the mcp-proxy-for-aws proxy, keeping the wrapper a lightweight ephemeral process. The signing service and region are inferred from the endpoint hostname (e.g. aws-mcp.us-east-1.api.aws -> service aws-mcp, region us-east-1); this signing region is independent of the secret's own region, which is passed as --region to the server-side CLI command.

Credentials for signing are resolved in order: environment variables (AWS_ACCESS_KEY_ID etc.), aws configure export-credentials (AWS CLI v2), then aws configure get (AWS CLI v1).

Prerequisites

Either backend must be reachable, with credentials that have secretsmanager:GetSecretValue permission:

  • AWS Secrets Manager Agent (SMA) running on localhost:2773, OR
  • AWS credentials resolvable for SigV4 signing of the MCP endpoint (see above). For cross-region secrets, set AWS_REGION (or use a full ARN) so the correct region is targeted.

See SMA setup guide.

Common Patterns

Database connections

asm-exec -- psql "postgresql://{{resolve:secretsmanager:prod/db:SecretString:username}}:{{resolve:secretsmanager:prod/db:SecretString:password}}@db.example.com:5432/mydb"

Docker with secrets

asm-exec -- docker run -e "DB_PASSWORD={{resolve:secretsmanager:prod/db:SecretString:password}}" myapp:latest

Configuration file templating

# Generate config with resolved secrets, write to file
asm-exec -- sh -c 'echo "password={{resolve:secretsmanager:app/db:SecretString:password}}" > /tmp/app.conf'

Structural Enforcement (Plugin Hook)

When the aws-core plugin is enabled, a PreToolUse hook automatically blocks any attempt to call get-secret-value or batch-get-secret-value -- via AWS CLI, MCP tools, or direct SMA access. No manual configuration needed.

The hook is defined at plugins/aws-core/hooks/hooks.json and activates automatically when the plugin is installed.

Troubleshooting

"Secret not found" errors

Verify the secret exists and your IAM role has secretsmanager:GetSecretValue permission. Check the secret name matches exactly (case-sensitive).

SMA connection refused

The Secrets Manager Agent may not be running. This is non-fatal: asm-exec falls through to the SigV4-signed MCP endpoint. Ensure AWS credentials are resolvable (see SigV4 signing above) so that backend can authenticate.

"Failed to resolve" errors

Both backends were unreachable or returned no value. Check that either SMA is running or AWS credentials are valid (aws sts get-caller-identity), that the secret's region is correct (set AWS_REGION or use a full ARN), and that your identity has secretsmanager:GetSecretValue on the secret. A 401 from the MCP endpoint indicates a SigV4 signing or credential problem, not a missing secret.

Resolution produces empty string

The JSON key may not exist in the secret value. Verify the secret structure in the AWS Console or ask the secret owner to confirm the available keys.

原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。