Skip to content

Pod Security

Default security context

When neither the WorkspaceProfile nor the Team’s SecurityProfile specifies a security context, the operator applies hardened defaults (DefaultPodSecurityContext and DefaultContainerSecurityContext in operator/internal/controllers/v1alpha1/workspace/helpers/helpers.go).

These defaults are the normal way to run workspaces. Images built FROM the official codercom/code-server image, including the kornercode-images flavors, start directly as the non-root coder user (uid and gid 1000) and need no profile security settings at all. See Image requirements.

Pod level

corev1.PodSecurityContext{
    RunAsNonRoot:   &true,
    RunAsUser:      &1000,
    RunAsGroup:     &1000,
    FSGroup:        &1000,
    SeccompProfile: &corev1.SeccompProfile{Type: corev1.SeccompProfileTypeRuntimeDefault},
}
  • runAsNonRoot: true: the kubelet refuses to start a container whose effective user is UID 0.
  • runAsUser, runAsGroup, fsGroup set to 1000: processes run as the coder user of codercom/code-server images, and mounted volumes are group-owned by GID 1000.
  • fsGroupChangePolicy: OnRootMismatch: added by the operator, not by DefaultPodSecurityContext itself (see fsGroup change policy).
  • seccompProfile: RuntimeDefault: the container runtime’s default seccomp filter, which blocks system calls that applications rarely need and that are commonly used in container escapes.

Container level

Applied to the code-server container:

corev1.SecurityContext{
    AllowPrivilegeEscalation: &false,
    Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
}
  • allowPrivilegeEscalation: false: setuid and setgid binaries cannot raise the privileges of the process (no_new_privs).
  • capabilities.drop: [ALL]: the container has no Linux capabilities, so it cannot, for example, reconfigure network interfaces, use raw sockets, or change file ownership.

readOnlyRootFilesystem is intentionally not set, because code-server and developer tooling write outside the mounted volumes.


SecurityProfile

SecurityProfile is a cluster-scoped resource that bundles security context, ServiceAccount and NetworkPolicy settings. A Team references it with spec.securityProfileRef; every Workspace of that Team uses it.

apiVersion: code.kornercloud.dev/v1alpha1
kind: SecurityProfile
metadata:
  name: developer
spec:
  podSecurityContext:           # replaces the WorkspaceProfile value and the defaults
    runAsNonRoot: true
    runAsUser: 1000

  containerSecurityContext:     # replaces the WorkspaceProfile value and the defaults
    allowPrivilegeEscalation: false
    capabilities:
      drop: [ALL]

  disableDefaultSecurityContext: false
  # When true, no pod or container security context is set at all, and the
  # two fields above are ignored. An escape hatch for images that must start
  # as root; images built from codercom/code-server do not need it.

  # serviceAccount:
  #   name: platform-tools      # optional; naming a ServiceAccount also mounts its token

  networkPolicy:
    disabled: false             # when true, no NetworkPolicy activation labels are applied
    policyRefs:                 # policy IDs to activate
      - internet-only

All fields are optional. A Team without a SecurityProfile uses the WorkspaceProfile’s settings and the operator defaults, the workspace-runner ServiceAccount without a token, and no NetworkPolicy (see Network isolation).


Priority chain

Each security context is resolved independently, highest priority first:

SecurityProfile  >  WorkspaceProfile  >  operator defaults
// operator/internal/controllers/v1alpha1/workspace/helpers/helpers.go
func EffectivePodSecurityContext(profile *WorkspaceProfile, secProfile *SecurityProfile) *corev1.PodSecurityContext {
    if secProfile != nil && secProfile.Spec.DisableDefaultSecurityContext {
        return nil // SecurityProfile: no context at all
    }
    if secProfile != nil && secProfile.Spec.PodSecurityContext != nil {
        return withFSGroupChangePolicy(secProfile.Spec.PodSecurityContext)
    }
    if profile.Spec.DisableDefaultSecurityContext {
        return nil // WorkspaceProfile: no context at all
    }
    if profile.Spec.PodSecurityContext != nil {
        return withFSGroupChangePolicy(profile.Spec.PodSecurityContext)
    }
    return withFSGroupChangePolicy(DefaultPodSecurityContext())
}

