Skip to content

Commit 9bbcd21

Browse files
Support security posture config (#8185) (#5821)
* support security posture config in the same format as the API * move create code up Signed-off-by: Modular Magician <[email protected]>
1 parent 42feb33 commit 9bbcd21

File tree

6 files changed

+177
-2
lines changed

6 files changed

+177
-2
lines changed

.changelog/8185.txt

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

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ require (
2525
github.com/sirupsen/logrus v1.8.1
2626
golang.org/x/net v0.10.0
2727
golang.org/x/oauth2 v0.8.0
28-
google.golang.org/api v0.125.0
28+
google.golang.org/api v0.128.0
2929
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc
3030
google.golang.org/grpc v1.55.0
3131
google.golang.org/protobuf v1.30.0
@@ -56,7 +56,7 @@ require (
5656
github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932 // indirect
5757
github.com/google/s2a-go v0.1.4 // indirect
5858
github.com/google/uuid v1.3.0 // indirect
59-
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
59+
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
6060
github.com/googleapis/gax-go/v2 v2.10.0 // indirect
6161
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
6262
github.com/hashicorp/go-hclog v1.2.1 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
142142
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
143143
github.com/googleapis/enterprise-certificate-proxy v0.2.3 h1:yk9/cqRKtT9wXZSsRH9aurXEpJX+U6FLtpYTdC3R06k=
144144
github.com/googleapis/enterprise-certificate-proxy v0.2.3/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
145+
github.com/googleapis/enterprise-certificate-proxy v0.2.4 h1:uGy6JWR/uMIILU8wbf+OkstIrNiMjGpEIyhx8f6W7s4=
146+
github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
145147
github.com/googleapis/gax-go/v2 v2.10.0 h1:ebSgKfMxynOdxw8QQuFOKMgomqeLGPqNLQox2bo42zg=
146148
github.com/googleapis/gax-go/v2 v2.10.0/go.mod h1:4UOEnMCrxsSqQ940WnTiD6qJ63le2ev3xfyagutxiPw=
147149
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
@@ -405,6 +407,8 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3j
405407
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
406408
google.golang.org/api v0.125.0 h1:7xGvEY4fyWbhWMHf3R2/4w7L4fXyfpRGE9g6lp8+DCk=
407409
google.golang.org/api v0.125.0/go.mod h1:mBwVAtz+87bEN6CbA1GtZPDOqY2R5ONPqJeIlvyo4Aw=
410+
google.golang.org/api v0.128.0 h1:RjPESny5CnQRn9V6siglged+DZCgfu9l6mO9dkX9VOg=
411+
google.golang.org/api v0.128.0/go.mod h1:Y611qgqaE92On/7g65MQgxYul3c0rEB894kniWLY750=
408412
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
409413
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
410414
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=

google-beta/resource_container_cluster_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,6 +3555,84 @@ func TestAccContainerCluster_withProtectConfig(t *testing.T) {
35553555
})
35563556
}
35573557

3558+
func TestAccContainerCluster_withSecurityPostureConfig(t *testing.T) {
3559+
t.Parallel()
3560+
3561+
clusterName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))
3562+
3563+
VcrTest(t, resource.TestCase{
3564+
PreCheck: func() { AccTestPreCheck(t) },
3565+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
3566+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
3567+
Steps: []resource.TestStep{
3568+
{
3569+
Config: testAccContainerCluster_SetSecurityPostureToStandard(clusterName),
3570+
},
3571+
{
3572+
ResourceName: "google_container_cluster.with_security_posture_config",
3573+
ImportState: true,
3574+
ImportStateVerify: true,
3575+
},
3576+
{
3577+
Config: testAccContainerCluster_SetWorkloadVulnerabilityToStandard(clusterName),
3578+
},
3579+
{
3580+
ResourceName: "google_container_cluster.with_security_posture_config",
3581+
ImportState: true,
3582+
ImportStateVerify: true,
3583+
},
3584+
{
3585+
Config: testAccContainerCluster_DisableALL(clusterName),
3586+
},
3587+
{
3588+
ResourceName: "google_container_cluster.with_security_posture_config",
3589+
ImportState: true,
3590+
ImportStateVerify: true,
3591+
},
3592+
},
3593+
})
3594+
}
3595+
3596+
func testAccContainerCluster_SetSecurityPostureToStandard(resource_name string) string {
3597+
return fmt.Sprintf(`
3598+
resource "google_container_cluster" "with_security_posture_config" {
3599+
name = "%s"
3600+
location = "us-central1-a"
3601+
initial_node_count = 1
3602+
security_posture_config {
3603+
mode = "BASIC"
3604+
}
3605+
}
3606+
`, resource_name)
3607+
}
3608+
3609+
func testAccContainerCluster_SetWorkloadVulnerabilityToStandard(resource_name string) string {
3610+
return fmt.Sprintf(`
3611+
resource "google_container_cluster" "with_security_posture_config" {
3612+
name = "%s"
3613+
location = "us-central1-a"
3614+
initial_node_count = 1
3615+
security_posture_config {
3616+
vulnerability_mode = "VULNERABILITY_BASIC"
3617+
}
3618+
}
3619+
`, resource_name)
3620+
}
3621+
3622+
func testAccContainerCluster_DisableALL(resource_name string) string {
3623+
return fmt.Sprintf(`
3624+
resource "google_container_cluster" "with_security_posture_config" {
3625+
name = "%s"
3626+
location = "us-central1-a"
3627+
initial_node_count = 1
3628+
security_posture_config {
3629+
mode = "DISABLED"
3630+
vulnerability_mode = "VULNERABILITY_DISABLED"
3631+
}
3632+
}
3633+
`, resource_name)
3634+
}
3635+
35583636
func TestAccContainerCluster_autopilot_minimal(t *testing.T) {
35593637
t.Parallel()
35603638

google-beta/services/container/resource_container_cluster.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,33 @@ func ResourceContainerCluster() *schema.Resource {
10711071
},
10721072
},
10731073

