-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(secrets): fix an issue with secret server listening on IPv6 (#134)
* fix(secrets): fix an issue with secret server listening on IPv6 * ci: bump version * feat(secrets): use precise address in `updateMasks()` * refactor(secrets): replace `request` with `got` * fix(secrets): store server address on fs * test(secrets): add tests for helpers * build: upgrade eslint, clean up dev env * build: upgrade dependencies * fix(addNewMask): fail by default * tests: fix Logger tests
- Loading branch information
1 parent
0ac8a9b
commit 86a6645
Showing
14 changed files
with
2,606 additions
and
1,511 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 |
---|---|---|
@@ -1,6 +1,10 @@ | ||
.git | ||
.gitignore | ||
.github | ||
node_modules | ||
logs/*.log | ||
lib/state.json | ||
*.md | ||
*.md | ||
.eslintrc.json | ||
test | ||
.eslintignore |
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 |
---|---|---|
@@ -1,41 +1,61 @@ | ||
const rp = require('request-promise'); | ||
const { getServerAddress } = require('./helpers'); | ||
|
||
function updateMasks(secret) { | ||
const port = process.env.PORT || 8080; | ||
const host = process.env.HOST || 'localhost'; | ||
const exitCodes = { | ||
success: 0, | ||
error: 1, | ||
missingArguments: 2, | ||
unexpectedSuccess: 3, | ||
}; | ||
|
||
const opt = { | ||
uri: `http://${host}:${port}/secrets`, | ||
method: 'POST', | ||
json: true, | ||
body: secret, | ||
resolveWithFullResponse: true, | ||
}; | ||
/** | ||
* Unexpected exit with code 0 can lead to the leakage of secrets in the build logs. | ||
* The exit should never be successful unless the secret was successfully masked. | ||
*/ | ||
let exitWithError = true; | ||
const exitHandler = (exitCode) => { | ||
if ((!exitCode || !process.exitCode) && exitWithError) { | ||
console.warn(`Unexpected exit with code 0. Exiting with ${exitCodes.unexpectedSuccess} instead`); | ||
process.exitCode = exitCodes.unexpectedSuccess; | ||
} | ||
}; | ||
process.on('exit', exitHandler); | ||
|
||
rp(opt) | ||
.then((res) => { | ||
if (res.statusCode >= 400) { | ||
console.log(`could not create mask for secret: ${secret.key}, because server responded with: ${res.statusCode}\n\n${res.body}`); | ||
process.exit(1); | ||
} | ||
console.log(`successfully updated masks with secret: ${secret.key}`); | ||
process.exit(0); | ||
}) | ||
.catch((err) => { | ||
console.log(`could not create mask for secret: ${secret.key}, due to error: ${err}`); | ||
process.exit(1); | ||
async function updateMasks(secret) { | ||
try { | ||
const serverAddress = await getServerAddress(); | ||
console.debug(`server address: ${serverAddress}`); | ||
const url = new URL('secrets', serverAddress); | ||
|
||
// eslint-disable-next-line import/no-unresolved | ||
const { default: httpClient } = await import('got'); | ||
const response = await httpClient.post(url, { | ||
json: secret, | ||
throwHttpErrors: false, | ||
}); | ||
|
||
if (response.statusCode === 201) { | ||
console.log(`successfully updated masks with secret: ${secret.key}`); | ||
exitWithError = false; | ||
process.exit(exitCodes.success); | ||
} else { | ||
console.error(`could not create mask for secret: ${secret.key}. Server responded with: ${response.statusCode}\n\n${response.body}`); | ||
process.exit(exitCodes.error); | ||
} | ||
} catch (error) { | ||
console.error(`could not create mask for secret: ${secret.key}. Error: ${error}`); | ||
process.exit(exitCodes.error); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
// first argument is the secret key second argument is the secret value | ||
if (process.argv.length < 4) { | ||
console.log('not enough arguments, need secret key and secret value'); | ||
process.exit(2); | ||
process.exit(exitCodes.missingArguments); | ||
} | ||
const key = process.argv[2]; | ||
const value = process.argv[3]; | ||
updateMasks({ key, value }); | ||
} else { | ||
module.exports = updateMasks; | ||
module.exports = { updateMasks, exitHandler }; | ||
} |
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,8 @@ | ||
const { tmpdir } = require('node:os'); | ||
const { resolve } = require('node:path'); | ||
|
||
const SERVER_ADDRESS_PATH = resolve(tmpdir(), 'LOGGER_SERVER_ADDRESS'); | ||
|
||
module.exports = { | ||
SERVER_ADDRESS_PATH, | ||
}; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.