From 2b8b75199a7bc014f09521d0f2fc2b5b5e27aa71 Mon Sep 17 00:00:00 2001 From: Jorge Falcon <22119513+BCTBB@users.noreply.github.com> Date: Fri, 30 May 2025 10:05:42 -0400 Subject: [PATCH] Terraform k8s deployments - Handle runAsUser/runAsGroup/run_as_non_root when not defined for (#53) Handling use-case where kubernetes platform is openshift and runAsUser/runAsGroup is not defined by a user; Openshift automatically assigns runAsUser/runAsGroup. --- k8s/README.md | 4 ++-- k8s/cronjob.tf | 10 +++++----- k8s/deploy.tf | 8 ++++---- k8s/example/main.tf | 35 ++++++++++++++++++++++++++++++++++- k8s/job.tf | 6 +++--- k8s/variables.tf | 7 ++++--- 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/k8s/README.md b/k8s/README.md index 621937c..80f8ef1 100644 --- a/k8s/README.md +++ b/k8s/README.md @@ -292,7 +292,7 @@ No requirements. | Name | Version | |------|---------| -| [kubernetes](#provider\_kubernetes) | 2.36.0 | +| [kubernetes](#provider\_kubernetes) | 2.37.1 | ## Modules @@ -323,7 +323,7 @@ No modules. | [environment\_from\_config\_maps](#input\_environment\_from\_config\_maps) | Used to configure additional environment variables from a config map for the fleet deployment and vuln-processing cron job. | `list(map(string))` | `[]` | no | | [environment\_from\_secrets](#input\_environment\_from\_secrets) | Used to configure additional environment variables from a secret for the fleet deployment and vuln-processing cron job. | `list(map(string))` | `[]` | no | | [environment\_variables](#input\_environment\_variables) | Used to configure additional environment variables for the fleet deployment and vuln-processing cron job. | `list(map(string))` | `[]` | no | -| [fleet](#input\_fleet) | Used to configure Fleet specific values for use in the Fleet deployment, migration job, and vuln-processing cron job. |
object({
listen_port = optional(number, 8080)
secret_name = optional(string, "fleet")
migrations = object({
auto_apply_sql_migrations = optional(bool, true)
migration_job_annotations = optional(map(string), {})
parallelism = optional(number, 1)
completions = optional(number, 1)
active_deadline_seconds = optional(number, 900)
backoff_limit = optional(number, 6)
manual_selector = optional(bool, false)
restart_policy = optional(string, "Never")
})
tls = object({
enabled = optional(bool, false)
unique_tls_secret = optional(bool, false)
secret_name = optional(string, "fleet-tls")
compatibility = optional(string, "modern")
cert_secret_key = optional(string, "server.cert")
key_secret_key = optional(string, "server.key")
})
auth = object({
b_crypto_cost = optional(number, 12)
salt_key_size = optional(number, 24)
})
app = object({
token_key_size = optional(number, 24)
invite_token_validity_period = optional(string, "120h")
})
session = object({
key_size = optional(number, 64)
duration = optional(string, "2160h")
})
logging = object({
debug = optional(bool, false)
json = optional(bool, false)
disable_banner = optional(bool, false)
})
carving = object({
s3 = object({
bucket_name = optional(string, "")
prefix = optional(string, "")
access_key_id = optional(string, "")
secret_key = optional(string, "s3-bucket")
sts_assume_role_arn = optional(string, "")
})
})
license = object({
secret_name = optional(string, "")
license_key = optional(string, "license-key")
})
extra_volumes = optional(list(any), [])
extra_volume_mounts = optional(list(any), [])
security_context = object({
run_as_user = optional(number, 3333)
run_as_group = optional(number, 3333)
})
})
| n/a | yes | +| [fleet](#input\_fleet) | Used to configure Fleet specific values for use in the Fleet deployment, migration job, and vuln-processing cron job. |
object({
listen_port = optional(number, 8080)
secret_name = optional(string, "fleet")
migrations = object({
auto_apply_sql_migrations = optional(bool, true)
migration_job_annotations = optional(map(string), {})
parallelism = optional(number, 1)
completions = optional(number, 1)
active_deadline_seconds = optional(number, 900)
backoff_limit = optional(number, 6)
manual_selector = optional(bool, false)
restart_policy = optional(string, "Never")
})
tls = object({
enabled = optional(bool, false)
unique_tls_secret = optional(bool, false)
secret_name = optional(string, "fleet-tls")
compatibility = optional(string, "modern")
cert_secret_key = optional(string, "server.cert")
key_secret_key = optional(string, "server.key")
})
auth = object({
b_crypto_cost = optional(number, 12)
salt_key_size = optional(number, 24)
})
app = object({
token_key_size = optional(number, 24)
invite_token_validity_period = optional(string, "120h")
})
session = object({
key_size = optional(number, 64)
duration = optional(string, "2160h")
})
logging = object({
debug = optional(bool, false)
json = optional(bool, false)
disable_banner = optional(bool, false)
})
carving = object({
s3 = object({
bucket_name = optional(string, "")
prefix = optional(string, "")
access_key_id = optional(string, "")
secret_key = optional(string, "s3-bucket")
sts_assume_role_arn = optional(string, "")
})
})
license = object({
secret_name = optional(string, "")
license_key = optional(string, "license-key")
})
extra_volumes = optional(list(any), [])
extra_volume_mounts = optional(list(any), [])
security_context = object({
run_as_user = optional(number, null)
run_as_group = optional(number, null)
run_as_non_root = optional(bool, true)
})
})
| n/a | yes | | [gke](#input\_gke) | Used to configure gke specific values for use in the Fleet deployment, migration job, and vuln-processing cron job. |
object({
workload_identity_email = optional(string, "")
cloud_sql = object({
enable_proxy = optional(bool, false)
image_repository = optional(string, "gcr.io/cloudsql-docker/gce-proxy")
image_tag = optional(string, "1.17-alpine")
verbose = optional(bool, true)
instance_name = optional(string, "")
})
ingress = object({
use_managed_certificate = optional(bool, false)
use_gke_ingress = optional(bool, false)
node_port = optional(number, 0)
hostnames = optional(list(string), [""])
})
})
| n/a | yes | | [hostname](#input\_hostname) | Used as the hostname that you will access fleet on. | `string` | `"fleet.localhost"` | no | | [image\_pull\_secrets](#input\_image\_pull\_secrets) | Used to inject image pull secrets for access to a private container registry. |
list(object({
name = string
}))
| `[]` | no | diff --git a/k8s/cronjob.tf b/k8s/cronjob.tf index 8993d7b..998a113 100644 --- a/k8s/cronjob.tf +++ b/k8s/cronjob.tf @@ -171,7 +171,7 @@ resource "kubernetes_cron_job_v1" "fleet_vuln_processing_cron_job" { security_context { run_as_user = local.fleet.security_context.run_as_user run_as_group = local.fleet.security_context.run_as_group - run_as_non_root = "true" + run_as_non_root = local.fleet.security_context.run_as_non_root read_only_root_filesystem = "true" privileged = "false" allow_privilege_escalation = "false" @@ -231,10 +231,10 @@ resource "kubernetes_cron_job_v1" "fleet_vuln_processing_cron_job" { drop = ["ALL"] }, privileged = false, - read_only_root_filesystem = true, - run_as_group = local.fleet.security_context.run_as_group, + read_only_root_filesystem = true, run_as_user = local.fleet.security_context.run_as_user, - run_as_non_root = true + run_as_group = local.fleet.security_context.run_as_group, + run_as_non_root = local.fleet.security_context.run_as_non_root } } ] : [] @@ -260,8 +260,8 @@ resource "kubernetes_cron_job_v1" "fleet_vuln_processing_cron_job" { } privileged = container.value.security_context.privileged read_only_root_filesystem = container.value.security_context.read_only_root_filesystem - run_as_group = container.value.security_context.run_as_group run_as_user = container.value.security_context.run_as_user + run_as_group = container.value.security_context.run_as_group run_as_non_root = container.value.security_context.run_as_non_root } } diff --git a/k8s/deploy.tf b/k8s/deploy.tf index d455015..bac5a30 100644 --- a/k8s/deploy.tf +++ b/k8s/deploy.tf @@ -582,7 +582,7 @@ resource "kubernetes_deployment" "fleet" { security_context { run_as_user = local.fleet.security_context.run_as_user run_as_group = local.fleet.security_context.run_as_group - run_as_non_root = "true" + run_as_non_root = local.fleet.security_context.run_as_non_root read_only_root_filesystem = "true" privileged = "false" allow_privilege_escalation = "false" @@ -696,9 +696,9 @@ resource "kubernetes_deployment" "fleet" { }, privileged = false, read_only_root_filesystem = true, - run_as_group = local.fleet.security_context.run_as_group, run_as_user = local.fleet.security_context.run_as_user, - run_as_non_root = true + run_as_group = local.fleet.security_context.run_as_group, + run_as_non_root = local.fleet.security_context.run_as_non_root } } ] : [] @@ -870,4 +870,4 @@ resource "kubernetes_deployment" "fleet" { depends_on = [ resource.kubernetes_job.migration // dependency should only be here if migration is enabled ] -} \ No newline at end of file +} diff --git a/k8s/example/main.tf b/k8s/example/main.tf index dc882cf..ecd3aa9 100644 --- a/k8s/example/main.tf +++ b/k8s/example/main.tf @@ -1,5 +1,5 @@ module "fleet" { - source = "git::https://github.com/fleetdm/fleet-terraform//k8s?depth=1&ref=tf-mod-k8s-v1.1.0" + source = "git::https://github.com/fleetdm/fleet-terraform//k8s?depth=1&ref=tf-mod-k8s-v1.1.1" namespace = "fleet" hostname = "fleet.localhost.local" @@ -196,9 +196,42 @@ module "fleet" { } extra_volumes = [] extra_volume_mounts = [] + /* + If you're not providing an id for run_as_user or run_as_group, + and you're not running on Openshift (Openshift will set the userid/groupid, if left blank), + you might encounter an issue with Fleet pods starting up, where + k8s is not able to determine whether the fleet user, in the + Fleet container image, is running as root. + + Examples: + 1. Openshift example (without id's): + security_context = { + run_as_non_root = true + } + + 2. Openshift example (with id's): + security_context = { + run_as_user = 1000653333 + run_as_group = 1000653333 + run_as_non_root = true + } + + 3. Non-Openshift (without id's): + security_context = { + run_as_non_root = false + } + + 4. Non-Openshift (with id's): + security_context = { + run_as_user = 3333 + run_as_group = 3333 + run_as_non_root = true + } + */ security_context = { run_as_user = 3333 run_as_group = 3333 + run_as_non_root = true } } diff --git a/k8s/job.tf b/k8s/job.tf index c843bbb..da11bfc 100644 --- a/k8s/job.tf +++ b/k8s/job.tf @@ -118,7 +118,7 @@ resource "kubernetes_job" "migration" { security_context { run_as_user = local.fleet.security_context.run_as_user run_as_group = local.fleet.security_context.run_as_group - run_as_non_root = "true" + run_as_non_root = local.fleet.security_context.run_as_non_root read_only_root_filesystem = "true" privileged = "false" allow_privilege_escalation = "false" @@ -184,9 +184,9 @@ resource "kubernetes_job" "migration" { }, privileged = false, read_only_root_filesystem = true, - run_as_group = local.fleet.security_context.run_as_group, run_as_user = local.fleet.security_context.run_as_user, - run_as_non_root = true + run_as_group = local.fleet.security_context.run_as_group, + run_as_non_root = local.fleet.security_context.run_as_non_root } } ] : [] diff --git a/k8s/variables.tf b/k8s/variables.tf index eeb963e..a47b5be 100644 --- a/k8s/variables.tf +++ b/k8s/variables.tf @@ -242,8 +242,9 @@ variable "fleet" { extra_volumes = optional(list(any), []) extra_volume_mounts = optional(list(any), []) security_context = object({ - run_as_user = optional(number, 3333) - run_as_group = optional(number, 3333) + run_as_user = optional(number, null) + run_as_group = optional(number, null) + run_as_non_root = optional(bool, true) }) }) description = "Used to configure Fleet specific values for use in the Fleet deployment, migration job, and vuln-processing cron job." @@ -353,4 +354,4 @@ variable "gke" { }) }) description = "Used to configure gke specific values for use in the Fleet deployment, migration job, and vuln-processing cron job." -} \ No newline at end of file +}