1074+
"security_posture_config": {
1075+
Type: schema.TypeList,
1076+
Optional: true,
1077+
MaxItems: 1,
1078+
Computed: true,
1079+
Description: `Defines the config needed to enable/disable features for the Security Posture API`,
1080+
Elem: &schema.Resource{
1081+
Schema: map[string]*schema.Schema{
1082+
"mode": {
1083+
Type: schema.TypeString,
1084+
Optional: true,
1085+
Computed: true,
1086+
ValidateFunc: validation.StringInSlice([]string{"DISABLED", "BASIC", "MODE_UNSPECIFIED"}, false),
1087+
Description: `Sets the mode of the Kubernetes security posture API's off-cluster features. Available options include DISABLED and BASIC.`,
1088+
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("MODE_UNSPECIFIED"),
1089+
},
1090+
"vulnerability_mode": {
1091+
Type: schema.TypeString,
1092+
Optional: true,
1093+
Computed: true,
1094+
ValidateFunc: validation.StringInSlice([]string{"VULNERABILITY_DISABLED", "VULNERABILITY_BASIC", "VULNERABILITY_MODE_UNSPECIFIED"}, false),
1095+
Description: `Sets the mode of the Kubernetes security posture API's workload vulnerability scanning. Available options include VULNERABILITY_DISABLED and VULNERABILITY_BASIC.`,
1096+
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("VULNERABILITY_MODE_UNSPECIFIED"),
1097+
},
1098+
},
1099+
},
1100+
},
10741101
"monitoring_config": {
10751102
Type: schema.TypeList,
10761103
Optional: true,
@@ -2163,6 +2190,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
21632190
return err
21642191
}
21652192

2193+
if v, ok := d.GetOk("security_posture_config"); ok {
2194+
cluster.SecurityPostureConfig = expandSecurityPostureConfig(v)
2195+
}
2196+
21662197
req := &container.CreateClusterRequest{
21672198
Cluster: cluster,
21682199
}
@@ -2557,6 +2588,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
25572588
return err
25582589
}
25592590

2591+
if err := d.Set("security_posture_config", flattenSecurityPostureConfig(cluster.SecurityPostureConfig)); err != nil {
2592+
return err
2593+
}
2594+
25602595
if err := d.Set("protect_config", flattenProtectConfig(cluster.ProtectConfig)); err != nil {
25612596
return err
25622597
}
@@ -3612,6 +3647,20 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
36123647
}
36133648
}
36143649

