Skip to content

Commit

Permalink
private keys
Browse files Browse the repository at this point in the history
  • Loading branch information
vincelwt committed Mar 30, 2024
1 parent 33f078a commit d59b021
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
43 changes: 43 additions & 0 deletions e2e/api.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { test, expect } from "@playwright/test"

let privateKey = null
let publicKey = null

// run tests one after another
test.describe.configure({ mode: "serial" })

test("regenerate api keys", async ({ page }) => {
await page.goto("/settings")

publicKey = await page.getByTestId("private-key").textContent()

const firstPrivateKey = await page.getByTestId("private-key").textContent()

// expect to not be empty string
expect(firstPrivateKey).not.toEqual("")

await page.getByTestId("regenerate-private-key-button").click()

const secondPrivateKey = await page.getByTestId("private-key").textContent()

console.log({ firstPrivateKey, secondPrivateKey })

expect(firstPrivateKey).not.toEqual(secondPrivateKey)

Check failure on line 25 in e2e/api.spec.ts

View workflow job for this annotation

GitHub Actions / run-tests

[chromium] › api.spec.ts:9:5 › regenerate api keys

1) [chromium] › api.spec.ts:9:5 › regenerate api keys ──────────────────────────────────────────── Error: expect(received).not.toEqual(expected) // deep equality Expected: not "416b4e02-0bb8-4494-b532-d8535a9fa65d" 23 | console.log({ firstPrivateKey, secondPrivateKey }) 24 | > 25 | expect(firstPrivateKey).not.toEqual(secondPrivateKey) | ^ 26 | 27 | privateKey = secondPrivateKey 28 | }) at /home/runner/work/lunary/lunary/e2e/api.spec.ts:25:31

Check failure on line 25 in e2e/api.spec.ts

View workflow job for this annotation

GitHub Actions / run-tests

[chromium] › api.spec.ts:9:5 › regenerate api keys

1) [chromium] › api.spec.ts:9:5 › regenerate api keys ──────────────────────────────────────────── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).not.toEqual(expected) // deep equality Expected: not "416b4e02-0bb8-4494-b532-d8535a9fa65d" 23 | console.log({ firstPrivateKey, secondPrivateKey }) 24 | > 25 | expect(firstPrivateKey).not.toEqual(secondPrivateKey) | ^ 26 | 27 | privateKey = secondPrivateKey 28 | }) at /home/runner/work/lunary/lunary/e2e/api.spec.ts:25:31

Check failure on line 25 in e2e/api.spec.ts

View workflow job for this annotation

GitHub Actions / run-tests

[chromium] › api.spec.ts:9:5 › regenerate api keys

1) [chromium] › api.spec.ts:9:5 › regenerate api keys ──────────────────────────────────────────── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: expect(received).not.toEqual(expected) // deep equality Expected: not "416b4e02-0bb8-4494-b532-d8535a9fa65d" 23 | console.log({ firstPrivateKey, secondPrivateKey }) 24 | > 25 | expect(firstPrivateKey).not.toEqual(secondPrivateKey) | ^ 26 | 27 | privateKey = secondPrivateKey 28 | }) at /home/runner/work/lunary/lunary/e2e/api.spec.ts:25:31

privateKey = secondPrivateKey
})

test("private api /logs", async ({ page }) => {
// Test API query

const res = await fetch(process.env.API_URL + "/api/v1/runs", {
method: "POST",
headers: {
"X-API-Key": privateKey!,
},
})

const json = await res.json()

expect(json).toEqual([])
})
4 changes: 3 additions & 1 deletion e2e/projects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { test, expect } from "@playwright/test"
import { setOrgFree, setOrgPro } from "./db-utils"

// run tests one after another
test.describe.configure({ mode: "serial" })

test("create new project, rename it and delete it", async ({ page }) => {
await page.goto("/")
Expand Down
8 changes: 6 additions & 2 deletions packages/backend/src/api/v1/auth/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,18 @@ export async function authMiddleware(ctx: Context, next: Next) {
typeof route === "string" ? route === ctx.path : route.test(ctx.path),
)

const key = ctx.request?.headers?.authorization?.split(" ")[1]
const bearer = ctx.request?.headers?.authorization?.split(" ")[1] as string
const apiKey = ctx.request?.headers?.["x-api-key"] as string

const key = bearer || apiKey

// For routes like signup, login, etc
if (isPublicRoute && !key) {
await next()
return
}
// Check if API key is valid
// Support passing as bearer because legacy SDKs did that
else if (validateUUID(key)) {
try {
const { type, projectId, orgId } = await checkApiKey(key as string)
Expand All @@ -140,7 +144,7 @@ export async function authMiddleware(ctx: Context, next: Next) {
} else {
// Check if JWT is valid
try {
if (!key) {
if (!bearer) {
throw new Error("No bearer token provided.")
}
const { payload } = await verifyJwt<SessionData>(key)
Expand Down
4 changes: 2 additions & 2 deletions packages/frontend/components/blocks/CopyText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export const SuperCopyButton = ({ value }) => (
</CopyButton>
)

export default function CopyText({ c = "violet", value }) {
export default function CopyText({ c = "violet", value, ...props }) {
return (
<Group gap={0} display="inline-flex">
<Code ml={5} c={c}>
<Code ml={5} c={c} {...props}>
{value}
</Code>
<SuperCopyButton value={value} />
Expand Down
14 changes: 11 additions & 3 deletions packages/frontend/pages/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ function Keys() {
title={
<Group>
<Text fw={500}>Project ID / Public Key:</Text>
<CopyText c="green.8" value={project?.id} />
<CopyText
c="green.8"
value={project?.id}
data-testid="public-key"
/>
</Group>
}
color="green"
Expand All @@ -79,14 +83,18 @@ function Keys() {
<Group justify="space-between" w="100%">
<Group>
<Text fw={500}>Private Key:</Text>
<CopyText c="red.8" value={project?.privateApiKey} />
<CopyText
c="red.8"
value={project?.privateApiKey}
data-testid="private-key"
/>
</Group>
<Button
ml="auto"
size="xs"
color="red"
loading={regenerating}
data-testid="regenerate-key-button"
data-testid="regenerate-private-key-button"
onClick={() => {
modals.openConfirmModal({
title: "Please confirm your action",
Expand Down

0 comments on commit d59b021

Please sign in to comment.