事象
s3cmd利用時に適切に作成したIAMユーザ権限でアクセスしたはずが下記の様なエラーで弾かれてしまった
利用環境 : s3cmd 1.5.0-alpha3 / AmazonLinux2015.09 を利用
1 2 3 | ERROR: S3 error: 403 (AccessDenied): Access Denied ERROR: Parameter problem: Nothing to download. Expecting S3 URI. ... |
切り分け
.s3cfg 中のKeyを適当な物に変更しても/コマンドラインでキーを直接渡しても挙動が変わらない
→ 設定したAccessKey/SecretKeyが利用されていない
では何を利用しているか... インスタンスに設定したIAMロールが使われていた...
対処法
.s3cfg 中の access_token を空に設定した上で access_key と secret_key を設定する
IAM Role無しのインスタンスでs3cmd設定
1 2 3 4 5 6 7 8 | # s3cmd --configure Enter new values or accept defaults in brackets with Enter. Refer to user manual for detailed description of all options. Access key and Secret key are your identifiers for Amazon S3 Access Key: Secret Key: |
IAM Role有りのインスタンスでs3cmd設定
1 2 3 4 5 6 7 8 | # s3cmd --configure Enter new values or accept defaults in brackets with Enter. Refer to user manual for detailed description of all options. Access key and Secret key are your identifiers for Amazon S3 Access Key [ASXXXXXX]: <- .s3cfg ファイルは無いが既にKeyが入っている Secret Key [iSXXXXXX]: <- .s3cfg ファイルは無いが既にKeyが入っている |
どこから Key を自動取得しているか?
ソースを確認した所 Config実行時に下記処理が入っておりました
インスタンスメタデータにアクセスしRoleが設定してある場合、そのロールのAccessKey/SecretKey/Token を取得しておりました。
http://169.254.169.254/latest/meta-data/iam/security-credentials/
http://169.254.169.254/latest/meta-data/iam/security-credentials/[ロール名]
s3cmdソースにてroleを取得している箇所
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def role_config( self ): conn = httplib.HTTPConnection(host = '169.254.169.254' ,timeout = 0.1 ) try : conn.request( 'GET' , "/latest/meta-data/iam/security-credentials/" ) resp = conn.getresponse() files = resp.read() if resp.status = = 200 and len (files)> 1 : conn.request( 'GET' , "/latest/meta-data/iam/security-credentials/%s" % files) resp = conn.getresponse() if resp.status = = 200 : creds = json.load(resp) Config().update_option( 'access_key' , creds[ 'AccessKeyId' ].encode( 'ascii' )) Config().update_option( 'secret_key' , creds[ 'SecretAccessKey' ].encode( 'ascii' )) Config().update_option( 'access_token' , creds[ 'Token' ].encode( 'ascii' )) else : raise IOError else : raise IOError except : raise |
EC2に付与したIAM Roleを確認
EC2に付与したIAM Roleのキー/Tokenを確認
1 2 3 4 5 6 7 8 9 | { "Code" : "Success", "LastUpdated" : "2015-11-17T06:51:35Z", "Type" : "AWS-HMAC", "AccessKeyId" : "Axxx", "SecretAccessKey" : "Kxxx", "Token" : "AQzxxxxxxxxxxxgU=", "Expiration" : "2015-11-17T13:09:15Z" |
投稿者プロフィール
-
Japan AWS Ambassadors 2023, 2024
開発会社での ASP型WEBサービス企画 / 開発 / サーバ運用 を経て
2010年よりスカイアーチネットワークスに在籍しております
機械化/効率化/システム構築を軸に人に喜んで頂ける物作りが大好きです。
最新の投稿
Amazon Q2025年6月11日Amazon Q Developer CLI でMCPサーバ起動に失敗したのでトラブルシュートしてもらった
AWS2025年2月25日WebスクレイピングにAmazon CloudWatch Synthetics CanaryとBedrockを利用する
AWS re:Invent 20242025年1月1日AWS re:Invent 2024から読み解く、次世代クラウドコンピューティングの潮流
AWS re:Invent 20242024年12月7日Bedrock Flows と Amazon Q Developer Agentの新機能で生成AIアプリを短時間で作成する