s3cmdで指定したIAMユーザの権限が利用されない場合のトラブルシューティング

事象

s3cmd利用時に適切に作成したIAMユーザ権限でアクセスしたはずが下記の様なエラーで弾かれてしまった
利用環境 : s3cmd 1.5.0-alpha3 / AmazonLinux2015.09 を利用

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設定

# 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設定

# 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を取得している箇所

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を確認

$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/
testS3

EC2に付与したIAM Roleのキー/Tokenを確認

$ curl http://169.254.169.254/latest/meta-data/iam/security-credentials/testS3
{
  "Code" : "Success",
  "LastUpdated" : "2015-11-17T06:51:35Z",
  "Type" : "AWS-HMAC",
  "AccessKeyId" : "Axxx",
  "SecretAccessKey" : "Kxxx",
  "Token" : "AQzxxxxxxxxxxxgU=",
  "Expiration" : "2015-11-17T13:09:15Z"

投稿者プロフィール

takashi
Japan AWS Ambassadors 2023-
開発会社での ASP型WEBサービス企画 / 開発 / サーバ運用 を経て
2010年よりスカイアーチネットワークスに在籍しております

機械化/効率化/システム構築を軸に人に喜んで頂ける物作りが大好きです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

Time limit is exhausted. Please reload CAPTCHA.

ABOUTこの記事をかいた人

Japan AWS Ambassadors 2023- 開発会社での ASP型WEBサービス企画 / 開発 / サーバ運用 を経て 2010年よりスカイアーチネットワークスに在籍しております 機械化/効率化/システム構築を軸に人に喜んで頂ける物作りが大好きです。