-
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: create encrypted file to store environment variables
- Loading branch information
Showing
2 changed files
with
57 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
7084d01ff2b0524e6b0596a0b6014db1:bbc29979cc5c18131563f1572f9301b68563baac85e22bbecb7e8b19f965f5e5b3fd0d32fe0c8bbeb8afcf1e43940ee7e55820b4a86991cb06ff99d4003e6d528db770b770ea55cd81601c21c7ccbe7c17ac05b22116c8f982f4cef1c20bddf5d1a863fa2dac59cd37a11554c59b5663e30ead1078893bbbf634fb14e04861212ceb4d40acc96b1ee86b55bd007d6973536bd62d19e5ec14d96d8d6a08bf85877f062acbc363608245a62e0b194484d79f9c65399cc970b821c3d01ef739bf34cde5f13af3212abb004e5dc76955979ef82042e27a8ef42caa33038a267a45f4829bf19dd81e7107aab5390b23306b6c5e1126854cbf336bb82af76cdfe663cc5bc6cdb5ed57e70387d72c16a2aed58abfd6823d68ec895e080e89a2e55e33863ea97fc5f5160fff84c3f3a70c4f701c00f88fd2641b7834f3be22ebdd5b84f1a0bc6849851162ae247e0e747b457c445660a4758ba4a2536a3048fc8197aa8f4e62eb425058711e24b28e824c93ee679a514200885a53c684cb1cfdd12abdc5bfb9c27a4b7da48bddc5f8c7a3da0db4300ea3a25d40a859cbc2e10ff40d7ea0bb9b6df1273111cb122c28b5bc19970704dbd9d8b6c767d6687362c364098f43ba7e16e9ea525409678a272874d5314ed3a4b03419ca19367179525af379c3a35783591f0c1e34516bbeba0ed85df82c18dd680c66cc07a8d6cf4e32a83d323a6a312740eec791bfff8d63b99144d4d1f70d090e6b140604932985caf89bd4dbbe8eb94322611cf1071780bd2bc203e9867538a8024d214d603d4a9bf728b6fb31657ee93cc80881207e82cc55a8f7090046de270068e61687d27e386a897dea4ef894f5370d95696e4bacd4691b694fce9409f42876a9759b087ec3d557ce6dc40cd2c22a888438f7ee1da5b3500e1d2bfbe4b35603f2110aa6769baaa0b9cebf9201f6a2ffcc4a63b2dffb7a860b35233959db86038707815cf9907d945eaebcfa57b79f1eac045e6da83a4bbfe67cff649768d42f57815d56e9e09287d9b9785f2ddcd8f92e408e729b4e0b839077615f20422f14265f5b685b3b94606cd9cdd267f06c1288e7840cd4dd87142e074b28e09e09a0fa4160d64639c055b59920301e21ffd9a518e4c7ffdf0a81e1a4f72bbcfce8e26a37b94fc92504378bcb35c5f7b865d3e9539afc1645ab6954cb827f9f42e0772ab3de3b5898b2ad9f576cbb5c23cdf34577f1d472a53ccc1192e0cda2491859c1c5d1d06e902846006dd6495efd6557ee809d889509d4a540c1cfa82939ed411c20bd1230e2db2342ebf0543741c1d75f0d2471a66baf9c677b3ef80b8fb771c612ab3700314a42545cc9a645370626695df1abe95fb30e81c415cde514110078ea2a745e7c2433376fbc8820b7c9f19b5d6826e007d367b61f4715ec429ea23d91fbacf2c4471a673b90a20891d7ec69b9664c52844fc3d2344158d9bc369a173d8edf99818f0549c31662dc02769b0daa93a6f2eb286f5d09ae8623801d75b9ebd7d16523a7133bb37d91c224ff6417acde1961413245116ffacd7846699c42d14667479299a76f475efd3d9cf202098373c71effe51fdd8552df75c4ac256d10904171f0494de6cedaa63330b878650b2cab03da196d67065e176753dc58cfd0c7e4659bc4ea39b4a78f159c2e3bdc3158a8f42f78af24f32777f69c7e8cc6f9ed48b1b632ee34db20ef8269af565c0991293fb6b7a58d6f7fdfb104e1cf50cf473682ae09e59d8769135e2b07233fee4d51c91340676745ab1fadcf72b62e24e59ca5899e8f00efcae4303b0158e1a72466bb997ce5740161bc125aff9a1c76a73eed4811a4136592275123a4ac5c4ded98694859aefde63f78fe192360293885c30b91c73ffe2fd4228e32522bd59b8495a743b2c6ca12b41a02aff97caceaf3c512eafca115ce9c873f61b1df75b1362cab5209c4b064d0047309e5e66c9a4dedd5fe2e64ffce009b073b474207a7d7096240037c0cc97b3c590426a2dd38f93eec465d8241c09a972e81099da636b1407f9babf0e081b607b81447c3be5724be7d858e8925d7276b228c7979b7070c836216d84ca15f1453617c1f575862167278ded289ea1e9af8b0f48a0f3622b1aecd4c4776e92afc23bb3b8fa8e5452d6bf052d0d14ece207dc37c712ad2a9222c3975b2aced64dcdd4b3968d649067e88fb33483378c738da779ee330a51c44a529d04606afb4172cef705b8bd6b60e3b63db4efa36ccc49c9b56587dd96e5d9b00a5417fe1aaacbeb0f6684d0fbabe659ecfd870216d7fa4b0ed02de74f047ab290418181f9a7f8f24d65d913b6d5b91ee114fe5a887e4479d720076c30b096feb43916ea023f0f38dacff55bf32060eb220b9439be3f8ea419495f15e08a5cb42bde00573e084245109aea85687df751ac748d2f93716088750ca48a91257816a165f89611c4926e076cff1e58b9b73bd597624dc632529a1500c489f44e72a70ad40e187bdaa99daaa03b62dd276d54889d9d308ec5b6017f614349d3f273b61f649af6a2bbbf82e80597a86e9b6fccb1a07f1988c8cc767c75b509e7c8374769d23b8408f07b1486bf6e125256fd11089a0becec0599b41787619849e533854c97748f744a150bb87b41a0443b9da00191c03513b317a3ff22eef8a406006747ebab5c6be36604322f495644dcd6316341a8bba5812aaca016690c28f5ea8c4b619e35207953ba840841ffce856198f19b5ce9131 |
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,56 @@ | ||
import crypto from 'crypto' | ||
import fs from 'fs' | ||
|
||
const SECRETS_PATH = 'sheets-secrets' | ||
let secrets: { [key: string]: string } | undefined = undefined | ||
|
||
const getSecrets = () => { | ||
if (!secrets) { | ||
console.log('leu arquivo') | ||
let data = fs.readFileSync(SECRETS_PATH, 'utf-8') | ||
data = decrypt(data) | ||
try { | ||
secrets = JSON.parse(data) | ||
} catch (err) { | ||
throw err | ||
} | ||
} | ||
} | ||
|
||
const getEncryptionKey = (): string => { | ||
const unvalidatedEnvironmentVariable = process.env.ENCRYPTION_KEY | ||
if (!unvalidatedEnvironmentVariable) { | ||
throw new Error(`Couldn't find environment variable: ENCRYPTION_KEY`) | ||
} else { | ||
return unvalidatedEnvironmentVariable | ||
} | ||
} | ||
|
||
function decrypt(text: string) { | ||
const textParts = text.split(':') | ||
const iv = Buffer.from(textParts.shift() ?? '', 'hex') | ||
const encryptedText = Buffer.from(textParts.join(':'), 'hex') | ||
const decipher = crypto.createDecipheriv( | ||
'aes-256-cbc', | ||
Buffer.from(getEncryptionKey()), | ||
iv | ||
) | ||
let decrypted = decipher.update(encryptedText) | ||
|
||
decrypted = Buffer.concat([decrypted, decipher.final()]) | ||
|
||
return decrypted.toString() | ||
} | ||
|
||
export const getVariable = (environmentVariable: string): string => { | ||
getSecrets() | ||
const secret = secrets?.[environmentVariable] | ||
|
||
if (!secret) { | ||
throw new Error( | ||
`Couldn't find environment variable: ${environmentVariable}` | ||
) | ||
} | ||
|
||
return secret | ||
} |