Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

Fix PublishableKey -> PublicKey (#124)

Marc Klingen5526467f8d2a
diff --git a/README.md b/README.md
index cf5ef265c9..252d58016b 100644
--- a/README.md
+++ b/README.md
@@ -241,6 +241,12 @@ npm run db:migrate
npm run dev
```
+Run tests
+
+```bash
+npm run test
+```
+
## License
Langfuse is MIT licensed, except for `ee/` folder. See [LICENSE](LICENSE) and [docs](https://langfuse.com/docs/open-source) for more details.
diff --git a/prisma/migrations/20230907225604_api_keys_publishable_to_public/migration.sql b/prisma/migrations/20230907225604_api_keys_publishable_to_public/migration.sql
new file mode 100644
index 0000000000..76ce007da7
--- /dev/null
+++ b/prisma/migrations/20230907225604_api_keys_publishable_to_public/migration.sql
@@ -0,0 +1,8 @@
+-- DropIndex
+DROP INDEX "api_keys_publishable_key_key";
+
+-- RENAME column from "publishable_key" to "public_key" on table "api_keys"
+ALTER TABLE "api_keys" rename column "publishable_key" to "public_key";
+
+-- CreateIndex
+CREATE UNIQUE INDEX "api_keys_public_key_key" ON "api_keys"("public_key");
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 469b343318..b1951ea2dd 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -89,7 +89,7 @@ model ApiKey {
id String @id @unique @default(cuid())
createdAt DateTime @default(now()) @map("created_at")
note String?
- publishableKey String @unique @map("publishable_key")
+ publicKey String @unique @map("public_key")
hashedSecretKey String @unique @map("hashed_secret_key")
displaySecretKey String @map("display_secret_key")
lastUsedAt DateTime? @map("last_used_at")
diff --git a/prisma/seed.ts b/prisma/seed.ts
index 45c7015d25..89c7f4c787 100755
--- a/prisma/seed.ts
+++ b/prisma/seed.ts
@@ -65,7 +65,7 @@ async function main() {
data: {
note: seedApiKey.note,
id: seedApiKey.id,
- publishableKey: seedApiKey.public,
+ publicKey: seedApiKey.public,
hashedSecretKey: await hashSecretKey(seedApiKey.secret),
displaySecretKey: getDisplaySecretKey(seedApiKey.secret),
project: {
@@ -89,7 +89,7 @@ async function main() {
note: "seeded key",
hashedSecretKey: await hashSecretKey("sk-lf-asdfghjkl"),
displaySecretKey: getDisplaySecretKey("sk-lf-asdfghjkl"),
- publishableKey: "pk-lf-asdfghjkl",
+ publicKey: "pk-lf-asdfghjkl",
},
],
},
@@ -111,7 +111,8 @@ async function main() {
}
// random date within last 90 days, with a linear bias towards more recent dates
const traceTs = new Date(
- Date.now() - Math.floor(Math.random() ** 1.5 * 90 * 24 * 60 * 60 * 1000)
+ Date.now() -
+ Math.floor(Math.random() ** 1.5 * 90 * 24 * 60 * 60 * 1000),
);
const trace = await prisma.trace.create({
@@ -163,11 +164,11 @@ async function main() {
for (let j = 0; j < Math.floor(Math.random() * 10) + 1; j++) {
// add between 1 and 30 ms to trace timestamp
const spanTsStart = new Date(
- traceTs.getTime() + Math.floor(Math.random() * 30)
+ traceTs.getTime() + Math.floor(Math.random() * 30),
);
// random duration of upto 30ms
const spanTsEnd = new Date(
- spanTsStart.getTime() + Math.floor(Math.random() * 30)
+ spanTsStart.getTime() + Math.floor(Math.random() * 30),
);
const span = await prisma.observation.create({
@@ -201,15 +202,15 @@ async function main() {
const generationTsStart = new Date(
spanTsStart.getTime() +
Math.floor(
- Math.random() * (spanTsEnd.getTime() - spanTsStart.getTime())
- )
+ Math.random() * (spanTsEnd.getTime() - spanTsStart.getTime()),
+ ),
);
const generationTsEnd = new Date(
generationTsStart.getTime() +
Math.floor(
Math.random() *
- (spanTsEnd.getTime() - generationTsStart.getTime())
- )
+ (spanTsEnd.getTime() - generationTsStart.getTime()),
+ ),
);
const promptTokens = Math.floor(Math.random() * 1000) + 300;
@@ -308,8 +309,8 @@ async function main() {
const eventTs = new Date(
spanTsStart.getTime() +
Math.floor(
- Math.random() * (spanTsEnd.getTime() - spanTsStart.getTime())
- )
+ Math.random() * (spanTsEnd.getTime() - spanTsStart.getTime()),
+ ),
);
await prisma.observation.create({
diff --git a/src/__tests__/test-utils.ts b/src/__tests__/test-utils.ts
index ab931e6254..ec44e354e9 100644
--- a/src/__tests__/test-utils.ts
+++ b/src/__tests__/test-utils.ts
@@ -7,10 +7,10 @@ import { hash } from "bcryptjs";
export function createBasicAuthHeader(
username: string,
- password: string
+ password: string,
): string {
const base64Credentials = Buffer.from(`${username}:${password}`).toString(
- "base64"
+ "base64",
);
return `Basic ${base64Credentials}`;
}
@@ -18,7 +18,7 @@ export function createBasicAuthHeader(
export async function makeAPICall(
method: "POST" | "GET" | "PUT" | "DELETE" | "PATCH",
url: string,
- body: unknown
+ body: unknown,
) {
const finalUrl = `http://localhost:3000/${url}`;
const options = {
@@ -28,7 +28,7 @@ export async function makeAPICall(
"Content-Type": "application/json;charset=UTF-8",
Authorization: createBasicAuthHeader(
"pk-lf-1234567890",
- "sk-lf-1234567890"
+ "sk-lf-1234567890",
),
},
body: JSON.stringify(body),
@@ -59,7 +59,7 @@ export const setupUserAndProject = async () => {
note: "seeded key",
hashedSecretKey: await hashSecretKey("sk-lf-1234567890"),
displaySecretKey: getDisplaySecretKey("sk-lf-1234567890"),
- publishableKey: "pk-lf-1234567890",
+ publicKey: "pk-lf-1234567890",
},
],
},
diff --git a/src/features/publicApi/components/ApiKeyList.tsx b/src/features/publicApi/components/ApiKeyList.tsx
index 7c13832624..998d571108 100644
--- a/src/features/publicApi/components/ApiKeyList.tsx
+++ b/src/features/publicApi/components/ApiKeyList.tsx
@@ -36,7 +36,7 @@ export function ApiKeyList(props: { projectId: string }) {
},
{
enabled: hasAccess,
- }
+ },
);
if (!hasAccess) return null;
@@ -70,7 +70,7 @@ export function ApiKeyList(props: { projectId: string }) {
<TableCell className="font-mono">
<CodeView
className="inline-block"
- content={apiKey.publishableKey}
+ content={apiKey.publicKey}
/>
</TableCell>
<TableCell className="font-mono">
diff --git a/src/features/publicApi/components/CreateApiKeyButton.tsx b/src/features/publicApi/components/CreateApiKeyButton.tsx
index e8ee0f4524..43eb3fffe4 100644
--- a/src/features/publicApi/components/CreateApiKeyButton.tsx
+++ b/src/features/publicApi/components/CreateApiKeyButton.tsx
@@ -25,7 +25,7 @@ export function CreateApiKeyButton(props: { projectId: string }) {
const [open, setOpen] = useState(false);
const [generatedKeys, setGeneratedKeys] = useState<{
secretKey: string;
- publishableKey: string;
+ publicKey: string;
} | null>(null);
const createApiKey = () => {
@@ -37,10 +37,10 @@ export function CreateApiKeyButton(props: { projectId: string }) {
.mutateAsync({
projectId: props.projectId,
})
- .then(({ secretKey, publishableKey }) => {
+ .then(({ secretKey, publicKey }) => {
setGeneratedKeys({
secretKey,
- publishableKey,
+ publicKey,
});
setOpen(true);
})
@@ -77,7 +77,7 @@ export function CreateApiKeyButton(props: { projectId: string }) {
</div>
<div>
<div className="text-md mb-2 font-semibold">Public Key</div>
- <CodeView content={generatedKeys?.publishableKey ?? "Loading ..."} />
+ <CodeView content={generatedKeys?.publicKey ?? "Loading ..."} />
</div>
</DialogContent>
</Dialog>
diff --git a/src/features/publicApi/lib/apiKeys.ts b/src/features/publicApi/lib/apiKeys.ts
index 5dcdc4a6dd..c1f208a8af 100644
--- a/src/features/publicApi/lib/apiKeys.ts
+++ b/src/features/publicApi/lib/apiKeys.ts
@@ -5,7 +5,7 @@ export function generateSecretKey() {
return `sk-lf-${randomUUID()}`;
}
-export function generatePublishableKey() {
+export function generatePublicKey() {
return `pk-lf-${randomUUID()}`;
}
@@ -19,7 +19,7 @@ export async function hashSecretKey(key: string) {
}
export async function generateKeySet() {
- const pk = generatePublishableKey();
+ const pk = generatePublicKey();
const sk = generateSecretKey();
const hashedSk = await hashSecretKey(sk);
const displaySk = getDisplaySecretKey(sk);
diff --git a/src/features/publicApi/server/apiAuth.ts b/src/features/publicApi/server/apiAuth.ts
index 3410055b3f..56d081b327 100644
--- a/src/features/publicApi/server/apiAuth.ts
+++ b/src/features/publicApi/server/apiAuth.ts
@@ -13,7 +13,7 @@ type AuthHeaderVerificationResult =
};
export async function verifyAuthHeaderAndReturnScope(
- authHeader: string | undefined
+ authHeader: string | undefined,
): Promise<AuthHeaderVerificationResult> {
if (!authHeader) {
console.error("No authorization header");
@@ -24,12 +24,12 @@ export async function verifyAuthHeaderAndReturnScope(
}
try {
- // Basic auth, full scope, needs secret key and publishable key
+ // Basic auth, full scope, needs secret key and public key
if (authHeader.startsWith("Basic ")) {
- const { username: publishableKey, password: secretKey } =
+ const { username: publicKey, password: secretKey } =
extractBasicAuthCredentials(authHeader);
- const dbKey = await findDbKeyOrThrow(publishableKey);
+ const dbKey = await findDbKeyOrThrow(publicKey);
const isValid = await verifySecretKey(secretKey, dbKey.hashedSecretKey);
if (!isValid) throw new Error("Invalid credentials");
@@ -42,11 +42,11 @@ export async function verifyAuthHeaderAndReturnScope(
},
};
}
- // Bearer auth, limited scope, only needs publishable key
+ // Bearer auth, limited scope, only needs public key
if (authHeader.startsWith("Bearer ")) {
- const publishableKey = authHeader.replace("Bearer ", "");
+ const publicKey = authHeader.replace("Bearer ", "");
- const dbKey = await findDbKeyOrThrow(publishableKey);
+ const dbKey = await findDbKeyOrThrow(publicKey);
return {
validKey: true,
@@ -82,9 +82,9 @@ function extractBasicAuthCredentials(basicAuthHeader: string): {
return { username, password };
}
-async function findDbKeyOrThrow(publishableKey: string) {
+async function findDbKeyOrThrow(publicKey: string) {
const dbKey = await prisma.apiKey.findUnique({
- where: { publishableKey },
+ where: { publicKey },
});
if (!dbKey) throw new Error("Invalid public key");
return dbKey;
diff --git a/src/features/publicApi/server/apiKeyRouter.ts b/src/features/publicApi/server/apiKeyRouter.ts
index 1daec7d36d..f83dcbc7df 100644
--- a/src/features/publicApi/server/apiKeyRouter.ts
+++ b/src/features/publicApi/server/apiKeyRouter.ts
@@ -11,7 +11,7 @@ export const apiKeysRouter = createTRPCRouter({
.input(
z.object({
projectId: z.string(),
- })
+ }),
)
.query(async ({ input, ctx }) => {
throwIfNoAccess({
@@ -30,7 +30,7 @@ export const apiKeysRouter = createTRPCRouter({
expiresAt: true,
lastUsedAt: true,
note: true,
- publishableKey: true,
+ publicKey: true,
displaySecretKey: true,
},
orderBy: {
@@ -43,7 +43,7 @@ export const apiKeysRouter = createTRPCRouter({
z.object({
projectId: z.string(),
note: z.string().optional(),
- })
+ }),
)
.mutation(async ({ input, ctx }) => {
throwIfNoAccess({
@@ -57,7 +57,7 @@ export const apiKeysRouter = createTRPCRouter({
const apiKey = await ctx.prisma.apiKey.create({
data: {
projectId: input.projectId,
- publishableKey: pk,
+ publicKey: pk,
hashedSecretKey: hashedSk,
displaySecretKey: displaySk,
note: input.note,
@@ -68,7 +68,7 @@ export const apiKeysRouter = createTRPCRouter({
id: apiKey.id,
createdAt: apiKey.createdAt,
note: input.note,
- publishableKey: apiKey.publishableKey,
+ publicKey: apiKey.publicKey,
secretKey: sk,
displaySecretKey: displaySk,
};
@@ -78,7 +78,7 @@ export const apiKeysRouter = createTRPCRouter({
z.object({
projectId: z.string(),
id: z.string(),
- })
+ }),
)
.mutation(async ({ input, ctx }) => {
throwIfNoAccess({