-
Notifications
You must be signed in to change notification settings - Fork 24
/
scorecard.go
126 lines (102 loc) · 3.23 KB
/
scorecard.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
125
126
package scorecard
import (
"encoding/json"
"fmt"
"strings"
)
type ReportValue struct {
Ratio float32
Reasoning string
}
const validPoints = 25
const generationPoints = 15
const packageSectionWeight = 20
// TODO capture generation points
const maxPoints = validPoints + packageSectionWeight*3
type SbomReport interface {
IsSpecCompliant() ReportValue
PackageIdentification() ReportValue
PackageVersions() ReportValue
PackageLicenses() ReportValue
CreationInfo() ReportValue
Report() string
}
type ScoreValue struct {
ReportValue
MaxPoints float32
}
func (sv *ScoreValue) Score() float32 {
return sv.Ratio * sv.MaxPoints
}
type ReportResult struct {
Compliance ScoreValue
PackageIdentification ScoreValue
PackageVersions ScoreValue
PackageLicenses ScoreValue
CreationInfo ScoreValue
Total ScoreValue
}
func reportValueToScore(rv ReportValue, maxPoints float32) ScoreValue {
sv := ScoreValue{
MaxPoints: maxPoints,
}
sv.Ratio = rv.Ratio
sv.Reasoning = rv.Reasoning
return sv
}
func getScore(sr SbomReport) ReportResult {
rr := ReportResult{
Compliance: reportValueToScore(sr.IsSpecCompliant(), validPoints),
PackageIdentification: reportValueToScore(sr.PackageIdentification(), packageSectionWeight),
PackageVersions: reportValueToScore(sr.PackageVersions(), packageSectionWeight),
PackageLicenses: reportValueToScore(sr.PackageLicenses(), packageSectionWeight),
CreationInfo: reportValueToScore(sr.CreationInfo(), generationPoints),
}
var totalPoints float32
var maxPoints float32
maxPoints += rr.Compliance.MaxPoints
totalPoints += rr.Compliance.Score()
maxPoints += rr.PackageIdentification.MaxPoints
totalPoints += rr.PackageIdentification.Score()
maxPoints += rr.PackageVersions.MaxPoints
totalPoints += rr.PackageVersions.Score()
maxPoints += rr.PackageLicenses.MaxPoints
totalPoints += rr.PackageLicenses.Score()
maxPoints += rr.CreationInfo.MaxPoints
totalPoints += rr.CreationInfo.Score()
rr.Total = ScoreValue{
MaxPoints: maxPoints,
}
rr.Total.Ratio = totalPoints / maxPoints
return rr
}
func JsonGrade(sr SbomReport) string {
out, _ := json.Marshal(getScore(sr))
return string(out)
}
func getReportValueInfo(title string, sv ScoreValue) string {
var sb strings.Builder
sb.WriteString(fmt.Sprintf("%s: %d/%d", title, int(sv.Score()), int(sv.MaxPoints)))
if sv.Reasoning != "" {
sb.WriteString(fmt.Sprintf(" (%s)", sv.Reasoning))
}
sb.WriteString("\n")
return sb.String()
}
func Grade(sr SbomReport) string {
sv := getScore(sr)
var sb strings.Builder
sb.WriteString(getReportValueInfo("Spec Compliance", sv.Compliance))
sb.WriteString(getReportValueInfo("Package ID", sv.PackageIdentification))
sb.WriteString(getReportValueInfo("Package Versions", sv.PackageVersions))
sb.WriteString(getReportValueInfo("Package Licenses", sv.PackageLicenses))
sb.WriteString(getReportValueInfo("Creation Info", sv.CreationInfo))
sb.WriteString(fmt.Sprintf("Total points: %d/%d or %d%%\n", int(sv.Total.Score()), int(sv.Total.MaxPoints), PrettyPercent(int(sv.Total.Score()), int(sv.Total.MaxPoints))))
return sb.String()
}
func PrettyPercent(num, denom int) int {
if denom == 0 {
return 0
}
return 100 * (1.0 * num) / denom
}