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

Add run_as_group to container security contexts attributes #414

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: 4 additions & 0 deletions kubernetes/resource_kubernetes_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions kubernetes/resource_kubernetes_pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions kubernetes/resource_kubernetes_replication_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions kubernetes/schema_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
5 changes: 5 additions & 0 deletions kubernetes/schema_pod_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/structures_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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))
}
Expand Down
6 changes: 6 additions & 0 deletions kubernetes/structures_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/deployment.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/pod.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down