Skip to content

Commit 54214a2

Browse files
committed
feature: Add support for Trivy config parsing PLUTO-1360
1 parent 7db65fd commit 54214a2

File tree

5 files changed

+426
-3
lines changed

5 files changed

+426
-3
lines changed

.codacy/codacy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ runtimes:
22
33
tools:
44
5-
- trivy@0.46.0
5+
- trivy@0.59.1

cmd/init.go

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ func configFileTemplate(tools []tools.Tool) string {
7474

7575
// Default versions
7676
eslintVersion := "9.3.0"
77-
trivyVersion := "0.50.0" // Use the latest stable version
77+
trivyVersion := "0.59.1" // Latest stable version
7878

7979
for _, tool := range tools {
8080
if tool.Uuid == "f8b29663-2cb2-498d-b923-a10c6a8c05cd" {
8181
eslintVersion = tool.Version
8282
}
83-
// If Codacy API provides UUID for Trivy, you would check it here
83+
if tool.Uuid == "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb" {
84+
trivyVersion = tool.Version
85+
}
8486
}
8587

8688
return fmt.Sprintf(`runtimes:
@@ -153,7 +155,24 @@ func buildRepositoryConfigurationFiles(token string) error {
153155
_, err = eslintConfigFile.WriteString(eslintConfigurationString)
154156
if err != nil {
155157
log.Fatal(err)
158+
}
156159

160+
// Create Trivy configuration after processing ESLint
161+
trivyApiConfiguration := extractTrivyConfiguration(apiToolConfigurations)
162+
if trivyApiConfiguration != nil {
163+
// Create trivy.yaml file based on API configuration
164+
err = createTrivyConfigFile(*trivyApiConfiguration)
165+
if err != nil {
166+
log.Fatal(err)
167+
}
168+
fmt.Println("Trivy configuration created based on Codacy settings")
169+
} else {
170+
// Create default trivy.yaml if no configuration from API
171+
err = createDefaultTrivyConfigFile()
172+
if err != nil {
173+
log.Fatal(err)
174+
}
175+
fmt.Println("Default Trivy configuration created")
157176
}
158177

159178
return nil
@@ -200,6 +219,20 @@ func extractESLintConfiguration(toolConfigurations []CodacyToolConfiguration) *C
200219
return nil
201220
}
202221

222+
// extractTrivyConfiguration extracts Trivy configuration from the Codacy API response
223+
func extractTrivyConfiguration(toolConfigurations []CodacyToolConfiguration) *CodacyToolConfiguration {
224+
// Trivy internal codacy uuid
225+
const TrivyUUID = "2fd7fbe0-33f9-4ab3-ab73-e9b62404e2cb"
226+
227+
for _, toolConfiguration := range toolConfigurations {
228+
if toolConfiguration.Uuid == TrivyUUID {
229+
return &toolConfiguration
230+
}
231+
}
232+
233+
return nil
234+
}
235+
203236
type CodacyToolConfiguration struct {
204237
Uuid string `json:"uuid"`
205238
IsEnabled bool `json:"isEnabled"`
@@ -215,3 +248,65 @@ type ParameterConfiguration struct {
215248
name string `json:"name"`
216249
value string `json:"value"`
217250
}
251+
252+
// createTrivyConfigFile creates a trivy.yaml configuration file based on the API configuration
253+
func createTrivyConfigFile(config CodacyToolConfiguration) error {
254+
// Convert CodacyToolConfiguration to tools.ToolConfiguration
255+
trivyDomainConfiguration := convertAPIToolConfigurationForTrivy(config)
256+
257+
// Use the shared CreateTrivyConfig function to generate the config content
258+
trivyConfigurationString := tools.CreateTrivyConfig(trivyDomainConfiguration)
259+
260+
// Write to file
261+
return os.WriteFile("trivy.yaml", []byte(trivyConfigurationString), 0644)
262+
}
263+
264+
// convertAPIToolConfigurationForTrivy converts API tool configuration to domain model for Trivy
265+
func convertAPIToolConfigurationForTrivy(config CodacyToolConfiguration) tools.ToolConfiguration {
266+
var patterns []tools.PatternConfiguration
267+
268+
// Only process if tool is enabled
269+
if config.IsEnabled {
270+
for _, pattern := range config.Patterns {
271+
var parameters []tools.PatternParameterConfiguration
272+
273+
// By default patterns are enabled
274+
patternEnabled := true
275+
276+
// Check if there's an explicit enabled parameter
277+
for _, param := range pattern.Parameters {
278+
if param.name == "enabled" && param.value == "false" {
279+
patternEnabled = false
280+
}
281+
}
282+
283+
// Add enabled parameter
284+
parameters = append(parameters, tools.PatternParameterConfiguration{
285+
Name: "enabled",
286+
Value: fmt.Sprintf("%t", patternEnabled),
287+
})
288+
289+
patterns = append(
290+
patterns,
291+
tools.PatternConfiguration{
292+
PatternId: pattern.InternalId,
293+
ParamenterConfigurations: parameters,
294+
},
295+
)
296+
}
297+
}
298+
299+
return tools.ToolConfiguration{
300+
PatternsConfiguration: patterns,
301+
}
302+
}
303+
304+
// createDefaultTrivyConfigFile creates a default trivy.yaml configuration file
305+
func createDefaultTrivyConfigFile() error {
306+
// Use empty tool configuration to get default settings
307+
emptyConfig := tools.ToolConfiguration{}
308+
content := tools.CreateTrivyConfig(emptyConfig)
309+
310+
// Write to file
311+
return os.WriteFile("trivy.yaml", []byte(content), 0644)
312+
}

tools/trivyConfigCreator.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package tools
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// CreateTrivyConfig generates a Trivy configuration based on the tool configuration
9+
func CreateTrivyConfig(config ToolConfiguration) string {
10+
// Default settings - include all severities and scanners
11+
includeLow := true
12+
includeMedium := true
13+
includeHigh := true
14+
includeCritical := true
15+
includeSecret := true
16+
17+
// Process patterns from Codacy API
18+
for _, pattern := range config.PatternsConfiguration {
19+
// Check if pattern is enabled
20+
patternEnabled := true
21+
for _, param := range pattern.ParamenterConfigurations {
22+
if param.Name == "enabled" && param.Value == "false" {
23+
patternEnabled = false
24+
}
25+
}
26+
27+
// Map patterns to configurations
28+
if pattern.PatternId == "Trivy_vulnerability_minor" {
29+
includeLow = patternEnabled
30+
}
31+
if pattern.PatternId == "Trivy_vulnerability_medium" {
32+
includeMedium = patternEnabled
33+
}
34+
if pattern.PatternId == "Trivy_vulnerability" {
35+
// This covers HIGH and CRITICAL
36+
includeHigh = patternEnabled
37+
includeCritical = patternEnabled
38+
}
39+
if pattern.PatternId == "Trivy_secret" {
40+
includeSecret = patternEnabled
41+
}
42+
}
43+
44+
// Build the severity list based on enabled patterns
45+
var severities []string
46+
if includeLow {
47+
severities = append(severities, "LOW")
48+
}
49+
if includeMedium {
50+
severities = append(severities, "MEDIUM")
51+
}
52+
if includeHigh {
53+
severities = append(severities, "HIGH")
54+
}
55+
if includeCritical {
56+
severities = append(severities, "CRITICAL")
57+
}
58+
59+
// Build the scanners list
60+
var scanners []string
61+
scanners = append(scanners, "vuln") // Always include vuln scanner
62+
if includeSecret {
63+
scanners = append(scanners, "secret")
64+
}
65+
66+
// Generate trivy.yaml content
67+
var contentBuilder strings.Builder
68+
contentBuilder.WriteString("severity:\n")
69+
for _, sev := range severities {
70+
contentBuilder.WriteString(fmt.Sprintf(" - %s\n", sev))
71+
}
72+
73+
contentBuilder.WriteString("\nscan:\n")
74+
contentBuilder.WriteString(" scanners:\n")
75+
for _, scanner := range scanners {
76+
contentBuilder.WriteString(fmt.Sprintf(" - %s\n", scanner))
77+
}
78+
79+
return contentBuilder.String()
80+
}

0 commit comments

Comments
 (0)