跳转到内容
搜索文档

Terraform

最后更新 查看 MarkdownAgent 设置

通过以下示例开始使用 Terraform 配置 API Shield。有关如何将 Terraform 与 Cloudflare 配合使用的更多信息,请参阅 Terraform 文档

以下资源可通过 Terraform 进行配置:

会话标识符

Endpoint management(端点管理)

Schema validation(架构验证)

JWT 验证

管理 API Shield 会话标识符

请参阅下面的示例配置,在您的区域上设置会话标识符

配置示例tf
resource "cloudflare_api_shield" "session_identifiers" {
  zone_id = var.zone_id
  auth_id_characteristics = [{
    name = "authorization"
    type = "header"
  }]
}

管理 API Shield 端点管理

请参阅下面的示例配置,在您的区域上管理端点

配置示例tf
resource "cloudflare_api_shield_operation" "get_image" {
  zone_id  = var.zone_id
  method   = "GET"
  host     = "example.com"
  endpoint = "/api/images/{var1}"
}

resource "cloudflare_api_shield_operation" "post_image" {
  zone_id  = var.zone_id
  method   = "POST"
  host     = "example.com"
  endpoint = "/api/images/{var1}"
}

管理架构验证

请参阅下面的示例配置,在您的区域上管理架构验证

配置示例tf
# 应该用于架构验证的架构
resource "cloudflare_schema_validation_schemas" "example_schema" {
  zone_id            = var.zone_id
  kind               = "openapi_v3"
  name               = "example-schema.yaml"
  # 在此示例中,我们假设 `example-schema.yaml` 包含上述的 `get_image` 和 `post_image` 操作
  source             = file("./schemas/example-schema.yaml")
  validation_enabled = true
}

# 默认阻止所有违反架构的请求
resource "cloudflare_schema_validation_settings" "zone_level_settings" {
  zone_id                              = var.zone_id
  validation_default_mitigation_action = "block"
}

# 对于端点 post_image - 仅记录违反架构的请求
resource "cloudflare_schema_validation_operation_settings" "post_image_log_only" {
  zone_id           = var.zone_id
  operation_id      = cloudflare_api_shield_operation.post_image.id
  mitigation_action = "log"
}

验证 JWT

请参阅下面的示例配置,在您的区域上执行 JWT 验证

配置示例tf
# 设置带有特定密钥材料和令牌位置的 JWT 验证
resource "cloudflare_token_validation_config" "example_es256_config" {
  zone_id       = var.zone_id
  token_type    = "JWT"
  title         = "ES256 Example"
  description   = "一个在授权标头中验证带有 `b0078548-c9bc-46e5-a678-06fb72443427` 密钥 ID 的 ES256 JWT 的配置示例"
  token_sources = ["http.request.headers[\"authorization\"][0]"]
  credentials   = {
    keys = [
      {
        alg = "ES256"
        kid = "b0078548-c9bc-46e5-a678-06fb72443427"
        kty = "EC"
        crv = "P-256"
        x   = "yl_BZSxUG5II7kJCMxDfWImiU6zkcJcBYaTgzV3Jgnk"
        y   = "0qAzLQe_YGEdotb54qWq00k74QdiTOiWnuw_YzuIqr0"
      }
    ]
  }
}

# 为 `example.com` 上除 `get_image` 之外的所有已配置端点设置 JWT 规则
resource "cloudflare_token_validation_rules" "example_com" {
 zone_id      = var.zone_id
 title        = "Validate JWTs on example.com"
 description  = "这会对发往 example.com(get_image 端点除外)请求的 JWT 验证结果执行操作"
 action       = "block"
 enabled      = true
 # 要求通过 example_es256_config 描述的 JWT 是有效的。
 # 引用生成的令牌配置的 ID,这将构建:is_jwt_valid("<id>")
 # 如果表达式为 >not true<,Cloudflare 将对请求执行配置的操作
 expression   = format("(is_jwt_valid(%q))", cloudflare_token_validation_config.example_es256_config.id)
 selector     = {
    # 匹配此 include 选择器的所有当前和未来操作都将在表达式匹配失败时执行所描述的操作
    include = [
      {
        host          = ["example.com"]
      }
    ]
    exclude = [
      {
        # 引用 get_image 操作的 ID 以将其排除
        operation_ids = ["${cloudflare_api_shield_operation.get_image.id}"]
      }
    ]
 }
}

# 通过 JWT 验证,我们还可以优化会话标识符以使用来自 JWT 的声明
resource "cloudflare_api_shield" "session_identifiers" {
  zone_id = var.zone_id
  auth_id_characteristics = [{
    # 选择 JWT 的 `sub` 声明作为一个极其稳定的会话标识符
    # 这是 "<token_config_id:json_path>" 格式
    name = "${cloudflare_token_validation_config.example_es256_config.id}:$.sub"
    type = "jwt"
  }]
}

这篇文档对您有帮助吗?