-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error handling for encrypt fail as well as disabled store encrypt keys
- Loading branch information
1 parent
babbde4
commit 4b74619
Showing
2 changed files
with
32 additions
and
10 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
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 |
---|---|---|
|
@@ -103,22 +103,45 @@ export const createKey = t.procedure | |
"We are unable to create the key. Please contact support using support.unkey.dev", | ||
}); | ||
}); | ||
if (input.recoverEnabled && !keyAuth?.storeEncryptedKeys) { | ||
throw new TRPCError({ | ||
code: "INTERNAL_SERVER_ERROR", | ||
message: | ||
"Storing encrypted keys for your workspace is disabled. Please contact support using [email protected]", | ||
}); | ||
} | ||
|
||
if (input.recoverEnabled && keyAuth?.storeEncryptedKeys) { | ||
const vault = new Vault(env().AGENT_URL, env().AGENT_TOKEN); | ||
const encryptReq: EncryptRequest = { | ||
keyring: workspace.id, | ||
keyring: workspace?.id, | ||
data: key, | ||
}; | ||
const requestId = crypto.randomUUID(); | ||
const context: RequestContext = { requestId }; | ||
const vaultRes = await vault.encrypt(context, encryptReq); | ||
await db.insert(schema.encryptedKeys).values({ | ||
keyId: keyId, | ||
workspaceId: workspace.id, | ||
encrypted: vaultRes.encrypted, | ||
encryptionKeyId: vaultRes.keyId, | ||
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) | ||
.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, | ||
actor: { type: "user", id: ctx.user.id }, | ||
|