Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Rename OperatingSystemAliases #2352

Merged
merged 1 commit into from
Jan 8, 2025
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
4 changes: 2 additions & 2 deletions grype/db/v6/affected_package_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ func (s *affectedPackageStore) applyAlias(d *OSSpecifier) error {
return nil
}

var aliases []OperatingSystemAlias
var aliases []OperatingSystemSpecifierOverride
err := s.db.Where("alias = ?", d.Name).Find(&aliases).Error
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
Expand All @@ -481,7 +481,7 @@ func (s *affectedPackageStore) applyAlias(d *OSSpecifier) error {
return nil
}

var alias *OperatingSystemAlias
var alias *OperatingSystemSpecifierOverride

for _, a := range aliases {
if a.Codename != "" && a.Codename != d.LabelVersion {
Expand Down
13 changes: 7 additions & 6 deletions grype/db/v6/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Models() []any {
// package related search tables
&AffectedPackageHandle{}, // join on package, operating system
&OperatingSystem{},
&OperatingSystemAlias{},
&OperatingSystemSpecifierOverride{},
&Package{},

// CPE related search tables
Expand Down Expand Up @@ -297,8 +297,9 @@ func (os *OperatingSystem) BeforeCreate(tx *gorm.DB) (err error) {
return nil
}

type OperatingSystemAlias struct {
// Name is alias name for the operating system.
// OperatingSystemSpecifierOverride is a table that allows for overriding fields on v6.OSSpecifier instances when searching for specific OperatingSystems.
type OperatingSystemSpecifierOverride struct {
// Alias is an alternative name/ID for the operating system.
Alias string `gorm:"column:alias;primaryKey;index:os_alias_idx"`

// Version is the matching version as found in the VERSION_ID field if the /etc/os-release file
Expand All @@ -320,11 +321,11 @@ type OperatingSystemAlias struct {
}

// TODO: in a future iteration these should be raised up more explicitly by the vunnel providers
func KnownOperatingSystemAliases() []OperatingSystemAlias {
func KnownOperatingSystemSpecifierOverrides() []OperatingSystemSpecifierOverride {
strRef := func(s string) *string {
return &s
}
return []OperatingSystemAlias{
return []OperatingSystemSpecifierOverride{
{Alias: "centos", ReplacementName: strRef("rhel")},
{Alias: "rocky", ReplacementName: strRef("rhel")},
{Alias: "rockylinux", ReplacementName: strRef("rhel")}, // non-standard, but common (dockerhub uses "rockylinux")
Expand Down Expand Up @@ -358,7 +359,7 @@ func KnownOperatingSystemAliases() []OperatingSystemAlias {
}
}

func (os *OperatingSystemAlias) BeforeCreate(_ *gorm.DB) (err error) {
func (os *OperatingSystemSpecifierOverride) BeforeCreate(_ *gorm.DB) (err error) {
if os.Version != "" && os.VersionPattern != "" {
return fmt.Errorf("cannot have both version and version_pattern set")
}
Expand Down
8 changes: 4 additions & 4 deletions grype/db/v6/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func TestOperatingSystemAlias_VersionMutualExclusivity(t *testing.T) {

tests := []struct {
name string
input *OperatingSystemAlias
input *OperatingSystemSpecifierOverride
errMsg string
}{
{
name: "version and version_pattern are mutually exclusive",
input: &OperatingSystemAlias{
input: &OperatingSystemSpecifierOverride{
Alias: "ubuntu",
Version: "20.04",
VersionPattern: "20.*",
Expand All @@ -28,15 +28,15 @@ func TestOperatingSystemAlias_VersionMutualExclusivity(t *testing.T) {
},
{
name: "only version is set",
input: &OperatingSystemAlias{
input: &OperatingSystemSpecifierOverride{
Alias: "ubuntu",
Version: "20.04",
},
errMsg: "",
},
{
name: "only version_pattern is set",
input: &OperatingSystemAlias{
input: &OperatingSystemSpecifierOverride{
Alias: "ubuntu",
VersionPattern: "20.*",
},
Expand Down
2 changes: 1 addition & 1 deletion grype/db/v6/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type store struct {
}

func InitialData() []any {
d := KnownOperatingSystemAliases()
d := KnownOperatingSystemSpecifierOverrides()
var data []any
for _, v := range d {
data = append(data, &v)
Expand Down
Loading