3650+
if d.HasChange("security_posture_config") {
3651+
req := &container.UpdateClusterRequest{
3652+
Update: &container.ClusterUpdate{
3653+
DesiredSecurityPostureConfig: expandSecurityPostureConfig(d.Get("security_posture_config")),
3654+
},
3655+
}
3656+
updateF := updateFunc(req, "updating GKE cluster master Security Posture Config")
3657+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
3658+
return err
3659+
}
3660+
3661+
log.Printf("[INFO] GKE cluster %s Security Posture Config has been updated to %#v", d.Id(), req.Update.DesiredSecurityPostureConfig)
3662+
}
3663+
36153664
if d.HasChange("node_pool_auto_config.0.network_tags.0.tags") {
36163665
tags := d.Get("node_pool_auto_config.0.network_tags.0.tags").([]interface{})
36173666

@@ -4323,6 +4372,36 @@ func flattenProtectConfigWorkloadConfig(wc *container.WorkloadConfig) []map[stri
43234372
return []map[string]interface{}{result}
43244373
}
43254374

4375+
func expandSecurityPostureConfig(configured interface{}) *container.SecurityPostureConfig {
4376+
l := configured.([]interface{})
4377+
if len(l) == 0 || l[0] == nil {
4378+
return nil
4379+
}
4380+
4381+
spc := &container.SecurityPostureConfig{}
4382+
spConfig := l[0].(map[string]interface{})
4383+
if v, ok := spConfig["mode"]; ok {
4384+
spc.Mode = v.(string)
4385+
}
4386+
4387+
if v, ok := spConfig["vulnerability_mode"]; ok {
4388+
spc.VulnerabilityMode = v.(string)
4389+
}
4390+
return spc
4391+
}
4392+
4393+
func flattenSecurityPostureConfig(spc *container.SecurityPostureConfig) []map[string]interface{} {
4394+
if spc == nil {
4395+
return nil
4396+
}
4397+
result := make(map[string]interface{})
4398+
4399+
result["mode"] = spc.Mode
4400+
result["vulnerability_mode"] = spc.VulnerabilityMode
4401+
4402+
return []map[string]interface{}{result}
4403+
}
4404+
43264405
func expandNotificationConfig(configured interface{}) *container.NotificationConfig {
43274406
l := configured.([]interface{})
43284407
if len(l) == 0 || l[0] == nil {

website/docs/r/container_cluster.html.markdown

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,9 @@ subnetwork in which the cluster's instances are launched.
360360
* `protect_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
361361
Enable/Disable Protect API features for the cluster. Structure is [documented below](#nested_protect_config).
362362

363+
* `security_posture_config` - (Optional)
364+
Enable/Disable Security Posture API features for the cluster. Structure is [documented below](#nested_security_posture_config).
365+
363366
<a name="nested_default_snat_status"></a>The `default_snat_status` block supports
364367

365368
* `disabled` - (Required) Whether the cluster disables default in-node sNAT rules. In-node sNAT rules will be disabled when defaultSnatStatus is disabled.When disabled is set to false, default IP masquerade rules will be applied to the nodes to prevent sNAT on cluster internal traffic
@@ -1207,6 +1210,14 @@ and all pods running on the nodes. Specified as a map from the key, such as
12071210

12081211
* `audit_mode` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html)) Sets which mode of auditing should be used for the cluster's workloads. Accepted values are DISABLED, BASIC.
12091212

1213+
<a name="nested_security_posture_config"></a>The `security_posture_config` block supports:
1214+
1215+
* `mode` - (Optional) Sets the mode of the Kubernetes security posture API's off-cluster features. Available options include `DISABLED` and `BASIC`.
1216+
1217+
1218+
* `vulnerability_mode` - (Optional) Sets the mode of the Kubernetes security posture API's workload vulnerability scanning. Available options include `VULNERABILITY_DISABLED` and `VULNERABILITY_BASIC`.
1219+
1220+
12101221
## Attributes Reference
12111222

12121223
In addition to the arguments listed above, the following computed attributes are

0 commit comments

Comments
 (0)