fffleet-worker 2.8.0 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,6 +12,8 @@ docker run -p 5100:5100 -e FFFLEET_TOKEN=secret ghcr.io/jsilvanus/fffleet-worker
12
12
  | `PORT`, `HOST` | `5100`, `0.0.0.0` | |
13
13
  | `FFFLEET_TOKEN` | `FFFLEET_WORKER_TOKEN` | Bearer token callers must send (full access, and what the orchestrator uses). With neither set the API is open. |
14
14
  | `FFFLEET_KEYS_URL` | the orchestrator's `/v1/auth/keys` when a token and an orchestrator are set | Also accept tokens the orchestrator issued, with their scopes. |
15
+ | `FFFLEET_OIDC_ISSUER`, `FFFLEET_OIDC_JWKS_URL`, `FFFLEET_OIDC_AUDIENCE` | | Accept tokens from an external OIDC issuer (the orchestrator's `fleet:dispatch` token, audience = this worker's resource identifier). Scope names: `FFFLEET_OIDC_SCOPES`, as on the orchestrator. |
16
+ | `FFFLEET_WORKER_CLIENT_ID`, `FFFLEET_WORKER_CLIENT_SECRET`, `FFFLEET_OIDC_TOKEN_URL`, `FFFLEET_OIDC_REGISTER_RESOURCE` | | Register and send heartbeats with a token from the issuer (scope `fleet:work`, `FFFLEET_OIDC_REGISTER_SCOPE` to change it; the resource is the orchestrator's audience) instead of `FFFLEET_WORKER_TOKEN`. A fresh token is fetched as needed. The worker id must equal the client id, or start with the orchestrator's worker id prefix. |
15
17
  | `FFFLEET_SLOTS` | `default=2` | Slot pools, e.g. `default=2,stream=1`, or `auto` (from the CPU count) / `auto:4` (4 cores per slot). |
16
18
  | `FFFLEET_KINDS` | `batch,stream` | Job kinds this worker takes. Others get 422 `UNSUPPORTED_KIND`, and the orchestrator does not send them. |
17
19
  | `FFFLEET_CAPABILITIES` | | Extra capabilities, comma separated, e.g. `mount:/media,site:hel1`. |
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
  import { isAbsolute, resolve } from 'node:path';
3
3
  import { pathToFileURL } from 'node:url';
4
- import { s3ConfigFromEnv } from 'fffleet';
4
+ import { createTokenProvider, s3ConfigFromEnv } from 'fffleet';
5
5
  import { createWorker } from '../src/worker.js';
6
6
 
7
7
  const env = process.env;
@@ -19,7 +19,24 @@ for (const ref of list(env.FFFLEET_EXECUTORS)) {
19
19
  }
20
20
  }
21
21
 
