-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
Centralized error throwing for encryption key #3105
Changes from 1 commit
bc0be05
cca7c99
f53b793
6d6b872
2c2d2ff
9b5d3d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ test('POST /credentials should fail with invalid inputs', async () => { | |
|
||
test('POST /credentials should fail with missing encryption key', async () => { | ||
const mock = jest.spyOn(UserSettings, 'getEncryptionKey'); | ||
mock.mockResolvedValue(undefined); | ||
mock.mockRejectedValue(new Error('Encryption key not found.')); | ||
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. if we apply the change in if we don't change it then we should move 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. Exactly, the wording is not super important but def if we can get everything equal that is best for consistency |
||
|
||
const owner = await Db.collections.User!.findOneOrFail(); | ||
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner }); | ||
|
@@ -348,7 +348,8 @@ test('PATCH /credentials/:id should fail if cred not found', async () => { | |
|
||
test('PATCH /credentials/:id should fail with missing encryption key', async () => { | ||
const mock = jest.spyOn(UserSettings, 'getEncryptionKey'); | ||
mock.mockResolvedValue(undefined); | ||
mock.mockRejectedValue(new Error('Encryption key not found.')); | ||
|
||
|
||
const owner = await Db.collections.User!.findOneOrFail(); | ||
const authOwnerAgent = utils.createAgent(app, { auth: true, user: owner }); | ||
|
@@ -494,7 +495,8 @@ test('GET /credentials/:id should fail with missing encryption key', async () => | |
const savedCredential = await saveCredential(credentialPayload(), { user: owner }); | ||
|
||
const mock = jest.spyOn(UserSettings, 'getEncryptionKey'); | ||
mock.mockResolvedValue(undefined); | ||
mock.mockRejectedValue(new Error('Encryption key not found.')); | ||
|
||
|
||
const response = await authOwnerAgent | ||
.get(`/credentials/${savedCredential.id}`) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,19 +73,15 @@ export async function prepareUserSettings(): Promise<IUserSettings> { | |
* @returns | ||
*/ | ||
|
||
export async function getEncryptionKey(): Promise<string | undefined> { | ||
export async function getEncryptionKey(): Promise<string> { | ||
if (process.env[ENCRYPTION_KEY_ENV_OVERWRITE] !== undefined) { | ||
return process.env[ENCRYPTION_KEY_ENV_OVERWRITE]; | ||
return process.env[ENCRYPTION_KEY_ENV_OVERWRITE] as string; | ||
} | ||
|
||
const userSettings = await getUserSettings(); | ||
|
||
if (userSettings === undefined) { | ||
return undefined; | ||
} | ||
|
||
if (userSettings.encryptionKey === undefined) { | ||
return undefined; | ||
if (userSettings === undefined || userSettings.encryptionKey === undefined) { | ||
throw new Error('Encryption key not set'); | ||
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. could we throw 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. Unfortunately no, this file resides in the |
||
} | ||
|
||
return userSettings.encryptionKey; | ||
|
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.
ref comment in
UserSettings.ts
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.
IN this case specifically we're changing the regular error to a new type of error, so this try / catch block is still necessary