-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
127 lines (117 loc) · 5.57 KB
/
Copy pathvalidate.go
File metadata and controls
127 lines (117 loc) · 5.57 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
package sdk
import (
"fmt"
"strings"
"unicode/utf8"
)
// ValidateDetectorDescriptor validates typed detector registration data.
func ValidateDetectorDescriptor(descriptor *DetectorDescriptor) error {
if descriptor == nil {
return fmt.Errorf("detector descriptor is nil")
}
if err := validateComponentDescriptor("detector", componentFromDetectorDescriptor(*descriptor)); err != nil {
return err
}
for _, manager := range descriptor.SupportedManagers {
if strings.TrimSpace(manager.Name()) == "" {
return fmt.Errorf("detector descriptor supported managers must not contain empty values")
}
}
for _, support := range descriptor.PackageManagerSupport {
if strings.TrimSpace(support.PackageManager.Name()) == "" {
return fmt.Errorf("detector descriptor package manager support must not contain empty package manager values")
}
}
for _, fallback := range descriptor.FallbackDetectors {
if strings.TrimSpace(fallback) == "" {
return fmt.Errorf("detector descriptor fallback detectors must not contain empty values")
}
}
for _, capability := range descriptor.RemediationCapabilities {
if len(capability.SupportedManagers) == 0 {
return fmt.Errorf("detector descriptor remediation capability must include a package manager")
}
for _, manager := range capability.SupportedManagers {
if strings.TrimSpace(manager.Name()) == "" {
return fmt.Errorf("detector descriptor remediation capability managers must not contain empty values")
}
}
if len(capability.Actions) == 0 {
return fmt.Errorf("detector descriptor remediation capability must include an action")
}
for _, action := range capability.Actions {
switch action {
case RemediationActionDirectBump,
RemediationActionTransitiveOverride,
RemediationActionLockfileRefresh:
default:
return fmt.Errorf("detector descriptor remediation capability action %q is invalid", action)
}
}
}
return nil
}
// ValidateMatcherDescriptor validates typed matcher registration data.
func ValidateMatcherDescriptor(descriptor *MatcherDescriptor) error {
if descriptor == nil {
return fmt.Errorf("matcher descriptor is nil")
}
return validateComponentDescriptor("matcher", componentFromMatcherDescriptor(*descriptor))
}
// ValidateAuditorDescriptor validates typed auditor registration data.
func ValidateAuditorDescriptor(descriptor *AuditorDescriptor) error {
if descriptor == nil {
return fmt.Errorf("auditor descriptor is nil")
}
return validateComponentDescriptor("auditor", componentFromAuditorDescriptor(*descriptor))
}
// ValidateAnalyzerDescriptor validates typed analyzer registration data.
func ValidateAnalyzerDescriptor(descriptor *AnalyzerDescriptor) error {
if descriptor == nil {
return fmt.Errorf("analyzer descriptor is nil")
}
return validateComponentDescriptor("analyzer", componentFromAnalyzerDescriptor(*descriptor))
}
func validateComponentDescriptor(kind string, descriptor ComponentDescriptor) error {
name := strings.TrimSpace(descriptor.Name)
if name == "" {
return fmt.Errorf("%s descriptor name is required", kind)
}
// The name reaches published documents as a license source, so it is
// held to the same domain there and here: bounded, valid UTF-8, no
// control characters. Checked on the stored value, not a trimmed copy:
// validation does not rewrite the descriptor, so a name that passes here
// is the name that gets marshaled, and a control character at an edge or
// unbounded padding would otherwise ride through a gate that claims to
// refuse them. The source gate trims before it measures, which only
// shrinks, so every name accepted here still survives there.
if len(descriptor.Name) > maxComponentNameLength {
return fmt.Errorf("%s descriptor name exceeds %d bytes", kind, maxComponentNameLength)
}
if !utf8.ValidString(descriptor.Name) || containsControlChar(descriptor.Name) {
return fmt.Errorf("%s descriptor name must be valid UTF-8 without control characters", kind)
}
for _, alias := range descriptor.Aliases {
if strings.TrimSpace(alias) == "" {
return fmt.Errorf("%s descriptor aliases must not contain empty values", kind)
}
}
for _, manager := range descriptor.SupportedManagers {
if strings.TrimSpace(manager.Name()) == "" {
return fmt.Errorf("%s descriptor supported managers must not contain empty values", kind)
}
}
return nil
}
func componentFromDetectorDescriptor(descriptor DetectorDescriptor) ComponentDescriptor {
return ComponentDescriptor{Name: descriptor.Name, DisplayName: descriptor.DisplayName, Aliases: descriptor.Aliases, Tags: descriptor.Tags, SupportedEcosystems: descriptor.SupportedEcosystems, SupportedManagers: descriptor.SupportedManagers}
}
func componentFromMatcherDescriptor(descriptor MatcherDescriptor) ComponentDescriptor {
return ComponentDescriptor{Name: descriptor.Name, DisplayName: descriptor.DisplayName, Aliases: descriptor.Aliases, Tags: descriptor.Tags, SupportedEcosystems: descriptor.SupportedEcosystems, SupportedManagers: descriptor.SupportedManagers}
}
func componentFromAuditorDescriptor(descriptor AuditorDescriptor) ComponentDescriptor {
return ComponentDescriptor{Name: descriptor.Name, DisplayName: descriptor.DisplayName, Aliases: descriptor.Aliases, Tags: descriptor.Tags, SupportedEcosystems: descriptor.SupportedEcosystems, SupportedManagers: descriptor.SupportedManagers}
}
func componentFromAnalyzerDescriptor(descriptor AnalyzerDescriptor) ComponentDescriptor {
return ComponentDescriptor{Name: descriptor.Name, DisplayName: descriptor.DisplayName, Aliases: descriptor.Aliases, Tags: descriptor.Tags, SupportedEcosystems: descriptor.SupportedEcosystems, SupportedManagers: descriptor.SupportedManagers}
}