Skip to content

Commit 82b1deb

Browse files
committed
feat(supervisor): select the pinned uid by runtime
1 parent 5e07593 commit 82b1deb

3 files changed

Lines changed: 27 additions & 10 deletions

File tree

apps/supervisor/src/workloadManager/kubernetes.test.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,22 +157,32 @@ describe("withRunnerSeccompProfile", () => {
157157

158158
describe("runnerSecurityContext", () => {
159159
it("sets nothing when off", () => {
160-
expect(runnerSecurityContext("off", 1000)).toBeUndefined();
160+
expect(runnerSecurityContext("off", 1000, "node-24")).toBeUndefined();
161161
});
162162

163163
it("drops all capabilities and blocks escalation at baseline", () => {
164-
expect(runnerSecurityContext("baseline", 1000)).toEqual({
164+
expect(runnerSecurityContext("baseline", 1000, "node-24")).toEqual({
165165
allowPrivilegeEscalation: false,
166166
capabilities: { drop: ["ALL"] },
167167
});
168168
});
169169

170-
it("additionally requires a non-root image when restricted", () => {
171-
expect(runnerSecurityContext("restricted", 1000)).toEqual({
170+
it("pins the configured uid when restricted", () => {
171+
expect(runnerSecurityContext("restricted", 1000, "node-24")).toEqual({
172172
allowPrivilegeEscalation: false,
173173
capabilities: { drop: ["ALL"] },
174174
runAsNonRoot: true,
175175
runAsUser: 1000,
176176
});
177177
});
178+
179+
it("pins bun's own uid, which differs from node's", () => {
180+
expect(runnerSecurityContext("restricted", 1000, "bun")?.runAsUser).toBe(1001);
181+
});
182+
183+
it("falls back to the configured uid when the runtime is unknown", () => {
184+
for (const runtime of [undefined, null, "", "node", "node-22", "node-26"]) {
185+
expect(runnerSecurityContext("restricted", 1000, runtime)?.runAsUser).toBe(1000);
186+
}
187+
});
178188
});

apps/supervisor/src/workloadManager/kubernetes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ export class KubernetesWorkloadManager implements WorkloadManager {
178178
resources: this.#getResourcesForMachine(opts.machine),
179179
securityContext: runnerSecurityContext(
180180
env.KUBERNETES_RUNNER_SECURITY_CONTEXT,
181-
env.KUBERNETES_RUNNER_RUN_AS_USER
181+
env.KUBERNETES_RUNNER_RUN_AS_USER,
182+
opts.runtime
182183
),
183184
env: [
184185
{

apps/supervisor/src/workloadManager/kubernetesPodSpec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,22 @@ export function withRunnerSeccompProfile(
9797
};
9898
}
9999

100+
const BUN_RUN_AS_USER = 1001;
101+
100102
/**
101103
* runnerSecurityContext maps a configured level onto the run container's security
102104
* context. "baseline" drops the capability bounding set and blocks setuid
103105
* escalation; "restricted" additionally pins the container to a non-root uid.
104106
*
105-
* The uid is set explicitly rather than relying on the image: the kubelet cannot
106-
* verify `runAsNonRoot` when an image declares a named user, and fails the
107-
* container instead.
107+
* The uid is set explicitly rather than read from the image: the kubelet cannot
108+
* verify `runAsNonRoot` against an image that declares a named user, and fails
109+
* the container instead. Bun images carry their user at a different uid to
110+
* node's, so the runtime selects which uid is pinned.
108111
*/
109112
export function runnerSecurityContext(
110113
level: "off" | "baseline" | "restricted",
111-
runAsUser: number
114+
runAsUser: number,
115+
runtime: string | null | undefined
112116
): k8s.V1SecurityContext | undefined {
113117
if (level === "off") {
114118
return undefined;
@@ -117,6 +121,8 @@ export function runnerSecurityContext(
117121
return {
118122
allowPrivilegeEscalation: false,
119123
capabilities: { drop: ["ALL"] },
120-
...(level === "restricted" ? { runAsNonRoot: true, runAsUser } : {}),
124+
...(level === "restricted"
125+
? { runAsNonRoot: true, runAsUser: runtime === "bun" ? BUN_RUN_AS_USER : runAsUser }
126+
: {}),
121127
};
122128
}

0 commit comments

Comments
 (0)