-
Notifications
You must be signed in to change notification settings - Fork 311
/
helper.go
124 lines (111 loc) · 3.33 KB
/
helper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package utils
import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
)
// CmdOutput stores the structure of kics output
type CmdOutput struct {
Output []string
Status int
}
const windowsOs = "windows"
// RunCommand executes the kics in a terminal
func RunCommand(kicsArgs []string, useDocker, useMock bool, kicsDockerImage string) (*CmdOutput, error) {
descriptionServer := getDescriptionServer(useDocker, useMock)
var source string
var args []string
if useDocker {
source, args = runKicsDocker(kicsArgs, descriptionServer, kicsDockerImage)
} else {
source, args = runKicsDev(kicsArgs)
}
cmd := exec.Command(source, args...) //#nosec
cmd.Env = append(os.Environ(), descriptionServer)
stdOutput, err := cmd.CombinedOutput()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
return &CmdOutput{
Output: strings.Split(string(stdOutput), "\n"),
Status: exitError.ExitCode(),
}, nil
}
return &CmdOutput{}, err
}
return &CmdOutput{
Output: strings.Split(string(stdOutput), "\n"),
Status: 0,
}, nil
}
// KicsDevPathAdapter adapts the path to enable kics locally execution
func KicsDevPathAdapter(path string) string {
// [e2e-029] and [e2e-056] config tests
if path == "/path/e2e/fixtures/samples/configs/config.json" {
path = strings.Replace(path, "config.json", "config-dev.json", -1)
} else if path == "/path/e2e/fixtures/samples/configs/config.yaml" {
path = strings.Replace(path, "config.yaml", "config-dev.yaml", -1)
}
regex := regexp.MustCompile(`/path/\w+/`)
matches := regex.FindString(path)
switch matches {
case "":
return path
case "/path/e2e/":
return strings.Replace(path, matches, "", -1)
default:
return strings.Replace(path, "/path/", "../", -1)
}
}
// GetKICSDockerImageName gets the kics docker image name
func GetKICSDockerImageName() string {
return os.Getenv("E2E_KICS_DOCKER")
}
// GetKICSLocalBin returns the kics local bin path
func GetKICSLocalBin() string {
if runtime.GOOS == windowsOs {
return filepath.Join("..", "bin", "kics.exe")
}
return filepath.Join("..", "bin", "kics")
}
func runKicsDev(kicsArgs []string) (bin string, args []string) {
kicsRun := GetKICSLocalBin()
var formatArgs []string
for _, param := range kicsArgs {
formatArgs = append(formatArgs, KicsDevPathAdapter(param))
}
return kicsRun, formatArgs
}
func runKicsDocker(kicsArgs []string, descriptionServer, kicsDockerImage string) (docker string, args []string) {
cwd, cwdErr := os.Getwd()
if cwdErr != nil {
return "", []string{}
}
baseDir := filepath.Dir(cwd)
dockerArgs := []string{"run", "-e", descriptionServer, "--add-host=host.docker.internal:host-gateway",
"-v", baseDir + ":/path", kicsDockerImage}
completeArgs := append(dockerArgs, kicsArgs...) //nolint
return "docker", completeArgs
}
func getDescriptionServer(useDocker, useMock bool) string {
descriptionServer := "KICS_DESCRIPTIONS_ENDPOINT=http://kics.io"
if useMock {
if useDocker {
descriptionServer = "KICS_DESCRIPTIONS_ENDPOINT=http://host.docker.internal:3000/kics-mock"
} else {
descriptionServer = "KICS_DESCRIPTIONS_ENDPOINT=http://localhost:3000/kics-mock"
}
}
return descriptionServer
}
// Contains returns if a string list contains an specific term
func Contains(list []string, searchTerm string) bool {
for _, a := range list {
if a == searchTerm {
return true
}
}
return false
}