Skip to content

Commit 5714840

Browse files
Added new profile type "URL_FILTERING" for SecurityProfile (#13342)
1 parent 7002fc9 commit 5714840

File tree

6 files changed

+369
-0
lines changed

6 files changed

+369
-0
lines changed

mmv1/products/networksecurity/SecurityProfile.yaml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ examples:
7878
endpoint_group_id: 'my-eg'
7979
test_env_vars:
8080
org_id: 'ORG_ID'
81+
- name: 'network_security_security_profile_url_filtering'
82+
min_version: 'beta'
83+
primary_resource_id: 'default'
84+
vars:
85+
resource_name: 'my-security-profile'
86+
test_env_vars:
87+
org_id: 'ORG_ID'
8188
parameters:
8289
- name: 'name'
8390
type: String
@@ -223,6 +230,48 @@ properties:
223230
- 'DEFAULT_ACTION'
224231
- 'DENY'
225232
conflicts:
233+
- 'urlFilteringProfile'
234+
- 'customMirroringProfile'
235+
- 'customInterceptProfile'
236+
- name: 'urlFilteringProfile'
237+
min_version: 'beta'
238+
type: NestedObject
239+
description: The url filtering configuration for the security profile.
240+
custom_flatten: 'templates/terraform/custom_flatten/network_security_security_profile_url_filters_flatten.go.tmpl'
241+
properties:
242+
- name: 'urlFilters'
243+
type: Array
244+
is_set: true
245+
description: |
246+
The configuration for action to take based on domain name match.
247+
A domain name would be checked for matching filters through the list in order of highest to lowest priority,
248+
and the first filter that a domain name matches with is the one whose actions gets applied.
249+
item_type:
250+
type: NestedObject
251+
properties:
252+
- name: 'filteringAction'
253+
type: Enum
254+
description: The action to take when the filter is applied.
255+
required: true
256+
enum_values:
257+
- 'ALLOW'
258+
- 'DENY'
259+
- name: 'urls'
260+
type: Array
261+
description: |
262+
A list of domain matcher strings that a domain name gets compared with to determine if the filter is applicable.
263+
A domain name must match with at least one of the strings in the list for a filter to be applicable.
264+
item_type:
265+
type: String
266+
- name: 'priority'
267+
type: Integer
268+
description: |
269+
The priority of the filter within the URL filtering profile.
270+
Must be an integer from 0 and 2147483647, inclusive. Lower integers indicate higher priorities.
271+
The priority of a filter must be unique within a URL filtering profile.
272+
required: true
273+
conflicts:
274+
- 'threatPreventionProfile'
226275
- 'customMirroringProfile'
227276
- 'customInterceptProfile'
228277
- name: 'customMirroringProfile'
@@ -239,6 +288,7 @@ properties:
239288
required: true
240289
conflicts:
241290
- 'threatPreventionProfile'
291+
- 'urlFilteringProfile'
242292
- 'customInterceptProfile'
243293
- name: 'customInterceptProfile'
244294
type: NestedObject
@@ -254,6 +304,7 @@ properties:
254304
required: true
255305
conflicts:
256306
- 'threatPreventionProfile'
307+
- 'urlFilteringProfile'
257308
- 'customMirroringProfile'
258309
- name: 'type'
259310
type: Enum
@@ -262,5 +313,6 @@ properties:
262313
immutable: true
263314
enum_values:
264315
- 'THREAT_PREVENTION'
316+
- 'URL_FILTERING'
265317
- 'CUSTOM_MIRRORING'
266318
- 'CUSTOM_INTERCEPT'

mmv1/products/networksecurity/SecurityProfileGroup.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ examples:
7272
security_profile_group_name: 'sec-profile-group'
7373
test_env_vars:
7474
org_id: 'ORG_ID'
75+
- name: 'network_security_security_profile_group_url_filtering'
76+
min_version: 'beta'
77+
primary_resource_id: 'default'
78+
vars:
79+
security_profile_group_name: 'sec-profile-group'
80+
security_profile_name: 'sec-profile'
81+
test_env_vars:
82+
org_id: 'ORG_ID'
7583
parameters:
7684
- name: 'name'
7785
type: String
@@ -123,6 +131,11 @@ properties:
123131
type: String
124132
description: |
125133
Reference to a SecurityProfile with the threat prevention configuration for the SecurityProfileGroup.
134+
- name: 'urlFilteringProfile'
135+
min_version: 'beta'
136+
type: String
137+
description: |
138+
Reference to a SecurityProfile with the URL filtering configuration for the SecurityProfileGroup.
126139
- name: 'customMirroringProfile'
127140
type: String
128141
description: |
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
{{/*
2+
The license inside this block applies to this file
3+
Copyright 2024 Google Inc.
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/ -}}
13+
func flattenNetworkSecuritySecurityProfileUrlFilteringProfile(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
14+
if v == nil {
15+
return nil
16+
}
17+
original := v.(map[string]interface{})
18+
if len(original) == 0 {
19+
return nil
20+
}
21+
transformed := make(map[string]interface{})
22+
transformed["url_filters"] =
23+
flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFilters(original["urlFilters"], d, config)
24+
25+
// We check again the length after removing the default url_filter
26+
if transformed["url_filters"].(*schema.Set).Len() == 0 {
27+
return nil
28+
}
29+
return []interface{}{transformed}
30+
}
31+
func flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFilters(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
32+
if v == nil {
33+
return v
34+
}
35+
36+
// We check if the user included the default filter in his config
37+
resourceDataContainsDefaultFilter := false
38+
resourceData, ok := d.GetOk("url_filtering_profile.0.url_filters")
39+
if ok {
40+
for _, raw := range resourceData.(*schema.Set).List() {
41+
if raw.(map[string]interface{})["priority"] == 2147483647 {
42+
resourceDataContainsDefaultFilter = true
43+
break
44+
}
45+
}
46+
}
47+
48+
l := v.([]interface{})
49+
transformed := schema.NewSet(schema.HashResource(networksecuritySecurityProfileUrlFilteringProfileUrlFiltersSchema()), []interface{}{})
50+
for _, raw := range l {
51+
original := raw.(map[string]interface{})
52+
if len(original) < 1 {
53+
// Do not include empty json objects coming back from the api
54+
continue
55+
}
56+
57+
priorityFlatten := flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFiltersPriority(original["priority"], d, config)
58+
// Do not include the auto created default url_filter coming back from the api unless the user included it in his config
59+
if priorityFlatten == 2147483647 && !resourceDataContainsDefaultFilter {
60+
continue
61+
}
62+
63+
transformed.Add(map[string]interface{}{
64+
"filtering_action": flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFiltersFilteringAction(original["filteringAction"], d, config),
65+
"urls": flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFiltersUrls(original["urls"], d, config),
66+
"priority": priorityFlatten,
67+
})
68+
}
69+
return transformed
70+
}
71+
func flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFiltersFilteringAction(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
72+
return v
73+
}
74+
75+
func flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFiltersUrls(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
76+
return v
77+
}
78+
79+
func flattenNetworkSecuritySecurityProfileUrlFilteringProfileUrlFiltersPriority(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
80+
// Handles the string fixed64 format
81+
if strVal, ok := v.(string); ok {
82+
if intVal, err := tpgresource.StringToFixed64(strVal); err == nil {
83+
return intVal
84+
}
85+
}
86+
87+
// number values are represented as float64
88+
if floatVal, ok := v.(float64); ok {
89+
intVal := int(floatVal)
90+
return intVal
91+
}
92+
93+
return v // let terraform core handle it otherwise
94+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
resource "google_network_security_security_profile_group" "{{$.PrimaryResourceId}}" {
2+
provider = google-beta
3+
name = "{{index $.Vars "security_profile_group_name"}}"
4+
parent = "organizations/{{index $.TestEnvVars "org_id"}}"
5+
description = "my description"
6+
url_filtering_profile = google_network_security_security_profile.security_profile.id
7+
8+
labels = {
9+
foo = "bar"
10+
}
11+
}
12+
13+
resource "google_network_security_security_profile" "security_profile" {
14+
provider = google-beta
15+
name = "{{index $.Vars "security_profile_name"}}"
16+
location = "global"
17+
type = "URL_FILTERING"
18+
19+
url_filtering_profile {
20+
url_filters {
21+
priority = 1
22+
filtering_action = "ALLOW"
23+
urls = ["*example.com", "*about.example.com", "*help.example.com"]
24+
}
25+
}
26+
parent = "organizations/{{index $.TestEnvVars "org_id"}}"
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
resource "google_network_security_security_profile" "{{$.PrimaryResourceId}}" {
2+
provider = google-beta
3+
name = "{{index $.Vars "resource_name"}}"
4+
parent = "organizations/{{index $.TestEnvVars "org_id"}}"
5+
description = "my description"
6+
type = "URL_FILTERING"
7+
8+
url_filtering_profile {
9+
url_filters {
10+
priority = 1
11+
filtering_action = "ALLOW"
12+
urls = ["*example.com", "*about.example.com", "*help.example.com"]
13+
}
14+
url_filters {
15+
priority = 2
16+
filtering_action = "DENY"
17+
urls = ["*restricted.example.com"]
18+
}
19+
}
20+
21+
labels = {
22+
foo = "bar"
23+
}
24+
}

0 commit comments

Comments
 (0)