-
Notifications
You must be signed in to change notification settings - Fork 22
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
chore: pre-commit secrets scanner #1170
Merged
+92
−2
Merged
Changes from all commits
Commits
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
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,76 @@ | ||
#!/usr/bin/env node | ||
|
||
const { resolve } = require('path') | ||
const { writeFileSync, unlinkSync, existsSync, readFileSync } = require('fs') | ||
const homedir = require('os').homedir() | ||
const shell = require('shelljs') | ||
|
||
const TEMP_CRED_FILE = resolve(process.cwd(), './.temp-credentials') | ||
const AWS_CRED_FILE = resolve(homedir, './.aws/credentials') | ||
|
||
/** | ||
* Used to build AWS credentials file using environment variable | ||
* https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-environment.html | ||
* and credential file | ||
* https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html | ||
*/ | ||
const buildTempCredFile = () => { | ||
const awsAccessKeyId = process.env.AWS_ACCESS_KEY_ID | ||
const awsSecretAccessKey = process.env.AWS_SECRET_ACCESS_KEY | ||
const contentFromEnv = | ||
awsAccessKeyId && awsSecretAccessKey | ||
? `[env] | ||
aws_access_key_id = ${awsAccessKeyId} | ||
aws_secret_access_key = ${awsSecretAccessKey} | ||
|
||
` | ||
: '' | ||
const contentFromCredFile = existsSync(AWS_CRED_FILE) ? readFileSync(AWS_CRED_FILE, { encoding: 'utf-8' }) : '' | ||
const finalContent = contentFromEnv + contentFromCredFile | ||
writeFileSync(TEMP_CRED_FILE, finalContent) | ||
console.log(`Wrote temp credentials file to: ${TEMP_CRED_FILE}\n`) | ||
} | ||
|
||
const removeTempCredFile = () => { | ||
unlinkSync(TEMP_CRED_FILE) | ||
console.log(`Removed credentials file ${TEMP_CRED_FILE}\n`) | ||
} | ||
|
||
/** | ||
* FLOW: | ||
* 1. GET AWS secrets from environment variables and ~/.aws/credentials | ||
* 2. Merge those secrets into one file (temporary file) | ||
* 3. Register AWS provider for git-secrets using the file in step 2 | ||
* 4. Scan for secrets in staged files using git secrets --scan | ||
* 5. Remove temporary file in step 2 | ||
*/ | ||
const run = () => { | ||
try { | ||
buildTempCredFile() | ||
// add AWS as a provider, with genereted credentials file | ||
const { error: errorAddProvider, stdout: stdoutAddProvider, stderr: stderrAddProvider } = shell.exec( | ||
`git secrets --add-provider -- git secrets --aws-provider ${TEMP_CRED_FILE}`, | ||
{ | ||
stdio: 'inherit', | ||
}, | ||
) | ||
if (errorAddProvider) throw errorAddProvider | ||
if (stderrAddProvider) throw stderrAddProvider | ||
console.log('Added AWS provider\n', stdoutAddProvider) | ||
|
||
// scan for secrets | ||
const { error: errorScan, stdout: stdoutScan, stderr: stderrScan } = shell.exec('git secrets --scan --cached', { | ||
stdio: 'inherit', | ||
}) | ||
if (errorScan) throw errorScan | ||
if (stderrScan) throw stderrScan | ||
console.log('No secret found in staged files!\n', stdoutScan) | ||
removeTempCredFile() | ||
} catch (err) { | ||
console.error(err) | ||
removeTempCredFile() | ||
throw err | ||
} | ||
} | ||
|
||
run() |
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
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.
I thinks this part could result unhandled error if
unlinksync
is fail to execute and throws errorLGTM. Nice 👍
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.
Yes if it throws an error, then just let it does, the script will auto exit with an error code.
If all things go well, then no error is returned.