Skip to content

Commit

Permalink
Add fs_group_change_policy to security_context (#1892)
Browse files Browse the repository at this point in the history
Co-authored-by: Gil De Grove <[email protected]>
Co-authored-by: Sheneska Williams <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2022
1 parent e2444cd commit ce7f13b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/1892.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
Add fs_group_change_policy to security_context
```
58 changes: 58 additions & 0 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,64 @@ func TestAccKubernetesPod_with_pod_security_context(t *testing.T) {
})
}

func TestAccKubernetesPod_with_pod_security_context_fs_group_change_policy(t *testing.T) {
var conf api.Pod

podName := acctest.RandomWithPrefix("tf-acc-test")
imageName := nginxImageVersion
resourceName := "kubernetes_pod.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); skipIfUnsupportedSecurityContextRunAsGroup(t) },
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckKubernetesPodDestroy,
Steps: []resource.TestStep{
{
Config: testAccKubernetesPodConfigWithSecurityContextFSChangePolicy(podName, imageName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckKubernetesPodExists(resourceName, &conf),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.fs_group", "100"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.run_as_group", "100"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.run_as_non_root", "true"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.run_as_user", "101"),
resource.TestCheckResourceAttr(resourceName, "spec.0.security_context.0.fs_group_change_policy", "OnRootMismatch"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"metadata.0.resource_version"},
},
},
})
}

func testAccKubernetesPodConfigWithSecurityContextFSChangePolicy(podName, imageName string) string {
return fmt.Sprintf(`resource "kubernetes_pod" "test" {
metadata {
labels = {
app = "pod_label"
}
name = "%s"
}
spec {
security_context {
fs_group = 100
run_as_group = 100
run_as_non_root = true
run_as_user = 101
fs_group_change_policy = "OnRootMismatch"
}
container {
image = "%s"
name = "containername"
}
}
}
`, podName, imageName)
}

func TestAccKubernetesPod_with_pod_security_context_run_as_group(t *testing.T) {
var conf api.Pod

Expand Down
10 changes: 10 additions & 0 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,16 @@ func podSpecFields(isUpdatable, isComputed bool) map[string]*schema.Schema {
Schema: seLinuxOptionsField(isUpdatable),
},
},
"fs_group_change_policy": {
Type: schema.TypeString,
Description: "fsGroupChangePolicy defines behavior of changing ownership and permission of the volume before being exposed inside Pod. This field will only apply to volume types which support fsGroup based ownership(and permissions). It will have no effect on ephemeral volume types such as: secret, configmaps and emptydir.",
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(api.FSGroupChangeAlways),
string(api.FSGroupChangeOnRootMismatch),
}, false),
ForceNew: !isUpdatable,
},
"supplemental_groups": {
Type: schema.TypeSet,
Description: "A list of groups applied to the first process run in each container, in addition to the container's primary GID. If unspecified, no groups will be added to any container.",
Expand Down
8 changes: 7 additions & 1 deletion kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ func flattenPodSecurityContext(in *v1.PodSecurityContext) []interface{} {
if in.SeccompProfile != nil {
att["seccomp_profile"] = flattenSeccompProfile(in.SeccompProfile)
}
if in.FSGroupChangePolicy != nil {
att["fs_group_change_policy"] = *in.FSGroupChangePolicy
}
if len(in.SupplementalGroups) > 0 {
att["supplemental_groups"] = newInt64Set(schema.HashSchema(&schema.Schema{
Type: schema.TypeInt,
Expand Down Expand Up @@ -898,7 +901,10 @@ func expandPodSecurityContext(l []interface{}) (*v1.PodSecurityContext, error) {
if v, ok := in["sysctl"].([]interface{}); ok && len(v) > 0 {
obj.Sysctls = expandSysctls(v)
}

if v, ok := in["fs_group_change_policy"].(string); ok && v != "" {
policy := v1.PodFSGroupChangePolicy(v)
obj.FSGroupChangePolicy = &policy
}
return obj, nil
}

Expand Down
1 change: 1 addition & 0 deletions website/docs/d/pod.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,7 @@ The `items` block supports the following:
* `run_as_user` - The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `seccomp_profile` - The seccomp options to use by the containers in this pod. Note that this field cannot be set when spec.os.name is windows.
* `se_linux_options` - The SELinux context to be applied to the container. If unspecified, the container runtime will allocate a random SELinux context for each container. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `fs_group_change_policy` - Defines behavior of changing ownership and permission of the volume before being exposed inside Pod. This field will only apply to volume types which support fsGroup based ownership(and permissions). It will have no effect on ephemeral volume types such as: secret, configmaps and emptydir. Valid values are "OnRootMismatch" and "Always". If not specified, "Always" is used. Note that this field cannot be set when spec.os.name is windows.

### `capabilities`

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/daemonset.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ The `items` block supports the following:
* `run_as_user` - (Optional) The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `seccomp_profile` - The seccomp options to use by the containers in this pod. Note that this field cannot be set when spec.os.name is windows.
* `se_linux_options` - (Optional) The SELinux context to be applied to the container. If unspecified, the container runtime will allocate a random SELinux context for each container. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `fs_group_change_policy` - Defines behavior of changing ownership and permission of the volume before being exposed inside Pod. This field will only apply to volume types which support fsGroup based ownership(and permissions). It will have no effect on ephemeral volume types such as: secret, configmaps and emptydir. Valid values are "OnRootMismatch" and "Always". If not specified, "Always" is used. Note that this field cannot be set when spec.os.name is windows.

### `capabilities`

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ The `items` block supports the following:
* `run_as_user` - (Optional) The UID to run the entrypoint of the container process. Defaults to user specified in image metadata if unspecified. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `seccomp_profile` - The seccomp options to use by the containers in this pod. Note that this field cannot be set when spec.os.name is windows.
* `se_linux_options` - (Optional) The SELinux context to be applied to the container. If unspecified, the container runtime will allocate a random SELinux context for each container. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `fs_group_change_policy` - Defines behavior of changing ownership and permission of the volume before being exposed inside Pod. This field will only apply to volume types which support fsGroup based ownership(and permissions). It will have no effect on ephemeral volume types such as: secret, configmaps and emptydir. Valid values are "OnRootMismatch" and "Always". If not specified, "Always" is used. Note that this field cannot be set when spec.os.name is windows.

### `capabilities`

Expand Down
1 change: 1 addition & 0 deletions website/docs/r/pod.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,7 @@ The `items` block supports the following:
* `seccomp_profile` - The seccomp options to use by the containers in this pod. Note that this field cannot be set when spec.os.name is windows.
* `se_linux_options` - (Optional) The SELinux context to be applied to the container. If unspecified, the container runtime will allocate a random SELinux context for each container. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.
* `sysctl` - (Optional) holds a list of namespaced sysctls used for the pod. see [Sysctl](#sysctl) block. See [official docs](https://kubernetes.io/docs/tasks/administer-cluster/sysctl-cluster/) for more details.
* `fs_group_change_policy` - Defines behavior of changing ownership and permission of the volume before being exposed inside Pod. This field will only apply to volume types which support fsGroup based ownership(and permissions). It will have no effect on ephemeral volume types such as: secret, configmaps and emptydir. Valid values are "OnRootMismatch" and "Always". If not specified, "Always" is used. Note that this field cannot be set when spec.os.name is windows.

##### Sysctl

Expand Down

0 comments on commit ce7f13b

Please sign in to comment.