-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
team.getIntegrations()
function (#281)
## What does this PR do? - Replaces `team.getBopsSubmissionURL()` for the more generic `team.getIntegrations()` - Handles decryption of secrets from db to plaintext for use in `planx-new` - Implemented in theopensystemslab/planx-new#2703
- Loading branch information
1 parent
1c88948
commit 02c3999
Showing
6 changed files
with
124 additions
and
15 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
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { decrypt, encrypt } from "./encryption"; | ||
|
||
const key = "mySecretKey".padEnd(32, "0"); | ||
|
||
describe("encrypt function", () => { | ||
it("should correctly encrypt a secret", () => { | ||
const originalSecret = "sensitiveInformation"; | ||
|
||
const encrypted = encrypt(originalSecret, key); | ||
expect(encrypted).toMatch(/:/); | ||
|
||
const decryptedResult = decrypt(encrypted, key); | ||
expect(decryptedResult).toBe(originalSecret); | ||
}); | ||
}); | ||
|
||
describe("decrypt function", () => { | ||
it("should return undefined when secret is null", () => { | ||
const result = decrypt(null, "someKey"); | ||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("should correctly decrypt a valid secret", () => { | ||
const originalSecret = "sensitiveInformation"; | ||
const encryptedSecret = encrypt(originalSecret, key); | ||
|
||
const result = decrypt(encryptedSecret, key); | ||
expect(result).toBe(originalSecret); | ||
}); | ||
}); |
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,44 @@ | ||
import * as crypto from "crypto"; | ||
|
||
/** | ||
* Encrypts a secret using AES-256-CBC encryption | ||
* | ||
* @param secret - The secret to be encrypted | ||
* @param key - The encryption key - a 32-byte string | ||
* @returns The encrypted secret along with the initialization vector (IV), separated by a colon | ||
*/ | ||
export function encrypt(secret: string, key: string): string { | ||
const keyBuffer = Buffer.from(key); | ||
const iv = crypto.randomBytes(16); | ||
const cipher = crypto.createCipheriv("AES-256-CBC", keyBuffer, iv); | ||
let encrypted = cipher.update(secret, "utf-8", "hex"); | ||
encrypted += cipher.final("hex"); | ||
const ivString = iv.toString("hex"); | ||
|
||
return `${encrypted}:${ivString}`; | ||
} | ||
|
||
/** | ||
* Decrypts a secret that was encrypted using AES-256-CBC encryption | ||
* | ||
* @param secret - The secret to be decrypted. If null, returns undefined. | ||
* @param key - The encryption key - a 32-byte string | ||
* @returns The decrypted secret | ||
*/ | ||
export function decrypt( | ||
secret: string | null, | ||
key: string, | ||
): string | undefined { | ||
if (!secret) return undefined; | ||
|
||
const [encryptedToken, iv] = secret.split(":"); | ||
const decipher = crypto.createDecipheriv( | ||
"AES-256-CBC", | ||
Buffer.from(key, "utf-8"), | ||
Buffer.from(iv, "hex"), | ||
); | ||
let decryptedToken = decipher.update(encryptedToken, "hex", "utf-8"); | ||
decryptedToken += decipher.final("utf-8"); | ||
|
||
return decryptedToken; | ||
} |
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 @@ | ||
export * from "./encryption"; |