-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ee40896
commit ca22641
Showing
1 changed file
with
57 additions
and
54 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,68 +86,71 @@ export const createKey = rateLimitedProcedure(ratelimit.create) | |
prefix: input.prefix, | ||
byteLength: input.bytes, | ||
}); | ||
await db | ||
.insert(schema.keys) | ||
.values({ | ||
id: keyId, | ||
keyAuthId: keyAuth.id, | ||
name: input.name, | ||
hash, | ||
start, | ||
ownerId: input.ownerId, | ||
meta: JSON.stringify(input.meta ?? {}), | ||
workspaceId: workspace.id, | ||
forWorkspaceId: null, | ||
expires: input.expires ? new Date(input.expires) : null, | ||
createdAt: new Date(), | ||
ratelimitAsync: input.ratelimit?.async, | ||
ratelimitLimit: input.ratelimit?.limit, | ||
ratelimitDuration: input.ratelimit?.duration, | ||
remaining: input.remaining, | ||
refillInterval: input.refill?.interval ?? null, | ||
refillAmount: input.refill?.amount ?? null, | ||
lastRefillAt: input.refill?.interval ? new Date() : null, | ||
deletedAt: null, | ||
enabled: input.enabled, | ||
environment: input.environment, | ||
}) | ||
.catch((_err) => { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: | ||
"We are unable to create the key. Please contact support using support.unkey.dev", | ||
}); | ||
}); | ||
if (input.recoverEnabled && keyAuth?.storeEncryptedKeys) { | ||
const vault = new Vault(env().AGENT_URL, env().AGENT_TOKEN); | ||
const encryptReq: EncryptRequest = { | ||
keyring: workspace?.id, | ||
data: key, | ||
}; | ||
const requestId = crypto.randomUUID(); | ||
const context: RequestContext = { requestId }; | ||
const vaultRes = await vault.encrypt(context, encryptReq).catch((_err) => { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: "Encryption Failed. Please contact support using [email protected]", | ||
}); | ||
}); | ||
await db | ||
.insert(schema.encryptedKeys) | ||
await db.transaction(async (tx) => { | ||
await tx | ||
.insert(schema.keys) | ||
.values({ | ||
keyId: keyId, | ||
workspaceId: workspace?.id, | ||
encrypted: vaultRes.encrypted, | ||
encryptionKeyId: vaultRes.keyId, | ||
id: keyId, | ||
keyAuthId: keyAuth.id, | ||
name: input.name, | ||
hash, | ||
start, | ||
ownerId: input.ownerId, | ||
meta: JSON.stringify(input.meta ?? {}), | ||
workspaceId: workspace.id, | ||
forWorkspaceId: null, | ||
expires: input.expires ? new Date(input.expires) : null, | ||
createdAt: new Date(), | ||
ratelimitAsync: input.ratelimit?.async, | ||
ratelimitLimit: input.ratelimit?.limit, | ||
ratelimitDuration: input.ratelimit?.duration, | ||
remaining: input.remaining, | ||
refillInterval: input.refill?.interval ?? null, | ||
refillAmount: input.refill?.amount ?? null, | ||
lastRefillAt: input.refill?.interval ? new Date() : null, | ||
deletedAt: null, | ||
enabled: input.enabled, | ||
environment: input.environment, | ||
}) | ||
.catch((_err) => { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: | ||
"We are unable to store encrypt the key. Please contact support using support@unkey.dev", | ||
"We are unable to create the key. Please contact support using support.unkey.dev", | ||
}); | ||
}); | ||
} | ||
|
||
if (input.recoverEnabled && keyAuth?.storeEncryptedKeys) { | ||
const vault = new Vault(env().AGENT_URL, env().AGENT_TOKEN); | ||
const encryptReq: EncryptRequest = { | ||
keyring: workspace?.id, | ||
data: key, | ||
}; | ||
const requestId = crypto.randomUUID(); | ||
const context: RequestContext = { requestId }; | ||
const vaultRes = await vault.encrypt(context, encryptReq).catch((_err) => { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: "Encryption Failed. Please contact support using [email protected]", | ||
}); | ||
}); | ||
await tx | ||
.insert(schema.encryptedKeys) | ||
.values({ | ||
keyId: keyId, | ||
workspaceId: workspace?.id, | ||
encrypted: vaultRes.encrypted, | ||
encryptionKeyId: vaultRes.keyId, | ||
}) | ||
.catch((_err) => { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: | ||
"We are unable to store encrypt the key. Please contact support using [email protected]", | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
await ingestAuditLogs({ | ||
workspaceId: workspace.id, | ||
|