Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support security posture config #8185

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mmv1/third_party/terraform/go.mod.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/sirupsen/logrus v1.8.1
golang.org/x/net v0.10.0
golang.org/x/oauth2 v0.8.0
google.golang.org/api v0.125.0
google.golang.org/api v0.128.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
Expand Down Expand Up @@ -58,7 +58,7 @@ require (
github.com/google/go-cpy v0.0.0-20211218193943-a9c933c06932 // indirect
github.com/google/s2a-go v0.1.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.4 // indirect
github.com/googleapis/gax-go/v2 v2.10.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions mmv1/third_party/terraform/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.2.3 h1:yk9/cqRKtT9wXZSsRH9aurXEpJX+U6FLtpYTdC3R06k=
github.com/googleapis/enterprise-certificate-proxy v0.2.3/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
github.com/googleapis/enterprise-certificate-proxy v0.2.4 h1:uGy6JWR/uMIILU8wbf+OkstIrNiMjGpEIyhx8f6W7s4=
github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
github.com/googleapis/gax-go/v2 v2.10.0 h1:ebSgKfMxynOdxw8QQuFOKMgomqeLGPqNLQox2bo42zg=
github.com/googleapis/gax-go/v2 v2.10.0/go.mod h1:4UOEnMCrxsSqQ940WnTiD6qJ63le2ev3xfyagutxiPw=
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaWhq2GjuNUt0aUU0YBYw=
Expand Down Expand Up @@ -405,6 +407,8 @@ golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3j
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
google.golang.org/api v0.125.0 h1:7xGvEY4fyWbhWMHf3R2/4w7L4fXyfpRGE9g6lp8+DCk=
google.golang.org/api v0.125.0/go.mod h1:mBwVAtz+87bEN6CbA1GtZPDOqY2R5ONPqJeIlvyo4Aw=
google.golang.org/api v0.128.0 h1:RjPESny5CnQRn9V6siglged+DZCgfu9l6mO9dkX9VOg=
google.golang.org/api v0.128.0/go.mod h1:Y611qgqaE92On/7g65MQgxYul3c0rEB894kniWLY750=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,33 @@ func ResourceContainerCluster() *schema.Resource {
},
<% end -%>


"security_posture_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Computed: true,
Description: `Defines the config needed to enable/disable features for the Security Posture API`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"DISABLED", "BASIC", "MODE_UNSPECIFIED"}, false),
Description: `Sets the mode of the Kubernetes security posture API's off-cluster features. Available options include DISABLED and BASIC.`,
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("MODE_UNSPECIFIED"),
},
"vulnerability_mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"VULNERABILITY_DISABLED", "VULNERABILITY_BASIC", "VULNERABILITY_MODE_UNSPECIFIED"}, false),
Description: `Sets the mode of the Kubernetes security posture API's workload vulnerability scanning. Available options include VULNERABILITY_DISABLED and VULNERABILITY_BASIC.`,
DiffSuppressFunc: tpgresource.EmptyOrDefaultStringSuppress("VULNERABILITY_MODE_UNSPECIFIED"),
},
},
},
},
"monitoring_config": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -2299,6 +2325,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
}
}

if v, ok := d.GetOk("security_posture_config"); ok {
cluster.SecurityPostureConfig = expandSecurityPostureConfig(v)
}

DDDDarrenWB marked this conversation as resolved.
Show resolved Hide resolved
if err := resourceContainerClusterRead(d, meta); err != nil {
return err
}
Expand Down Expand Up @@ -2612,6 +2642,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
return err
}

if err := d.Set("security_posture_config", flattenSecurityPostureConfig(cluster.SecurityPostureConfig)); err != nil {
return err
}

<% unless version == 'ga' -%>
if err := d.Set("protect_config", flattenProtectConfig(cluster.ProtectConfig)); err != nil {
return err
Expand Down Expand Up @@ -3675,6 +3709,20 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
}
<% end -%>

if d.HasChange("security_posture_config") {
req := &container.UpdateClusterRequest{
Update: &container.ClusterUpdate{
DesiredSecurityPostureConfig: expandSecurityPostureConfig(d.Get("security_posture_config")),
},
}
updateF := updateFunc(req, "updating GKE cluster master Security Posture Config")
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
return err
}

