如何搭配 AWS IoT Core 使用 JITP?

6 分的閱讀內容
0

我想要設定具有在 AWS IoT Core 註冊的自訂根憑證授權單位 (CA) 的即時佈建 (JITP) 環境。如何使用 AWS IoT Core 設定 JITP?

簡短說明

若要使用 AWS IoT Core 設定 JITP 環境,請先向 AWS IoT Core 註冊您的 CA。然後,將佈建範本附加到 CA

解決方法

**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請確保您使用的是最新的 AWS CLI 版本

建立自我簽署的根 CA 和驗證憑證

1.    如果您尚未這麼做,請安裝 OpenSSL

2.    執行下列 OpenSSL 命令,以建立裝置根 CA 私密金鑰:

$ openssl genrsa -out deviceRootCA.key 2048

3.    使用 VIM 文字編輯器,建立自訂的 OpenSSL.conf 檔案。若要建立和編輯自訂 OpenSSL.conf 檔案,請執行下列動作:
執行下列 VIM 命令,以建立自訂的 OpenSSL.conf 檔案:

$ vi deviceRootCA_openssl.conf

按鍵盤上的 i 以編輯 .conf 檔案。然後,將以下內容複製並貼至檔案中:

[ req ]
distinguished_name       = req_distinguished_name
extensions               = v3_ca
req_extensions           = v3_ca

[ v3_ca ]
basicConstraints         = CA:TRUE

[ req_distinguished_name ]
countryName              = Country Name (2 letter code)
countryName_default      = IN
countryName_min          = 2
countryName_max          = 2
organizationName         = Organization Name (eg, company)
organizationName_default = AMZ

按鍵盤上的 esc 鍵,再按 :wq! 以儲存 .conf 檔案。然後,按 Enter 鍵結束檔案。
**注意:**若要確認已建立 OpenSSL.conf 檔案,您可以執行下列 Linux 命令:

$ cat deviceRootCA_openssl.conf

4.    執行下列 OpenSSL 命令,以建立裝置根 CA 憑證簽署要求 (CSR):

$ openssl req -new -sha256 -key deviceRootCA.key -nodes -out deviceRootCA.csr -config deviceRootCA_openssl.conf

5.    執行下列 OpenSSL 命令,以建立裝置根 CA 憑證:

$ openssl x509 -req -days 3650 -extfile deviceRootCA_openssl.conf -extensions v3_ca -in deviceRootCA.csr -signkey deviceRootCA.key -out deviceRootCA.pem

6.    執行下列 AWS CLI 命令,擷取您要在其中使用 JITP 的 AWS 區域的註冊碼:

**重要:**請確定您已將 us-east-2 取代為您要在其中使用 JITP 的區域。

$ aws iot get-registration-code --region us-east-2

7.    執行下列 OpenSSL 命令以建立驗證金鑰:

$ openssl genrsa -out verificationCert.key 2048

8.    執行下列 OpenSSL 命令,以建立驗證憑證 CSR:

$ openssl req -new -key verificationCert.key -out verificationCert.csr

然後,在「 一般名稱 」欄位中輸入「 註冊碼 」。例如: 一般名稱 (伺服器 FQDN 或您的名稱) []:xxxxxxxx8a33da.將其他欄位保留空白。

9.    執行下列 OpenSSL 命令來建立驗證憑證:

$ openssl x509 -req -in verificationCert.csr -CA deviceRootCA.pem -CAkey deviceRootCA.key -CAcreateserial -out verificationCert.crt -days 500 -sha256

**重要:**驗證憑證必須提供根 CA 區域的註冊碼,才能通過 AWS IoT Core 認證。

如需詳細資訊,請參閱即時佈建

建立一個 JITP 範本

1.    為您的 AWS IoT Core 服務建立 AWS 身分和存取管理 (IAM) 角色,並將其命名為 JITPRole。如需指示,請參閱建立記錄角色 (步驟一和步驟二)。

**重要:**您必須在下列 JITP 範本中包含 IAM 角色的Amazon Resource Name (ARN)

2.    使用 VIM 文字編輯器,執行下列動作來建立 JITP 範本 JSON 檔案:
執行下列 VIM 命令,以建立 JITP 範本 JSON 檔案:

