Skip to content

Commit 1da108e

Browse files
Add json_custom_config field to compute_security_policy resource (#6521) (#4703)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent b257bd9 commit 1da108e

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

.changelog/6521.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `json_custom_config` field to `google_compute_security_policy` resource
3+
```

google-beta/resource_compute_security_policy.go

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,23 @@ func resourceComputeSecurityPolicy() *schema.Resource {
324324
ValidateFunc: validation.StringInSlice([]string{"DISABLED", "STANDARD"}, false),
325325
Description: `JSON body parsing. Supported values include: "DISABLED", "STANDARD".`,
326326
},
327+
"json_custom_config": {
328+
Type: schema.TypeList,
329+
Optional: true,
330+
Computed: true,
331+
Description: `Custom configuration to apply the JSON parsing. Only applicable when JSON parsing is set to STANDARD.`,
332+
MaxItems: 1,
333+
Elem: &schema.Resource{
334+
Schema: map[string]*schema.Schema{
335+
"content_types": {
336+
Type: schema.TypeSet,
337+
Required: true,
338+
Elem: &schema.Schema{Type: schema.TypeString},
339+
Description: `A list of custom Content-Type header values to apply the JSON parsing.`,
340+
},
341+
},
342+
},
343+
},
327344
"log_level": {
328345
Type: schema.TypeString,
329346
Optional: true,
@@ -530,7 +547,7 @@ func resourceComputeSecurityPolicyUpdate(d *schema.ResourceData, meta interface{
530547

531548
if d.HasChange("advanced_options_config") {
532549
securityPolicy.AdvancedOptionsConfig = expandSecurityPolicyAdvancedOptionsConfig(d.Get("advanced_options_config").([]interface{}))
533-
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "AdvancedOptionsConfig", "advancedOptionsConfig.jsonParsing", "advancedOptionsConfig.logLevel")
550+
securityPolicy.ForceSendFields = append(securityPolicy.ForceSendFields, "AdvancedOptionsConfig", "advancedOptionsConfig.jsonParsing", "advancedOptionsConfig.jsonCustomConfig", "advancedOptionsConfig.logLevel")
534551
}
535552

536553
if d.HasChange("adaptive_protection_config") {
@@ -778,8 +795,9 @@ func expandSecurityPolicyAdvancedOptionsConfig(configured []interface{}) *comput
778795

779796
data := configured[0].(map[string]interface{})
780797
return &compute.SecurityPolicyAdvancedOptionsConfig{
781-
JsonParsing: data["json_parsing"].(string),
782-
LogLevel: data["log_level"].(string),
798+
JsonParsing: data["json_parsing"].(string),
799+
JsonCustomConfig: expandSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(data["json_custom_config"].([]interface{})),
800+
LogLevel: data["log_level"].(string),
783801
}
784802
}
785803

@@ -789,8 +807,33 @@ func flattenSecurityPolicyAdvancedOptionsConfig(conf *compute.SecurityPolicyAdva
789807
}
790808

791809
data := map[string]interface{}{
792-
"json_parsing": conf.JsonParsing,
793-
"log_level": conf.LogLevel,
810+
"json_parsing": conf.JsonParsing,
811+
"json_custom_config": flattenSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(conf.JsonCustomConfig),
812+
"log_level": conf.LogLevel,
813+
}
814+
815+
return []map[string]interface{}{data}
816+
}
817+
818+
func expandSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(configured []interface{}) *compute.SecurityPolicyAdvancedOptionsConfigJsonCustomConfig {
819+
if len(configured) == 0 || configured[0] == nil {
820+
// If configuration is unset, return an empty JsonCustomConfig; this ensures the ContentTypes list can be cleared
821+
return &compute.SecurityPolicyAdvancedOptionsConfigJsonCustomConfig{}
822+
}
823+
824+
data := configured[0].(map[string]interface{})
825+
return &compute.SecurityPolicyAdvancedOptionsConfigJsonCustomConfig{
826+
ContentTypes: convertStringArr(data["content_types"].(*schema.Set).List()),
827+
}
828+
}
829+
830+
func flattenSecurityPolicyAdvancedOptionsConfigJsonCustomConfig(conf *compute.SecurityPolicyAdvancedOptionsConfigJsonCustomConfig) []map[string]interface{} {
831+
if conf == nil {
832+
return nil
833+
}
834+
835+
data := map[string]interface{}{
836+
"content_types": schema.NewSet(schema.HashString, convertStringArrToInterface(conf.ContentTypes)),
794837
}
795838

796839
return []map[string]interface{}{data}

google-beta/resource_compute_security_policy_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,14 @@ resource "google_compute_security_policy" "policy" {
423423
424424
advanced_options_config {
425425
json_parsing = "STANDARD"
426+
json_custom_config {
427+
content_types = [
428+
"application/json",
429+
"application/vnd.api+json",
430+
"application/vnd.collection+json",
431+
"application/vnd.hyper+json"
432+
]
433+
}
426434
log_level = "VERBOSE"
427435
}
428436
}

website/docs/r/compute_security_policy.html.markdown

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,36 @@ The following arguments are supported:
6363
security policy, a default rule with action "allow" will be added. Structure is [documented below](#nested_rule).
6464

6565
* `advanced_options_config` - (Optional) [Advanced Configuration Options](https://cloud.google.com/armor/docs/security-policy-overview#json-parsing).
66+
Structure is [documented below](#nested_advanced_options_config).
67+
68+
* `adaptive_protection_config` - (Optional) Configuration for [Google Cloud Armor Adaptive Protection](https://cloud.google.com/armor/docs/adaptive-protection-overview?hl=en). Structure is [documented below](#nested_adaptive_protection_config).
69+
70+
* `type` - The type indicates the intended use of the security policy.
71+
* CLOUD_ARMOR - Cloud Armor backend security policies can be configured to filter incoming HTTP requests targeting backend services.
72+
They filter requests before they hit the origin servers.
73+
* CLOUD_ARMOR_EDGE - Cloud Armor edge security policies can be configured to filter incoming HTTP requests targeting backend services
74+
(including Cloud CDN-enabled) as well as backend buckets (Cloud Storage).
75+
They filter requests before the request is served from Google's cache.
6676

6777
<a name="nested_advanced_options_config"></a>The `advanced_options_config` block supports:
6878

6979
* `json_parsing` - Whether or not to JSON parse the payload body. Defaults to `DISABLED`.
7080
* DISABLED - Don't parse JSON payloads in POST bodies.
7181
* STANDARD - Parse JSON payloads in POST bodies.
7282

83+
* `json_custom_config` - Custom configuration to apply the JSON parsing. Only applicable when
84+
`json_parsing` is set to `STANDARD`. Structure is [documented below](#nested_json_custom_config).
85+
7386
* `log_level` - Log level to use. Defaults to `NORMAL`.
7487
* NORMAL - Normal log level.
7588
* VERBOSE - Verbose log level.
7689

77-
* `adaptive_protection_config` - (Optional) Configuration for [Google Cloud Armor Adaptive Protection](https://cloud.google.com/armor/docs/adaptive-protection-overview?hl=en). Structure is [documented below](#nested_adaptive_protection_config).
90+
<a name="nested_json_custom_config"></a>The `json_custom_config` block supports:
7891

79-
* `type` - The type indicates the intended use of the security policy.
80-
* CLOUD_ARMOR - Cloud Armor backend security policies can be configured to filter incoming HTTP requests targeting backend services.
81-
They filter requests before they hit the origin servers.
82-
* CLOUD_ARMOR_EDGE - Cloud Armor edge security policies can be configured to filter incoming HTTP requests targeting backend services
83-
(including Cloud CDN-enabled) as well as backend buckets (Cloud Storage).
84-
They filter requests before the request is served from Google's cache.
92+
* `content_types` - A list of custom Content-Type header values to apply the JSON parsing. The
93+
format of the Content-Type header values is defined in
94+
[RFC 1341](https://www.ietf.org/rfc/rfc1341.txt). When configuring a custom Content-Type header
95+
value, only the type/subtype needs to be specified, and the parameters should be excluded.
8596

8697
<a name="nested_rule"></a>The `rule` block supports:
8798

0 commit comments

Comments
 (0)