📝

Secrets Manager の GetRandomPassword ではリソースレベルでのアクセス許可ができない話

2025/02/10に公開

Actions, resources, and condition keys for AWS Secrets Manager - Service Authorization Reference

Actions Description Access level Resource types (*required)
GetRandomPassword Grants permission to generate a random string for use in password creation Read

上記ドキュメントに記載の通り、GetRandomPassword では以下のようなリソースレベルでのアクセス許可がサポートされていません。

{ 
    "Action": "secretsmanager:GetRandomPassword", 
    "Effect": "Allow", 
    "Resource": "arn:aws:secretsmanager:ap-northeast-1:012345678901:secret:test-??????", 
}

そのため、GetRandomPassword のようにリソースレベルでのアクセス許可がサポートされていないアクションについては、リソースをワイルドカードで指定する必要があります。

If there is no value for this column, you must specify all resources ("*") to which the policy applies in the Resource element of your policy statement.

{ 
    "Action": "secretsmanager:GetRandomPassword", 
    "Effect": "Allow", 
    "Resource": "*", 
}

試してみた

今回は CloudFormation で試してみました。
参考にしたテンプレートは以下のドキュメントのテンプレートです。
Create an AWS Secrets Manager secret with AWS CloudFormation - AWS Secrets Manager

Name プロパティのみ追加した以下のテンプレートを使用します。

Resources:
  CloudFormationCreatedSecret:
    Type: "AWS::SecretsManager::Secret"
    Properties:
      Name: test # 追加
      Description: Simple secret created by AWS CloudFormation.
      GenerateSecretString:
        SecretStringTemplate: '{"username": "saanvi"}'
        GenerateStringKey: password
        PasswordLength: 32

失敗する IAM ポリシー

CloudFormation で以下の権限を付与した IAM ロールを使用してスタックをデプロイします。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:*"
            ],
            "Resource": "*"
        },
        {
            "Action": "secretsmanager:*",
            "Effect": "Allow",
            "Resource": "arn:aws:secretsmanager:ap-northeast-1:012345678901:secret:test-??????"
        }
    ]
}

上記 IAM ポリシーでは Secrets Manager に関するすべてのアクションで特定のシークレットに関するアクセスを許可しています。

CloudFormation スタックをデプロイしたところ、以下のエラーが発生しました。

Resource handler returned message: "User: arn:aws:sts::012345678901:assumed-role/test/AWSCloudFormation is not authorized to perform: secretsmanager:GetRandomPassword because no identity-based policy allows the secretsmanager:GetRandomPassword action

上記エラーは GetRandomPassword のアクションが IAM ポリシーで許可されていないことを示しています。

成功する IAM ポリシー

上述の失敗する IAM ポリシーを変更して、GetRandomPassword に関するリソースをワイルドカードで指定します。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "cloudformation:*"
            ],
            "Resource": "*"
        },
        {
            "Action": "secretsmanager:*",
            "Effect": "Allow",
            "Resource": "arn:aws:secretsmanager:ap-northeast-1:012345678901:secret:test-??????"
        },
        {
            "Action": "secretsmanager:GetRandomPassword",
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}

再度同じ CloudFormation テンプレートをデプロイすることでシークレットの作成に成功しました。

ワイルドカード ?????? について

上記検証では Secrets Manager に関するすべてのアクションに対するリソース指定で、リソース名の末尾にワイルドカード ?????? を指定しています。

{
  "Action": "secretsmanager:*",
  "Effect": "Allow",
  "Resource": "arn:aws:secretsmanager:ap-northeast-1:012345678901:secret:test-??????"
}

Identity-based policies - AWS Secrets Manager

Secrets Manager appends six random characters to secret names as part of their ARN, so you can use this wildcard to match those characters.

Secrets Manager はシークレットの作成時に末尾にハイフンとランダムな 6 文字を追加する仕様があるため、以下のリソース定義ではシークレットの作成に失敗します。

{
  "Action": "secretsmanager:*",
  "Effect": "Allow",
  "Resource": "arn:aws:secretsmanager:ap-northeast-1:012345678901:secret:test
}
Resource handler returned message: "User: arn:aws:sts::012345678901:assumed-role/test/AWSCloudFormation is not authorized to perform: secretsmanager:CreateSecret on resource: test because no identity-based policy allows the secretsmanager:CreateSecret action

未作成のシークレットに対するリソースレベルでのアクセス許可を定義するためには、ハイフンと ??????のワイルドカードを追加する必要があります。

まとめ

今回は Secrets Manager の GetRandomPassword ではリソースレベルでのアクセス許可ができない話を紹介しました。
どなたかの参考になれば幸いです。

参考資料

Discussion