Skip to content

Commit 16595a4

Browse files
authored
Add fix availability information to DB schema (#2862)
* add fix availability information to DB schema Signed-off-by: Alex Goodman <[email protected]> * update json schemas Signed-off-by: Alex Goodman <[email protected]> * fix linting Signed-off-by: Alex Goodman <[email protected]> * revert vuln schema change Signed-off-by: Alex Goodman <[email protected]> --------- Signed-off-by: Alex Goodman <[email protected]>
1 parent 1bb9d43 commit 16595a4

File tree

8 files changed

+641
-28
lines changed

8 files changed

+641
-28
lines changed

cmd/grype/cli/commands/internal/dbsearch/versions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package dbsearch
22

33
const (
44
// MatchesSchemaVersion is the schema version for the `db search` command
5-
MatchesSchemaVersion = "1.0.3"
5+
MatchesSchemaVersion = "1.1.0"
66

77
// MatchesSchemaVersion Changelog:
88
// 1.0.0 - Initial schema 🎉
99
// 1.0.1 - Add KEV and EPSS data to vulnerability matches
1010
// 1.0.2 - Add v5 namespace emulation for affected packages
1111
// 1.0.3 - Add severity string field to vulnerability object
12+
// 1.1.0 - Add fix available date information to vulnerability range object. This removes existing unused git-commit and date fields from the schema, but is a non-breaking change.
1213

1314
// VulnerabilitiesSchemaVersion is the schema version for the `db search vuln` command
1415
VulnerabilitiesSchemaVersion = "1.0.3"

grype/db/v6/blobs.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,39 @@ func (f Fix) String() string {
182182

183183
// FixDetail is additional information about a fix, such as commit details and patch URLs.
184184
type FixDetail struct {
185-
// GitCommit is the identifier for the Git commit associated with the fix.
186-
GitCommit string `json:"git_commit,omitempty"`
187-
188-
// Timestamp is the date and time when the fix was committed.
189-
Timestamp *time.Time `json:"timestamp,omitempty"`
185+
// Available indicates when the fix information became available and how it was obtained.
186+
Available *FixAvailability `json:"available,omitempty"`
190187

191188
// References contains URLs or identifiers for additional resources on the fix.
192189
References []Reference `json:"references,omitempty"`
193190
}
194191

192+
type FixAvailability struct {
193+
// Date is the date and time when fix information became available. Note: this might not be when the fix was created, committed or merged.
194+
Date *time.Time `json:"date,omitempty"`
195+
196+
// Kind describes how this date was obtained (e.g. advisory, release, commit, PR, issue, first-observed-record)
197+
Kind string `json:"kind,omitempty"`
198+
}
199+
200+
func (f FixAvailability) MarshalJSON() ([]byte, error) {
201+
type Alias FixAvailability
202+
aux := &struct {
203+
Date *string `json:"date,omitempty"`
204+
*Alias
205+
}{
206+
Alias: (*Alias)(&f),
207+
}
208+
209+
// the JSON marshaller should interpret the time.Time as a Date, not a timestamp
210+
if f.Date != nil {
211+
dateStr := f.Date.Format("2006-01-02")
212+
aux.Date = &dateStr
213+
}
214+
215+
return json.Marshal(aux)
216+
}
217+
195218
// AffectedVersion defines the versioning format and constraints.
196219
type AffectedVersion struct {
197220
// Type specifies the versioning system used (e.g., "semver", "rpm").

grype/db/v6/db.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ const (
2121
ModelVersion = 6
2222

2323
// Revision indicates how many changes have been introduced which **may** prevent interaction with some historical data
24-
Revision = 0
24+
Revision = 1
2525

2626
// Addition indicates how many changes have been introduced that are compatible with all historical data
27-
Addition = 3
27+
Addition = 0
2828

2929
// v6 model changelog:
3030
// 6.0.0: Initial version 🎉
3131
// 6.0.1: Add CISA KEV to VulnerabilityDecorator store
3232
// 6.0.2: Add EPSS to VulnerabilityDecorator store
3333
// 6.0.3: Add channel column to OperatingSystem model
34+
// 6.1.0: Add Fix availability information to AffectedPackageBlob.AffectedRange.Fix.Detail.
35+
// Existing git commit and timestamp information was removed (as it was unused)
3436
)
3537

3638
const (

grype/presenter/models/vulnerability.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ type Vulnerability struct {
1616
}
1717

1818
type Fix struct {
19-
Versions []string `json:"versions"`
20-
State string `json:"state"`
19+
Versions []string `json:"versions"`
20+
State string `json:"state"`
21+
Available *FixAvailable `json:"available,omitempty"`
22+
}
23+
24+
type FixAvailable struct {
25+
Date string `json:"date"`
26+
Kind string `json:"kind"`
2127
}
2228

2329
type Advisory struct {
@@ -49,14 +55,26 @@ func NewVulnerability(vuln vulnerability.Vulnerability, metadata *vulnerability.
4955
return Vulnerability{
5056
VulnerabilityMetadata: NewVulnerabilityMetadata(vuln.ID, vuln.Namespace, metadata),
5157
Fix: Fix{
52-
Versions: sortVersions(fixedInVersions, versionFormat),
53-
State: string(vuln.Fix.State),
58+
Versions: sortVersions(fixedInVersions, versionFormat),
59+
State: string(vuln.Fix.State),
60+
Available: getFixAvailable(vuln.Fix.Available),
5461
},
5562
Advisories: advisories,
5663
Risk: metadata.RiskScore(),
5764
}
5865
}
5966

67+
func getFixAvailable(fixAvailable *vulnerability.FixAvailable) *FixAvailable {
68+
if fixAvailable == nil {
69+
return nil
70+
}
71+
72+
return &FixAvailable{
73+
Date: fixAvailable.Date.Format("2006-01-02"), // just extract the date
74+
Kind: fixAvailable.Kind,
75+
}
76+
}
77+
6078
func sortVersions(fixedVersions []string, format version.Format) []string {
6179
if len(fixedVersions) <= 1 {
6280
return fixedVersions

grype/vex/csaf/implementation_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package csaf
22

33
import (
4-
"github.com/google/go-cmp/cmp"
5-
"github.com/stretchr/testify/require"
64
"slices"
75
"testing"
86

97
"github.com/gocsaf/csaf/v3/csaf"
8+
"github.com/google/go-cmp/cmp"
109
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
1111

1212
"github.com/anchore/grype/grype/match"
1313
"github.com/anchore/grype/grype/pkg"

grype/vulnerability/fix.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package vulnerability
22

3+
import "time"
4+
35
type FixState string
46

57
const (
@@ -19,8 +21,14 @@ func AllFixStates() []FixState {
1921
}
2022

2123
type Fix struct {
22-
Versions []string
23-
State FixState
24+
Versions []string
25+
State FixState
26+
Available *FixAvailable
27+
}
28+
29+
type FixAvailable struct {
30+
Date time.Time
31+
Kind string
2432
}
2533

2634
func (f FixState) String() string {

0 commit comments

Comments
 (0)