Skip to content

Commit f117446

Browse files
[API Shield] Document new terraform resources (#26613)
* [API Shield] Document new terraform resources * Apply suggestions from code review * Fixup terraform examples * Update src/content/docs/api-shield/reference/terraform.mdx --------- Co-authored-by: Patricia Santa Ana <[email protected]>
1 parent 57f8e48 commit f117446

File tree

2 files changed

+98
-22
lines changed

2 files changed

+98
-22
lines changed

src/content/docs/api-shield/reference/terraform.mdx

Lines changed: 96 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,31 @@ The following resources are available to configure through Terraform:
1818

1919
- [`api_shield_operation`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_operation) for configuring <GlossaryTooltip term="API endpoint">endpoints</GlossaryTooltip> in Endpoint Management.
2020

21-
**Schema validation 2.0**
21+
**Schema validation**
2222

23-
- [`api_shield_schema`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_schema) for configuring a schema in [Schema validation 2.0](/api-shield/security/schema-validation/).
24-
- [`api_shield_schema_validation_settings`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_schema_validation_settings) for configuring zone-level Schema validation 2.0 settings.
25-
- [`api_shield_operation_schema_validation_settings`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_operation_schema_validation_settings) for configuring operation-level Schema validation 2.0 settings.
23+
- [`cloudflare_schema_validation_schemas`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/schema_validation_schemas) for configuring a schema in [Schema validation](/api-shield/security/schema-validation/).
24+
~~[`api_shield_schema`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_schema)~~ has been deprecated and will be removed in a future version of the terraform provider.
25+
- [`cloudflare_schema_validation_settings`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/schema_validation_settings) for configuring zone-level Schema validation settings.
26+
~~[`api_shield_schema_validation_settings`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_schema_validation_settings)~~ has been deprecated and will be removed in a future version of the terraform provider.
27+
- [`cloudflare_schema_validation_operation_settings`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/schema_validation_operation_settings) for configuring operation-level Schema validation settings.
28+
~~[`api_shield_operation_schema_validation_settings`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/api_shield_operation_schema_validation_settings)~~ has been deprecated and will be removed in a future version of the terraform provider.
29+
30+
**JWT Validation**
31+
32+
- [`cloudflare_token_validation_config`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/token_validation_config) for setting up JWT validation with specific keying material and token locations.
33+
- [`cloudflare_token_validation_rules`](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/token_validation_rules) for setting up rules to action on the validation result.
2634

2735
## Manage API Shield session identifiers
2836

2937
Refer to the example configuration below to set up [session identifiers](/api-shield/get-started/#to-set-up-session-identifiers) on your zone.
3038

3139
```tf title="Example configuration"
32-
resource "cloudflare_api_shield" "my_api_shield" {
33-
zone_id = var.zone_id
34-
auth_id_characteristics {
40+
resource "cloudflare_api_shield" "session_identifiers" {
41+
zone_id = var.zone_id
42+
auth_id_characteristics = [{
3543
name = "authorization"
3644
type = "header"
37-
}
45+
}]
3846
}
3947
```
4048

@@ -58,35 +66,101 @@ resource "cloudflare_api_shield_operation" "post_image" {
5866
}
5967
```
6068

61-
## Manage Schema validation 2.0
69+
## Manage Schema validation
6270

6371
:::note
6472

65-
It is required to configure Endpoint Management if you want to set up Schema validation 2.0 using Terraform.
73+
It is required to configure Endpoint Management if you want to set up Schema validation using Terraform.
6674
:::
6775

68-
Refer to the example configuration below to manage [Schema validation 2.0](/api-shield/security/schema-validation/api/) on your zone.
76+
Refer to the example configuration below to manage [Schema validation](/api-shield/security/schema-validation/api/) on your zone.
6977

7078
```tf title="Example configuration"
71-
# Schema that should be used for Schema validation 2.0
72-
resource "cloudflare_api_shield_schema" "example_schema" {
73-
zone_id = var.zone_id
74-
name = "example-schema"
75-
kind = "openapi_v3"
76-
validation_enabled = true
77-
source = file("./schemas/example-schema.json")
79+
# Schema that should be used for Schema validation
80+
resource "cloudflare_schema_validation_schemas" "example_schema" {
81+
zone_id = var.zone_id
82+
kind = "openapi_v3"
83+
name = "example-schema.yaml"
84+
# In this example, we assume that the `example-schema.yaml` includes `get_image` and `post_image` operations from above
85+
source = file("./schemas/example-schema.yaml")
86+
validation_enabled = true
7887
}
7988
8089
# Block all requests that violate schema by default
81-
resource "cloudflare_api_shield_schema_validation_settings" "zone_level_settings" {
82-
zone_id = var.zone_id
83-
validation_default_mitigation_action = "block"
90+
resource "cloudflare_schema_validation_settings" "zone_level_settings" {
91+
zone_id = var.zone_id
92+
validation_default_mitigation_action = "block"
8493
}
8594
8695
# For endpoint post_image - only log requests that violate schema
87-
resource "cloudflare_api_shield_operation_schema_validation_settings" "post_image_log_only" {
96+
resource "cloudflare_schema_validation_operation_settings" "post_image_log_only" {
8897
zone_id = var.zone_id
8998
operation_id = cloudflare_api_shield_operation.post_image.id
9099
mitigation_action = "log"
91100
}
92101
```
102+
103+
## Validate JWTs
104+
105+
Refer to the example configuration below to perform [JWT Validation](/api-shield/security/jwt-validation/) on your zone.
106+
107+
```tf title="Example configuration"
108+
# Setting up JWT validation with specific keying material and location of the token
109+
resource "cloudflare_token_validation_config" "example_es256_config" {
110+
zone_id = var.zone_id
111+
token_type = "JWT"
112+
title = "ES256 Example"
113+
description = "An example configuration that validates ES256 JWTs with `b0078548-c9bc-46e5-a678-06fb72443427` key ID in the authorization header"
114+
token_sources = ["http.request.headers[\"authorization\"][0]"]
115+
credentials = {
116+
keys = [
117+
{
118+
alg = "ES256"
119+
kid = "b0078548-c9bc-46e5-a678-06fb72443427"
120+
kty = "EC"
121+
crv = "P-256"
122+
x = "yl_BZSxUG5II7kJCMxDfWImiU6zkcJcBYaTgzV3Jgnk"
123+
y = "0qAzLQe_YGEdotb54qWq00k74QdiTOiWnuw_YzuIqr0"
124+
}
125+
]
126+
}
127+
}
128+
129+
# Setting up JWT rules for all configured endpoints on `example.com` except for `get_image`
130+
resource "cloudflare_token_validation_rules" "example_com" {
131+
zone_id = var.zone_id
132+
title = "Validate JWTs on example.com"
133+
description = "This actions JWT validation results for requests to example.com except for the get_image endpoint"
134+
action = "block"
135+
enabled = true
136+
# Require that the JWT described through the example_es256_config is valid.
137+
# Reference the ID of the generated token config, this constructs: is_jwt_valid("<id>")
138+
# If the expression is >not true<, Cloudflare will perform the configured action on the request
139+
expression = format("(is_jwt_valid(%q))", cloudflare_token_validation_config.example_es256_config.id)
140+
selector = {
141+
# all current and future operations matching this include selector will perform the described action when the expression fails to match
142+
include = [
143+
{
144+
host = ["example.com"]
145+
}
146+
]
147+
exclude = [
148+
{
149+
# reference the ID of the get_image operation to exclude it
150+
operation_ids = ["${cloudflare_api_shield_operation.get_image.id}"]
151+
}
152+
]
153+
}
154+
}
155+
156+
# With JWT validation, we can also refine session identifiers to use claims from the JWT
157+
resource "cloudflare_api_shield" "session_identifiers" {
158+
zone_id = var.zone_id
159+
auth_id_characteristics = [{
160+
# select the JWT's `sub` claim as an extremly stable session identifier
161+
# this is "<token_config_id:json_path>" format
162+
name = "${cloudflare_token_validation_config.example_es256_config.id}:$.sub"
163+
type = "jwt"
164+
}]
165+
}
166+
```

src/content/docs/api-shield/security/jwt-validation/api.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,3 +643,5 @@ The same accuracy applies as for EXP claims. As such, a token may be already reg
643643
:::
644644

645645
7. The final validation result and whether a token was present at all is made available to the WAF which applies the policy’s configured action (`log`/`block`).
646+
647+
8. Security Analytics events in the Cloudflare dashboard for the `API Shield - Token Validation` service will explain violation reasons in the `Token validation violations` section of the event.

0 commit comments

Comments
 (0)