forked from aquasecurity/trivy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_schema.go
More file actions
185 lines (165 loc) · 4.91 KB
/
Copy pathconfig_schema.go
File metadata and controls
185 lines (165 loc) · 4.91 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//go:build mage_docs
package main
import (
"encoding/json"
"fmt"
"os"
"strings"
"time"
"github.com/google/jsonschema-go/jsonschema"
"github.com/samber/lo"
"github.com/aquasecurity/trivy/pkg/flag"
)
// JSON Schema type constants
const (
schemaTypeString = "string"
schemaTypeBoolean = "boolean"
schemaTypeInteger = "integer"
schemaTypeNumber = "number"
schemaTypeArray = "array"
schemaTypeObject = "object"
)
const configSchemaPath = "schema/trivy-config.json"
// generateConfigSchema generates a JSON schema for trivy.yaml configuration file.
func generateConfigSchema(outputPath string, allFlagGroups []flag.FlagGroup) error {
root := &jsonschema.Schema{
Schema: "https://json-schema.org/draft/2020-12/schema",
ID: "https://raw.githubusercontent.com/aquasecurity/trivy/main/schema/trivy-config.json",
Type: schemaTypeObject,
Title: "Trivy Configuration",
Description: "Configuration file for Trivy security scanner (trivy.yaml)",
Properties: make(map[string]*jsonschema.Schema),
}
for _, group := range allFlagGroups {
for _, f := range group.Flags() {
configName := f.GetConfigName()
if configName == "" || f.Hidden() {
continue
}
if err := addFlagToSchema(root, f); err != nil {
return err
}
}
}
data, err := json.MarshalIndent(root, "", " ")
if err != nil {
return err
}
// Ensure directory exists
if err := os.MkdirAll("schema", 0755); err != nil {
return err
}
return os.WriteFile(outputPath, data, 0644)
}
// addFlagToSchema adds a flag to the schema, creating nested objects as needed.
func addFlagToSchema(root *jsonschema.Schema, f flag.Flagger) error {
configName := f.GetConfigName()
parts := strings.Split(configName, ".")
// Split into parent path and leaf name
parentParts, leafName := parts[:len(parts)-1], parts[len(parts)-1]
// Navigate/create intermediate objects
current := root
for _, part := range parentParts {
if existing, ok := current.Properties[part]; ok {
current = existing
} else {
newSchema := &jsonschema.Schema{
Type: schemaTypeObject,
Properties: make(map[string]*jsonschema.Schema),
}
current.Properties[part] = newSchema
current.PropertyOrder = append(current.PropertyOrder, part)
current = newSchema
}
}
// Add the leaf property
schema, err := schemaFromFlag(f)
if err != nil {
return err
}
current.Properties[leafName] = schema
current.PropertyOrder = append(current.PropertyOrder, leafName)
return nil
}
// schemaFromFlag creates a JSON schema based on the flag's type, description, and allowed values.
func schemaFromFlag(f flag.Flagger) (*jsonschema.Schema, error) {
schema, err := schemaFromFlagValue(f.GetDefaultValue())
if err != nil {
return nil, fmt.Errorf("flag %q: %w", f.GetConfigName(), err)
}
// Add description from Usage
if usage := f.GetUsage(); usage != "" {
schema.Description = usage
}
// Add enum if Values is set
if values := f.GetValues(); len(values) > 0 {
enumValues := make([]any, len(values))
for i, v := range values {
enumValues[i] = v
}
// For array types, enum should be in items, not at the array level
if schema.Type == schemaTypeArray && schema.Items != nil {
schema.Items.Enum = enumValues
} else {
schema.Enum = enumValues
}
}
return schema, nil
}
// schemaFromFlagValue creates a JSON schema based on the flag's default value type.
func schemaFromFlagValue(val any) (*jsonschema.Schema, error) {
switch val.(type) {
case string:
return &jsonschema.Schema{Type: schemaTypeString}, nil
case bool:
return &jsonschema.Schema{Type: schemaTypeBoolean}, nil
case int:
return &jsonschema.Schema{Type: schemaTypeInteger}, nil
case float64:
return &jsonschema.Schema{Type: schemaTypeNumber}, nil
case []string:
return &jsonschema.Schema{
Type: schemaTypeArray,
Items: &jsonschema.Schema{Type: schemaTypeString},
}, nil
case time.Duration:
return &jsonschema.Schema{Type: schemaTypeString}, nil
case map[string][]string:
return &jsonschema.Schema{
Type: schemaTypeObject,
AdditionalProperties: &jsonschema.Schema{
Type: schemaTypeArray,
Items: &jsonschema.Schema{Type: schemaTypeString},
},
}, nil
case []flag.MavenMirror:
return &jsonschema.Schema{
Type: schemaTypeArray,
Items: &jsonschema.Schema{
Type: schemaTypeObject,
Properties: map[string]*jsonschema.Schema{
"source": {
Type: schemaTypeString,
Description: "URL of the mirrored Maven repository",
},
"targets": {
Type: schemaTypeArray,
Description: "URLs of the mirrors serving the repository, tried in order",
Items: &jsonschema.Schema{Type: schemaTypeString},
MinItems: lo.ToPtr(1),
},
},
PropertyOrder: []string{
"source",
"targets",
},
Required: []string{
"source",
"targets",
},
},
}, nil
default:
return nil, fmt.Errorf("unknown type %T, please update schemaFromFlagValue()", val)
}
}