Skip to content

Commit e8d8aa9

Browse files
feat: add support for checkov (#267)
* feat: add support for checkov --------- Co-authored-by: Dias Saparov <dias@digger.dev>
1 parent cdbf355 commit e8d8aa9

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

action.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ inputs:
5656
upload-plan-destination:
5757
description: Destination to upload the plan to. gcp and github are currently supported
5858
required: false
59+
setup-checkov:
60+
description: Setup Checkov
61+
required: false
62+
default: 'false'
63+
checkov-version:
64+
description: Checkov version
65+
required: false
66+
default: '2.3.245'
5967

6068
outputs:
6169
output:
@@ -126,11 +134,22 @@ runs:
126134
terragrunt_version: ${{ inputs.terragrunt-version }}
127135
if: inputs.setup-terragrunt == 'true'
128136

137+
- name: Setup Checkov
138+
run: |
139+
python3 -m venv .venv
140+
source .venv/bin/activate
141+
pip3 install --upgrade pip
142+
pip3 install --upgrade setuptools
143+
pip3 install -U checkov==${{ inputs.checkov-version }}
144+
shell: bash
145+
if: inputs.setup-checkov == 'true'
146+
129147
- name: build and run digger
130148
if: ${{ !startsWith(github.action_ref, 'v') }}
131149
shell: bash
132150
env:
133151
PLAN_UPLOAD_DESTINATION: ${{ inputs.upload-plan-destination }}
152+
ACTIVATE_VENV: ${{ inputs.setup-checkov == 'true' }}
134153
run: |
135154
cd ${{ github.action_path }}
136155
go build -o digger ./cmd/digger

pkg/digger/digger.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"errors"
1212
"fmt"
1313
"io"
14+
"io/ioutil"
1415
"log"
1516
"os"
1617
"os/exec"
@@ -373,25 +374,42 @@ type DiggerExecutor struct {
373374
}
374375

375376
type CommandRun interface {
376-
Run(workingDir string, shell string, command string) (string, string, error)
377+
Run(workingDir string, shell string, commands []string) (string, string, error)
377378
}
378379

379380
type CommandRunner struct {
380381
}
381382

382-
func (c CommandRunner) Run(workingDir string, shell string, command string) (string, string, error) {
383+
func (c CommandRunner) Run(workingDir string, shell string, commands []string) (string, string, error) {
384+
var args []string
383385
if shell == "" {
384386
shell = "bash"
387+
args = []string{"-eo", "pipefail"}
385388
}
386-
cmd := exec.Command(shell, "-c", command)
389+
390+
scriptFile, err := ioutil.TempFile("", "run-script")
391+
if err != nil {
392+
return "", "", fmt.Errorf("error creating script file: %v", err)
393+
}
394+
defer os.Remove(scriptFile.Name())
395+
396+
for _, command := range commands {
397+
_, err := scriptFile.WriteString(command + "\n")
398+
if err != nil {
399+
return "", "", fmt.Errorf("error writing to script file: %v", err)
400+
}
401+
}
402+
args = append(args, scriptFile.Name())
403+
404+
cmd := exec.Command(shell, args...)
387405
cmd.Dir = workingDir
388406

389407
var stdout, stderr bytes.Buffer
390408
mwout := io.MultiWriter(os.Stdout, &stdout)
391409
mwerr := io.MultiWriter(os.Stderr, &stderr)
392410
cmd.Stdout = mwout
393411
cmd.Stderr = mwerr
394-
err := cmd.Run()
412+
err = cmd.Run()
395413

396414
if err != nil {
397415
return stdout.String(), stderr.String(), fmt.Errorf("error: %v", err)
@@ -458,8 +476,13 @@ func (d DiggerExecutor) Plan(prNumber int) error {
458476
d.CIService.PublishComment(prNumber, comment)
459477
}
460478
if step.Action == "run" {
461-
stdout, stderr, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, step.Value)
462-
log.Printf("Running %v for **%v**\n%v%v", step.Value, d.ProjectLock.LockId(), stdout, stderr)
479+
var commands []string
480+
if os.Getenv("ACTIVATE_VENV") == "true" {
481+
commands = append(commands, fmt.Sprintf("source %v/.venv/bin/activate", os.Getenv("GITHUB_WORKSPACE")))
482+
}
483+
commands = append(commands, step.Value)
484+
log.Printf("Running %v for **%v**\n", step.Value, d.ProjectLock.LockId())
485+
_, _, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, commands)
463486
if err != nil {
464487
return fmt.Errorf("error running command: %v", err)
465488
}
@@ -522,8 +545,13 @@ func (d DiggerExecutor) Apply(prNumber int) error {
522545
}
523546
}
524547
if step.Action == "run" {
525-
stdout, stderr, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, step.Value)
526-
log.Printf("Running %v for **%v**\n%v%v", step.Value, d.ProjectLock.LockId(), stdout, stderr)
548+
var commands []string
549+
if os.Getenv("ACTIVATE_VENV") == "true" {
550+
commands = append(commands, fmt.Sprintf("source %v/.venv/bin/activate", os.Getenv("GITHUB_WORKSPACE")))
551+
}
552+
commands = append(commands, step.Value)
553+
log.Printf("Running %v for **%v**\n", step.Value, d.ProjectLock.LockId())
554+
_, _, err := d.CommandRunner.Run(d.ProjectPath, step.Shell, commands)
527555
if err != nil {
528556
return fmt.Errorf("error running command: %v", err)
529557
}

pkg/digger/digger_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ type MockCommandRunner struct {
2121
Commands []RunInfo
2222
}
2323

24-
func (m *MockCommandRunner) Run(workDir string, shell string, command string) (string, string, error) {
25-
m.Commands = append(m.Commands, RunInfo{"Run", workDir + " " + shell + " " + command, time.Now()})
24+
func (m *MockCommandRunner) Run(workDir string, shell string, commands []string) (string, string, error) {
25+
m.Commands = append(m.Commands, RunInfo{"Run", workDir + " " + shell + " " + strings.Join(commands, " "), time.Now()})
2626
return "", "", nil
2727
}
2828

@@ -182,7 +182,7 @@ func TestCorrectCommandExecutionWhenApplying(t *testing.T) {
182182

183183
commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage)
184184

185-
assert.Equal(t, []string{"RetrievePlan #.tfplan", "IsMergeable 1", "Lock 1", "Init ", "Apply ", "LockId ", "PublishComment 1 <details>\n <summary>Apply for ****</summary>\n\n ```terraform\n\n ```\n</details>", "Run echo", "LockId "}, commandStrings)
185+
assert.Equal(t, []string{"RetrievePlan #.tfplan", "IsMergeable 1", "Lock 1", "Init ", "Apply ", "LockId ", "PublishComment 1 <details>\n <summary>Apply for ****</summary>\n\n ```terraform\n\n ```\n</details>", "LockId ", "Run echo"}, commandStrings)
186186
}
187187

188188
func TestCorrectCommandExecutionWhenPlanning(t *testing.T) {
@@ -224,7 +224,7 @@ func TestCorrectCommandExecutionWhenPlanning(t *testing.T) {
224224

225225
commandStrings := allCommandsInOrderWithParams(terraformExecutor, commandRunner, prManager, lock, planStorage)
226226

227-
assert.Equal(t, []string{"Lock 1", "Init ", "Plan -out #.tfplan", "StorePlan #.tfplan", "LockId ", "PublishComment 1 <details>\n <summary>Plan for ****</summary>\n\n ```terraform\n\n ```\n</details>", "Run echo", "LockId "}, commandStrings)
227+
assert.Equal(t, []string{"Lock 1", "Init ", "Plan -out #.tfplan", "StorePlan #.tfplan", "LockId ", "PublishComment 1 <details>\n <summary>Plan for ****</summary>\n\n ```terraform\n\n ```\n</details>", "LockId ", "Run echo"}, commandStrings)
228228
}
229229

230230
func allCommandsInOrderWithParams(terraformExecutor *MockTerraformExecutor, commandRunner *MockCommandRunner, prManager *MockPRManager, lock *MockProjectLock, planStorage *MockPlanStorage) []string {

0 commit comments

Comments
 (0)