Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
getModifiedFiles,
getOptionalEnvVariable,
readMarkdownFile,
shouldRunIaCScanner,
generateCacheKey,
} from './util'

Expand Down Expand Up @@ -53,13 +52,7 @@ async function runAnalysis() {
}
}

// Skip the IaC scan if there no IaC-related files have been modified in the PR
let enableIacRunning = true
if (modifiedFiles && target == 'new') {
if (!shouldRunIaCScanner(modifiedFiles)) {
enableIacRunning = false
}
}
const enableIacRunning = true

// Create scan-results directory
const resultsPath = path.join(process.cwd(), 'scan-results')
Expand Down
43 changes: 12 additions & 31 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ export async function callCommand(command: string, ...args: string[]) {
}
}

export async function tryCallCommand(command: string, ...args: string[]): Promise<boolean> {
const child = spawn(command, args, { stdio: 'ignore' })
const exitCode = await new Promise((resolve, _) => {
child.on('close', resolve)
})
return exitCode === 0
}

export function getRequiredEnvVariable(name: string) {
const value = process.env[name]
if (!value) {
Expand Down Expand Up @@ -127,36 +135,6 @@ export async function getModifiedFiles(): Promise<string | undefined> {
}
}

export function shouldRunIaCScanner(modifiedFiles: string): boolean {
const iacFileExtensions = ['.tf', '.hcl', '.yaml', '.yml', '.json']
const nonIaCFilenames = [
'package.json',
'package-lock.json',
'tsconfig.json',
'tsconfig.build.json',
'tslint.json',
'jest.config.json',
'.eslintrc.json',
'.prettierrc.json',
'.prettierrc.yaml',
'.prettierrc.yml',
'renovate.json',
'lerna.json',
'bower.json',
'composer.json',
'composer.lock',
'Pipfile.lock',
'cargo.lock',
]
return modifiedFiles.split(',').some((file) => {
const filename = file.split('/').pop() || ''
if (nonIaCFilenames.includes(filename.toLowerCase())) {
return false
}
return iacFileExtensions.some((ext) => file.endsWith(ext))
})
}

// runCodesec - Docker-based scanner using codesec:latest image
//
// Modes:
Expand Down Expand Up @@ -261,13 +239,16 @@ export async function runCodesec(
if (runIac) {
const iacDir = path.join(reportsDir, 'iac')
mkdirSync(iacDir, { recursive: true })
await callCommand(
const copied = await tryCallCommand(
'docker',
'container',
'cp',
`${containerName}:/tmp/scan-results/iac/iac-${scanTarget || 'scan'}.json`,
path.join(iacDir, `iac-${scanTarget || 'scan'}.json`)
)
if (!copied) {
info('IaC results not produced — scanner likely skipped IaC')
}
}

// Cleanup container
Expand Down
Loading