22
+ // Tokens from an external OIDC issuer. Incoming: the orchestrator's dispatch token (FFFLEET_OIDC_AUDIENCE is this worker's resource identifier).
23
+ const oidc = env.FFFLEET_OIDC_ISSUER && env.FFFLEET_OIDC_JWKS_URL && env.FFFLEET_OIDC_AUDIENCE
24
+ ? { issuer: env.FFFLEET_OIDC_ISSUER, jwksUrl: env.FFFLEET_OIDC_JWKS_URL, audience: env.FFFLEET_OIDC_AUDIENCE, scopes: env.FFFLEET_OIDC_SCOPES || null }
25
+ : null;
26
+ if (env.FFFLEET_OIDC_ISSUER && !oidc) throw new Error('FFFLEET_OIDC_ISSUER needs FFFLEET_OIDC_JWKS_URL and FFFLEET_OIDC_AUDIENCE');
27
+ // Outgoing: registering with the orchestrator with this worker's own issuer client (scope fleet:work).
28
+ const registerLogin = env.FFFLEET_WORKER_CLIENT_ID && env.FFFLEET_WORKER_CLIENT_SECRET && env.FFFLEET_OIDC_TOKEN_URL
29
+ ? createTokenProvider({
30
+ tokenUrl: env.FFFLEET_OIDC_TOKEN_URL,
31
+ resource: env.FFFLEET_OIDC_REGISTER_RESOURCE || undefined,
32
+ clientId: env.FFFLEET_WORKER_CLIENT_ID,
33
+ clientSecret: env.FFFLEET_WORKER_CLIENT_SECRET,
34
+ scope: env.FFFLEET_OIDC_REGISTER_SCOPE || 'fleet:work',
35
+ })
36
+ : null;
37
+
22
38
  const worker = createWorker({
39
+ oidc,
23
40
  executors,
24
41
  id: env.FFFLEET_WORKER_ID || undefined,
25
42
  port: Number(env.PORT ?? 5100),
@@ -37,7 +54,7 @@ const worker = createWorker({
37
54
  probe: list(env.FFFLEET_PROBE),
38
55
  probeIntervalMs: Number(env.FFFLEET_PROBE_INTERVAL_MS ?? 30000),
39
56
  orchestratorUrl: env.FFFLEET_ORCHESTRATOR_URL || null,
40
- orchestratorToken: env.FFFLEET_WORKER_TOKEN || null,
57
+ orchestratorToken: registerLogin ?? (env.FFFLEET_WORKER_TOKEN || null),
41
58
  advertiseUrl: env.FFFLEET_ADVERTISE_URL || null,
42
59
  heartbeatMs: Number(env.FFFLEET_HEARTBEAT_MS ?? 5000),
43
60
  s3: s3ConfigFromEnv(env),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fffleet-worker",
3
- "version": "2.8.0",
3
+ "version": "2.10.0",
4
4
  "description": "fffleet worker daemon: runs ffmpeg jobs and serves the fffleet job API",
5
5
  "type": "module",
6
6
  "main": "./src/worker.js",
@@ -22,7 +22,7 @@
22
22
  "test": "node --test test/*.test.js"
23
23
  },
24
24
  "dependencies": {
25
- "fffleet": "^2.8.0"
25
+ "fffleet": "^2.10.0"
26
26
  },
27
27
  "keywords": [
28
28
  "ffmpeg",
package/src/worker.js CHANGED
@@ -2,7 +2,7 @@ import { hostname } from 'node:os';
2
2
  import { readFileSync } from 'node:fs';
3
3
  import { JobManager, detectCapabilities, parseProbes, probeCapabilities } from 'fffleet';
4
4
  import {
5
- Registry, close, createApiHandler, createAuthenticator, createRemoteKeySet, createTokenVerifier,
5
+ Registry, close, createApiHandler, createAuthenticator, createExternalIssuer, createRemoteKeySet, createTokenVerifier,
6
6
  hostMetrics, jobMetrics, listen, procStats, processMetrics,
7
7
  } from 'fffleet/server';
8
8
 
@@ -33,6 +33,8 @@ const VERSION = JSON.parse(readFileSync(new URL('../package.json', import.meta.u
33
33
  * @param {number} [opts.progressIntervalMs]
34
34
  * @param {Record<string, Function>} [opts.executors] Extra job types: type -> (spec, runtime) => Promise<result>.
35
35
  * @param {object | null} [opts.s3] S3 settings (see s3ConfigFromEnv); adds the scheme:s3 capability.
36
+ * @param {object | null} [opts.oidc] Tokens from an external OIDC issuer: { issuer, jwksUrl, audience, scopes?, fetch? }. `audience` is this worker's resource identifier; the orchestrator's dispatch token must carry the `dispatch` scope for it.
37
+ * @param {string | { get(): Promise<string> } | null} [opts.orchestratorToken] Worker token, or a token provider (createTokenProvider) that is asked for a fresh token on every registration and heartbeat.
36
38
  * @param {(msg: string) => void} [opts.log]
37
39
  */
38
40
  export function createWorker({
@@ -41,6 +43,7 @@ export function createWorker({
41
43
  host = '0.0.0.0',
42
44
  token = null,
43
45
  keysUrl = null,
46
+ oidc = null,
44
47
  slots = { default: 2 },
45
48
  kinds = undefined,
46
49
  ffmpegPath = 'ffmpeg',
@@ -80,7 +83,8 @@ export function createWorker({
80
83
  const authenticate = createAuthenticator({
81
84
  token,
82
85
  verify: keysUrl ? createTokenVerifier({ getKey: createRemoteKeySet(keysUrl) }) : null,
83
- open: !token && !keysUrl,
86
+ external: oidc ? createExternalIssuer({ issuer: oidc.issuer, jwksUrl: oidc.jwksUrl, audience: oidc.audience, scopes: oidc.scopes, fetch: oidc.fetch }) : null,
87
+ open: !token && !keysUrl && !oidc,
84
88
  });
85
89
 
86
90
  const registry = new Registry();
@@ -133,9 +137,10 @@ export function createWorker({
133
137
  };
134
138
 
135
139
  async function register() {
140
+ const bearer = typeof orchestratorToken === 'object' && orchestratorToken ? await orchestratorToken.get() : orchestratorToken;
136
141
  const res = await fetch(`${orchestratorUrl.replace(/\/+$/, '')}/v1/workers/register`, {
137
142
  method: 'POST',
138
- headers: { 'content-type': 'application/json', ...(orchestratorToken ? { authorization: `Bearer ${orchestratorToken}` } : {}) },
143
+ headers: { 'content-type': 'application/json', ...(bearer ? { authorization: `Bearer ${bearer}` } : {}) },
139
144
  body: JSON.stringify({ id, url: advertiseUrl ?? url, slots: manager.slots, kinds: manager.kinds, capabilities, version: VERSION, active: manager.activeIds() }),
140
145
  signal: AbortSignal.timeout(5000),
141
146
  });
@@ -163,8 +168,9 @@ export function createWorker({
163
168
  },
164
169
 
165
170
  async start() {
166
- // type:ffmpeg and type:ffprobe come from detection, so a machine without ffmpeg does not claim it.
167
- const types = Object.keys(manager.executors).filter(t => t !== 'ffmpeg' && t !== 'ffprobe').map(t => `type:${t}`);
171
+ // type:ffmpeg, type:ffprobe and type:flash-analysis come from detection, so a machine without ffmpeg does not claim them.
172
+ const builtIn = new Set(['ffmpeg', 'ffprobe', 'flash-analysis']);
173
+ const types = Object.keys(manager.executors).filter(t => !builtIn.has(t)).map(t => `type:${t}`);
168
174
  const schemes = manager.s3 ? ['scheme:s3'] : [];
169
175
  baseCapabilities = [...new Set([...(await detectCapabilities(ffmpegPath)), ...types, ...schemes, ...extraCapabilities])];
170
176
  await refreshProbes();
@@ -174,7 +180,7 @@ export function createWorker({
174
180
  probeTimer.unref?.();
175
181
  }
176
182
  ({ server, url } = await listen(createApiHandler({ backend, authenticate, metrics: () => registry.render() }), { port, host }));
177
- if (!token && !keysUrl && !['127.0.0.1', '::1', 'localhost'].includes(host)) {
183
+ if (!token && !keysUrl && !oidc && !['127.0.0.1', '::1', 'localhost'].includes(host)) {
178
184
  log('no FFFLEET_TOKEN or FFFLEET_WORKER_TOKEN: anyone who can reach this port can run ffmpeg as this user (docs/security.md)');
179
185
  }
180
186
  log(`fffleet-worker ${id} listening on ${url} with slots ${JSON.stringify(manager.slots)} for ${manager.kinds.join(' and ')} jobs`);