EffectiveContainerSecurityContext follows the same order for the container context.

An explicit context replaces the lower-priority value; fields are not merged. A SecurityProfile that sets only runAsUser, for example, yields a pod context without runAsNonRoot or a seccomp profile.

disableDefaultSecurityContext: true overrides all security context fields on the resource that sets it, including explicit podSecurityContext and containerSecurityContext values. Because the explicit fields would be ignored, the admission webhooks and the gateway’s admin API reject a profile that sets both. It remains available as an escape hatch for images that cannot run as a non-root user, but none of the supported images needs it: the local development profiles in hack/local-dev/workspace-profiles.yaml keep the defaults.

fsGroup change policy

When the effective pod security context sets fsGroup and leaves fsGroupChangePolicy unset, the operator sets fsGroupChangePolicy: OnRootMismatch. This applies to the defaults and to contexts supplied by a SecurityProfile or WorkspaceProfile. The kubelet then changes the ownership and permissions of a volume only when its root directory does not already match fsGroup, instead of walking the whole volume on every pod start, which would make a large restored home slow to mount. An explicit fsGroupChangePolicy (for example Always) is never overridden. The policy is a performance setting, not a relaxation: the volume root still has to match fsGroup, and the kubelet fixes it when it does not.


Image requirements

The operator runs the code-server container with the upstream image ENTRYPOINT and appends its own arguments (--bind-addr, --auth none; see Mutual TLS). An image works under the default security context when:

  • it is built FROM codercom/code-server:<version> (or equivalent) and keeps the upstream ENTRYPOINT, WORKDIR and HOME of /home/coder;
  • it ends with a non-root USER whose uid and gid match the effective pod security context (1000/1000 with the defaults, which is the upstream coder user);
  • everything that belongs to the user lives under /home/coder, the single volume the operator mounts (see Home volume). Tooling installed at build time must go to system paths, because the volume hides anything the image puts under /home/coder.

The operator injects no uid, gid or HOME environment variables; WorkspaceProfile.spec.extraEnv is the only source of extra environment variables.

uid must match the image user

runAsUser and runAsGroup must match the uid and gid of the image’s built-in user. The upstream entrypoint uses fixuid to remap that user when the runtime uid differs, but fixuid needs privileges that the default container context removes (no privilege escalation, all capabilities dropped), so the remap cannot happen. With a SecurityProfile or WorkspaceProfile that sets a different uid, code-server runs as a uid that is not the image user, has no passwd entry and does not own the files the image prepared. To run as another uid, build the image with a matching user instead.


Applying changes to running workspaces

ReconcilePod stamps each workspace pod with a hash of the pod spec it was built from (code.kornercloud.dev/pod-spec-hash). When a WorkspaceProfile, Team or SecurityProfile change produces a different spec, the operator sets status.restartRequired: true on the Workspace but does not recreate the running pod. Security context, ServiceAccount and token changes therefore take effect only when the workspace is restarted, either from the dashboard (Restart) or by deleting the pod. The same applies when an administrator gives a running workspace’s owner a restored home volume: the new home is mounted at the next restart.

NetworkPolicy activation labels are not part of the pod spec. They are updated on the running pod without a restart.


ServiceAccount isolation

ReconcileServiceAccount (operator/internal/controllers/v1alpha1/workspace/apply/reconcileserviceaccount.go) ensures that the ServiceAccount used by the workspace exists in the workspace’s namespace:

SecurityProfileServiceAccountToken mounted
None, or no serviceAccount.nameworkspace-runnerNo (automountServiceAccountToken: false on the ServiceAccount and the pod)
serviceAccount.name: <name><name>Yes

Naming a ServiceAccount and mounting its token are deliberately one decision (EffectiveAutomountToken): a named ServiceAccount without a token would have no effect, because the operator does not project tokens through volumes.

