Skip to content

Commit 48bf26a

Browse files
author
Rex Scaria
committed
add doc on handling categories, porecedence and app types in gateway via terraform
1 parent b5d99ea commit 48bf26a

File tree

4 files changed

+126
-2
lines changed

4 files changed

+126
-2
lines changed

src/content/docs/cloudflare-one/policies/gateway/application-app-types.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,37 @@ To turn on the Microsoft 365 integration:
7676
3. To verify the policy was created, select **View policy**. Alternatively, go to **Gateway** > **Firewall policies** > **HTTP**. A policy named Microsoft 365 Auto Generated will be enabled in your list.
7777

7878
All future Microsoft 365 traffic will bypass Gateway logging and filtering. To disable this behavior, turn off or delete the policy.
79+
80+
### How to use app types in terraform?
81+
82+
For terraform users, we offer app types list as a dataset, so that you don't have to mention them by integer id, and instead you can mention them in your policy by the app name.
83+
84+
Example terraform app types setup
85+
86+
<pre>
87+
```
88+
data "cloudflare_zero_trust_gateway_app_types_list" "gateway_apptypes" {
89+
account_id = "<accounbt-id-string>"
90+
}
91+
92+
93+
locals {
94+
apptypes_map = merge([
95+
for c in data.cloudflare_zero_trust_gateway_app_types_list.gateway_apptypes.result : {(c.name) = c.id}]...)
96+
}
97+
98+
resource "cloudflare_zero_trust_gateway_policy" "zt_block_dns_apps" {
99+
account_id = "<accounbt-id-string>"
100+
name = "DNS Blocked apps"
101+
action = "block"
102+
traffic = "any(app.ids[*] in {${join(" ", [
103+
local.apptypes_map["Discord"],
104+
local.apptypes_map["GoToMeeting"],
105+
local.apptypes_map["Greenhouse"],
106+
local.apptypes_map["Zelle"],
107+
local.apptypes_map["Microsoft Visual Studio"]
108+
])}})"
109+
}
110+
111+
```
112+
</pre>

src/content/docs/cloudflare-one/policies/gateway/domain-categories.mdx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,44 @@ Then, the initial categorization is refined via:
229229
3. Machine learning models. Our algorithms, including DGA Domains, DNS tunneling, and phishing detection models analyze patterns and behaviors to detect new and evolving threats.
230230

231231
4. Community feedback. Through a review process, Cloudflare assesses feedback by both our internal models and threat analysts. This ensures that our categorizations reflect the most current and accurate threat intelligence.
232+
233+
## How to use categories in terraform?
234+
235+
For terraform users, we offer categories as a dataset, so that you don't have to mention them by integer id, and instead you can mention them in your policy by the category name.
236+
237+
Example terraform category setup
238+
239+
<pre>
240+
```
241+
data "cloudflare_zero_trust_gateway_categories_list" "categories" {
242+
account_id = "<accounbt-id-string>"
243+
}
244+
245+
246+
locals {
247+
main_categories_map = {
248+
for idx, c in data.cloudflare_zero_trust_gateway_categories_list.categories[0].result:
249+
c.name => c.id
250+
}
251+
252+
subcategories_map = merge(flatten([
253+
for idx, c in data.cloudflare_zero_trust_gateway_categories_list.categories[0].result: {
254+
for k,v in coalesce(c.subcategories, []): v.name => v.id
255+
}])...)
256+
}
257+
258+
resource "cloudflare_zero_trust_gateway_policy" "zt_block_dns_tech_categories" {
259+
account_id = "<accounbt-id-string>"
260+
name = "DNS Blocked"
261+
action = "block"
262+
traffic = "any(dns.content_category[*] in {${join(" ", [
263+
local.main_categories_map["Technology"],
264+
local.subcategories_map["APIs"],
265+
local.subcategories_map["Artificial Intelligence"],
266+
local.subcategories_map["Content Servers"],
267+
local.subcategories_map["Translator"]
268+
])}})"
269+
}
270+
271+
```
272+
</pre>

