Skip to content

Commit 92db0dc

Browse files
container: add rbac config binding (#14692) (#10441)
[upstream:e43963ca763cd7172e93626876d9b00686eb5d42] Signed-off-by: Modular Magician <[email protected]>
1 parent fc9f6d3 commit 92db0dc

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

.changelog/14692.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 support for `rbac_binding_config` in `google_container_cluster`
3+
```

google-beta/services/container/resource_container_cluster.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2537,6 +2537,27 @@ func ResourceContainerCluster() *schema.Resource {
25372537
},
25382538
},
25392539
},
2540+
"rbac_binding_config": {
2541+
Type: schema.TypeList,
2542+
Optional: true,
2543+
MaxItems: 1,
2544+
Computed: true,
2545+
Description: `RBACBindingConfig allows user to restrict ClusterRoleBindings an RoleBindings that can be created.`,
2546+
Elem: &schema.Resource{
2547+
Schema: map[string]*schema.Schema{
2548+
"enable_insecure_binding_system_unauthenticated": {
2549+
Type: schema.TypeBool,
2550+
Optional: true,
2551+
Description: `Setting this to true will allow any ClusterRoleBinding and RoleBinding with subjects system:anonymous or system:unauthenticated.`,
2552+
},
2553+
"enable_insecure_binding_system_authenticated": {
2554+
Type: schema.TypeBool,
2555+
Optional: true,
2556+
Description: `Setting this to true will allow any ClusterRoleBinding and RoleBinding with subjects system:authenticated.`,
2557+
},
2558+
},
2559+
},
2560+
},
25402561
},
25412562
}
25422563
}
@@ -2862,6 +2883,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
28622883
cluster.AnonymousAuthenticationConfig = expandAnonymousAuthenticationConfig(v)
28632884
}
28642885

2886+
if v, ok := d.GetOk("rbac_binding_config"); ok {
2887+
cluster.RbacBindingConfig = expandRBACBindingConfig(v)
2888+
}
2889+
28652890
needUpdateAfterCreate := false
28662891

28672892
// For now PSC based cluster don't support `enable_private_endpoint` on `create`, but only on `update` API call.
@@ -3438,6 +3463,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
34383463
return err
34393464
}
34403465

3466+
if err := d.Set("rbac_binding_config", flattenRBACBindingConfig(cluster.RbacBindingConfig)); err != nil {
3467+
return err
3468+
}
3469+
34413470
return nil
34423471
}
34433472

@@ -4970,6 +4999,22 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
49704999
}
49715000
}
49725001

5002+
if d.HasChange("rbac_binding_config") {
5003+
req := &container.UpdateClusterRequest{
5004+
Update: &container.ClusterUpdate{
5005+
DesiredRbacBindingConfig: expandRBACBindingConfig(d.Get("rbac_binding_config")),
5006+
ForceSendFields: []string{"DesiredRbacBindingConfig"},
5007+
}}
5008+
5009+
updateF := updateFunc(req, "updating GKE cluster RBAC binding config")
5010+
// Call update serially.
5011+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
5012+
return err
5013+
}
5014+
5015+
log.Printf("[INFO] GKE cluster %s's RBAC binding config has been updated", d.Id())
5016+
}
5017+
49735018
d.Partial(false)
49745019

49755020
if d.HasChange("cluster_telemetry") {
@@ -6523,6 +6568,20 @@ func expandWorkloadAltsConfig(configured interface{}) *container.WorkloadALTSCon
65236568
}
65246569
}
65256570

6571+
func expandRBACBindingConfig(configured interface{}) *container.RBACBindingConfig {
6572+
l := configured.([]interface{})
6573+
if len(l) == 0 || l[0] == nil {
6574+
return nil
6575+
}
6576+
6577+
config := l[0].(map[string]interface{})
6578+
return &container.RBACBindingConfig{
6579+
EnableInsecureBindingSystemUnauthenticated: config["enable_insecure_binding_system_unauthenticated"].(bool),
6580+
EnableInsecureBindingSystemAuthenticated: config["enable_insecure_binding_system_authenticated"].(bool),
6581+
ForceSendFields: []string{"EnableInsecureBindingSystemUnauthenticated", "EnableInsecureBindingSystemAuthenticated"},
6582+
}
6583+
}
6584+
65266585
func flattenNotificationConfig(c *container.NotificationConfig) []map[string]interface{} {
65276586
if c == nil {
65286587
return nil
@@ -7501,6 +7560,18 @@ func flattenWorkloadAltsConfig(c *container.WorkloadALTSConfig) []map[string]int
75017560
}
75027561
}
75037562

7563+
func flattenRBACBindingConfig(c *container.RBACBindingConfig) []map[string]interface{} {
7564+
if c == nil {
7565+
return nil
7566+
}
7567+
return []map[string]interface{}{
7568+
{
7569+
"enable_insecure_binding_system_authenticated": c.EnableInsecureBindingSystemAuthenticated,
7570+
"enable_insecure_binding_system_unauthenticated": c.EnableInsecureBindingSystemUnauthenticated,
7571+
},
7572+
}
7573+
}
7574+
75047575
func resourceContainerClusterStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
75057576
config := meta.(*transport_tpg.Config)
75067577

google-beta/services/container/resource_container_cluster_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14066,3 +14066,67 @@ resource "google_container_cluster" "primary" {
1406614066
}
1406714067
`, name, networkName, subnetworkName, mode)
1406814068
}
14069+
14070+
func TestAccContainerCluster_RbacBindingConfig(t *testing.T) {
14071+
t.Parallel()
14072+
14073+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
14074+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
14075+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
14076+
14077+
acctest.VcrTest(t, resource.TestCase{
14078+
PreCheck: func() { acctest.AccTestPreCheck(t) },
14079+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
14080+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
14081+
Steps: []resource.TestStep{
14082+
{
14083+
Config: testAccContainerCluster_RbacBindingConfig(clusterName, networkName, subnetworkName, true, true),
14084+
Check: resource.ComposeAggregateTestCheckFunc(
14085+
resource.TestCheckResourceAttr("google_container_cluster.primary", "rbac_binding_config.#", "1"),
14086+
resource.TestCheckResourceAttr("google_container_cluster.primary", "rbac_binding_config.0.enable_insecure_binding_system_unauthenticated", "true"),
14087+
resource.TestCheckResourceAttr("google_container_cluster.primary", "rbac_binding_config.0.enable_insecure_binding_system_authenticated", "true"),
14088+
),
14089+
},
14090+
{
14091+
ResourceName: "google_container_cluster.primary",
14092+
ImportState: true,
14093+
ImportStateVerify: true,
14094+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14095+
},
14096+
{
14097+
Config: testAccContainerCluster_RbacBindingConfig(clusterName, networkName, subnetworkName, false, false),
14098+
Check: resource.ComposeAggregateTestCheckFunc(
14099+
resource.TestCheckResourceAttr("google_container_cluster.primary", "rbac_binding_config.#", "1"),
14100+
resource.TestCheckResourceAttr("google_container_cluster.primary", "rbac_binding_config.0.enable_insecure_binding_system_unauthenticated", "false"),
14101+
resource.TestCheckResourceAttr("google_container_cluster.primary", "rbac_binding_config.0.enable_insecure_binding_system_authenticated", "false"),
14102+
),
14103+
},
14104+
{
14105+
ResourceName: "google_container_cluster.primary",
14106+
ImportState: true,
14107+
ImportStateVerify: true,
14108+
ImportStateVerifyIgnore: []string{"deletion_protection"},
14109+
},
14110+
},
14111+
})
14112+
}
14113+
14114+
func testAccContainerCluster_RbacBindingConfig(clusterName, networkName, subnetworkName string, unauthenticated, authenticated bool) string {
14115+
return fmt.Sprintf(`
14116+
resource "google_container_cluster" "primary" {
14117+
name = "%s"
14118+
location = "us-central1-a"
14119+
initial_node_count = 1
14120+
14121+
network = "%s"
14122+
subnetwork = "%s"
14123+
14124+
rbac_binding_config {
14125+
enable_insecure_binding_system_unauthenticated = %t
14126+
enable_insecure_binding_system_authenticated = %t
14127+
}
14128+
14129+
deletion_protection = false
14130+
}
14131+
`, clusterName, networkName, subnetworkName, unauthenticated, authenticated)
14132+
}