workspace-runner

  • Created by the operator in each namespace that hosts workspaces.
  • Has no role bindings in team namespaces, and its token is not mounted, so workspace code has no Kubernetes API credentials.
  • When operator.workspaceRBAC.enabled is true (the default), the chart also creates a workspace-runner Role and RoleBinding in the release namespace only, granting read access to Pods, pod logs, Services, PVCs, Events, Workspaces, WorkspaceProfiles and Teams there. It matters only for Teams placed in the operator namespace whose SecurityProfile names workspace-runner explicitly.

Privileged ServiceAccounts

A SecurityProfile can name any ServiceAccount, for example workspace-admin for platform engineers who need kubectl inside their workspace. The operator creates the ServiceAccount in the team namespace if needed, but never binds it to a role. The permissions are whatever the cluster administrator binds to that ServiceAccount in that namespace, and the token is available to every workspace of every Team that uses the SecurityProfile.

Never grant a workspace ServiceAccount write access to PersistentVolumeClaims in a team namespace. Users reach that ServiceAccount through kubectl in their IDE, and a label on a PVC decides whose home it is (see Migrating existing home directories): a user who can create or label PVCs can claim a volume as another user’s home. The same holds for anything else a user controls. The operator does not enforce this rule. The local development setup breaks it: the workspace-admin chart described below binds cluster-admin in the operator namespace, which is also the local team namespace, so super-admin workspaces can write PVCs there. That setup is for local development only and must not be copied into production.

The chart does not grant cluster-wide permissions to any workspace ServiceAccount. The local development environment installs hack/local-dev/workspace-admin, an opt-in chart that binds the workspace-admin ServiceAccount in the operator namespace to cluster-admin. A compromise of a workspace using such a ServiceAccount is a compromise of the cluster. Bind the narrowest role that serves the purpose, and do not install that chart in production.

Shared and retained

The ServiceAccount is shared by all workspaces in the namespace and has no owner reference to any Workspace. The Workspace deletion steps do not delete it, so removing one workspace never breaks the others.


Envoy sidecar security context

When mTLS is enabled, the mtls-proxy sidecar (see Mutual TLS) has a fixed container security context that does not depend on the profiles:

corev1.SecurityContext{
    AllowPrivilegeEscalation: &false,
    RunAsNonRoot:             &true,
    RunAsUser:                &101, // envoy user of the official image
    Capabilities: &corev1.Capabilities{Drop: []corev1.Capability{"ALL"}},
}

The sidecar never runs as root, cannot escalate privileges, and has no capabilities. The pod-level context, including the seccomp profile, also applies to it when it is set. Its resources are bounded (requests 20m CPU and 32Mi memory, limits 200m CPU and 128Mi memory) so it cannot starve code-server.


Team mounts

Team.spec.secretMounts, configMapMounts and pvcMounts mount Secrets, ConfigMaps and PVCs from the team namespace into every workspace of the Team. Their contents are readable by any code running in those workspaces. The objects must exist in the team namespace before the workspace pod starts; the admission webhooks do not check them.


GPU support

WorkspaceProfile.spec.gpu allocates GPU devices to the code-server container:

spec:
  gpu:
    vendor: nvidia.com/gpu   # default when omitted
    count: 1

The operator sets the resource in both limits and requests, as Kubernetes requires for extended resources. GPU allocation does not change the security context.


WorkspaceProfile security fields

FieldTypeEffect
spec.podSecurityContextPodSecurityContextReplaces the operator default at pod level, unless the SecurityProfile sets one
spec.containerSecurityContextSecurityContextReplaces the operator default for the code-server container, unless the SecurityProfile sets one
spec.disableDefaultSecurityContextboolWhen true and the SecurityProfile sets no context, no security context is applied. Not needed for images built from codercom/code-server
spec.disableNetworkPolicyboolWhen true, no NetworkPolicy activation labels are applied to the pod
spec.extraVolumes, spec.extraVolumeMountsVolume listsAdded to the pod as given; any volume type the cluster allows can be mounted