Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 59e4a26

Browse files
Enable encryption of secret config data in DB.
1 parent 42a24c3 commit 59e4a26

File tree

7 files changed

+57
-3
lines changed

7 files changed

+57
-3
lines changed

.vercelignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
api/dist

api/app/config/get_app_config.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { DB, CError, disableApiRequestsHere } from '../../tools'
1+
import { DB, CError, disableApiRequestsHere, decryptText } from '../../tools'
22
import { IAppConfig, IAppConfigDbItem } from '../../types'
3-
import { ROOMS_DB_NAME } from '../../constants'
3+
import { ROOMS_DB_NAME, ENV_ENCRYPTION_DETAILS } from '../../constants'
44

55
export default disableApiRequestsHere
66

@@ -36,6 +36,7 @@ async function getAppConfig(): Promise<IAppConfig> {
3636
_id: 0,
3737
key: 1,
3838
value: 1,
39+
encrypted: 1,
3940
},
4041
}
4142

@@ -46,7 +47,13 @@ async function getAppConfig(): Promise<IAppConfig> {
4647
}
4748

4849
await cursor.forEach((item: IAppConfigDbItem) => {
49-
appConfig[item.key] = item.value
50+
let value = item.value
51+
52+
if (item.encrypted === true) {
53+
value = decryptText(ENV_ENCRYPTION_DETAILS, value)
54+
}
55+
56+
appConfig[item.key] = value
5057
})
5158
} catch (err) {
5259
throw new CError(500, 'Something went wrong while getting app config.')

api/constants/app.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const COMMIT_SHA: string = process.env.VERCEL_GITHUB_COMMIT_SHA || '{commit_hash
99

1010
const APP_VERSION = `${COMMIT_REF}:${COMMIT_SHA}`
1111

12+
const ENV_ENCRYPTION_DETAILS: string = process.env.ENV_ENCRYPTION_DETAILS || '{commit_hash}'
13+
1214
export {
1315
APP_VERSION,
16+
ENV_ENCRYPTION_DETAILS,
1417
}

api/constants/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export default disableApiRequestsHere
66

77
export {
88
APP_VERSION,
9+
ENV_ENCRYPTION_DETAILS,
910
} from './app'
1011

1112
export {

api/tools/crypto.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as crypto from 'crypto'
2+
3+
import { disableApiRequestsHere } from '../tools'
4+
5+
export default disableApiRequestsHere
6+
7+
/* --------------- internal API methods/structure below --------------- */
8+
9+
function encryptText(encryptionDetails: string, text: string): string {
10+
const [algorithm, secretKeyRaw, ivRaw] = encryptionDetails.split(':')
11+
12+
const secretKey = crypto.createHash('sha256').update(String(secretKeyRaw)).digest('base64').substr(0, 32)
13+
const iv = Buffer.from(ivRaw, 'hex')
14+
15+
const cipher = crypto.createCipheriv(algorithm, secretKey, iv);
16+
const encrypted = Buffer.concat([cipher.update(text), cipher.final()]);
17+
18+
return encrypted.toString('hex')
19+
}
20+
21+
function decryptText(encryptionDetails: string, hash: string): string {
22+
const [algorithm, secretKeyRaw, ivRaw] = encryptionDetails.split(':')
23+
24+
const secretKey = crypto.createHash('sha256').update(String(secretKeyRaw)).digest('base64').substr(0, 32)
25+
const iv = Buffer.from(ivRaw, 'hex')
26+
27+
const decipher = crypto.createDecipheriv(algorithm, secretKey, iv);
28+
const decrpyted = Buffer.concat([decipher.update(Buffer.from(hash, 'hex')), decipher.final()]);
29+
30+
return decrpyted.toString()
31+
}
32+
33+
export {
34+
encryptText,
35+
decryptText,
36+
}

api/tools/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,8 @@ export {
4848
export {
4949
AppConfig,
5050
} from './app_config'
51+
52+
export {
53+
encryptText,
54+
decryptText,
55+
} from './crypto'

api/types/app_config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface IAppConfig extends IObjectHash {
2424
interface IAppConfigDbItem {
2525
key: string
2626
value: string
27+
encrypted: boolean
2728
}
2829

2930
export {

0 commit comments

Comments
 (0)