

# 经过身份验证的 Cognito 角色过于宽容
<a name="audit-chk-auth-cognito-role-permissive"></a>

附加到经身份验证的 Amazon Cognito 身份池角色的策略被视为过于宽容，因为它会授权执行以下 AWS IoT 操作：
+ 管理或修改事物。
+ 管理非事物相关的数据或资源。

或者，因为它授权在一系列设备上执行以下 AWS IoT 操作：
+ 读取事物管理数据。
+ 使用 MQTT 连接/发布/订阅预留主题（包括影子或任务执行数据）。
+ 使用 API 命令读取或修改影子或任务执行数据。

一般情况下，使用经身份验证的 Amazon Cognito 身份池角色进行连接的设备只应具备有限的权限，读取事物特定管理数据、发布和订阅事物特定 MQTT 主题，或使用 API 命令读取和修改与影子或任务执行数据相关的事物特定数据。

此检查在 CLI 和 API 中显示为 `AUTHENTICATED_COGNITO_ROLE_OVERLY_PERMISSIVE_CHECK`。

**严重性：**严重

## Details
<a name="audit-chk-auth-cognito-role-permissive-details"></a>

对于此检查，AWS IoT Device Defender 会审计在执行审计之前 31 天内用于连接到 AWS IoT 消息代理的所有 Amazon Cognito 身份池。经过身份验证或未经身份验证的 Amazon Cognito 身份连接的所有 Amazon Cognito 身份池都在审核范围内。

此检查发现不合规的经身份验证的 Amazon Cognito 身份池角色时，会返回以下原因代码：
+ ALLOWS\$1BROAD\$1ACCESS\$1TO\$1IOT\$1THING\$1ADMIN\$1READ\$1ACTIONS
+ ALLOWS\$1ACCESS\$1TO\$1IOT\$1NON\$1THING\$1ADMIN\$1ACTIONS
+ ALLOWS\$1ACCESS\$1TO\$1IOT\$1THING\$1ADMIN\$1WRITE\$1ACTIONS

## 为什么这非常重要
<a name="audit-chk-auth-cognito-role-permissive-why-it-matters"></a>

如果经身份验证的身份遭到破坏，它可能使用管理操作来修改账户设置、删除资源或获取对敏感数据的访问权限。

## 如何修复
<a name="audit-chk-auth-cognito-role-permissive-how-to-fix"></a>

附加到经过身份验证的 Amazon Cognito 身份池角色的策略应仅为设备授予执行任务所需的这些权限。我们建议您完成以下步骤：

1. 创建新的合规角色。

1. 创建新的 Amazon Cognito 身份池并为其附加合规角色。

1. 验证您的身份能否使用新池访问 AWS IoT。

1. 验证完成后，将合规角色附加到标记为不合规的 Amazon Cognito 身份池。

您还可以使用缓解操作实现以下目的：
+ 应用 `PUBLISH_FINDINGS_TO_SNS` 缓解操作，以实施自定义响应来响应 Amazon SNS 消息。

有关更多信息，请参阅 [缓解操作](dd-mitigation-actions.md)。

## 管理或修改事物。
<a name="audit-chk-auth-cognito-role-permissive-manage-things"></a>

以下 AWS IoT API 操作可用于管理或修改事物，因此不应该为通过经身份验证的 Amazon Cognito 身份池进行连接的设备授予执行以下操作的权限：
+ `AddThingToThingGroup` 
+ `AttachThingPrincipal` 
+ `CreateThing` 
+ `DeleteThing` 
+ `DetachThingPrincipal` 
+ `ListThings`
+ `ListThingsInThingGroup` 
+ `RegisterThing` 
+ `RemoveThingFromThingGroup` 
+ `UpdateThing` 
+ `UpdateThingGroupsForThing`

任何授权执行这些操作的角色，即便是在单一资源上执行这些操作，也会被视为不合规。

## 管理非事物对象
<a name="audit-chk-auth-cognito-role-permissive-manage-non-things"></a>

通过经身份验证的 Amazon Cognito 身份池进行连接的设备，不应该被授予执行除这些部分中所述之外的 AWS IoT API 操作的权限。要使用通过经身份验证 Amazon Cognito 身份池进行连接的应用程序来管理账户，请创建一个不供设备使用的单独身份池。

## 读取事物管理数据
<a name="audit-chk-auth-cognito-role-permissive-read-things-admin-data"></a>

以下 AWS IoT API 操作可用于读取事物数据，因此应该为通过经身份验证的 Amazon Cognito 身份池进行连接的设备授予仅对有限事物集执行以下操作的权限：
+ `DescribeThing`
+ `ListJobExecutionsForThing`
+ `ListThingGroupsForThing`
+ `ListThingPrincipals`
+ 不合规：

  ```
  arn:aws:iot:region:account-id:thing/*
  ```

  此示例允许设备对任何事物执行指定操作。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [ 
            "iot:DescribeThing",
            "iot:ListJobExecutionsForThing",
            "iot:ListThingGroupsForThing",
            "iot:ListThingPrincipals"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:thing/MyThing"
        ]
      }
    ]
  }
  ```

------

  此示例允许设备仅对一个事物执行指定操作。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:DescribeThing",
                  "iot:ListJobExecutionsForThing",
                  "iot:ListThingGroupsForThing",
                  "iot:ListThingPrincipals"
              ],
              "Resource": [
                  "arn:aws:iot:us-east-1:123456789012:thing/MyThing*"
              ]
          }
      ]
  }
  ```

