Skip to content

Commit

Permalink
Added transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
harshsbhat committed Sep 20, 2024
1 parent ee40896 commit ca22641
Showing 1 changed file with 57 additions and 54 deletions.
111 changes: 57 additions & 54 deletions apps/dashboard/lib/trpc/routers/key/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ca22641

Please sign in to comment.