Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
如何搭配 AWS IoT Core 使用 JITP?
我想要設定具有在 AWS IoT Core 註冊的自訂根憑證授權單位 (CA) 的即時佈建 (JITP) 環境。
簡短說明
若要使用 AWS IoT Core 設定 JITP 環境,請先向 AWS IoT Core 註冊您的 CA 憑證。然後,將佈建範本附加到 CA 。
解決方法
**注意:**如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,則請參閱對 AWS CLI 進行錯誤疑難排解。此外,請確定您使用的是最新的 AWS CLI 版本。
建立自我簽署的根 CA 和驗證憑證
先決條件:
安裝 OpenSSL。
若要建立自我簽署的根 CA 和驗證憑證,請採取下列操作:
-
建立裝置根 CA 私密金鑰並執行下列 OpenSSL 命令:
$ openssl genrsa -out deviceRootCA.key 2048
-
開啟 VIM 文字編輯器,並建立自訂的 OpenSSL.conf 檔案。如需有關 VIM 文字編輯器的更多資訊,請參閱 Vim 提示 Wiki 網站上的教學課程。
-
若要建立自訂的 OpenSSL.conf 檔案,請執行下列 VIM 命令:
$ 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 (e.g., company) organizationName_default = AMZ
-
若要儲存 .conf 檔案,請按鍵盤上的 Esc,然後按下鍵盤上的 :wq!。若要結束檔案,請按 Enter 鍵。
-
若要確認已建立 OpenSSL.conf 檔案,請執行下列 Linux 命令:
$ cat deviceRootCA_openssl.conf
-
建立裝置根 CA 憑證簽署要求 (CSR)。執行下列 OpenSSL 命令:
$ openssl req -new -sha256 -key deviceRootCA.key -nodes -out deviceRootCA.csr -config deviceRootCA_openssl.conf
-
建立裝置根 CA 憑證。執行下列 OpenSSL 命令:
$ openssl x509 -req -days 3650 -extfile deviceRootCA_openssl.conf -extensions v3_ca -in deviceRootCA.csr -signkey deviceRootCA.key -out deviceRootCA.pem
-
擷取您要使用 JITP 的 AWS 區域的註冊碼。接著,執行下列 AWS CLI 命令:
$ aws iot get-registration-code --region us-east-2
**注意:**以您想要使用 JITP 的區域以取代 us-east-2。
-
建立驗證金鑰。執行下列 OpenSSL 命令:
$ openssl genrsa -out verificationCert.key 2048
- 建立驗證憑證 CSR。執行下列 OpenSSL 命令:
$ openssl req -new -key verificationCert.key -out verificationCert.csr
然後,在「 一般名稱 」欄位中輸入「 註冊碼 」。例如: 一般名稱 (伺服器 FQDN 或您的名稱) []:xxxxxxxx8a33da.
將其他欄位保留空白。
- 建立驗證憑證。執行下列 OpenSSL 命令:
$ openssl x509 -req -in verificationCert.csr -CA deviceRootCA.pem -CAkey deviceRootCA.key -CAcreateserial -out verificationCert.crt -days 500 -sha256
**重要:**當 AWS IoT Core 憑證驗證憑證時,必須提供您的根 CA 區域的註冊碼。
如需詳細資訊,請參閱即時佈建。
建立一個 JITP 範本
-
為您的 AWS IoT Core 服務建立 AWS 身分和存取管理 (IAM) 角色。將其命名為 JITProle。如需指示,請參閱建立記錄角色。
**重要:**在下列 JITP 範本中,您必須包含 IAM 角色的Amazon Resource Name (ARN)。
-
使用 VIM 文字編輯器建立 JITP 範本 JSON 檔案。請完成下列步驟:
-
建立 JITP 範本 JSON 檔案。執行下列 VIM 命令:
$ vi jitp_template.json
**注意:**請確定您使用檔案名稱 jitp_template.json. 保存範本。
-
要編輯 JITP 範本,請按下鍵盤上的 i。然後,將下列 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 值。以您的 AWS 帳戶 ID 取代該值。將 us-east-2 取代為您的 AWS 區域。
-
要儲存 JITP 範本檔案,請按鍵盤上的 Esc 鍵,再按 :wq!。
-
選擇「輸入」以結束檔案。
**注意:**下列 IAM 政策包含在範例 JITP 範本中:
您必須登入 AWS 帳戶才能檢視政策連結。如需詳細資訊,請參閱佈建範本。
使用 AWS IoT Core 註冊您的自我簽署根 CA 憑證
-
在 AWS IoT Core 中將裝置根 CA 註冊為 CA 憑證。
-
執行下列 register-ca-certificate AWS CLI 指令:
$ 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
**注意:**以您想要使用 JITP 的區域以取代 us-east-2。
**注意:**使用參數 --registration-config 會將您建立的 JITP 範本附加至 CA 憑證。若要傳回 CA 憑證的 ARN,請使用指令回應。
如需詳細資訊,請參閱註冊您的 CA 憑證。
建立裝置憑證並執行 JITP
**重要:**請確定您使用與建立原始裝置根 CA 檔案相同的目錄。
-
下載 RootCA1 並使用檔案名稱 awsRootCA.pem 將其保存。
注意: RootCA1 用於發佈到 AWS IoT Core 的請求的伺服器端身份驗證。如需詳細資訊,請參閱伺服器驗證的 CA 憑證。
-
建立裝置私有金鑰。執行下列 OpenSSL 命令:
$ openssl genrsa -out deviceCert.key 2048
-
建立 CSR 裝置。執行下列 OpenSSL 命令:
$ openssl req -new -key deviceCert.key -out deviceCert.csr
重要:範例 JITP 範本要求 ThingName 值等於憑證的 CommonName 值。範本也要求 CountryName 值等於 CA 憑證中的國家/地區值。例如:
Country Name (two-letter code) []:IN Common Name (e.g. 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
-
建立裝置憑證。執行下列 OpenSSL 命令:
$ openssl x509 -req -in deviceCert.csr -CA deviceRootCA.pem -CAkey deviceRootCA.key -CAcreateserial -out deviceCert.crt -days 365 -sha256
-
結合根 CA 憑證和裝置憑證。執行下列命令:
$ cat deviceCert.crt deviceRootCA.pem > deviceCertAndCACert.crt
-
使用 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 時,第一次建立的 AWS IoT Core 政策就是使用的政策。未建立新的 AWS IoT Core 政策。
-
執行下列動作,確認已建立必要的資源:
開啟 AWS IoT Core 主控台。
選擇「 管理」。
選擇「 物件」。
選擇「DemoThing」。 -
確認憑證已建立且「 處於作用中 」狀態。然後,選擇「 政策 」並確認已附加 IAM 政策。
在一般作業中使用裝置憑證
注意:在發佈命令中新增的用戶端 ID 值必須與在 JITP 流程期間建立的 ThingName 相符。新增至發行命令的主題名稱也必須遵循 ThingName/* 格式。在下一個發佈呼叫中,您可以使用 deviceCert.crt 而不是deviceCertAndCACert.crt。
-
開啟 AWS IoT Core 主控台。
-
選擇測試。
-
針對「 訂閱主題」,輸入 DemoThing/test。
-
執行下列 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 核心測試主控台上收到訊息。
建立其他裝置憑證
若要建立更多裝置憑證並將其註冊到 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。
相關內容
- 已提問 1 年前lg...
- 已提問 6 個月前lg...
- 已提問 5 個月前lg...
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前
- AWS 官方已更新 2 年前