-
Notifications
You must be signed in to change notification settings - Fork 410
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
test(encryption): add integration test for db encryption #2133
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
82 changes: 82 additions & 0 deletions
82
packages/shared/lib/utils/encryption.manager.integration.test.ts
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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { beforeAll, describe, expect, it } from 'vitest'; | ||
import encryptionManager, { EncryptionManager } from './encryption.manager'; | ||
import db, { multipleMigrations } from '../db/database.js'; | ||
import { seedAccountEnvAndUser } from '../db/seeders/index.js'; | ||
import environmentService from '../services/environment.service'; | ||
|
||
describe('encryption', () => { | ||
beforeAll(async () => { | ||
await multipleMigrations(); | ||
}); | ||
|
||
describe('status', () => { | ||
it('should report disabled if no key and no previous key', async () => { | ||
const res = await new EncryptionManager('').encryptionStatus(); | ||
expect(res).toBe('disabled'); | ||
}); | ||
|
||
it('should report not_started if key and no previous key', async () => { | ||
const res = await new EncryptionManager('aHcTnJX5yaDJHF/EJLc6IMFSo2+aiz1hPsTkpsufxa0=').encryptionStatus(); | ||
expect(res).toBe('not_started'); | ||
}); | ||
|
||
it('should report require_decryption if no key and one previous key', async () => { | ||
const res = await new EncryptionManager('').encryptionStatus({ encryption_complete: true, encryption_key_hash: 'erer' }); | ||
expect(res).toBe('require_decryption'); | ||
}); | ||
|
||
it('should report require_rotation if different keys', async () => { | ||
const res = await new EncryptionManager('aHcTnJX5yaDJHF/EJLc6IMFSo2+aiz1hPsTkpsufxa0=').encryptionStatus({ | ||
encryption_complete: true, | ||
encryption_key_hash: 'erer' | ||
}); | ||
expect(res).toBe('require_rotation'); | ||
}); | ||
|
||
it('should report incomplete if same key but not finished', async () => { | ||
const res = await new EncryptionManager('aHcTnJX5yaDJHF/EJLc6IMFSo2+aiz1hPsTkpsufxa0=').encryptionStatus({ | ||
encryption_complete: false, | ||
encryption_key_hash: 'sM+EkzNi7o4Crw3cVfg01jBbmSEAfDdmTzYWoxbryvk=' | ||
}); | ||
expect(res).toBe('incomplete'); | ||
}); | ||
|
||
it('should report done if same key and complete', async () => { | ||
const res = await new EncryptionManager('aHcTnJX5yaDJHF/EJLc6IMFSo2+aiz1hPsTkpsufxa0=').encryptionStatus({ | ||
encryption_complete: true, | ||
encryption_key_hash: 'sM+EkzNi7o4Crw3cVfg01jBbmSEAfDdmTzYWoxbryvk=' | ||
}); | ||
expect(res).toBe('done'); | ||
}); | ||
}); | ||
|
||
describe('encryption', () => { | ||
it('should encrypt environment', async () => { | ||
// we create a different schema because we have only one DB for all tests | ||
db.knex.client.config.searchPath = 'nango_encrypt'; | ||
db.schema = () => 'nango_encrypt'; | ||
|
||
await multipleMigrations(); | ||
|
||
// Disable encryption manually since it's set by default | ||
// @ts-expect-error Modify the key on the fly | ||
encryptionManager.key = ''; | ||
await db.knex.from(`_nango_db_config`).del(); | ||
|
||
const { env } = await seedAccountEnvAndUser(); | ||
expect(env.secret_key_iv).toBeNull(); | ||
expect(env.secret_key_hashed).toBe(env.secret_key); | ||
|
||
// Re-enable encryption | ||
// @ts-expect-error Modify the key on the fly | ||
encryptionManager.key = 'aHcTnJX5yaDJHF/EJLc6IMFSo2+aiz1hPsTkpsufxa0='; | ||
await encryptionManager.encryptDatabaseIfNeeded(); | ||
|
||
const envAfterEnc = (await environmentService.getRawById(env.id))!; | ||
expect(envAfterEnc.secret_key_iv).not.toBeNull(); | ||
expect(envAfterEnc.secret_key_tag).not.toBeNull(); | ||
expect(envAfterEnc.secret_key).not.toBe(env.secret_key); | ||
expect(envAfterEnc.secret_key_hashed).not.toBe(env.secret_key); | ||
}); | ||
}); | ||
}); |
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
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.
unrelated to your changes but all the queries in
encryptDatabase
andsaveDbConfig
should probably be inside a transaction.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.
At first glance yes, I'm just wondering about the size of the transaction if you have a large db. I think the script assume it can fail at any time and try everything from the start every time which is nice imo