Skip to content

Commit 2a42942

Browse files
committed
The CLI now checks for a dev server API key in init and dev commands
1 parent 7b29a92 commit 2a42942

File tree

4 files changed

+120
-10
lines changed

4 files changed

+120
-10
lines changed

packages/cli/src/cli/index.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { telemetryClient } from "../telemetry/telemetry";
1010
import { getVersion } from "../utils/getVersion";
1111
import { updateCommand } from "../commands/update";
1212
import { sendEventCommand } from "../commands/sendEvent";
13+
import { checkApiKeyIsDevServer } from "../utils/getApiKeyType";
1314

1415
export const program = new Command();
1516

@@ -195,21 +196,17 @@ export const promptApiKey = async (instanceUrl: string): Promise<string> => {
195196
return "Please enter your secret dev API key";
196197
}
197198

198-
// If they enter a public key like pk_dev_, let them know
199-
if (input.startsWith("pk_dev_")) {
200-
return "Please enter your secret dev API key, you've entered a public key";
201-
}
199+
const result = checkApiKeyIsDevServer(input);
202200

203-
// If they enter a prod key (tr_prod_), let them know
204-
if (input.startsWith("tr_prod_")) {
205-
return "Please enter your secret dev API key, you've entered a production key";
201+
if (result.success) {
202+
return true;
206203
}
207204

208-
if (!input.startsWith("tr_dev_")) {
209-
return "Please enter a valid development API key or leave blank to skip (should start with tr_dev_)";
205+
if (!result.type) {
206+
return "Please enter a valid development API key (should start with tr_dev_)";
210207
}
211208

212-
return true;
209+
return `Please enter your secret dev API key, you've entered a ${result.type.environment} ${result.type.type} key`;
213210
},
214211
});
215212

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { checkApiKeyIsDevServer } from "./getApiKeyType";
2+
3+
describe("Test API keys", () => {
4+
test("dev server succeeds", async () => {
5+
const result = checkApiKeyIsDevServer("tr_dev_12345");
6+
expect(result.success).toEqual(true);
7+
});
8+
9+
test("dev public fails", async () => {
10+
const result = checkApiKeyIsDevServer("pk_dev_12345");
11+
expect(result.success).toEqual(false);
12+
if (result.success) return;
13+
expect(result.type?.environment).toEqual("dev");
14+
expect(result.type?.type).toEqual("public");
15+
});
16+
17+
test("prod server fails", async () => {
18+
const result = checkApiKeyIsDevServer("tr_prod_12345");
19+
expect(result.success).toEqual(false);
20+
if (result.success) return;
21+
expect(result.type?.environment).toEqual("prod");
22+
expect(result.type?.type).toEqual("server");
23+
});
24+
25+
test("prod public fails", async () => {
26+
const result = checkApiKeyIsDevServer("pk_prod_12345");
27+
expect(result.success).toEqual(false);
28+
if (result.success) return;
29+
expect(result.type?.environment).toEqual("prod");
30+
expect(result.type?.type).toEqual("public");
31+
});
32+
});
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
export type ApiKeyType = {
2+
environment: "dev" | "prod";
3+
type: "server" | "public";
4+
};
5+
6+
type Result =
7+
| {
8+
success: true;
9+
}
10+
| {
11+
success: false;
12+
type: ApiKeyType | undefined;
13+
};
14+
15+
export function checkApiKeyIsDevServer(apiKey: string): Result {
16+
const type = getApiKeyType(apiKey);
17+
18+
if (!type) {
19+
return { success: false, type: undefined };
20+
}
21+
22+
if (type.environment === "dev" && type.type === "server") {
23+
return {
24+
success: true,
25+
};
26+
}
27+
28+
return {
29+
success: false,
30+
type,
31+
};
32+
}
33+
34+
export function getApiKeyType(apiKey: string): ApiKeyType | undefined {
35+
if (apiKey.startsWith("tr_dev_")) {
36+
return {
37+
environment: "dev",
38+
type: "server",
39+
};
40+
}
41+
42+
if (apiKey.startsWith("pk_dev_")) {
43+
return {
44+
environment: "dev",
45+
type: "public",
46+
};
47+
}
48+
49+
// If they enter a prod key (tr_prod_), let them know
50+
if (apiKey.startsWith("tr_prod_")) {
51+
return {
52+
environment: "prod",
53+
type: "server",
54+
};
55+
}
56+
57+
if (apiKey.startsWith("pk_prod_")) {
58+
return {
59+
environment: "prod",
60+
type: "public",
61+
};
62+
}
63+
64+
return;
65+
}

packages/cli/src/utils/getTriggerApiDetails.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { pathExists, readFile } from "./fileSystem";
33
import { logger } from "./logger";
44
import dotenv from "dotenv";
55
import { CLOUD_API_URL } from "../consts";
6+
import { checkApiKeyIsDevServer } from "./getApiKeyType";
67

78
export async function readEnvFilesWithBackups(
89
path: string,
@@ -59,5 +60,20 @@ export async function getTriggerApiDetails(path: string, envFile: string) {
5960
return;
6061
}
6162

63+
const result = checkApiKeyIsDevServer(apiKey);
64+
65+
if (!result.success) {
66+
if (result.type) {
67+
logger.error(
68+
`Your TRIGGER_API_KEY isn't a secret dev API key, you've entered a ${result.type.environment} ${result.type.type} key`
69+
);
70+
} else {
71+
logger.error(
72+
"Your TRIGGER_API_KEY isn't a secret dev API key. It should start with tr_dev_."
73+
);
74+
}
75+
return;
76+
}
77+
6278
return { apiKey, apiUrl: apiUrl ?? CLOUD_API_URL, envFile: resolvedEnvFile.fileName };
6379
}

0 commit comments

Comments
 (0)