Project: KICS (Provisioning / Security & Compliance)
File: a9a13d4f-f17a-491b-b074-f54bffffcb4a.md

title: Service Account Token Automount Not Disabled hide: toc: true navigation: true <style> .highlight .hll { background-color: #ff171742; } .md-content { max-width: 1100px; margin: 0 auto; } </style> Query id: a9a13d4f-f17a-491b-b074-f54bffffcb4a Query name: Service Account Token Automount Not Disabled Platform: Terraform Severity: <span style="color:#ff7213">Medium</span> Category: Insecure Defaults URL: Service Account Tokens are automatically mounted even if not necessary<br> ```tf title="Positive test num. 1 - tf file" hl_lines="88 25 162 144" resource "kubernetes_deployment" "example" { metadata { name = "terraform-example" labels = { test = "MyExampleApp" } } spec { replicas = 3 selector { match_labels = { test = "MyExampleApp" } } template { metadata { labels = { test = "MyExampleApp" } } spec { container { image = "nginx:1.7.8" name = "example" resources { limits = { cpu = "0.5" memory = "512Mi" } requests = { cpu = "250m" memory = "50Mi" } } liveness_probe { http_get { path = "/nginx_status" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initialdelayseconds = 3 period_seconds = 3 } } } } } } resource "kubernetes_daemonset" "example2" { metadata { name = "terraform-example" namespace = "something" labels = { test = "MyExampleApp" } } spec { selector { match_labels = { test = "MyExampleApp" } } template { metadata { labels = { test = "MyExampleApp" } } spec { automountserviceaccount_token = true container { image = "nginx:1.7.8" name = "example" resources { limits = { cpu = "0.5" memory = "512Mi" } requests = { cpu = "250m" memory = "50Mi" } } liveness_probe { http_get { path = "/nginx_status" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initialdelayseconds = 3 period_seconds = 3 } } } } } } resource "kubernetescronjob" "demo3" { metadata { name = "demo" } spec { concurrency_policy = "Replace" failedjobshistory_limit = 5 schedule = "1 0 *" startingdeadlineseconds = 10 successfuljobshistory_limit = 10 job_template { metadata {} spec { backoff_limit = 2 ttlsecondsafter_finished = 10 template { metadata {} spec { automountserviceaccount_token = true container { name = "hello" image = "busybox" command = ["/bin/sh", "-c", "date; echo Hello from the Kubernetes cluster"] } } } } } } } resource "kubernetes_pod" "test6" { metadata { name = "terraform-example" } spec { container { image = "nginx:1.7.9" name = "example" env { name = "environment" value = "test" } port { container_port = 8080 } liveness_probe { http_get { path = "/nginx_status" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initialdelayseconds = 3 period_seconds = 3 } } dns_config { nameservers =

"8.8.8.8", "9.9.9.9"] searches = ["example.com"] option { name = "ndots" value = 1 } option { name = "use-vc" } } dns_policy = "None" } } ``` ```tf title="Negative test num. 1 - tf file" resource "kubernetes_deployment" "example9" { metadata { name = "terraform-example" labels = { test = "MyExampleApp" } } spec { replicas = 3 selector { match_labels = { test = "MyExampleApp" } } template { metadata { labels = { test = "MyExampleApp" } } spec { automountserviceaccount_token = false container { image = "nginx:1.7.8" name = "example" resources { limits = { cpu = "0.5" memory = "512Mi" } requests = { cpu = "250m" memory = "50Mi" } } liveness_probe { http_get { path = "/nginx_status" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initialdelayseconds = 3 period_seconds = 3 } } } } } } resource "kubernetes_daemonset" "example22" { metadata { name = "terraform-example" namespace = "something" labels = { test = "MyExampleApp" } } spec { selector { match_labels = { test = "MyExampleApp" } } template { metadata { labels = { test = "MyExampleApp" } } spec { automountserviceaccount_token = false container { image = "nginx:1.7.8" name = "example" resources { limits = { cpu = "0.5" memory = "512Mi" } requests = { cpu = "250m" memory = "50Mi" } } liveness_probe { http_get { path = "/nginx_status" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initialdelayseconds = 3 period_seconds = 3 } } } } } } resource "kubernetescronjob" "demo32" { metadata { name = "demo" } spec { concurrency_policy = "Replace" failedjobshistory_limit = 5 schedule = "1 0 *" startingdeadlineseconds = 10 successfuljobshistory_limit = 10 job_template { metadata {} spec { backoff_limit = 2 ttlsecondsafter_finished = 10 template { metadata {} spec { automountserviceaccount_token = false container { name = "hello" image = "busybox" command = ["/bin/sh", "-c", "date; echo Hello from the Kubernetes cluster"] } } } } } } } resource "kubernetes_pod" "test62" { metadata { name = "terraform-example" } spec { automountserviceaccount_token = false container { image = "nginx:1.7.9" name = "example" env { name = "environment" value = "test" } port { container_port = 8080 } liveness_probe { http_get { path = "/nginx_status" port = 80 http_header { name = "X-Custom-Header" value = "Awesome" } } initialdelayseconds = 3 period_seconds = 3 } } dns_config { nameservers = ["1.1.1.1", "8.8.8.8", "9.9.9.9"] searches = ["example.com"] option { name = "ndots" value = 1 } option { name = "use-vc" } } dns_policy = "None" } } ```