More Info:
Service account tokens should be mounted into pods only when needed (automountServiceAccountToken=false by default). Auto-mounting tokens everywhere expands credential exposure if a pod is compromised.Risk Level
MediumAddress
Compliance, SecurityCompliance Standards
- CIS OKE
Triage and Remediation
- Remediation
Remediation
Using Console
Using Console
In OKE this is a Kubernetes‑level setting, but you can fix it entirely starting from the OCI Console by using the built‑in Cloud Shell and the OKE UI.Below are concise, step‑by‑step instructions.
2. Get
7. (Optional) Use the OKE Workloads UI Instead of
If you prefer not to edit YAML via terminal:
Summary of the remediation:
1. Open Your OKE Cluster from OCI Console
- Sign in to OCI Console.
- In the left menu: Developer Services → Kubernetes Clusters (OKE).
- Select the compartment that contains the cluster.
- Click on your cluster name.
2. Get kubectl Access from the Console (Cloud Shell)
- On the cluster details page, click Access Cluster.
- Choose Cloud Shell (recommended) so you run commands directly from the browser.
-
In the panel that opens, click the button to Set Up Cluster Access (this usually runs the
oci ce cluster create-kubeconfig…command for you). -
Verify connectivity:
3. Identify Service Accounts That Auto‑Mount Tokens
By default, service accounts auto‑mount tokens unless you disable it. Find which service accounts are doing that.-
List all service accounts in all namespaces:
-
For each namespace where you have workloads, check whether
automountServiceAccountTokenis set:You will see either:- No
automountServiceAccountTokenfield (means default true), or automountServiceAccountToken: true, orautomountServiceAccountToken: false.
- No
4. Disable Token Auto‑Mounting at the Service Account Level
For each service account that should not have tokens automatically mounted:-
Edit the service account:
-
In the YAML that opens, under
metadata:(same level assecrets:), add or change:Example: -
Save and exit (in
vi, pressEsc, then type:wqand press Enter).
5. Override at Pod/Workload Level (Only Where Needed)
Some workloads may legitimately need the service account token. For those, keep the service account default as false, and enable auto‑mount only at the Pod / workload level.5.1. Edit the Deployment/StatefulSet/Pod
-
List deployments in a namespace:
-
Edit the workload that needs the token:
-
Under
spec.template.spec, add:Example: - Save and exit; Kubernetes will roll out updated pods.
6. Validate That Tokens Are No Longer Auto‑Mounted
-
After changes roll out, check one of the pods:
-
Inside the container, confirm that the token file is absent for workloads that should not have it:
- If directory or
tokenfile is missing, auto‑mount is effectively disabled. - For workloads that should have it, it should still exist.
- If directory or
7. (Optional) Use the OKE Workloads UI Instead of kubectl edit
If you prefer not to edit YAML via terminal:- In OCI Console → your OKE cluster → left menu Workloads.
- Choose the correct namespace.
- Click on a Deployment / StatefulSet / Pod.
- Use Edit YAML (or equivalent) and:
- For
ServiceAccountobjects, addautomountServiceAccountToken: false. - For Pod templates under workloads, add
spec.automountServiceAccountToken: trueonly where needed.
- For
- Save; OKE will apply the updated manifest.
Summary of the remediation:
- Set
automountServiceAccountToken: falseon all service accounts by default. - Only enable
automountServiceAccountToken: trueat the pod/workload level where a token is strictly required. - All of this can be done starting from the OCI Console using the OKE page + Cloud Shell or the Workloads UI editor.
Using CLI
Using CLI
In OKE this is remediated with standard Kubernetes controls; OCI CLI is only used to get your kubeconfig so you can run
Confirm access:
To verify:Create new ServiceAccounts with token mounting disabled by default:
For raw Pods you can patch similarly:When defining new workloads, include in the pod spec:
Patch any with
kubectl against the cluster.Below are the steps using OCI CLI + kubectl.1. Get kubeconfig for your OKE cluster (via OCI CLI)
2. Disable token auto-mounting at the ServiceAccount level
For each namespace, disable token auto-mounting on thedefault ServiceAccount (or any SA you use):3. Disable token auto-mounting at the Pod/Deployment level
For existing Deployments/Pods that must not have tokens, explicitly setautomountServiceAccountToken: false:4. Optionally audit current ServiceAccount token usage
List all ServiceAccounts and check forautomountServiceAccountToken:true or unset (Kubernetes default is true) as above.Using Python
Using Python
To restrict OKE service account token mounting you need to:
Run it:
If you share how your OKE clusters are provisioned (Terraform, OCI Resource Manager, manual), I can add Terraform or pipeline examples to enforce this setting at creation time as well.
- Turn off automatic token mounting at the ServiceAccount level (cluster-wide or per namespace).
- Explicitly turn it on only for workloads that truly need it.
- Do this programmatically using the Kubernetes Python client.
1. Prerequisites
- Ensure you can access your OKE cluster with
kubectl: - Install the Kubernetes Python client:
- Ensure your
KUBECONFIG(or default~/.kube/config) is set to the OKE cluster context you want to fix.
2. Hardening Strategy
Best practice:- Set
automountServiceAccountToken: falseon all ServiceAccounts by default, especially thedefaultServiceAccount in every namespace. - For Pods/Deployments that actually need the token (e.g., in‑cluster controllers, tools using Kubernetes API), explicitly set:
3. Python Script: Disable Token Mounting for ServiceAccounts
This script:- Connects to the OKE cluster using your kubeconfig.
- Iterates through all namespaces.
- For each ServiceAccount (including
default), setsautomountServiceAccountToken: falseunless you explicitly exempt it (via a skip list).
4. (Optional) Explicitly Enable Token for Specific Workloads
For workloads that require the token, you can either:-
Set at Pod spec level (preferred):
-
Or adjust
SA_SKIP_LISTin the Python script so that their ServiceAccount keepsautomountServiceAccountTokendefault or true, then upgrade/redeploy them.
5. Integrating with OCI / OKE Automation (Optional)
If you want to run this as part of CI/CD or automation:- Package the script into a container image.
- Run it as a Job or CronJob inside the OKE cluster with a ServiceAccount that has permissions to list/patch ServiceAccounts (
rbacClusterRolewithget,list,patchonserviceaccounts). - Or run from an OCI DevOps pipeline or OCI Shell with kubeconfig pointing to the OKE cluster.
If you share how your OKE clusters are provisioned (Terraform, OCI Resource Manager, manual), I can add Terraform or pipeline examples to enforce this setting at creation time as well.
Using Terraform
Using Terraform
This setting cannot be controlled on the
oci_containerengine_cluster (Terraform oci_containerengine_cluster) resource: the OCI Container Engine for Kubernetes API does not expose a cluster‑level flag to set automountServiceAccountToken=false by default.To remediate with Terraform you must manage this at the Kubernetes object level (not on the OKE cluster resource), for example by:- Defining
kubernetes_service_accountresources (via thehashicorp/kubernetesprovider) withautomount_service_account_token = falseand only enabling it where explicitly required, or - Applying Kubernetes manifests (e.g., via
kubectl, Helm, or Terraformkubernetes_manifest/helm_release) that setautomountServiceAccountToken: falseon ServiceAccounts/Pods, and/or admission policies that enforce this.
automountServiceAccountToken: false by default and enable it only for workloads that truly need the token.
