From d5771272f05f3a936a37e9a55e9d92438bd208e2 Mon Sep 17 00:00:00 2001 From: Alex Goodman Date: Wed, 4 Dec 2024 15:32:35 -0500 Subject: [PATCH] add initial os aliases to the DB after migration Signed-off-by: Alex Goodman --- grype/db/v6/affected_package_store_test.go | 11 +------- grype/db/v6/models.go | 31 ++++++++++++++++++++++ grype/db/v6/store.go | 7 +++++ 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/grype/db/v6/affected_package_store_test.go b/grype/db/v6/affected_package_store_test.go index 280615add82..e3d7b877100 100644 --- a/grype/db/v6/affected_package_store_test.go +++ b/grype/db/v6/affected_package_store_test.go @@ -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"} diff --git a/grype/db/v6/models.go b/grype/db/v6/models.go index 910f021350d..3a4dbf30c9e 100644 --- a/grype/db/v6/models.go +++ b/grype/db/v6/models.go @@ -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 + } +} + func (os *OperatingSystemAlias) BeforeCreate(_ *gorm.DB) (err error) { if os.Version != "" && os.VersionPattern != "" { return fmt.Errorf("cannot have both version and version_pattern set") diff --git a/grype/db/v6/store.go b/grype/db/v6/store.go index d96d94a889e..1624bd57930 100644 --- a/grype/db/v6/store.go +++ b/grype/db/v6/store.go @@ -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),