$ vi jitp_template.json

**重要:**請確定您使用檔案名稱 jitp_template.json 保存範本。
按下鍵盤上的 i 來編輯 JITP 範本。然後,將下列 JITP 範本複製並貼到檔案中:

{
 "templateBody":"{ \"Parameters\" : { \"AWS::IoT::Certificate::CommonName\" : { \"Type\" : \"String\" },\"AWS::IoT::Certificate::Country\" : { \"Type\" : \"String\" }, \"AWS::IoT::Certificate::Id\" : { \"Type\" : \"String\" }}, \"Resources\" : { \"thing\" : { \"Type\" : \"AWS::IoT::Thing\", \"Properties\" : { \"ThingName\" : {\"Ref\" : \"AWS::IoT::Certificate::CommonName\"}, \"AttributePayload\" : { \"version\" : \"v1\", \"country\" : {\"Ref\" : \"AWS::IoT::Certificate::Country\"}} } }, \"certificate\" : { \"Type\" : \"AWS::IoT::Certificate\", \"Properties\" : { \"CertificateId\": {\"Ref\" : \"AWS::IoT::Certificate::Id\"}, \"Status\" : \"ACTIVE\" } }, \"policy\" : {\"Type\" : \"AWS::IoT::Policy\", \"Properties\" : { \"PolicyDocument\" : \"{ \\\"Version\\\": \\\"2012-10-17\\\", \\\"Statement\\\": [ { \\\"Effect\\\": \\\"Allow\\\", \\\"Action\\\": [ \\\"iot:Connect\\\" ], \\\"Resource\\\": [ \\\"arn:aws:iot:us-east-2:<ACCOUNT_ID>:client\\\/${iot:Connection.Thing.ThingName}\\\" ] }, { \\\"Effect\\\": \\\"Allow\\\", \\\"Action\\\": [ \\\"iot:Publish\\\", \\\"iot:Receive\\\" ], \\\"Resource\\\": [ \\\"arn:aws:iot:us-east-2:<ACCOUNT_ID>:topic\\\/${iot:Connection.Thing.ThingName}\\\/*\\\" ] }, { \\\"Effect\\\": \\\"Allow\\\", \\\"Action\\\": [ \\\"iot:Subscribe\\\" ], \\\"Resource\\\": [ \\\"arn:aws:iot:us-east-2:<ACCOUNT_ID>:topicfilter\\\/${iot:Connection.Thing.ThingName}\\\/*\\\" ] } ] }\" } } } }",
 "roleArn":"arn:aws:iam::<ACCOUNT_ID>:role/JITPRole"
}

**重要:**使用 AWS IoT Core 服務的 IAM 角色 ARN 取代 roleArn 值。將該 <ACCOUNT_ID> 值取代為您的 AWS 帳戶 ID。將 us-east-2 取代為您正在使用的 AWS 區域。按鍵盤上的 esc 鍵,再按 :wq! 以儲存 JITP 範本檔案。選擇「 輸入 」以結束檔案。

**注意:**下列 IAM 政策包含在範例 JITP 範本中:

您必須登入 AWS 帳戶才能檢視政策連結。如需詳細資訊,請參閱佈建範本

使用 AWS IoT Core 註冊您的自我簽署根 CA 憑證

透過執行下列 register-ca-certificate AWS CLI 命令,在 AWS IoT Core 中將裝置根 CA 註冊為 CA 憑證:

**重要:**請確定您已將 us-east-2 取代為您要在其中使用 JITP 的區域。

$ aws iot register-ca-certificate --ca-certificate file://deviceRootCA.pem --verification-cert file://verificationCert.crt --set-as-active --allow-auto-registration --registration-config file://jitp_template.json --region us-east-2

**注意:**新增參數 --registration-config 會將您建立的 JITP 範本附加至 CA 憑證。命令回應會傳回 CA 憑證的 ARN。

如需詳細資訊,請參閱註冊您的 CA 憑證

建立裝置憑證並執行 JITP

**重要:**請確定您使用與建立原始裝置根 CA 檔案相同的目錄。