website/docs/r/container_cluster.html.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,8 @@ Fleet configuration for the cluster. Structure is [documented below](#nested_fle
440440
* `anonymous_authentication_config` - (Optional)
441441
Configuration for [anonymous authentication restrictions](https://cloud.google.com/kubernetes-engine/docs/how-to/hardening-your-cluster#restrict-anon-access). Structure is [documented below](#anonymous_authentication_config).
442442

443+
* `rbac_binding_config` - (Optional)
444+
RBACBindingConfig allows user to restrict ClusterRoleBindings an RoleBindings that can be created. Structure is [documented below](#nested_rbac_binding_config).
443445

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

@@ -1574,6 +1576,11 @@ linux_node_config {
15741576

15751577
* `mode` - (Optional) Sets or removes authentication restrictions. Available options include `LIMITED` and `ENABLED`.
15761578

1579+
<a name="nested_rbac_binding_config"></a>The `rbac_binding_config` block supports:
1580+
1581+
* `enable_insecure_binding_system_unauthenticated` - (Optional) Setting this to true will allow any ClusterRoleBinding and RoleBinding with subjects system:anonymous or system:unauthenticated.
1582+
* `enable_insecure_binding_system_authenticated` - (Optional) Setting this to true will allow any ClusterRoleBinding and RoleBinding with subjects system:authenticated.
1583+
15771584

15781585
## Attributes Reference
15791586

0 commit comments

Comments
 (0)