src/content/partials/cloudflare-one/gateway/order-of-enforcement.mdx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,51 @@ When a user goes to `https://test.example.com`, Gateway performs the following o
180180
3. Policy #3 is not evaluated because there has already been an explicit match.
181181

182182
Therefore, the user is able to connect to `https://test.example.com`.
183+
184+
### How gateway calculates precedence?
185+
186+
The gateway API starts assigning precedence to policies created in an account, starting with 1000, unless explictly soecified in the policy otherwise.
187+
Every time a new policy is added to the bottom of the order, we simply calculate the highest precedence in the account at the moment and
188+
add 1000 + rand(1...100) to the new policy, so that it now claims the maximum precedence in the account.
189+
190+
Gateway also provides a PUT endpoint to update the precedence of a policy, where the user can set any value for precedence that is not already claimed by another policy. This helps to position a policy in between two already existing policies.
191+
When polcies are re arranged using dahsboard drag and drop UI, we calculate an appropriate precedence number and assigns it to the policy.
192+
Changing the order both at UI/API and at terraform might result in stale precedence issues.
193+
194+
### Precedenc management via terraform
195+
196+
Now let's see how we can manage a large number of gateway policies and order of execution via terraform
197+
198+
With terraform v5, gateway users can list thier policies in a terraform file with any integer precedence value as they wish. We suggest starting with 1000 and leave enoughg of a gap inbetween the adjacent policy precedences, so we could sandwitch policies in between those precedence gap in the future.
199+
200+
<pre>
201+
```
202+
203+
resource "cloudflare_zero_trust_gateway_policy" "policy_1" {
204+
account_id = "<account-id>"
205+
.....
206+
precedence = 1000
207+
}
208+
209+
resource "cloudflare_zero_trust_gateway_policy" "policy_2" {
210+
account_id = "<account-id>"
211+
.....
212+
precedence = 2000
213+
}
214+
215+
216+
resource "cloudflare_zero_trust_gateway_policy" "policy_3" {
217+
account_id = "<account-id>"
218+
.....
219+
precedence = 3000
220+
}
221+
222+
223+
```
224+
</pre>
225+
226+
As shown above, way we can arrange the polcies in ascending order of precedence. To move policy 3, to in between policy 1 and policy 2, simply update the precedence of policy 3 to anumber in between [1001..1999], and also move the terraform definition to in between policy 1 and policy 2.
227+
Mandating a sufficient gap between precedences will make re ordering of policies smoother in the future.
228+
The only thing to be careful about here is, we recomment moving one policy at a time and applying the state changes. This will cause a complicated reordering manuever to be performed as series of single moves and plan-apply cycles.
229+
230+
Note: If the customer uses a mix of terraform and UI/API, please sync your polices with plan refresh before you start reordering policies in the terraform. You may also lock down your account as terraform only, from the zero trust dashboard settings.

src/content/partials/cloudflare-one/gateway/terraform-precedence-warning.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{}
33
---
44

5-
:::caution[Terraform precedence limitation]
6-
To avoid conflicts, Terraform applies a hash calculation to policy precedence. For example, a precedence of `1000` may become `1000901`. This can cause errors when reordering policies. To avoid this issue, manually set the precedence of policies created with Terraform using the [Update a Zero Trust Gateway rule](/api/resources/zero_trust/subresources/gateway/subresources/rules/methods/update/) endpoint.
5+
:::caution[Terraform precedence limitation in v4]
6+
To avoid conflicts, Terraform applies a hash calculation to policy precedence in v4. For example, a precedence of `1000` may become `1000901`. This can cause errors when reordering policies. To avoid this issue, manually set the precedence of policies created with Terraform using the [Update a Zero Trust Gateway rule](/api/resources/zero_trust/subresources/gateway/subresources/rules/methods/update/) endpoint.
7+
We suggest you to move to terrafoprm v5, for hazle free handling of precedence. In v5, terraform maintains the exact precedence marked at the policy terraform file.
78
:::

0 commit comments

Comments
 (0)