From e2c43cd98c2f232db5f777c8e79fe0026bad4a86 Mon Sep 17 00:00:00 2001 From: Patrick Decat Date: Fri, 7 Dec 2018 19:32:51 +0100 Subject: [PATCH] Add run_as_group property to container and pod security contexts, update documentation and acceptance tests --- kubernetes/resource_kubernetes_deployment_test.go | 4 ++++ kubernetes/resource_kubernetes_pod_test.go | 2 ++ .../resource_kubernetes_replication_controller_test.go | 2 ++ kubernetes/schema_container.go | 5 +++++ kubernetes/schema_pod_spec.go | 5 +++++ kubernetes/structures_container.go | 6 ++++++ kubernetes/structures_pod.go | 6 ++++++ website/docs/r/deployment.html.markdown | 2 ++ website/docs/r/pod.html.markdown | 2 ++ 9 files changed, 34 insertions(+) diff --git a/kubernetes/resource_kubernetes_deployment_test.go b/kubernetes/resource_kubernetes_deployment_test.go index 45c00b2553..124617fd33 100644 --- a/kubernetes/resource_kubernetes_deployment_test.go +++ b/kubernetes/resource_kubernetes_deployment_test.go @@ -185,6 +185,7 @@ func TestAccKubernetesDeployment_with_security_context(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckKubernetesDeploymentExists(deploymentTestResourceName, &conf), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.security_context.0.fs_group", "100"), + resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.security_context.0.run_as_group", "100"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.security_context.0.run_as_non_root", "true"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.security_context.0.run_as_user", "101"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.security_context.0.supplemental_groups.#", "1"), @@ -340,6 +341,7 @@ func TestAccKubernetesDeployment_with_container_security_context(t *testing.T) { resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.capabilities.0.drop.0", "all"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.privileged", "true"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.read_only_root_filesystem", "true"), + resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.run_as_group", "200"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.run_as_non_root", "true"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.run_as_user", "201"), resource.TestCheckResourceAttr(deploymentTestResourceName, "spec.0.template.0.spec.0.container.1.security_context.0.se_linux_options.#", "1"), @@ -932,6 +934,7 @@ resource "kubernetes_deployment" "test" { spec { security_context { fs_group = 100 + run_as_group = 100 run_as_non_root = true run_as_user = 101 supplemental_groups = [101] @@ -1202,6 +1205,7 @@ resource "kubernetes_deployment" "test" { privileged = true read_only_root_filesystem = true + run_as_group = 200 run_as_non_root = true run_as_user = 201 diff --git a/kubernetes/resource_kubernetes_pod_test.go b/kubernetes/resource_kubernetes_pod_test.go index 54460c5a77..4b422d28a0 100644 --- a/kubernetes/resource_kubernetes_pod_test.go +++ b/kubernetes/resource_kubernetes_pod_test.go @@ -282,6 +282,7 @@ func TestAccKubernetesPod_with_pod_security_context(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( testAccCheckKubernetesPodExists("kubernetes_pod.test", &conf), resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.fs_group", "100"), + resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.run_as_group", "100"), resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.run_as_non_root", "true"), resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.run_as_user", "101"), resource.TestCheckResourceAttr("kubernetes_pod.test", "spec.0.security_context.0.supplemental_groups.#", "1"), @@ -845,6 +846,7 @@ resource "kubernetes_pod" "test" { spec { security_context { fs_group = 100 + run_as_group = 100 run_as_non_root = true run_as_user = 101 supplemental_groups = [101] diff --git a/kubernetes/resource_kubernetes_replication_controller_test.go b/kubernetes/resource_kubernetes_replication_controller_test.go index 8fe01bf62d..ae1c90a8c2 100644 --- a/kubernetes/resource_kubernetes_replication_controller_test.go +++ b/kubernetes/resource_kubernetes_replication_controller_test.go @@ -205,6 +205,7 @@ func TestAccKubernetesReplicationController_with_security_context(t *testing.T) Check: resource.ComposeAggregateTestCheckFunc( testAccCheckKubernetesReplicationControllerExists("kubernetes_replication_controller.test", &conf), resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.spec.0.security_context.0.fs_group", "100"), + resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.spec.0.security_context.0.run_as_group", "100"), resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.spec.0.security_context.0.run_as_non_root", "true"), resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.spec.0.security_context.0.run_as_user", "101"), resource.TestCheckResourceAttr("kubernetes_replication_controller.test", "spec.0.template.0.spec.0.security_context.0.supplemental_groups.#", "1"), @@ -739,6 +740,7 @@ resource "kubernetes_replication_controller" "test" { spec { security_context { fs_group = 100 + run_as_group = 100 run_as_non_root = true run_as_user = 101 supplemental_groups = [101] diff --git a/kubernetes/schema_container.go b/kubernetes/schema_container.go index 593a5ddc6f..9a3d15eb45 100644 --- a/kubernetes/schema_container.go +++ b/kubernetes/schema_container.go @@ -627,6 +627,11 @@ func securityContextSchema() *schema.Resource { Default: false, Description: "Whether this container has a read-only root filesystem. Default is false.", }, + "run_as_group": { + Type: schema.TypeInt, + Description: "The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.", + Optional: true, + }, "run_as_non_root": { Type: schema.TypeBool, Description: "Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.", diff --git a/kubernetes/schema_pod_spec.go b/kubernetes/schema_pod_spec.go index 3395530938..62f803ade2 100644 --- a/kubernetes/schema_pod_spec.go +++ b/kubernetes/schema_pod_spec.go @@ -203,6 +203,11 @@ func podSpecFields(isUpdatable, isDeprecated, isComputed bool) map[string]*schem Description: "A special supplemental group that applies to all containers in a pod. Some volume types allow the Kubelet to change the ownership of that volume to be owned by the pod: 1. The owning GID will be the FSGroup 2. The setgid bit is set (new files created in the volume will be owned by FSGroup) 3. The permission bits are OR'd with rw-rw---- If unset, the Kubelet will not modify the ownership and permissions of any volume.", Optional: true, }, + "run_as_group": { + Type: schema.TypeInt, + Description: "The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container.", + Optional: true, + }, "run_as_non_root": { Type: schema.TypeBool, Description: "Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence.", diff --git a/kubernetes/structures_container.go b/kubernetes/structures_container.go index 6b43adaed9..53a8c5400f 100644 --- a/kubernetes/structures_container.go +++ b/kubernetes/structures_container.go @@ -30,6 +30,9 @@ func flattenContainerSecurityContext(in *v1.SecurityContext) []interface{} { if in.ReadOnlyRootFilesystem != nil { att["read_only_root_filesystem"] = *in.ReadOnlyRootFilesystem } + if in.RunAsGroup != nil { + att["run_as_group"] = *in.RunAsGroup + } if in.RunAsNonRoot != nil { att["run_as_non_root"] = *in.RunAsNonRoot } @@ -554,6 +557,9 @@ func expandContainerSecurityContext(l []interface{}) *v1.SecurityContext { if v, ok := in["read_only_root_filesystem"]; ok { obj.ReadOnlyRootFilesystem = ptrToBool(v.(bool)) } + if v, ok := in["run_as_group"]; ok { + obj.RunAsGroup = ptrToInt64(int64(v.(int))) + } if v, ok := in["run_as_non_root"]; ok { obj.RunAsNonRoot = ptrToBool(v.(bool)) } diff --git a/kubernetes/structures_pod.go b/kubernetes/structures_pod.go index dcd6a93281..1f204b365e 100644 --- a/kubernetes/structures_pod.go +++ b/kubernetes/structures_pod.go @@ -127,6 +127,9 @@ func flattenPodSecurityContext(in *v1.PodSecurityContext) []interface{} { if in.FSGroup != nil { att["fs_group"] = *in.FSGroup } + if in.RunAsGroup != nil { + att["run_as_group"] = *in.RunAsGroup + } if in.RunAsNonRoot != nil { att["run_as_non_root"] = *in.RunAsNonRoot } @@ -529,6 +532,9 @@ func expandPodSecurityContext(l []interface{}) *v1.PodSecurityContext { if v, ok := in["fs_group"].(int); ok { obj.FSGroup = ptrToInt64(int64(v)) } + if v, ok := in["run_as_group"].(int); ok { + obj.RunAsGroup = ptrToInt64(int64(v)) + } if v, ok := in["run_as_non_root"].(bool); ok { obj.RunAsNonRoot = ptrToBool(v) } diff --git a/website/docs/r/deployment.html.markdown b/website/docs/r/deployment.html.markdown index 9d8ca35ecd..6f0dddbc3a 100644 --- a/website/docs/r/deployment.html.markdown +++ b/website/docs/r/deployment.html.markdown @@ -588,6 +588,7 @@ The `items` block supports the following: * `capabilities` - (Optional) The capabilities to add/drop when running containers. Defaults to the default set of capabilities granted by the container runtime. * `privileged` - (Optional) Run container in privileged mode. Processes in privileged containers are essentially equivalent to root on the host. Defaults to false. * `read_only_root_filesystem` - (Optional) Whether this container has a read-only root filesystem. Default is false. +* `run_as_group` - (Optional) The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. * `run_as_non_root` - (Optional) Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. * `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. * `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. @@ -604,6 +605,7 @@ The `items` block supports the following: #### Arguments * `fs_group` - (Optional) A special supplemental group that applies to all containers in a pod. Some volume types allow the Kubelet to change the ownership of that volume to be owned by the pod: 1. The owning GID will be the FSGroup 2. The setgid bit is set (new files created in the volume will be owned by FSGroup) 3. The permission bits are OR'd with rw-rw---- If unset, the Kubelet will not modify the ownership and permissions of any volume. +* `run_as_group` - (Optional) The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container. * `run_as_non_root` - (Optional) Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. * `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 SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container. * `se_linux_options` - (Optional) The SELinux context to be applied to all containers. If unspecified, the container runtime will allocate a random SELinux context for each container. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container. diff --git a/website/docs/r/pod.html.markdown b/website/docs/r/pod.html.markdown index b1710d9eec..29258f9d04 100644 --- a/website/docs/r/pod.html.markdown +++ b/website/docs/r/pod.html.markdown @@ -544,6 +544,7 @@ The `items` block supports the following: * `capabilities` - (Optional) The capabilities to add/drop when running containers. Defaults to the default set of capabilities granted by the container runtime. * `privileged` - (Optional) Run container in privileged mode. Processes in privileged containers are essentially equivalent to root on the host. Defaults to false. * `read_only_root_filesystem` - (Optional) Whether this container has a read-only root filesystem. Default is false. +* `run_as_group` - (Optional) The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. * `run_as_non_root` - (Optional) Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in PodSecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. * `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. * `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. @@ -560,6 +561,7 @@ The `items` block supports the following: #### Arguments * `fs_group` - (Optional) A special supplemental group that applies to all containers in a pod. Some volume types allow the Kubelet to change the ownership of that volume to be owned by the pod: 1. The owning GID will be the FSGroup 2. The setgid bit is set (new files created in the volume will be owned by FSGroup) 3. The permission bits are OR'd with rw-rw---- If unset, the Kubelet will not modify the ownership and permissions of any volume. +* `run_as_group` - (Optional) The GID to run the entrypoint of the container process. Uses runtime default if unset. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container. * `run_as_non_root` - (Optional) Indicates that the container must run as a non-root user. If true, the Kubelet will validate the image at runtime to ensure that it does not run as UID 0 (root) and fail to start the container if it does. If unset or false, no such validation will be performed. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence. * `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 SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container. * `se_linux_options` - (Optional) The SELinux context to be applied to all containers. If unspecified, the container runtime will allocate a random SELinux context for each container. May also be set in SecurityContext. If set in both SecurityContext and PodSecurityContext, the value specified in SecurityContext takes precedence for that container.