Skip to content

Commit a3b30a7

Browse files
committed
chore(rivetkit): auto-generate inspector token for engine driver
1 parent 2368e4d commit a3b30a7

File tree

8 files changed

+69
-7
lines changed

8 files changed

+69
-7
lines changed

rivetkit-typescript/packages/cloudflare-workers/src/actor-driver.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import type {
66
} from "rivetkit";
77
import { lookupInRegistry } from "rivetkit";
88
import type { Client } from "rivetkit/client";
9-
import type {
10-
ActorDriver,
11-
AnyActorInstance,
12-
ManagerDriver,
9+
import {
10+
type ActorDriver,
11+
type AnyActorInstance,
12+
generateRandomString,
13+
type ManagerDriver,
1314
} from "rivetkit/driver-helpers";
1415
import { promiseWithResolvers } from "rivetkit/utils";
1516
import { KEYS } from "./actor-handler-do";
@@ -154,6 +155,11 @@ export class CloudflareActorsActorDriver implements ActorDriver {
154155
async getDatabase(actorId: string): Promise<unknown | undefined> {
155156
return this.#getDOCtx(actorId).storage.sql;
156157
}
158+
159+
getInspectorToken(_actorId: string): string | undefined {
160+
// TODO: Inspector currently disabled. Need to implement a way to store the token in CF KV simplar to the Engine driver.
161+
return generateRandomString();
162+
}
157163
}
158164

159165
export function createCloudflareActorsActorDriverBuilder(

rivetkit-typescript/packages/rivetkit/src/actor/driver.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,11 @@ export interface ActorDriver {
5353

5454
/** Extra properties to add to logs for each actor. */
5555
getExtraActorLogParams?(): Record<string, string>;
56+
57+
/**
58+
* Get the inspector token for an actor.
59+
*
60+
* If returns undefined, auth is disabled.
61+
**/
62+
getInspectorToken(actorId: string): string | undefined;
5663
}

rivetkit-typescript/packages/rivetkit/src/drivers/engine/actor-driver.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
handleWebSocketConnect,
2020
truncateRawWebSocketPathPrefix,
2121
} from "@/actor/router-endpoints";
22+
import { generateSecureToken } from "@/actor/utils";
2223
import type { Client } from "@/client/client";
2324
import {
2425
PATH_CONNECT_WEBSOCKET,
@@ -60,6 +61,7 @@ interface ActorHandler {
6061
actor?: AnyActorInstance;
6162
actorStartPromise?: ReturnType<typeof promiseWithResolvers<void>>;
6263
persistedData?: Uint8Array;
64+
inspectorToken?: string;
6365
}
6466

6567
export type DriverContext = {};
@@ -438,9 +440,11 @@ export class EngineActorDriver implements ActorDriver {
438440
this.#actors.set(actorId, handler);
439441

440442
// Load persisted data from storage
441-
const [persistedValue] = await this.#runner.kvGet(actorId, [
442-
KEYS.PERSIST_DATA,
443-
]);
443+
const [persistedValue, inspectorTokenValue] =
444+
await this.#runner.kvGet(actorId, [
445+
KEYS.PERSIST_DATA,
446+
KEYS.INSPECTOR.TOKEN,
447+
]);
444448

445449
handler.persistedData =
446450
persistedValue !== null
@@ -453,6 +457,30 @@ export class EngineActorDriver implements ActorDriver {
453457
dataSize: handler.persistedData?.byteLength,
454458
wasInStorage: persistedValue !== null,
455459
});
460+
461+
// Get or create inspector token
462+
if (inspectorTokenValue !== null) {
463+
// Token exists, decode it
464+
const decoder = new TextDecoder();
465+
handler.inspectorToken = decoder.decode(inspectorTokenValue);
466+
logger().debug({
467+
msg: "loaded existing inspector token",
468+
actorId,
469+
});
470+
} else {
471+
// Create new token
472+
handler.inspectorToken = generateSecureToken();
473+
const tokenBytes = new TextEncoder().encode(
474+
handler.inspectorToken,
475+
);
476+
await this.#runner.kvPut(actorId, [
477+
[KEYS.INSPECTOR.TOKEN, tokenBytes],
478+
]);
479+
logger().debug({
480+
msg: "created new inspector token",
481+
actorId,
482+
});
483+
}
456484
}
457485

458486
const name = actorConfig.name as string;
@@ -909,4 +937,14 @@ export class EngineActorDriver implements ActorDriver {
909937
getExtraActorLogParams(): Record<string, string> {
910938
return { runnerId: this.#runner.runnerId ?? "-" };
911939
}
940+
941+
getInspectorToken(actorId: string): string | undefined {
942+
const handler = this.#actors.get(actorId);
943+
invariant(handler, "handler does not exist in getInspectorToken");
944+
invariant(
945+
handler.inspectorToken,
946+
"handler.inspectorToken does not exist in getInspectorToken",
947+
);
948+
return handler.inspectorToken;
949+
}
912950
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
export const KEYS = {
22
PERSIST_DATA: Uint8Array.from([1]),
3+
INSPECTOR: {
4+
TOKEN: Uint8Array.from([2]),
5+
},
36
};

rivetkit-typescript/packages/rivetkit/src/drivers/file-system/actor.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,8 @@ export class FileSystemActorDriver implements ActorDriver {
8383
// Spawns the sleepActor promise
8484
this.#state.sleepActor(actorId);
8585
}
86+
87+
getInspectorToken(_actorId: string): string | undefined {
88+
return undefined;
89+
}
8690
}

rivetkit-typescript/packages/rivetkit/src/inspector/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export function getInspectorUrl(runConfig: RunnerConfigInput | undefined) {
7777

7878
export const isInspectorEnabled = (
7979
runConfig: RunConfig,
80+
// TODO(kacper): Remove context in favor of using the gateway, so only context is the actor
8081
context: "actor" | "manager",
8182
) => {
8283
if (typeof runConfig.inspector?.enabled === "boolean") {

rivetkit-typescript/packages/rivetkit/src/manager/driver.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ export interface ManagerDriver {
4848
router: Hono,
4949
) => void;
5050

51+
// TODO(kacper): Remove this in favor of standard manager API
5152
/**
5253
* @internal
5354
*/
5455
readonly inspector?: ManagerInspector;
5556

57+
// TODO(kacper): Remove this in favor of ActorDriver.getinspectorToken
5658
/**
5759
* Get or create the inspector access token.
5860
* @experimental

rivetkit-typescript/packages/rivetkit/src/manager/router.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ function addManagerRoutes(
222222
managerDriver: ManagerDriver,
223223
router: OpenAPIHono,
224224
) {
225+
// TODO(kacper): Remove this in favor of standard manager API
225226
// Inspector
226227
if (isInspectorEnabled(runConfig, "manager")) {
227228
if (!managerDriver.inspector) {

0 commit comments

Comments
 (0)