Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add initial os aliases to the DB after DB v6 migration #2301

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions grype/db/v6/affected_package_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -686,20 +686,11 @@ func TestAffectedPackageStore_GetAffectedPackages(t *testing.T) {
}

func TestAffectedPackageStore_ResolveDistro(t *testing.T) {
// we always preload the OS aliases into the DB when staging for writing
db := setupTestStore(t).db
bs := newBlobStore(db)
s := newAffectedPackageStore(db, bs)

aliases := []OperatingSystemAlias{
{Name: "centos", ReplacementName: strRef("rhel")},
{Name: "rocky", ReplacementName: strRef("rhel")},
{Name: "alpine", VersionPattern: ".*_alpha.*", ReplacementLabelVersion: strRef("edge"), Rolling: true},
{Name: "wolfi", Rolling: true},
{Name: "arch", Rolling: true},
{Name: "debian", Codename: "trixie", Rolling: true}, // is currently sid, which is considered rolling
}
require.NoError(t, db.Create(&aliases).Error)

ubuntu2004 := &OperatingSystem{Name: "ubuntu", MajorVersion: "20", MinorVersion: "04", Codename: "focal"}
ubuntu2010 := &OperatingSystem{Name: "ubuntu", MajorVersion: "20", MinorVersion: "10", Codename: "groovy"}
rhel8 := &OperatingSystem{Name: "rhel", MajorVersion: "8"}
Expand Down
31 changes: 31 additions & 0 deletions grype/db/v6/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,37 @@ type OperatingSystemAlias struct {
Rolling bool `gorm:"column:rolling;primaryKey"`
}

func KnownOperatingSystemAliases() []OperatingSystemAlias {
strRef := func(s string) *string {
return &s
}
return []OperatingSystemAlias{
{Name: "centos", ReplacementName: strRef("rhel")},
{Name: "rocky", ReplacementName: strRef("rhel")},
{Name: "almalinux", ReplacementName: strRef("rhel")},
{Name: "gentoo", ReplacementName: strRef("rhel")},
{Name: "alpine", VersionPattern: ".*_alpha.*", ReplacementLabelVersion: strRef("edge"), Rolling: true},
{Name: "wolfi", Rolling: true},
{Name: "arch", Rolling: true},
// TODO: trixie is a placeholder for now, but should be updated to sid when the time comes
// this needs to be automated, but isn't clear how to do so since you'll see things like this:
//
// ❯ docker run --rm debian:sid cat /etc/os-release | grep VERSION_CODENAME
// VERSION_CODENAME=trixie
// ❯ docker run --rm debian:testing cat /etc/os-release | grep VERSION_CODENAME
// VERSION_CODENAME=trixie
//
// ❯ curl -s http://deb.debian.org/debian/dists/testing/Release | grep '^Codename:'
// Codename: trixie
// ❯ curl -s http://deb.debian.org/debian/dists/sid/Release | grep '^Codename:'
// Codename: sid
//
// depending where the team is during the development cycle you will see different behavior, making automating
// this a little challenging.
{Name: "debian", Codename: "trixie", Rolling: true}, // is currently sid, which is considered rolling
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we also need an entry for sid?

}
}

func (os *OperatingSystemAlias) BeforeCreate(_ *gorm.DB) (err error) {
if os.Version != "" && os.VersionPattern != "" {
return fmt.Errorf("cannot have both version and version_pattern set")
Expand Down
7 changes: 7 additions & 0 deletions grype/db/v6/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func newStore(cfg Config, write bool) (*store, error) {
return nil, fmt.Errorf("failed to open db: %w", err)
}

if write {
// add hard-coded os aliases
if err := db.Create(KnownOperatingSystemAliases()).Error; err != nil {
return nil, fmt.Errorf("failed to add os aliases: %w", err)
}
}

bs := newBlobStore(db)
return &store{
dbMetadataStore: newDBMetadataStore(db),
Expand Down
Loading