LoginSignup
0
0

More than 5 years have passed since last update.

[JAWS-UG CLI] IoT #12 デバイス証明書からIoTポリシーのデタッチ

Posted at

前提条件

IoTへの権限

AWS IoTに対してフル権限があること。

AWS CLIのバージョン

以下のバージョンで動作確認済

  • AWS CLI 1.11.14
コマンド
aws --version
結果(例)
      aws-cli/1.11.14 Python/2.7.10 Darwin/15.6.0 botocore/1.4.71

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

0. 準備

0.1. リージョンの決定

構築するリージョンを決めます。 (カレントユーザが利用するカレントリージ
ョンも切り変わります。)

コマンド(東京リージョンの場合)
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

プロファイルが想定のものになっていることを確認します。

コマンド
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile         administrator-prjz-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                         ap-northeast-1  env    AWS_DEFAULT_REGION

AssumeRoleを利用している場合はprofileが ''と表示されます。 そ
れ以外のときにprofileが '' と表示される場合は、以下を実行して
ください。

変数の設定:

     export AWS_DEFAULT_PROFILE=<IAMユーザ名>

0.3. デバイス証明書のID指定

デバイス証明書のリストを確認し、利用するデバイス証明書のIDを変数に格納
します。

コマンド
aws iot list-certificates 
結果(例)
      {
        "certificates": [
          {
              "certificateArn": "arn:aws:iot:ap-northeast-1:xxxxxxxxxxxxxxert/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
              "status": "ACTIVE",
              "creationDate": 1234567890.123,
              "certificateId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
          }
        ]
      }
変数の設定(例)
IOT_CERT_ID='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

デバイス証明書のARNを取得します。

コマンド
IOT_CERT_ARN=$( \
        aws iot describe-certificate \
          --certificate-id ${IOT_CERT_ID} \
          --query 'certificateDescription.certificateArn' \
          --output text \
) \
        && echo ${IOT_CERT_ARN}
結果(例)
      arn:aws:iot:ap-northeast-1:XXXXXXXXXXXX:cert/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

証明書にアタッチされているポリシーの一覧を確認します。

コマンド
aws iot list-principal-policies \
        --principal ${IOT_CERT_ARN}
結果(例)
      {
        "policies": [
          {
              "policyName": "AWSIoTFullAccess",
              "policyArn": "arn:aws:iot:ap-northeast-1:XXXXXXXXXXXX:policy/AWSIoTFullAccess"
          }
        ]
      }

ポリシーがアタッチされている証明書の一覧も確認できます。

コマンド
aws iot list-policy-principals \
        --policy-name ${IOT_POLICY_NAME}
結果(例)
      {
        "principals": [
          "arn:aws:iot:ap-northeast-1:XXXXXXXXXXXX:cert/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        ]
      }

1. 事前作業

1.2. IoTポリシー名の指定

変数の設定
IOT_POLICY_NAME='AWSIoTFullAccess'

2. ポリシーのデタッチ

変数の確認
cat << ETX

        IOT_POLICY_NAME: ${IOT_POLICY_NAME}
        IOT_CERT_ARN:    ${IOT_CERT_ARN}

ETX
コマンド
aws iot detach-principal-policy \
        --policy-name ${IOT_POLICY_NAME} \
        --principal ${IOT_CERT_ARN}
結果(例)
      (戻り値なし)

3. 事後作業

コマンド
aws iot list-principal-policies \
        --principal ${IOT_CERT_ARN}
結果(例)
      {
        "policies": []
      }
コマンド
aws iot list-policy-principals \
        --policy-name ${IOT_POLICY_NAME}
結果(例)
      {
        "principals": []
      }

完了

0
0
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
0
0