|
| 1 | +/*! |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import { describe } from 'mocha' |
| 7 | + |
| 8 | +import * as assert from 'assert' |
| 9 | +import * as path from 'path' |
| 10 | +import { platform } from 'os' |
| 11 | +import { existsSync, mkdirSync, unlinkSync, writeFileSync } from 'fs' |
| 12 | +import { runCmd } from './testUtils' |
| 13 | + |
| 14 | +/** |
| 15 | + * NOTES: |
| 16 | + * - git-secrets patterns are set in the project's `.git/config` file |
| 17 | + */ |
| 18 | +describe('git-secrets', function () { |
| 19 | + let accessKeyFilePath: string |
| 20 | + let toolkitProjectDir: string |
| 21 | + let testFixturesPath: string |
| 22 | + let gitSecrets: string // path to the git-secrets executable |
| 23 | + |
| 24 | + /** git-secrets patterns that will not cause a failure during the scan */ |
| 25 | + function setAllowListPatterns(gitSecrets: string) { |
| 26 | + const allowListPatterns: string[] = ['"accountId": "123456789012"'] |
| 27 | + |
| 28 | + allowListPatterns.forEach(pattern => { |
| 29 | + // Returns non-zero exit code if pattern already exists |
| 30 | + runCmd([gitSecrets, '--add', '--allowed', pattern], { cwd: toolkitProjectDir, throws: false }) |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + function setDenyListPatterns(gitSecrets: string) { |
| 35 | + const denyListPatterns: string[] = [] |
| 36 | + |
| 37 | + denyListPatterns.forEach(pattern => { |
| 38 | + // Returns non-zero exit code if pattern already exists |
| 39 | + runCmd([gitSecrets, '--add', pattern], { cwd: toolkitProjectDir, throws: false }) |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + function setupTestFixturesDir(toolkitProjectDir: string) { |
| 44 | + const testFixturesPath = path.join(toolkitProjectDir, 'src', 'testFixtures', 'bin') |
| 45 | + mkdirSync(testFixturesPath, { recursive: true }) |
| 46 | + return testFixturesPath |
| 47 | + } |
| 48 | + |
| 49 | + function setupAccessKeyFile(testFixturesPath: string) { |
| 50 | + const accessKeyFilePath = path.join(testFixturesPath, 'fileWithAccessKey.ts') |
| 51 | + deleteFileIfExists(accessKeyFilePath) |
| 52 | + return accessKeyFilePath |
| 53 | + } |
| 54 | + |
| 55 | + function setupGitSecretsExecutable(testFixturesPath: string) { |
| 56 | + const gitSecretsExecutablePath = path.join(testFixturesPath, 'git-secrets') |
| 57 | + |
| 58 | + if (existsSync(gitSecretsExecutablePath)) { |
| 59 | + console.log('INFO: git-secrets already installed') |
| 60 | + } else { |
| 61 | + console.log('INFO: Installing git-secrets...') |
| 62 | + runCmd(['mkdir', '-p', path.parse(gitSecretsExecutablePath).dir]) |
| 63 | + runCmd([ |
| 64 | + 'curl', |
| 65 | + '-o', |
| 66 | + gitSecretsExecutablePath, |
| 67 | + 'https://raw.githubusercontent.com/awslabs/git-secrets/99d01d58ebcc06e237c0e3f3ff5ae628aeef6aa6/git-secrets', |
| 68 | + ]) |
| 69 | + runCmd(['chmod', '+x', gitSecretsExecutablePath]) |
| 70 | + } |
| 71 | + |
| 72 | + return gitSecretsExecutablePath |
| 73 | + } |
| 74 | + |
| 75 | + function deleteFileIfExists(filePath: string) { |
| 76 | + if (existsSync(filePath)) { |
| 77 | + unlinkSync(filePath) |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + function createFileWithSecretKey(accessKeyFilePath: string) { |
| 82 | + // Create file in project that has secret key value. |
| 83 | + // Need to build access key string incrementally to not trigger git-secrets. |
| 84 | + const keyValue = 'yAki21XLhAIBiKvyaxr4p/ltr8OxkZTHISISFAKE' |
| 85 | + const mySecretAccessKey = `const x = { "aws_secret_access_key": "${keyValue}" }` |
| 86 | + const fileContent = ` |
| 87 | +/*! |
| 88 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 89 | + * SPDX-License-Identifier: Apache-2.0 |
| 90 | + */ |
| 91 | +
|
| 92 | +${mySecretAccessKey} |
| 93 | +`.trim() |
| 94 | + writeFileSync(accessKeyFilePath, fileContent) |
| 95 | + } |
| 96 | + |
| 97 | + before(function () { |
| 98 | + if (platform() === 'win32') { |
| 99 | + this.skip() |
| 100 | + } |
| 101 | + |
| 102 | + toolkitProjectDir = path.resolve() |
| 103 | + testFixturesPath = setupTestFixturesDir(toolkitProjectDir) |
| 104 | + gitSecrets = setupGitSecretsExecutable(testFixturesPath) |
| 105 | + accessKeyFilePath = setupAccessKeyFile(testFixturesPath) |
| 106 | + |
| 107 | + // Register all patterns with `git-secrets` |
| 108 | + runCmd([gitSecrets, '--register-aws'], { cwd: toolkitProjectDir }) |
| 109 | + setDenyListPatterns(gitSecrets) |
| 110 | + setAllowListPatterns(gitSecrets) |
| 111 | + }) |
| 112 | + |
| 113 | + afterEach(function () { |
| 114 | + deleteFileIfExists(accessKeyFilePath) |
| 115 | + }) |
| 116 | + |
| 117 | + it('ensures no git secrets are found', function () { |
| 118 | + const result = runCmd([gitSecrets, '--scan'], { cwd: toolkitProjectDir }) |
| 119 | + assert.strictEqual(result.status, 0, `Failure output: ${result.stderr.toString()}`) |
| 120 | + }) |
| 121 | + |
| 122 | + it('sanity check it finds secrets', function () { |
| 123 | + createFileWithSecretKey(accessKeyFilePath) |
| 124 | + const result = runCmd([gitSecrets, '--scan', accessKeyFilePath], { cwd: toolkitProjectDir, throws: false }) |
| 125 | + assert.strictEqual(result.status, 1) |
| 126 | + }) |
| 127 | +}) |
0 commit comments