------

  此示例之所以合规，是因为尽管该资源是使用通配符 (\$1) 指定的，但它的前面是特定字符串，这将仅限名称包含给定前缀的设备访问事物集。
+ 不合规：

  ```
  arn:aws:iot:region:account-id:thing/*
  ```

  此示例允许设备对任何事物执行指定操作。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [ 
            "iot:DescribeThing",
            "iot:ListJobExecutionsForThing",
            "iot:ListThingGroupsForThing",
            "iot:ListThingPrincipals"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:thing/MyThing"
        ]
      }
    ]
  }
  ```

------

  此示例允许设备仅对一个事物执行指定操作。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:DescribeThing",
                  "iot:ListJobExecutionsForThing",
                  "iot:ListThingGroupsForThing",
                  "iot:ListThingPrincipals"
              ],
              "Resource": [
                  "arn:aws:iot:us-east-1:123456789012:thing/MyThing*"
              ]
          }
      ]
  }
  ```

------

  此示例之所以合规，是因为尽管该资源是使用通配符 (\$1) 指定的，但它的前面是特定字符串，这将仅限名称包含给定前缀的设备访问事物集。

## 订阅/发布到 MQTT 主题
<a name="audit-chk-auth-cognito-role-permissive-mqtt-topic"></a>

MQTT 消息通过 AWS IoT 消息代理发送，并由设备用来执行许多不同的操作，包括访问和修改影子状态和任务执行状态。为设备授权以连接、发布或订阅 MQTT 消息的策略，应该限定对特定资源执行如下操作：

**Connect**  
+ 不合规：

  ```
  arn:aws:iot:region:account-id:client/*
  ```

  通配符 \$1 允许任何设备连接到 AWS IoT。

  ```
  arn:aws:iot:region:account-id:client/${iot:ClientId}
  ```

  除非 `iot:Connection.Thing.IsAttached` 在条件键中设置为 True，这相当于上例中的通配符 \$1。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [
          "iot:Connect"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
        ]
      }
    ]
  }
  ```

------

  资源规范包含与用于连接的设备名称匹配的变量，且条件语句通过检查 MQTT 客户端所用的证书是否与附加到使用此名称的事物的证书匹配，进一步限制权限。

**发布**  
+ 不合规：

  ```
  arn:aws:iot:region:account-id:topic/$aws/things/*/shadow/update
  ```

  此示例允许设备更新任何设备的影子（\$1 = 所有设备）。

  ```
  arn:aws:iot:region:account-id:topic/$aws/things/*
  ```

  此示例允许设备读取/更新/删除任何设备的影子。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:Publish"
              ],
              "Resource": [
                  "arn:aws:iot:us-east-1:123456789012:topic/$aws/things/${iot:Connection.Thing.ThingName}/shadow/*"
              ]
          }
      ]
  }
  ```

------

  资源规范包含通配符，但仅匹配其事物名称用于连接的设备的任何影子相关主题。

**订阅**  
+ 不合规：

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/*
  ```

  此示例允许设备订阅为所有设备预留的影子或任务主题。

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/#
  ```

  与上一个示例相同，不过使用的是 \$1 通配符。

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/+/shadow/update
  ```

  此示例允许设备查看任何设备上的影子更新（\$1 = 所有设备）。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "iot:Subscribe"
              ],
              "Resource": [
                  "arn:aws:iot:us-east-1:123456789012:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/shadow/*",
                  "arn:aws:iot:us-east-1:123456789012:topicfilter/$aws/things/${iot:Connection.Thing.ThingName}/jobs/*"
              ]
          }
      ]
  }
  ```

------

  资源规范包含通配符，但仅匹配其事物名称用于连接的设备的任何影子相关主题和任务相关主题。

**接收**  
+ 合规：

  ```
  arn:aws:iot:region:account-id:topicfilter/$aws/things/*
  ```

  这是合规的，因为设备只能从其有权订阅的主题接收消息。

## 读取或修改影子或任务数据
<a name="audit-chk-auth-cognito-role-permissive-shadow-job-data"></a>

为设备授予执行 API 操作权限以访问或修改设备影子或任务执行数据的策略，应该限定对特定资源执行以下操作。下面是一些 API 操作：
+ `DeleteThingShadow`
+ `GetThingShadow`
+ `UpdateThingShadow`
+ `DescribeJobExecution`
+ `GetPendingJobExecutions`
+ `StartNextPendingJobExecution`
+ `UpdateJobExecution`

**示例**
+ 不合规：

  ```
  arn:aws:iot:region:account-id:thing/*
  ```

  此示例允许设备对任何事物执行指定操作。
+ 合规：

------
#### [ JSON ]

****  

  ```
  {
    "Version":"2012-10-17",		 	 	 
    "Statement": [
      {
        "Effect": "Allow",
        "Action": [
          "iot:DeleteThingShadow",
          "iot:GetThingShadow",
          "iot:UpdateThingShadow",
          "iot:DescribeJobExecution",
          "iotjobsdata:DescribeJobExecution",
          "iotjobsdata:UpdateJobExecution"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:123456789012:thing/MyThing1",
          "arn:aws:iot:us-east-1:123456789012:thing/MyThing2"
        ]
      }
    ]
  }
  ```

------

  此示例允许设备仅对两个事物执行指定操作。