

# 使用条件键限制 Contributor Insights 用户对日志组的访问
<a name="iam-cw-condition-keys-contributor"></a>

要在 Contributor Insights 中创建规则并查看其结果，用户必须具有 `cloudwatch:PutInsightRule` 权限。默认情况下，具有此权限的用户可以创建 Contributor Insights 规则，用于评估 CloudWatch Logs 中的任何日志组，然后查看结果。结果可以包含这些日志组的贡献者数据。

您可以使用条件密钥创建 IAM 策略，以授予用户为某些日志组编写 Contributor Insights 规则的权限，同时阻止用户为其他日志组编写规则和查看此数据。

 有关 IAM 策略中 `Condition` 元素的更多信息，请参阅 [IAM JSON 策略元素：条件](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html)。

**仅允许对特定日志组的编写规则和查看结果的访问权限**

以下策略允许用户访问对名为 `AllowedLogGroup` 的日志组和名称以 `AllowedWildCard` 开头的所有日志组的编写规则和查看结果的访问权限。它不授予对其他日志组的编写规则或查看规则结果的访问权限。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowCertainLogGroups",
            "Effect": "Allow",
            "Action": "cloudwatch:PutInsightRule",
            "Resource": "arn:aws:cloudwatch:*:*:insight-rule/*",
            "Condition": {
                "ForAllValues:StringEqualsIgnoreCase": {
                    "cloudwatch:requestInsightRuleLogGroups": [
                        "AllowedLogGroup",
                        "AllowedWildcard*"
                    ]
                }
            }
        }
    ]
}
```

------

**拒绝为特定日志组编写规则，但允许为所有其他日志组编写规则**

以下策略显式拒绝用户对名为 `ExplicitlyDeniedLogGroup` 的日志组编写规则和查看结果的访问权限，但允许对所有其他日志组编写规则和查看规则结果。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowInsightRulesOnLogGroupsByDefault",
            "Effect": "Allow",
            "Action": "cloudwatch:PutInsightRule",
            "Resource": "arn:aws:cloudwatch:*:*:insight-rule/*"
          
        },
        {
            "Sid": "ExplicitDenySomeLogGroups",
            "Effect": "Deny",
            "Action": "cloudwatch:PutInsightRule",
            "Resource": "arn:aws:cloudwatch:*:*:insight-rule/*",
            "Condition": {
                "ForAllValues:StringEqualsIgnoreCase": {
                    "cloudwatch:requestInsightRuleLogGroups": [
                        "/test/alpine/ExplicitlyDeniedLogGroup"
                    ]
                }
            }
        }
    ]
}
```

------