Skip to content

Commit 87efeb0

Browse files
Add support for GKE anonymous authentication config (#14388) (#23491)
[upstream:47b57af50ad3d58eba09e26d99c1d466f8940096] Signed-off-by: Modular Magician <[email protected]>
1 parent d99e5c7 commit 87efeb0

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

.changelog/14388.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 `anonymous_authentication_config`
3+
```

google/services/container/resource_container_cluster.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2301,6 +2301,26 @@ func ResourceContainerCluster() *schema.Resource {
23012301
},
23022302
},
23032303
},
2304+
"anonymous_authentication_config": {
2305+
Type: schema.TypeList,
2306+
Optional: true,
2307+
MaxItems: 1,
2308+
Computed: true,
2309+
Description: `AnonymousAuthenticationConfig allows users to restrict or enable anonymous access to the cluster.`,
2310+
Elem: &schema.Resource{
2311+
Schema: map[string]*schema.Schema{
2312+
"mode": {
2313+
Type: schema.TypeString,
2314+
Required: true,
2315+
ValidateFunc: validation.StringInSlice([]string{"ENABLED", "LIMITED"}, false),
2316+
Description: `Setting this to LIMITED will restrict authentication of anonymous users to health check endpoints only.
2317+
Accepted values are:
2318+
* ENABLED: Authentication of anonymous users is enabled for all endpoints.
2319+
* LIMITED: Anonymous access is only allowed for health check endpoints.`,
2320+
},
2321+
},
2322+
},
2323+
},
23042324
},
23052325
}
23062326
}
@@ -2615,6 +2635,10 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
26152635
cluster.EnterpriseConfig = expandEnterpriseConfig(v)
26162636
}
26172637

2638+
if v, ok := d.GetOk("anonymous_authentication_config"); ok {
2639+
cluster.AnonymousAuthenticationConfig = expandAnonymousAuthenticationConfig(v)
2640+
}
2641+
26182642
needUpdateAfterCreate := false
26192643

26202644
// For now PSC based cluster don't support `enable_private_endpoint` on `create`, but only on `update` API call.
@@ -3168,6 +3192,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro
31683192
return err
31693193
}
31703194

3195+
if err := d.Set("anonymous_authentication_config", flattenAnonymousAuthenticationConfig(cluster.AnonymousAuthenticationConfig)); err != nil {
3196+
return err
3197+
}
3198+
31713199
return nil
31723200
}
31733201

@@ -4658,6 +4686,21 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
46584686
log.Printf("[INFO] GKE cluster %s Enterprise Config has been updated to %#v", d.Id(), req.Update.DesiredSecurityPostureConfig)
46594687
}
46604688

4689+
if d.HasChange("anonymous_authentication_config") {
4690+
req := &container.UpdateClusterRequest{
4691+
Update: &container.ClusterUpdate{
4692+
DesiredAnonymousAuthenticationConfig: expandAnonymousAuthenticationConfig(
4693+
d.Get("anonymous_authentication_config"),
4694+
),
4695+
},
4696+
}
4697+
updateF := updateFunc(req, "updating anonymous authentication config")
4698+
// Call update serially.
4699+
if err := transport_tpg.LockedCall(lockKey, updateF); err != nil {
4700+
return err
4701+
}
4702+
}
4703+
46614704
d.Partial(false)
46624705

46634706
if _, err := containerClusterAwaitRestingState(config, project, location, clusterName, userAgent, d.Timeout(schema.TimeoutUpdate)); err != nil {
@@ -5287,6 +5330,15 @@ func flattenEnterpriseConfig(ec *container.EnterpriseConfig) []map[string]interf
52875330
return []map[string]interface{}{result}
52885331
}
52895332

5333+
func flattenAnonymousAuthenticationConfig(aac *container.AnonymousAuthenticationConfig) []map[string]interface{} {
5334+
if aac == nil {
5335+
return nil
5336+
}
5337+
result := make(map[string]interface{})
5338+
result["mode"] = aac.Mode
5339+
return []map[string]interface{}{result}
5340+
}
5341+
52905342
func flattenAdditionalPodRangesConfig(ipAllocationPolicy *container.IPAllocationPolicy) []map[string]interface{} {
52915343
if ipAllocationPolicy == nil {
52925344
return nil
@@ -5413,6 +5465,23 @@ func expandMasterAuthorizedNetworksConfig(d *schema.ResourceData) *container.Mas
54135465
return result
54145466
}
54155467

5468+
func expandAnonymousAuthenticationConfig(configured interface{}) *container.AnonymousAuthenticationConfig {
5469+
l, ok := configured.([]interface{})
5470+
if len(l) == 0 || l[0] == nil || !ok {
5471+
return nil
5472+
}
5473+
5474+
anonAuthConfig := l[0].(map[string]interface{})
5475+
result := container.AnonymousAuthenticationConfig{}
5476+
5477+
if v, ok := anonAuthConfig["mode"]; ok {
5478+
if mode, ok := v.(string); ok && mode != "" {
5479+
result.Mode = mode
5480+
}
5481+
}
5482+
return &result
5483+
}
5484+
54165485
func expandManCidrBlocks(configured interface{}) []*container.CidrBlock {
54175486
config, ok := configured.(*schema.Set)
54185487
if !ok {

google/services/container/resource_container_cluster_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13094,3 +13094,59 @@ resource "google_container_cluster" "primary" {
1309413094
}
1309513095
`, name, networkName, subnetworkName, config)
1309613096
}
13097+
13098+
func TestAccContainerCluster_withAnonymousAuthenticationConfig(t *testing.T) {
13099+
t.Parallel()
13100+
13101+
clusterName := fmt.Sprintf("tf-test-cluster-%s", acctest.RandString(t, 10))
13102+
networkName := acctest.BootstrapSharedTestNetwork(t, "gke-cluster")
13103+
subnetworkName := acctest.BootstrapSubnet(t, "gke-cluster", networkName)
13104+
13105+
acctest.VcrTest(t, resource.TestCase{
13106+
PreCheck: func() { acctest.AccTestPreCheck(t) },
13107+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
13108+
CheckDestroy: testAccCheckContainerClusterDestroyProducer(t),
13109+
Steps: []resource.TestStep{
13110+
{
13111+
Config: testAccContainerCluster_withAnonymousAuthenticationConfig(clusterName, networkName, subnetworkName, "LIMITED"),
13112+
Check: resource.ComposeAggregateTestCheckFunc(
13113+
resource.TestCheckResourceAttr("google_container_cluster.primary", "anonymous_authentication_config.0.mode", "LIMITED"),
13114+
),
13115+
},
13116+
{
13117+
ResourceName: "google_container_cluster.primary",
13118+
ImportState: true,
13119+
ImportStateVerify: true,
13120+
ImportStateVerifyIgnore: []string{"deletion_protection"},
13121+
},
13122+
{
13123+
Config: testAccContainerCluster_withAnonymousAuthenticationConfig(clusterName, networkName, subnetworkName, "ENABLED"),
13124+
Check: resource.ComposeAggregateTestCheckFunc(
13125+
resource.TestCheckResourceAttr("google_container_cluster.primary", "anonymous_authentication_config.0.mode", "ENABLED"),
13126+
),
13127+
},
13128+
{
13129+
ResourceName: "google_container_cluster.primary",
13130+
ImportState: true,
13131+
ImportStateVerify: true,
13132+
ImportStateVerifyIgnore: []string{"deletion_protection"},
13133+
},
13134+
},
13135+
})
13136+
}
13137+
13138+
func testAccContainerCluster_withAnonymousAuthenticationConfig(name, networkName, subnetworkName string, mode string) string {
13139+
return fmt.Sprintf(`
13140+
resource "google_container_cluster" "primary" {
13141+
name = "%s"
13142+
network = "%s"
13143+
subnetwork = "%s"
13144+
initial_node_count = 1
13145+
deletion_protection = false
13146+
13147+
anonymous_authentication_config {
13148+
mode = "%s"
13149+
}
13150+
}
13151+
`, name, networkName, subnetworkName, mode)
13152+
}

website/docs/r/container_cluster.html.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,9 @@ Fleet configuration for the cluster. Structure is [documented below](#nested_fle
437437
* `enterprise_config` - (Optional)
438438
Configuration for [Enterprise edition].(https://cloud.google.com/kubernetes-engine/enterprise/docs/concepts/gke-editions). Structure is [documented below](#nested_enterprise_config).
439439

440+
* `anonymous_authentication_config` - (Optional)
441+
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).
442+
440443

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

@@ -1558,6 +1561,10 @@ linux_node_config {
15581561

15591562
* `desired_tier` - (Optional) Sets the tier of the cluster. Available options include `STANDARD` and `ENTERPRISE`.
15601563

1564+
<a name="anonymous_authentication_config"></a>The `anonymous_authentication_config` block supports:
1565+
1566+
* `mode` - (Optional) Sets or removes authentication restrictions. Available options include `LIMITED` and `ENABLED`.
1567+
15611568

15621569
## Attributes Reference
15631570

0 commit comments

Comments
 (0)