Open3

Amazon Bedrockのセキュリティ関連メモ

dehio3dehio3

Building with Amazon Bedrockから学ぶ

https://catalog.us-east-1.prod.workshops.aws/workshops/5677057b-93d7-4354-9a6c-db9434c15b5c/ja-JP

セキュリティとセーフガード ラボ

ガードレースを作成するTerraformリソース
https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/bedrock_guardrail

設定項目

  • 拒否されたトピック
    • ユーザーの入力とモデルの応答で特定のトピックをブロックする
  • ワードフィルター
    • ユーザー入力とモデル応答の両方で、特定の単語をブロックする
  • 機密情報フィルター
    • 個人を特定できる情報 (PII) などの機密情報をマスクまたはブロックする
    • 正規表現 (RegEx) を使用して、アカウント番号などの情報の種類にパターンマッチでマスクまたはブロックする
  • グラウディングチェック
    • 参照ソースで提供された情報に基づいて、モデルの応答に根拠があり、事実的に正しいかどうかを検証する
    • 定義されたグラウンディングのしきい値を下回る応答をブロックする
      • グラウンディングスコアは、モデルの応答が事実上正しく、ソースに根拠があるという信頼度
  • 関連性チェック
    • モデルの応答がユーザーのクエリに関連しているかどうかを検証
    • 定義された関連性のしきい値を下回る応答をブロックする
      • 関連性スコアは、モデルの応答がユーザーのクエリに関連しているという信頼度

利用方法

  • ガードレールを呼び出す際には、ガードレール ID とガードレールのバージョンを指定する
dehio3dehio3

ガードレールの強制適用ができそう?
https://aws.amazon.com/jp/about-aws/whats-new/2025/03/amazon-bedrock-guardrails-policy-based-enforcement-responsible-ai/

そもそもBedrockに必要なポリシーを調べる
https://docs.aws.amazon.com/ja_jp/bedrock/latest/userguide/security_iam_id-based-policy-examples.html

モデルに対するアクセス許可の例

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ProvisionedThroughputModelInvocation",
            "Effect": "Allow",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream"
            ],
            "Resource": "arn:aws:bedrock:aws-region:111122223333:provisioned-model/${my-provisioned-model}"
        }
    ]
}

上記の権限を設定してリクエストを投げてみる

aws bedrock-runtime invoke-model \
  --model-id anthropic.claude-3-5-sonnet-20240620-v1:0 \
  --profile sandbox-bedrock \
  --body "$(echo '{
  "anthropic_version": "bedrock-2023-05-31",
  "max_tokens": 1000,
  "messages": [
    {
      "role": "user",
      "content": "Hello, please introduce yourself."
    }
  ],
  "temperature": 0.7
}' | base64)" \
  --content-type application/json \
  --region ap-northeast-1 \
  output.json && cat output.json | jq
output
{
    "contentType": "application/json"
}
{
  "id": "msg_bdrk_01JXU51NQdXkDB1cc7koH23y",
  "type": "message",
  "role": "assistant",
  "model": "claude-3-5-sonnet-20240620",
  "content": [
    {
      "type": "text",
      "text": "Hello! I'm an AI assistant created by Anthropic to be helpful, harmless, and honest. I don't have a body or avatar - I'm just a conversational AI. How can I help you today?"
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 13,
    "output_tokens": 49
  }
}

強制する場合のポリシー例

{
    "Version": "2012-10-17",
    "Statement": [{
            "Sid": "InvokeFoundationModelStatement1",
            "Effect": "Allow",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream"
            ],
            "Resource": [
                "arn:aws:bedrock:region::foundation-model/*"
            ],
            "Condition": {
                "StringEquals": {
                    "bedrock:GuardrailIdentifier": "arn:aws:bedrock:region:account-id:guardrail/guardrail-id:1"
                }
            }
        },
        {
            "Sid": "InvokeFoundationModelStatement2",
            "Effect": "Deny",
            "Action": [
                "bedrock:InvokeModel",
                "bedrock:InvokeModelWithResponseStream"
            ],
            "Resource": [
                "arn:aws:bedrock:region::foundation-model/*"
            ],
            "Condition": {
                "StringNotEquals": {
                    "bedrock:GuardrailIdentifier": "arn:aws:bedrock:region:account-id:guardrail/guardrail-id:1"
                }
            }
        },
        {
            "Sid": "ApplyGuardrail",
            "Effect": "Allow",
            "Action": [
                "bedrock:ApplyGuardrail"
            ],
            "Resource": [
                "arn:aws:bedrock:region:account-id:guardrail/guardrail-id"
            ]
        }
    ]
}
$ aws bedrock-runtime invoke-model \
  --model-id anthropic.claude-3-5-sonnet-20240620-v1:0 \
  --profile sandbox-bedrock \
  --body "$(echo '{
  "anthropic_version": "bedrock-2023-05-31",
  "max_tokens": 1000,
  "messages": [
    {
      "role": "user",
      "content": "Hello, please introduce yourself."
    }
  ],
  "temperature": 0.7
}' | base64)" \
  --content-type application/json \
  --region ap-northeast-1 \
  output.json && cat output.json | jq
output
An error occurred (AccessDeniedException) when calling the InvokeModel operation: User: arn:aws:iam::*******:user/sample is not authorized to perform: bedrock:InvokeModel on resource: arn:aws:bedrock:ap-northeast-1::foundation-model/anthropic.claude-3-5-sonnet-20240620-v1:0 with an explicit deny in an identity-based policy

弾かれる

ガードレールオプションを追加する

--guardrail-identifier "arn:aws:bedrock:ap-northeast-1:*:guardrail/"
--guardrail-version 1 \

aws bedrock-runtime invoke-model \
  --model-id anthropic.claude-3-5-sonnet-20240620-v1:0 \
  --guardrail-identifier "arn:aws:bedrock:ap-northeast-1:**********:guardrail/*********" \
  --guardrail-version 1 \
  --profile sandbox-bedrock \
  --body "$(echo '{
  "anthropic_version": "bedrock-2023-05-31",
  "max_tokens": 1000,
  "messages": [
    {
      "role": "user",
      "content": "Hello, please introduce yourself."
    }
  ],
  "temperature": 0.7
}' | base64)" \
  --content-type application/json \
  --region ap-northeast-1 \
  output.json && cat output.json | jq

取れた。

output
{
  "id": "msg_bdrk_01WUy17PzBfDAogP1sVE1VRo",
  "type": "message",
  "role": "assistant",
  "model": "claude-3-5-sonnet-20240620",
  "content": [
    {
      "type": "text",
      "text": "Hello! I'm an AI assistant created by Anthropic to be helpful, harmless, and honest. I don't have a body or image, I'm just a conversational AI. How can I help you today?"
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 13,
    "output_tokens": 49
  },
  "amazon-bedrock-guardrailAction": "NONE"
}

オプションにガードレールを指定することを強制しているだけなので、全てのリクエストに強制的にガードレールを適用した要件にはマッチしなかった