

# 自訂您檢視 AWS IoT Device Defender 稽核結果的時間和方式
<a name="dd-suppressions-example"></a>

AWS IoT Device Defender 稽核會提供定期安全檢查，以確認 AWS IoT 裝置和資源都遵循最佳實務。對於每次檢查，稽核結果會分類為合規或不合規，其中不合規會產生主控台警告圖示。為了減少重複已知問題所產生的噪音，稽核發現結果抑制功能可讓您暫時讓這些不合規通知靜音。

您可以在預先決定的時段抑制特定資源或帳戶的選取稽核檢查。已抑制的稽核檢查結果會分類為抑制的發現結果，與合規和不合規的類別分開。這個新類別不會觸發警示，例如不合規的結果。這可讓您在已知維護期間或在排定的更新完成之前減少不合規的通知干擾。

## 開始使用
<a name="dd-gs-afs"></a>

下列各節詳述如何使用稽核發現結果抑制來抑制主控台和 CLI 中的 `Device certificate expiring` 檢查。如果想要遵循其中一個示範，您必須先建立兩個即將到期的憑證，以供 Device Defender 偵測。

使用下列動作來建立您的憑證。
+ 在《*AWS IoT Core開發人員指南*》中[建立和註冊 CA 憑證](https://docs.aws.amazon.com/iot/latest/developerguide/create-device-cert.html)
+ [使用您的憑證授權機構憑證建立用戶端憑證](https://docs.aws.amazon.com/iot/latest/developerguide/create-device-cert.html) 在步驟 3 中，將您的 `days` 參數設定為 **1**。

如果您是使用 CLI 建立憑證，請輸入以下命令。

```
openssl x509 -req \ 
    -in device_cert_csr_filename \ 
    -CA root_ca_pem_filename \ 
    -CAkey root_ca_key_filename \     
    -CAcreateserial \
    -out device_cert_pem_filename \ 
    -days 1 -sha256
```

## 在主控台中自訂您的稽核發現結果
<a name="dd-afs-example-console"></a>

下列演練會使用具有兩個過期裝置憑證的帳戶，這些憑證可觸發不合規的稽核檢查。在此案例中，我們想要停用警告，因為我們的開發人員正在測試將解決問題的新功能。我們會為每個憑證建立稽核發現結果抑制，以阻止稽核結果下週變成不合規。

1. 我們首先會執行隨需稽核，以顯示過期裝置憑證檢查不合規。

   從 [AWS IoT 主控台](https://console.aws.amazon.com/iot)的左側邊欄中，選擇 **Defend** (防禦)，然後依序選擇 **Audit** (稽核)、**Results** (結果)。在 **Audit Results** (稽核結果) 頁面上，選擇 **Create** (建立)。**Create a new audit** (建立新稽核) 視窗即會開啟。選擇 **Create** (建立)。  
![\[執行隨需稽核，以顯示過期裝置憑證檢查不合規。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-afs-noncompliant.png)

   從隨需稽核結果中，我們可以看到兩個資源的「裝置憑證即將到期」不合規。

1. 現在，我們想要停用「裝置憑證即將到期」不合規檢查警告，因為我們的開發人員正在測試將修正警告的新功能。

   從 **Defend** (防禦) 下的左側邊欄中，選擇 **Audit** (稽核)，然後選擇 **Finding suppressions** (發現結果抑制)。在 **Audit finding suppressions** (稽核發現結果抑制) 頁面上，選擇 **Create** (建立)。  
![\[在主控台中建立稽核問題清單抑制的流程。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-afs-suppressions.png)

1. 在 **Create an audit finding suppression** (建立稽核發現結果抑制) 視窗上，我們需要填寫以下內容。
   + **Audit check** (稽核檢查)：我們選取 `Device certificate expiring`，因為這是我們想要抑制的稽核檢查。
   + **Resource identifier** (資源識別符)：我們輸入其中一個想要抑制其稽核發現結果之憑證的裝置憑證 ID。
   + **Suppression duration** (抑制持續時間)：我們選取 `1 week`，因為這是我們想要抑制 `Device certificate expiring` 稽核檢查的時間長度
   + **Description (optional)** (描述 (選用))：我們新增附註，描述為什麼我們要抑制此稽核發現結果。  
![\[您需要在此輸入詳細資訊的「建立稽核問題清單抑制」頁面。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-afs-create.png)

   在填寫完這些欄位之後，請選擇 **Create** (建立)。在建立了稽核發現結果抑制之後，我們會看到成功橫幅。

1. 我們已抑制其中一個憑證的稽核發現結果，現在我們需要抑制第二個憑證的稽核發現結果。我們可以使用在步驟 3 中使用的同一抑制方法，但我們將使用不同的方法進行示範。

   從 **Defend** (防禦) 下的左側邊欄中，選擇 **Audit** (稽核)，然後選擇 **Results** (結果)。在 **Audit results** (稽核結果) 頁面上，選擇具有不合規資源的稽核。然後，在 **Non-compliant checks** (不合規檢查) 下選取資源。在我們的案例中，我們選取「裝置憑證即將到期」。

1. 在 **Device certificate expiring** (裝置憑證即將到期) 頁面上，於 **Non-compliant policy** (不合規政策) 下選擇需要抑制之發現結果旁邊的選項按鈕。接著，選擇 **Actions** (動作) 下拉式選單，然後選擇您想要抑制發現結果的持續時間。在我們的案例中，我們選擇 `1 week`，就像我們為其他憑證所做的一樣。在 **Confirm suppression** (確認抑制) 視窗上，選擇 **Enable suppression** (啟用抑制)。  
![\[您完成此流程的「建立稽核抑制」頁面。稽核問題清單抑制建立完成後，您將會看到成功橫幅。\]](http://docs.aws.amazon.com/zh_tw/iot-device-defender/latest/devguide/images/dd-afs-noncompliantcerts.png)

   在建立了稽核發現結果抑制之後，我們會看到成功橫幅。現在，這兩個稽核發現結果已被抑制 1 週，而我們的開發人員正在研究解決這個警告的解決方案。

## 在 CLI 中自訂您的稽核發現結果
<a name="dd-afs-example-cli"></a>

下列演練會使用具有過期裝置憑證的帳戶，此憑證可觸發不合規的稽核檢查。在此案例中，我們想要停用警告，因為我們的開發人員正在測試將解決問題的新功能。我們會為憑證建立稽核發現結果抑制，以阻止稽核結果下週變成不合規。

我們使用以下 CLI 命令。
+ [create-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/create-audit-suppression.html)
+ [describe-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/describe-audit-suppression.html)
+ [update-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/update-audit-suppression.html)
+ [delete-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/delete-audit-suppression.html)
+ [list-audit-suppressions](https://docs.aws.amazon.com/cli/latest/reference/iot/list-audit-suppressions.html)

1. 請使用下列命令來啟用稽核。

   ```
   aws iot update-account-audit-configuration \
      --audit-check-configurations "{\"DEVICE_CERTIFICATE_EXPIRING_CHECK\":{\"enabled\":true}}"
   ```

   輸出：

   無。

1. 使用下列命令來執行將 `DEVICE_CERTIFICATE_EXPIRING_CHECK` 稽核檢查設為目標的隨需稽核。

   ```
   aws iot start-on-demand-audit-task \
       --target-check-names DEVICE_CERTIFICATE_EXPIRING_CHECK
   ```

   輸出：

   ```
   {
       "taskId": "787ed873b69cb4d6cdbae6ddd06996c5"
   }
   ```

1. 使用 [describe-account-audit-configuration](https://docs.aws.amazon.com/cli/latest/reference/iot/describe-account-audit-configuration.html) 命令來描述稽核組態。我們想要確認已開啟 `DEVICE_CERTIFICATE_EXPIRING_CHECK` 的稽核檢查。

   ```
   aws iot describe-account-audit-configuration
   ```

   輸出：

   ```
   {
       "roleArn": "arn:aws:iam::<accountid>:role/service-role/project",
       "auditNotificationTargetConfigurations": {
           "SNS": {
               "targetArn": "arn:aws:sns:us-east-1:<accountid>:project_sns",
               "roleArn": "arn:aws:iam::<accountid>:role/service-role/project",
               "enabled": true
           }
       },
       "auditCheckConfigurations": {
           "AUTHENTICATED_COGNITO_ROLE_OVERLY_PERMISSIVE_CHECK": {
               "enabled": false
           },
           "CA_CERTIFICATE_EXPIRING_CHECK": {
               "enabled": false
           },
           "CA_CERTIFICATE_KEY_QUALITY_CHECK": {
               "enabled": false
           },
           "CONFLICTING_CLIENT_IDS_CHECK": {
               "enabled": false
           },
           "DEVICE_CERTIFICATE_EXPIRING_CHECK": {
               "enabled": true
           },
           "DEVICE_CERTIFICATE_KEY_QUALITY_CHECK": {
               "enabled": false
           },
           "DEVICE_CERTIFICATE_SHARED_CHECK": {
               "enabled": false
           },
           "IOT_POLICY_OVERLY_PERMISSIVE_CHECK": {
               "enabled": true
           },
           "IOT_ROLE_ALIAS_ALLOWS_ACCESS_TO_UNUSED_SERVICES_CHECK": {
               "enabled": false
           },
           "IOT_ROLE_ALIAS_OVERLY_PERMISSIVE_CHECK": {
               "enabled": false
           },
           "LOGGING_DISABLED_CHECK": {
               "enabled": false
           },
           "REVOKED_CA_CERTIFICATE_STILL_ACTIVE_CHECK": {
               "enabled": false
           },
           "REVOKED_DEVICE_CERTIFICATE_STILL_ACTIVE_CHECK": {
               "enabled": false
           },
           "UNAUTHENTICATED_COGNITO_ROLE_OVERLY_PERMISSIVE_CHECK": {
               "enabled": false
           }
       }
   }
   ```

   `DEVICE_CERTIFICATE_EXPIRING_CHECK` 應該具有 `true` 一值。

1. 使用 [list-audit-task](https://docs.aws.amazon.com/cli/latest/reference/iot/list-audit-task.html) 命令來識別已完成的稽核任務。

   ```
   aws iot list-audit-tasks \
       --task-status "COMPLETED" \
       --start-time 2020-07-31 \
       --end-time 2020-08-01
   ```

   輸出：

   ```
   {
       "tasks": [
           {
               "taskId": "787ed873b69cb4d6cdbae6ddd06996c5",
               "taskStatus": "COMPLETED",
               "taskType": "SCHEDULED_AUDIT_TASK"
           }
       ]
   }
   ```

   您在步驟 1 中執行的 `taskId` 稽核，其具有的 `taskStatus` 應為 `COMPLETED`。

1. 使用 [describe-audit-task](https://docs.aws.amazon.com/cli/latest/reference/iot/describe-audit-task.html) 命令，透過來自前一個步驟的 `taskId` 輸出，來取得已完成稽核的詳細資訊。此命令會列出稽核的詳細資訊。

   ```
   aws iot describe-audit-task \
       --task-id "787ed873b69cb4d6cdbae6ddd06996c5"
   ```

   輸出：

   ```
   {
       "taskStatus": "COMPLETED",
       "taskType": "SCHEDULED_AUDIT_TASK",
       "taskStartTime": 1596168096.157,
       "taskStatistics": {
           "totalChecks": 1,
           "inProgressChecks": 0,
           "waitingForDataCollectionChecks": 0,
           "compliantChecks": 0,
           "nonCompliantChecks": 1,
           "failedChecks": 0,
           "canceledChecks": 0
       },
       "scheduledAuditName": "AWSIoTDeviceDefenderDailyAudit",
       "auditDetails": {
           "DEVICE_CERTIFICATE_EXPIRING_CHECK": {
               "checkRunStatus": "COMPLETED_NON_COMPLIANT",
               "checkCompliant": false,
               "totalResourcesCount": 195,
               "nonCompliantResourcesCount": 2
           }
       }
   }
   ```

1. 使用 [list-audit-findings](https://docs.aws.amazon.com/cli/latest/reference/iot/list-audit-findings.html) 命令來尋找不合規的憑證 ID，以便我們可以暫停此資源的稽核警示。

   ```
   aws iot list-audit-findings \
       --start-time 2020-07-31 \
       --end-time 2020-08-01
   ```

   輸出：

   ```
   {
       "findings": [
           {
               "findingId": "296ccd39f806bf9d8f8de20d0ceb33a1",
               "taskId": "787ed873b69cb4d6cdbae6ddd06996c5",
               "checkName": "DEVICE_CERTIFICATE_EXPIRING_CHECK",
               "taskStartTime": 1596168096.157,
               "findingTime": 1596168096.651,
               "severity": "MEDIUM",
               "nonCompliantResource": {
                   "resourceType": "DEVICE_CERTIFICATE",
                   "resourceIdentifier": {
                       "deviceCertificateId": "b4490<shortened>"
                   },
                   "additionalInfo": {
                   "EXPIRATION_TIME": "1582862626000"
                   }
               },
               "reasonForNonCompliance": "Certificate is past its expiration.",
               "reasonForNonComplianceCode": "CERTIFICATE_PAST_EXPIRATION",
               "isSuppressed": false
           },
           {
               "findingId": "37ecb79b7afb53deb328ec78e647631c",
               "taskId": "787ed873b69cb4d6cdbae6ddd06996c5",
               "checkName": "DEVICE_CERTIFICATE_EXPIRING_CHECK",
               "taskStartTime": 1596168096.157,
               "findingTime": 1596168096.651,
               "severity": "MEDIUM",
               "nonCompliantResource": {
                   "resourceType": "DEVICE_CERTIFICATE",
                   "resourceIdentifier": {
                       "deviceCertificateId": "c7691<shortened>"
                   },
                   "additionalInfo": {
                   "EXPIRATION_TIME": "1583424717000"
                   }
               },
               "reasonForNonCompliance": "Certificate is past its expiration.",
               "reasonForNonComplianceCode": "CERTIFICATE_PAST_EXPIRATION",
               "isSuppressed": false
           }
      ]
   }
   ```

1. 使用 [create-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/create-audit-suppression.html) 命令，來抑制裝置憑證 (ID 為 `c7691e<shortened>`) 之 `DEVICE_CERTIFICATE_EXPIRING_CHECK` 稽核檢查的通知，直到 *2020-08-20*。

   ```
   aws iot create-audit-suppression \
       --check-name DEVICE_CERTIFICATE_EXPIRING_CHECK \
       --resource-identifier deviceCertificateId="c7691e<shortened>" \
       --no-suppress-indefinitely \ 
       --expiration-date 2020-08-20
   ```

1. 使用 [list-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/list-audit-suppression.html) 命令，來確認稽核抑制設定，並取得有關抑制的詳細資訊。

   ```
   aws iot list-audit-suppressions
   ```

   輸出：

   ```
   {
       "suppressions": [
           {
           "checkName": "DEVICE_CERTIFICATE_EXPIRING_CHECK",
               "resourceIdentifier": {
                   "deviceCertificateId": "c7691e<shortened>"
               },
           "expirationDate": 1597881600.0,
           "suppressIndefinitely": false
           }
       ]
   }
   ```

1. [update-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/update-audit-suppression.html) 命令可以用來更新稽核發現結果抑制。下列範例會將 `expiration-date` 更新為 `08/21/20`。

   ```
   aws iot update-audit-suppression \
       --check-name DEVICE_CERTIFICATE_EXPIRING_CHECK \
       --resource-identifier deviceCertificateId=c7691e<shortened> \
       --no-suppress-indefinitely \
       --expiration-date 2020-08-21
   ```

1. [delete-audit-suppression](https://docs.aws.amazon.com/cli/latest/reference/iot/delete-audit-suppression.html) 命令可以用來移除稽核發現結果抑制。

   ```
   aws iot delete-audit-suppression \
       --check-name DEVICE_CERTIFICATE_EXPIRING_CHECK \
       --resource-identifier deviceCertificateId="c7691e<shortened>"
   ```

   若要確認刪除，請使用 [list-audit-suppressions](https://docs.aws.amazon.com/cli/latest/reference/iot/list-audit-suppressions.html) 命令。

   ```
   aws iot list-audit-suppressions
   ```

   輸出：

   ```
   {
    "suppressions": []
   }
   ```

在本教學課程中，我們已展示如何抑制主控台和 CLI 中的 `Device certificate expiring` 檢查。如需稽核發現結果抑制的詳細資訊，請參閱 [稽核發現結果抑制](audit-finding-suppressions.md)