1.    下載 RootCA1 並使用檔案名稱 awsRootCA.pem 將其保存。
注意: RootCA1 用於發佈到 AWS IoT Core 的請求的伺服器端身份驗證。如需詳細資訊,請參閱伺服器驗證的 CA 憑證

2.    執行下列 OpenSSL 命令來建立裝置私密金鑰:

$ openssl genrsa -out deviceCert.key 2048

3.    執行下列 OpenSSL 命令以建立裝置 CSR:

$ openssl req -new -key deviceCert.key -out deviceCert.csr

重要:範例 JITP 範本要求 ThingName 值等於憑證的 CommonName 值。範本也要求 CountryName 值等於 CA 憑證中的國家/地區值。例如:

Country Name (two-letter code) []:IN
Common Name (eg. server FQDN or YOUR name) []: DemoThing

本文提供的 JITP 範本也使用 AWS::IoT::Certificate::Country 憑證參數,這會要求您新增值。其他可能的憑證參數包括: AWS::IoT::Certificate::Country AWS::IoT::Certificate::Organization AWS::IoT::Certificate::OrganizationalUnit AWS::IoT::Certificate::DistinguishedNameQualifier AWS::IoT::Certificate::StateName AWS::IoT::Certificate::CommonName AWS::IoT::Certificate::SerialNumber AWS::IoT::Certificate::Id

4.    執行下列 OpenSSL 命令以建立裝置憑證:

$ openssl x509 -req -in deviceCert.csr -CA deviceRootCA.pem -CAkey deviceRootCA.key -CAcreateserial -out deviceCert.crt -days 365 -sha256

5.    執行下列命令,結合根 CA 憑證和裝置憑證:

$ cat deviceCert.crt deviceRootCA.pem > deviceCertAndCACert.crt
  1. 使用 Eclipse Mosquitto 對 AWS IoT Core 進行測試發佈呼叫,並啟動 JITP 程序。

**注意:**您也可以使用 AWS 裝置 SDK對 AWS IoT Core 進行發佈呼叫。

例如 Eclipse Mosquitto 測試發佈呼叫命令

**重要:**在執行命令之前,請將 a27icbrpsxxx-ats.iot.us-east-2.amazonaws.com 取代為您自己的端點。若要確認您自己的端點,請開啟 AWS IoT Core 主控台。然後,選擇「 設置」。您的端點會列在「 自訂端點 」窗格中。

$ mosquitto_pub --cafile awsRootCA.pem --cert deviceCertAndCACert.crt --key deviceCert.key -h a27icbrpsxxx-ats.iot.us-east-2.amazonaws.com -p 8883 -q 1 -t  foo/bar -i  anyclientID --tls-version tlsv1.2 -m "Hello" -d

來自 Eclipse Mosquitto 測試發佈呼叫命令的範例響應

Client anyclientID sending CONNECT  
  Error: The connection was lost. // The error is expected for the first connect call

注意:第一次測試發佈呼叫失敗。當 AWS IoT Core 收到測試發佈呼叫時,就會建立憑證、政策和物件。它也會將政策附加至憑證,然後將憑證附加至物件。下次執行 JITP 時,第一次建立的 IoT 政策就是使用的政策。不會建立新的 IoT 政策。

7.    執行下列動作,確認已建立必要的資源: 開啟 AWS IoT Core 主控台。選擇「 管理」。選擇「 **物件」。**選擇「 DemoThing」。
確認憑證已建立且「 處於作用中 」狀態。
然後,選擇「 政策 」並確認已附加 IAM 政策。

在一般作業中使用裝置憑證

