Skip to content

Commit 365e53d

Browse files
wip 1 - trivy somewhat works
1 parent ca5f3aa commit 365e53d

File tree

6 files changed

+152
-3
lines changed

6 files changed

+152
-3
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ runtimes:
22
33
tools:
44
5+

cmd/init.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ func createConfigurationFile(tools []tools.Tool) error {
7171
}
7272

7373
func configFileTemplate(tools []tools.Tool) string {
74-
75-
// Default version
74+
// Default versions
7675
eslintVersion := "9.3.0"
76+
trivyVersion := "0.49.1"
7777

7878
for _, tool := range tools {
7979
if tool.Uuid == "f8b29663-2cb2-498d-b923-a10c6a8c05cd" {
@@ -85,7 +85,8 @@ func configFileTemplate(tools []tools.Tool) string {
8585
8686
tools:
8787
- eslint@%s
88-
`, eslintVersion)
88+
- trivy@%s
89+
`, eslintVersion, trivyVersion)
8990
}
9091

9192
func buildRepositoryConfigurationFiles(token string) error {

cmd/install.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func installTools(config *cfg.ConfigType) {
4343
fmt.Println(err.Error())
4444
log.Fatal(err)
4545
}
46+
case "trivy":
47+
err := cfg.InstallTrivy(tool, registry)
48+
if err != nil {
49+
fmt.Println(err.Error())
50+
log.Fatal(err)
51+
}
4652
default:
4753
log.Fatal("Unknown tool:", tool.Name())
4854
}

config/runtime.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ func (r *Runtime) populateInfo() {
3232
switch r.Name() {
3333
case "eslint":
3434
r.info = genInfoEslint(r)
35+
case "trivy":
36+
r.info = genInfoTrivy(r)
37+
}
38+
}
39+
40+
// genInfoTrivy generates the info map for Trivy
41+
func genInfoTrivy(r *Runtime) map[string]string {
42+
return map[string]string{
43+
"name": r.name,
44+
"version": r.version,
45+
"description": "Container and Filesystem Vulnerability Scanner",
46+
"binary": "trivy",
3547
}
3648
}
3749

config/trivy-utils.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"log"
7+
"os"
8+
"os/exec"
9+
"path/filepath"
10+
"runtime"
11+
)
12+
13+
/*
14+
* This installs Trivy using the official install script
15+
*/
16+
func InstallTrivy(trivyConfig *Runtime, registry string) error {
17+
log.Println("Installing Trivy")
18+
19+
// Create Trivy installation directory
20+
trivyFolder := fmt.Sprintf("%s@%s", trivyConfig.Name(), trivyConfig.Version())
21+
installDir := filepath.Join(Config.ToolsDirectory(), trivyFolder)
22+
23+
// Check if already installed
24+
if isTrivyInstalled(trivyConfig) {
25+
fmt.Printf("Trivy %s is already installed\n", trivyConfig.Version())
26+
return nil
27+
}
28+
29+
// Create installation directory
30+
err := os.MkdirAll(installDir, 0755)
31+
if err != nil {
32+
return fmt.Errorf("failed to create installation directory: %w", err)
33+
}
34+
35+
// Use the official install script to download and install Trivy
36+
version := fmt.Sprintf("v%s", trivyConfig.Version())
37+
installScriptURL := "https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh"
38+
39+
log.Printf("Installing Trivy %s using the official install script\n", version)
40+
41+
// Create a temporary directory for the installation
42+
tempDir := filepath.Join(installDir, "temp")
43+
err = os.MkdirAll(tempDir, 0755)
44+
if err != nil {
45+
return fmt.Errorf("failed to create temporary directory: %w", err)
46+
}
47+
48+
// Download and run the install script
49+
cmd := exec.Command("sh", "-c", fmt.Sprintf("curl -sfL %s | sh -s -- -b %s %s",
50+
installScriptURL, tempDir, version))
51+
52+
output, err := cmd.CombinedOutput()
53+
if err != nil {
54+
return fmt.Errorf("failed to run Trivy install script: %w\nOutput: %s", err, string(output))
55+
}
56+
57+
log.Printf("Install script output: %s\n", string(output))
58+
59+
// Copy the Trivy binary to the final location
60+
sourcePath := filepath.Join(tempDir, "trivy")
61+
if runtime.GOOS == "windows" {
62+
sourcePath += ".exe"
63+
}
64+
65+
binaryPath := filepath.Join(installDir, "trivy")
66+
if runtime.GOOS == "windows" {
67+
binaryPath += ".exe"
68+
}
69+
70+
// Check if the source binary exists
71+
if _, err := os.Stat(sourcePath); os.IsNotExist(err) {
72+
return fmt.Errorf("trivy binary not found at %s after installation", sourcePath)
73+
}
74+
75+
// Copy the binary to the final location
76+
source, err := os.Open(sourcePath)
77+
if err != nil {
78+
return fmt.Errorf("failed to open source binary: %w", err)
79+
}
80+
defer source.Close()
81+
82+
destination, err := os.Create(binaryPath)
83+
if err != nil {
84+
return fmt.Errorf("failed to create destination binary: %w", err)
85+
}
86+
defer destination.Close()
87+
88+
_, err = io.Copy(destination, source)
89+
if err != nil {
90+
return fmt.Errorf("failed to copy binary: %w", err)
91+
}
92+
93+
// Make the copied binary executable
94+
err = os.Chmod(binaryPath, 0755)
95+
if err != nil {
96+
return fmt.Errorf("failed to make binary executable: %w", err)
97+
}
98+
99+
// Clean up the temporary directory
100+
os.RemoveAll(tempDir)
101+
102+
log.Printf("Successfully installed Trivy %s\n", trivyConfig.Version())
103+
return nil
104+
}
105+
106+
// isTrivyInstalled checks if Trivy is already installed
107+
func isTrivyInstalled(trivyConfig *Runtime) bool {
108+
trivyFolder := fmt.Sprintf("%s@%s", trivyConfig.Name(), trivyConfig.Version())
109+
installDir := filepath.Join(Config.ToolsDirectory(), trivyFolder)
110+
binaryPath := filepath.Join(installDir, "trivy")
111+
112+
// Add .exe extension for Windows
113+
if runtime.GOOS == "windows" {
114+
binaryPath += ".exe"
115+
}
116+
117+
_, err := os.Stat(binaryPath)
118+
return err == nil
119+
}

plugins/tool-utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package plugins
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// LoadToolPlugin stub implementation until the plugin system is complete
8+
func LoadToolPlugin(toolName string) (interface{}, error) {
9+
return nil, fmt.Errorf("plugin system for tools is under development, tool %s cannot be loaded yet", toolName)
10+
}

0 commit comments

Comments
 (0)