log.Printf("[INFO] GKE cluster %s Security Posture Config has been updated to %#v", d.Id(), req.Update.DesiredSecurityPostureConfig)
}

<% unless version == 'ga' -%>
if d.HasChange("node_pool_auto_config.0.network_tags.0.tags") {
tags := d.Get("node_pool_auto_config.0.network_tags.0.tags").([]interface{})
Expand Down Expand Up @@ -4398,6 +4446,36 @@ func flattenProtectConfigWorkloadConfig(wc *container.WorkloadConfig) []map[stri
}
<% end -%>

func expandSecurityPostureConfig(configured interface{}) *container.SecurityPostureConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil
}

spc := &container.SecurityPostureConfig{}
spConfig := l[0].(map[string]interface{})
if v, ok := spConfig["mode"]; ok {
spc.Mode = v.(string)
}

if v, ok := spConfig["vulnerability_mode"]; ok {
spc.VulnerabilityMode = v.(string)
}
return spc
}

func flattenSecurityPostureConfig(spc *container.SecurityPostureConfig) []map[string]interface{} {
if spc == nil {
return nil
}
result := make(map[string]interface{})

result["mode"] = spc.Mode
result["vulnerability_mode"] = spc.VulnerabilityMode

return []map[string]interface{}{result}
}

func expandNotificationConfig(configured interface{}) *container.NotificationConfig {
l := configured.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3576,6 +3576,84 @@ func TestAccContainerCluster_withProtectConfig(t *testing.T) {
}
<% end -%>

func TestAccContainerCluster_withSecurityPostureConfig(t *testing.T) {
t.Parallel()

clusterName := fmt.Sprintf("tf-test-cluster-%s", RandString(t, 10))

VcrTest(t, resource.TestCase{
PreCheck: func() { AccTestPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_SetSecurityPostureToStandard(clusterName),
},
{
ResourceName: "google_container_cluster.with_security_posture_config",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_SetWorkloadVulnerabilityToStandard(clusterName),
},
{
ResourceName: "google_container_cluster.with_security_posture_config",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccContainerCluster_DisableALL(clusterName),
},
{
ResourceName: "google_container_cluster.with_security_posture_config",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccContainerCluster_SetSecurityPostureToStandard(resource_name string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_security_posture_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
security_posture_config {
mode = "BASIC"
}
}
`, resource_name)
}

func testAccContainerCluster_SetWorkloadVulnerabilityToStandard(resource_name string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_security_posture_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
security_posture_config {
vulnerability_mode = "VULNERABILITY_BASIC"
}
}
`, resource_name)
}

func testAccContainerCluster_DisableALL(resource_name string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_security_posture_config" {
name = "%s"
location = "us-central1-a"
initial_node_count = 1
security_posture_config {
mode = "DISABLED"
vulnerability_mode = "VULNERABILITY_DISABLED"
}
}
`, resource_name)
}

func TestAccContainerCluster_autopilot_minimal(t *testing.T) {
t.Parallel()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ subnetwork in which the cluster's instances are launched.
* `protect_config` - (Optional, [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html))
Enable/Disable Protect API features for the cluster. Structure is [documented below](#nested_protect_config).

* `security_posture_config` - (Optional)
Enable/Disable Security Posture API features for the cluster. Structure is [documented below](#nested_security_posture_config).

<a name="nested_default_snat_status"></a>The `default_snat_status` block supports

* `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
Expand Down Expand Up @@ -1207,6 +1210,14 @@ and all pods running on the nodes. Specified as a map from the key, such as

* `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.

<a name="nested_security_posture_config"></a>The `security_posture_config` block supports:

* `mode` - (Optional) Sets the mode of the Kubernetes security posture API's off-cluster features. Available options include `DISABLED` and `BASIC`.


* `vulnerability_mode` - (Optional) Sets the mode of the Kubernetes security posture API's workload vulnerability scanning. Available options include `VULNERABILITY_DISABLED` and `VULNERABILITY_BASIC`.


## Attributes Reference

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