aws-sdkでAccess Deniedの場合にどの権限が足りないのかを調べる方法

rubyの話だけど、aws-sdkを使って何かをしたときに次のようにAccess Deniedってエラーが出ることがある。

Aws::S3::Errors::AccessDenied (Access Denied)

これは、IAMとかで権限を絞っているときに、aws-sdkで使用とした操作に比べてIAMで許可されている操作が足りないぜ、って時に出るエラー。
なのだけど、Access Deniedだけ言われても何の権限が足りないかさっぱりわからんのだよね。

そんなときに使えるのがhttp_wire_traceオプション。
これは、設定時だったり

AWS.config(http_wire_trace: true)

オブジェクトをnewするときだったり

Aws::S3::Client.new(http_wire_trace: true)

などに遣うことができて、このオプションをtrueにすることにより、aws-sdkによるawsのAPI呼び出し時に次のような出力がなされる。

opening connection to test-bucket.s3.ap-northeast-1.amazonaws.com:443...
opened
starting SSL for test-bucket.s3.ap-northeast-1.amazonaws.com:443...
SSL established, protocol: TLSv1.2, cipher: ECDHE-RSA-AES128-GCM-SHA256
<- "PUT /cache/uploads/0b33081644485700878a9dca0b61808c.png HTTP/1.1\r\nContent-Type: image/png\r\nAccept-Encoding: \r\nUser-Agent: aws-sdk-ruby3/3.104.3 ruby/2.7.1 x86_64-darwin19 aws-sdk-s3/1.76.0\r\nContent-Disposition: inline; filename=\"IMG_2731.png\"; filename*=UTF-8''IMG_2731.png\r\nExpect: 100-continue\r\nContent-Md5: yc4QKJ2Tf7UrgSvzTXW74g==\r\nHost: test-bucket.s3.ap-northeast-1.amazonaws.com\r\nX-Amz-Date: 20200812T085636Z\r\nX-Amz-Content-Sha256: 20b101390f657a19375f2ee5467e217483d0deae290557e9d16ddabc66556963\r\nAuthorization: AWS4-HMAC-SHA256 Credential=AKIAZZZZZZZZZZZZZZ/20200812/ap-northeast-1/s3/aws4_request, SignedHeaders=content-disposition;content-md5;content-type;host;user-agent;x-amz-content-sha256;x-amz-date, Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\nContent-Length: 252381\r\nAccept: */*\r\n\r\n"
-> "HTTP/1.1 403 Forbidden\r\n"
-> "x-amz-request-id: 0A74AA9AFA111111\r\n"
-> "x-amz-id-2: Pr+PRUsif6GTPTd3y5UJNSAbLfZZZZZZZZZZZZZZS4aFY+ou8QqQZZZZZZZ2g=\r\n"
-> "Content-Type: application/xml\r\n"
-> "Transfer-Encoding: chunked\r\n"
-> "Date: Wed, 12 Aug 2020 08:56:35 GMT\r\n"
-> "Connection: close\r\n"
-> "Server: AmazonS3\r\n"
-> "\r\n"
-> "f3\r\n"
reading 243 bytes...
-> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Error><Code>AccessDenied</Code><Message>Access Denied</Message><RequestId>0A74AA9AFA111111</RequestId><HostId>Pr+PRZZZZZZZSAbLf8LDlrcOmnfAcEeXZHxZZZZZZZ8QqQ4uhBzQOi4a5ZZZZZZZg=</HostId></Error>"
read 243 bytes
reading 2 bytes...
-> "\r\n"
read 2 bytes
-> "0\r\n"
-> "\r\n"
Conn close

このHTTPリクエストを読むと、S3にオブジェクトをPUT時に403 forbiddenが返っていることが確認できるので、S3のPutObjectの権限を付与すれば解決することがわかる。
便利。

公式ドキュメントはこちら
https://docs.aws.amazon.com/ja_jp/sdk-for-ruby/v3/developer-guide/debugging.html

タイトルとURLをコピーしました