配置聊天机器人管理

本页面介绍如何为机器人管理配置 Google Cloud Armor 安全政策规则。在配置机器人管理之前,请确保您已熟悉机器人管理概览中的信息。

准备工作

以下部分介绍了配置 Google Cloud Armor 安全政策所需的所有 Identity and Access Management (IAM) 角色和权限。对于本文档中的应用场景,您只需拥有 compute.securityPolicies.createcompute.securityPolicies.update 权限。

为 Google Cloud Armor 安全政策设置 IAM 权限

以下操作需要 Identity and Access Management (IAM) Compute Security Admin 角色 (roles/compute.securityAdmin)

  • 配置、修改、更新和删除 Google Cloud Armor 安全政策
  • 使用以下 API 方法:
    • SecurityPolicies insert
    • SecurityPolicies delete
    • SecurityPolicies patch
    • SecurityPolicies addRule
    • SecurityPolicies patchRule
    • SecurityPolicies removeRule

具有 Compute Network Admin 角色 (roles/compute.networkAdmin) 的用户可以执行以下操作:

  • 为后端服务设置 Google Cloud Armor 安全政策
  • 使用以下 API 方法:
    • BackendServices setSecurityPolicy
    • BackendServices list(仅限 gcloud

具有 Security Admin 角色 (roles/iam.securityAdmin) 并且 Compute Network Admin 角色可以使用 SecurityPolicies API 方法 getlistgetRule 查看 Google Cloud Armor 安全政策。

为自定义角色设置 IAM 权限

下表列出了 IAM 角色的基本权限及其关联的 API 方法。

IAM 权限 API 方法
compute.securityPolicies.create SecurityPolicies insert
compute.securityPolicies.delete SecurityPolicies delete
compute.securityPolicies.get SecurityPolicies get
SecurityPolicies getRule
compute.securityPolicies.list SecurityPolicies list
compute.securityPolicies.use BackendServices setSecurityPolicy
compute.securityPolicies.update SecurityPolicies patch
SecurityPolicies addRule
SecurityPolicies patchRule
SecurityPolicies removeRule
compute.backendServices.setSecurityPolicy BackendServices setSecurityPolicy

使用 reCAPTCHA Enterprise 手动验证来区分真人和自动化客户端

如需将 reCAPTCHA Enterprise 与 Google Cloud Armor 搭配使用,您必须将 CHALLENGEPAGE 类型的 reCAPTCHA WAF 网站密钥(reCAPTCHA 密钥)与安全政策相关联。如需详细了解 reCAPTCHA 密钥,请参阅 reCAPTCHA 密钥概览

如需将您自己的 reCAPTCHA 密钥与安全政策关联或取消关联,请使用以下命令:

gcloud compute security-policies update SECURITY_POLICY \
    --recaptcha-redirect-site-key SITE_KEY

替换以下内容:

  • SECURITY_POLICY:安全政策的名称
  • SITE_KEY:您的 reCAPTCHA CHALLENGEPAGE 密钥

关联 reCAPTCHA 密钥

以下示例将 reCAPTCHA 密钥与安全政策相关联。关联的 reCAPTCHA 密钥适用于在给定安全政策下使用手动验证功能的所有规则。

gcloud compute security-policies update SECURITY_POLICY \
    --recaptcha-redirect-site-key "SITE_KEY"

取消关联 reCAPTCHA 密钥

要取消 reCAPTCHA 密钥与安全政策的关联,请使用以下命令:

gcloud compute security-policies update SECURITY_POLICY \
    --recaptcha-redirect-site-key ""

重定向流量以进行 reCAPTCHA Enterprise 评估

将 reCAPTCHA 密钥与安全政策关联后,您可以在该政策中创建规则,在内部重定向流量以进行 reCAPTCHA Enterprise 评估。在 gcloud CLI 中,请使用以下格式重定向流量:

gcloud compute security-policies rules create PRIORITY \
  --security-policy SECURITY_POLICY \
  {--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE} \
  --action redirect \
  --redirect-type google-recaptcha

替换以下内容:

  • PRIORITY:您要创建规则的优先级
  • SECURITY_POLICY:安全政策的名称
  • EXPRESSION:与要强制执行 reCAPTCHA 评估的流量匹配的自定义规则语言表达式
  • SRC_IP_RANGE:IP 地址范围。使用此选项可对来自此范围的所有请求强制执行 reCAPTCHA 评估。

以下示例将创建一条规则,该规则会重定向尝试到达 /login.html 的流量,以进行 reCAPTCHA Enterprise 手动验证:

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY \
    --expression "request.path.matches(\"/login.html\")" \
    --action redirect \
    --redirect-type google-recaptcha

强制执行 reCAPTCHA Enterprise 流畅评估

在继续操作之前,请参阅机器人管理概览,了解使用 reCAPTCHA Enterprise 操作令牌或会话令牌的前提条件。

如需从 reCAPTCHA Enterprise 操作令牌中提取属性,您可以使用 token.recaptcha_action.ATTRIBUTE。将 ATTRIBUTE 替换为 Google Cloud Armor 规则语言中的有效令牌属性。同样,使用 token.recaptcha_session.ATTRIBUTE 从 reCAPTCHA Enterprise 会话令牌中提取属性。如需详细了解可用的 reCAPTCHA Enterprise 令牌属性的语法,请参阅规则语言参考文档

操作令牌可以源自 Web 应用、iOS 应用或 Android 应用,而会话令牌只能源自 Web 应用。每个平台都需要单独的 reCAPTCHA 密钥。表达式 token.recaptcha_action.ATTRIBUTEtoken.recaptcha_session.ATTRIBUTE 适用于来自这些平台的令牌。为了区分来自不同平台的令牌并防止令牌盗用,我们建议您在配置使用这些表达式的规则时关联 reCAPTCHA 密钥。

示例

第一个示例创建了一条规则,以允许目标为 /login.html 且 reCAPTCHA Enterprise 操作令牌的分数不低于 0.8 的流量。

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY \
    --expression "request.path.matches(\"/login.html\") && token.recaptcha_action.score >= 0.8" \
    --action allow

第二个示例与第一个示例相同,但它还要求使用 example-site-key-1example-site-key-2 的 reCAPTCHA 密钥颁发操作令牌:

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY \
    --expression "request.path.matches(\"/login.html\") && token.recaptcha_action.score >= 0.8" \
    --recaptcha-action-site-keys "example-site-key-1,example-site-key-2" \
    --action allow

第三个示例创建了一条规则,该规则允许目标为 /login.html 的流量,其 reCAPTCHA Enterprise 会话令牌颁发的 reCAPTCHA 密钥为 example-site-key-3,得分不小于 0.8

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY \
    --expression "request.path.matches(\"/login.html\") && token.recaptcha_session.score >= 0.8" \
    --recaptcha-session-site-keys "example-site-key-3" \
    --action allow

重定向(302 响应)

如需创建规则以将流量重定向到用户配置的网址,请在 Google Cloud CLI 中使用以下格式:

gcloud compute security-policies rules create PRIORITY \
    --security-policy SECURITY_POLICY \
    {--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE} \
    --action redirect \
    --redirect-type external-302 \
    --redirect-target REDIRECT_URL

替换以下内容:

  • PRIORITY:您要创建规则的优先级
  • SECURITY_POLICY:安全政策的名称
  • EXPRESSION:与要强制执行 reCAPTCHA 评估的流量匹配的自定义规则语言表达式
  • SRC_IP_RANGE:IP 地址范围。使用此选项可对来自此范围的所有请求强制执行 reCAPTCHA 评估。
  • REDIRECT_URL:您要将流量重定向到的网址

以下示例会创建一条规则,用于将来自 10.10.10.0/24 的流量重定向到 https://www.example.com。

gcloud compute security-policies rules create 1000 \
   --security-policy SECURITY_POLICY \
   --src-ip-ranges "10.10.10.0/24" \
   --action redirect \
   --redirect-type external-302 \
   --redirect-target "https://www.example.com"

修饰请求

如要创建允许流量但在将自定义标头和用户定义的静态值发送到受保护的后端之前添加它们的规则,请在 gcloud CLI 中使用以下格式:

gcloud compute security-policies rules create PRIORITY \
    --security-policy SECURITY_POLICY \
    {--expression EXPRESSION | --src-ip-ranges SRC_IP_RANGE} \
    --action allow \
    --request-headers-to-add HEADER_1=VALUE_1,HEADER_2=VALUE_2,...

替换以下内容:

  • PRIORITY:您要创建规则的优先级
  • SECURITY_POLICY:安全政策的名称
  • EXPRESSION:与要强制执行 reCAPTCHA 评估的流量匹配的自定义规则语言表达式
  • SRC_IP_RANGE:IP 地址范围。使用此选项可对来自此范围的所有请求强制执行 reCAPTCHA 评估。
  • HEADER_#:要用来修饰请求的请求标头的名称
  • VALUE_#:要用来修饰请求的请求标头的值

以下示例会创建一条规则,以允许目标为 /login.html 的流量,前提是该请求的 reCAPTCHA Enterprise 操作令牌得分低于 0.2

gcloud compute security-policies rules create 1000 \
    --security-policy SECURITY_POLICY \
    --expression "request.path.matches("/login.html") && token.recaptcha_action.score < 0.2" \
    --action allow \
    --request-headers-to-add "reCAPTCHA-Warning=high"

后续步骤