LoginSignup
16
5

More than 5 years have passed since last update.

AWS IoTのX.509証明書を使ってAssumeRoleする

Last updated at Posted at 2017-12-15

IoTデバイスからAWSのサービスに繋ぐ時に困る事の一つに、デバイスからのAWSのクレデンシャルの取得があります。

最近、AWS IoTのX.509証明書を使ってhttpリクエストをする事で、AssumeRoleして一時クレデンシャルを取得する事ができるようになったとの事なので試しました。
Authorizing Direct Calls to AWS Services - Security and Identity for AWS IoT

現状だと、公式のドキュメントにはサンプルコードはありませんでした。フォーラムに試し方が書いてあったので、これに従います。

次の4ステップで確認できます。

  1. Thing+Certificateを作る
  2. デバイスに渡したいIAMRoleを作る
  3. RoleAliasを作る
  4. 一時クレデンシャルを取得するエンドポイントを取得する
  5. 接続チェック!

1. Thing+Certificateを作る

ウィザードに従ってThingとCertificateを作って下さい。Certificateは一旦「One-click certificate creation (recommended)」で作ります。CertificateにattachするPolicyは以下の内容にします。iot:AssumeRoleWithCertificateというactionを付与すると良いそうです。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iot:AssumeRoleWithCertificate"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}

作成後は証明書、秘密鍵、ルート証明書をダウンロードしておきます。

image.png

2. デバイスに渡したいIAMRoleを作る

デバイスに渡したいIAM Roleを作ります。Permissionの指定はお好きに設定してください。
Trust Relationshipは以下の内容にします。Management Consoleから作る時は一旦適当なServiceを割り当てた後に修正してください。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "credentials.iot.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

3. RoleAliasを作る

RoleAliasとは、AWS IoTで追加された概念で、デバイスからIAMRoleをリクエストする時にARNのハードコードをさけてRoleのアップデートをしやすくするために追加されたようです。

The device which is going to make direct calls to AWS Services must know what role ARN to use when connecting to AWS IoT. But hard-coding the role ARN is not a good solution because you would have to update the device anytime the role ARN changes. A better solution is to create a role alias that points to the role ARN and use that on your device. If the role ARN changes, you can update the role alias and no change is required on the device. Role aliases are created using the CreateRoleAlias API. This API takes the following parameters:

AWS IoTの"Secure > Role Aliases"タブからRoleAliasesを作成します。今回はcert-cred-test-role-aliasという名前にしています。roleには先ほど作ったIAMRoleを指定してください。

image.png

4. 一時クレデンシャル用エンドポイントを取得する

一時クレデンシャルを取得するためのエンドポイントはCLIから取得する事ができます。このエンドポイントはMQTT用のエンドポイントは別です。


$ aws iot describe-endpoint --endpoint-type=iot:CredentialProvider
{
    "endpointAddress": "xxxxxxxxx.credentials.iot.us-west-2.amazonaws.com"
}

5. 接続チェック!


$ wget --private-key=./1234567-private.pem.key \
       --certificate=./1234567-certificate.pem.crt \
       --ca-directory=./AmazonRootCA1.pem \
       https://xxxxxxxxx.credentials.iot.us-west-2.amazonaws.com:443/role-aliases/cert-cred-test-role-aliases/credentials
$ cat credentials
> (クレデンシャルが見えます)

:tada:

ちなみにPythonから取得する時はこんな感じ。

import requests

endpoint = 'https://xxxxxxxx.credentials.iot.us-west-2.amazonaws.com:443'
role_alias = 'cert-cred-test-role-alias'
cert_id = '1234567'

r = requests.get(
    f'{endpoint}/role-aliases/{role_alias}/credentials',                                                                                                                                                        
    cert=(f'./{cert_id}-certificate.pem.crt', f'./{cert_id}-private.pem.key')
)
print(r.status_code)
print(r.text)

16
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
5