Skip to content

Commit fee7c3a

Browse files
committed
fix: parse Tinybird workspace identity
1 parent 552b9e0 commit fee7c3a

2 files changed

Lines changed: 44 additions & 13 deletions

File tree

scripts/analytics/tests/tooling.test.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,17 +136,33 @@ test("cloud deploy verifies the token workspace before mutation", () => {
136136
TINYBIRD_WORKSPACE_ID: workspaceId,
137137
};
138138
assert.equal(
139-
verifyCloudWorkspace(env, () => `| production | ${workspaceId} | admin |`),
139+
verifyCloudWorkspace(env, (_command, args) => {
140+
assert.deepEqual(args.slice(-5), [
141+
"--cloud",
142+
"--output",
143+
"json",
144+
"workspace",
145+
"current",
146+
]);
147+
return JSON.stringify({ id: workspaceId, name: "production" });
148+
}),
140149
workspaceId,
141150
);
142151
assert.throws(
143152
() =>
144-
verifyCloudWorkspace(
145-
env,
146-
() => "| staging | 87654321-4321-4321-8321-cba987654321 | admin |",
153+
verifyCloudWorkspace(env, () =>
154+
JSON.stringify({
155+
id: "87654321-4321-4321-8321-cba987654321",
156+
metadata: workspaceId,
157+
name: "staging",
158+
}),
147159
),
148160
/does not target/,
149161
);
162+
assert.throws(
163+
() => verifyCloudWorkspace(env, () => "warning: invalid token"),
164+
/Unable to parse/,
165+
);
150166
});
151167

152168
test("local credentials are written to a private gitignored env file", () => {

scripts/analytics/tooling.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ const TEST_FILES = fs
2525
const CLOUD_URL_DEFAULT = "https://api.tinybird.co";
2626
const WORKSPACE_ID_PATTERN =
2727
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
28-
const WORKSPACE_ID_SEARCH_PATTERN =
29-
/[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/gi;
28+
const LOCAL_TOKEN_SIGNING_KEY = "tinybird-local";
3029
const LOCAL_IDENTIFIERS = {
3130
workspaceId: "00000000-0000-4000-8000-000000000001",
3231
workspaceTokenId: "00000000-0000-4000-8000-000000000002",
@@ -373,7 +372,7 @@ const cloudEnvironment = (env = process.env) => {
373372
const encodeLocalToken = (userId, tokenId) => {
374373
const payload = `{"u": "${userId}", "id": "${tokenId}", "host": null}`;
375374
const encodedPayload = Buffer.from(payload).toString("base64url");
376-
const signature = createHmac("sha256", "abcd")
375+
const signature = createHmac("sha256", LOCAL_TOKEN_SIGNING_KEY)
377376
.update(encodedPayload)
378377
.digest("base64url");
379378
return `p.${encodedPayload}.${signature}`;
@@ -433,18 +432,34 @@ const runProcessCapture = (command, args, options = {}) => {
433432
if (result.error || result.status !== 0) {
434433
throw new Error("Unable to verify Tinybird workspace identity.");
435434
}
436-
return `${result.stdout ?? ""}\n${result.stderr ?? ""}`;
435+
return result.stdout ?? "";
437436
};
438437

439438
const verifyCloudWorkspace = (env = process.env, run = runProcessCapture) => {
440439
const environment = cloudEnvironment(env);
441-
const step = cloudCliStep("--cloud", "workspace", "current");
440+
const step = cloudCliStep(
441+
"--cloud",
442+
"--output",
443+
"json",
444+
"workspace",
445+
"current",
446+
);
442447
assertSafeStep(step);
443448
const output = run(step.command, step.args, { env: environment });
444-
const workspaceIds = (output.match(WORKSPACE_ID_SEARCH_PATTERN) ?? []).map(
445-
(workspaceId) => workspaceId.toLowerCase(),
446-
);
447-
if (!workspaceIds.includes(environment.TINYBIRD_WORKSPACE_ID.toLowerCase())) {
449+
let workspace;
450+
try {
451+
workspace = JSON.parse(output);
452+
} catch {
453+
throw new Error("Unable to parse Tinybird workspace identity.");
454+
}
455+
if (
456+
!workspace ||
457+
typeof workspace !== "object" ||
458+
Array.isArray(workspace) ||
459+
typeof workspace.id !== "string" ||
460+
workspace.id.toLowerCase() !==
461+
environment.TINYBIRD_WORKSPACE_ID.toLowerCase()
462+
) {
448463
throw new Error(
449464
"Tinybird deploy token does not target TINYBIRD_WORKSPACE_ID.",
450465
);

0 commit comments

Comments
 (0)