-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: encrypt metadata feature #2059
Open
harshsbhat
wants to merge
14
commits into
unkeyed:main
Choose a base branch
from
harshsbhat:harshbhat/encrypted-meta
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
07b1fc8
Encrypted meta for API
harshsbhat 82db90b
Encrypted meta for dashboard
harshsbhat 20acdfd
removal of unused variables for linting
harshsbhat 07ad642
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat 7a4f0f7
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat c453788
Merge remote-tracking branch 'origin/main' into harshbhat/encrypted-meta
harshsbhat 5f1c4da
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat 7451f9b
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat f65066c
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat 342bfe2
Requested changes
harshsbhat cb44ea9
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat 875e826
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat 0e9c23d
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat 616beb4
Merge branch 'main' into harshbhat/encrypted-meta
harshsbhat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { rootKeyAuth } from "@/pkg/auth/root_key"; | ||
import { UnkeyApiError, openApiErrorResponses } from "@/pkg/errors"; | ||
import type { App } from "@/pkg/hono/app"; | ||
import { DisabledWorkspaceError, MissingRatelimitError } from "@/pkg/keys/service"; | ||
import { retry } from "@/pkg/util/retry"; | ||
import { createRoute, z } from "@hono/zod-openapi"; | ||
import { SchemaError } from "@unkey/error"; | ||
import { permissionQuerySchema } from "@unkey/rbac"; | ||
import { buildUnkeyQuery, permissionQuerySchema } from "@unkey/rbac"; | ||
|
||
const route = createRoute({ | ||
tags: ["keys"], | ||
|
@@ -176,6 +178,17 @@ A key could be invalid for a number of reasons, for example if it has expired, h | |
stripeCustomerId: "cus_1234", | ||
}, | ||
}), | ||
encryptedMeta: z | ||
.record(z.unknown()) | ||
.optional() | ||
.openapi({ | ||
description: | ||
"Encrypted metadata that might contain secrets that you want to store with the key", | ||
example: { | ||
roles: ["admin", "user"], | ||
stripeCustomerId: "cus_1234", | ||
}, | ||
}), | ||
expires: z.number().int().optional().openapi({ | ||
description: | ||
"The unix timestamp in milliseconds when the key will expire. If this field is null or undefined, the key is not expiring.", | ||
|
@@ -286,8 +299,7 @@ export type V1KeysVerifyKeyResponse = z.infer< | |
export const registerV1KeysVerifyKey = (app: App) => | ||
app.openapi(route, async (c) => { | ||
const req = c.req.valid("json"); | ||
const { keyService, analytics } = c.get("services"); | ||
|
||
const { keyService, analytics, vault, cache } = c.get("services"); | ||
const { val, err } = await keyService.verifyKey(c, { | ||
key: req.key, | ||
apiId: req.apiId, | ||
|
@@ -327,12 +339,45 @@ export const registerV1KeysVerifyKey = (app: App) => | |
}); | ||
} | ||
|
||
let metaSecret: string | undefined | null; | ||
if (val.key.encryptedMeta) { | ||
try { | ||
await rootKeyAuth( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what rootKey are you using here? |
||
c, | ||
buildUnkeyQuery(({ or }) => | ||
or("*", "api.*.decrypt_meta", `api.${val.api.id}.decrypt_meta`), | ||
), | ||
); | ||
const { val: vaultRes } = await cache.encryptedMeta.swr( | ||
val.key.encryptedMeta!, | ||
async () => { | ||
const encryptedMeta = | ||
typeof val.key.encryptedMeta === "string" | ||
? val.key.encryptedMeta | ||
: JSON.stringify(val.key.encryptedMeta); | ||
|
||
const decryptRes = await retry(3, () => | ||
vault.decrypt(c, { | ||
keyring: val.key.workspaceId, | ||
encrypted: encryptedMeta, | ||
}), | ||
); | ||
return decryptRes.plaintext; | ||
}, | ||
); | ||
metaSecret = vaultRes; | ||
} catch { | ||
metaSecret = undefined; | ||
} | ||
} | ||
|
||
const responseBody = { | ||
keyId: val.key?.id, | ||
valid: val.valid, | ||
name: val.key?.name ?? undefined, | ||
ownerId: val.key?.ownerId ?? undefined, | ||
meta: val.key?.meta ? JSON.parse(val.key?.meta) : undefined, | ||
encryptedMeta: metaSecret ? JSON.parse(metaSecret) : undefined, | ||
expires: val.key?.expires?.getTime(), | ||
remaining: val.remaining ?? undefined, | ||
ratelimit: val.ratelimit ?? undefined, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're ignoring the returned encryption key id, that needs to be stored as well, otherwise we can't reroll the encryption keys