注意:在發佈命令中新增的用戶端 ID 值必須與在 JITP 流程期間建立的 ThingName 相同。新增至發行命令的主題名稱也必須遵循 ThingName/* 格式。在下一個發佈呼叫中,您可以使用 deviceCert.crt 而不是deviceCertAndCACert.crt

1.    開啟 AWS IoT Core 主控台

2.    選擇「 測試」。

  1. 針對「 訂閱主題」,輸入 DemoThing/test

4.    執行下列 Eclipse Mosquitto 將呼叫命令發佈到 AWS IoT Core:

**重要:**在執行命令之前,請將 a27icbrpsxxx-ats.iot.us-east-2.amazonaws.com 取代為您自己的端點。若要確認您自己的端點,請開啟 AWS IoT Core 主控台。然後,選擇「 設置」。您的端點會顯示在「 自訂端點 」窗格中。此外,請確定您使用自訂根 CA 所產生的自訂裝置憑證。

$ mosquitto_pub --cafile awsRootCA.pem --cert deviceCert.crt --key deviceCert.key -h a27icbrpsxxx-ats.iot.us-east-2.amazonaws.com -p 8883 -q 1 -t  DemoThing/test -i  DemoThing --tls-version tlsv1.2 -m "Hello" -d

執行命令後,您會在 AWS IoT Core 測試主控台上看到訊息。

建立其他裝置憑證

若要建立更多裝置憑證並將其註冊到 AWS IoT Core,請重複**建立裝置憑證並執行 JITP **一節中概述的步驟。

其他 JITP 模板

欲從憑證的 CommonName 欄位擷取 ThingName 值,並在政策中提供管理員許可,請使用下列 JITP 範本:

{
 "templateBody":"{ \"Parameters\" : { \"AWS::IoT::Certificate::CommonName\" : { \"Type\" : \"String\" },\"AWS::IoT::Certificate::Country\" : { \"Type\" : \"String\" }, \"AWS::IoT::Certificate::Id\" : { \"Type\" : \"String\" }}, \"Resources\" : { \"thing\" : { \"Type\" : \"AWS::IoT::Thing\", \"Properties\" : { \"ThingName\" : {\"Ref\" : \"AWS::IoT::Certificate::CommonName\"}, \"AttributePayload\" : { \"version\" : \"v1\", \"country\" : {\"Ref\" : \"AWS::IoT::Certificate::Country\"}} } }, \"certificate\" : { \"Type\" : \"AWS::IoT::Certificate\", \"Properties\" : { \"CertificateId\": {\"Ref\" : \"AWS::IoT::Certificate::Id\"}, \"Status\" : \"ACTIVE\" } }, \"policy\" : {\"Type\" : \"AWS::IoT::Policy\", \"Properties\" : { \"PolicyDocument\" : \"{\\\"Version\\\":\\\"2012-10-17\\\",\\\"Statement\\\":[{\\\"Effect\\\":\\\"Allow\\\",\\\"Action\\\":\\\"iot:*\\\",\\\"Resource\\\":\\\"*\\\"}]}\" } } } }",
 "roleArn":"arn:aws:iam::<ACCOUNT_ID>:role/JITPRole"
}

CommonName 欄位擷取 ThingName 值,並提供預先定義的政策名稱,請使用下列 JITP 範本:

{
 "templateBody":"{ \"Parameters\" : { \"AWS::IoT::Certificate::CommonName\" : { \"Type\" : \"String\" },\"AWS::IoT::Certificate::Country\" : { \"Type\" : \"String\" }, \"AWS::IoT::Certificate::Id\" : { \"Type\" : \"String\" }}, \"Resources\" : { \"thing\" : { \"Type\" : \"AWS::IoT::Thing\", \"Properties\" : { \"ThingName\" : {\"Ref\" : \"AWS::IoT::Certificate::CommonName\"}, \"AttributePayload\" : { \"version\" : \"v1\", \"country\" : {\"Ref\" : \"AWS::IoT::Certificate::Country\"}} } }, \"certificate\" : { \"Type\" : \"AWS::IoT::Certificate\", \"Properties\" : { \"CertificateId\": {\"Ref\" : \"AWS::IoT::Certificate::Id\"}, \"Status\" : \"ACTIVE\" } }, \"policy\" :  {\"Type\" : \"AWS::IoT::Policy\", \"Properties\" : { \"PolicyName\" :  \"Policy_Name\"} } } }",
 "roleArn":"arn:aws:iam::<ACCOUNT_ID>:role/JITPRole"
}

**重要:**將 Policy_Name 取代為您選擇的政策名稱。


AWS 官方
AWS 官方已更新 2 年前