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

feat(azure): Add Azure Linux support #409

Merged
merged 5 commits into from
Jul 18, 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
81 changes: 60 additions & 21 deletions pkg/vulnsrc/mariner/mariner.go → pkg/vulnsrc/azure/azure.go
Copy link
Contributor

Choose a reason for hiding this comment

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

azure is a renamed mariner.
So I think we can use one package for them:

➜  trivy-db git:(azure-linux) ✗ ls -hl ./pkg/vulnsrc/azure 
total 36K
-rw-rw-r-- 1 dmitriy dmitriy  690 июл  2 13:12 azure.go
-rw-rw-r-- 1 dmitriy dmitriy 2,4K июл  2 13:12 azure_test.go
-rw-rw-r-- 1 dmitriy dmitriy 6,9K июл  2 13:39 mariner.go
-rw-rw-r-- 1 dmitriy dmitriy 6,1K июн 20 10:12 mariner_test.go
drwxrwxr-x 2 dmitriy dmitriy 4,0K июл  2 13:57 oval
drwxrwxr-x 4 dmitriy dmitriy 4,0K июл  2 13:12 testdata
-rw-rw-r-- 1 dmitriy dmitriy  206 окт 25  2022 types.go

I suggest the following refactoring:

  1. move logic from mariner.go to azure.go
  2. create only NewMarinerVulnSrc() function in mariner.go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've done this refactoring. Happy to rebase and git mv the mariner package if it makes reviewing easier.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mariner
package azure

import (
"fmt"
Expand All @@ -12,21 +12,37 @@ import (

"github.com/aquasecurity/trivy-db/pkg/db"
"github.com/aquasecurity/trivy-db/pkg/types"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/mariner/oval"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/azure/oval"
"github.com/aquasecurity/trivy-db/pkg/vulnsrc/vulnerability"
)

type Distribution int

const (
Azure Distribution = iota
Mariner

azureDir = "azure"
azurePlatformFormat = "Azure Linux %s"

marinerDir = "mariner"
marinerPlatformFormat = "CBL-Mariner %s"
)

var (
cblDir = filepath.Join("mariner")
platformFormat = "CBL-Mariner %s"
ErrNotSupported = xerrors.New("format not supported")

azureSource = types.DataSource{
ID: vulnerability.AzureLinux,
Name: "Azure Linux Vulnerability Data",
URL: "https://github.com/microsoft/AzureLinuxVulnerabilityData",
}

source = types.DataSource{
marinerSource = types.DataSource{
ID: vulnerability.CBLMariner,
Name: "CBL-Mariner Vulnerability Data",
URL: "https://github.com/microsoft/CBL-MarinerVulnerabilityData",
URL: "https://github.com/microsoft/AzureLinuxVulnerabilityData",
}

ErrNotSupported = xerrors.New("format not supported")
)

type resolvedTest struct {
Expand All @@ -36,21 +52,44 @@ type resolvedTest struct {
}

type VulnSrc struct {
dbc db.Operation
dbc db.Operation
azureDir string
source types.DataSource
platformFormat string
}

func NewVulnSrc(dist Distribution) VulnSrc {
vulnSrc := azureVulnSrc()
if dist == Mariner {
vulnSrc = marinerVulnSrc()
}
return vulnSrc
}

func azureVulnSrc() VulnSrc {
return VulnSrc{
dbc: db.Config{},
azureDir: azureDir,
source: azureSource,
platformFormat: azurePlatformFormat,
}
}

func NewVulnSrc() VulnSrc {
func marinerVulnSrc() VulnSrc {
return VulnSrc{
dbc: db.Config{},
dbc: db.Config{},
azureDir: marinerDir,
source: marinerSource,
platformFormat: marinerPlatformFormat,
}
}

func (vs VulnSrc) Name() types.SourceID {
return source.ID
return vs.source.ID
}

func (vs VulnSrc) Update(dir string) error {
rootDir := filepath.Join(dir, "vuln-list", cblDir)
rootDir := filepath.Join(dir, "vuln-list", vs.azureDir)
versions, err := os.ReadDir(rootDir)
if err != nil {
return xerrors.Errorf("unable to list directory entries (%s): %w", rootDir, err)
Expand Down Expand Up @@ -186,13 +225,13 @@ func followTestRefs(test oval.RpmInfoTest, objects map[string]string, states map

func (vs VulnSrc) save(majorVer string, entries []Entry) error {
err := vs.dbc.BatchUpdate(func(tx *bolt.Tx) error {
platformName := fmt.Sprintf(platformFormat, majorVer)
if err := vs.dbc.PutDataSource(tx, platformName, source); err != nil {
platformName := fmt.Sprintf(vs.platformFormat, majorVer)
if err := vs.dbc.PutDataSource(tx, platformName, vs.source); err != nil {
return xerrors.Errorf("failed to put data source: %w", err)
}

if err := vs.commit(tx, platformName, entries); err != nil {
return xerrors.Errorf("CBL-Mariner %s commit error: %w", majorVer, err)
return xerrors.Errorf("%s commit error: %w", platformName, err)
}
return nil
})
Expand All @@ -216,7 +255,7 @@ func (vs VulnSrc) commit(tx *bolt.Tx, platformName string, entries []Entry) erro
}

if err := vs.dbc.PutAdvisoryDetail(tx, cveID, entry.PkgName, []string{platformName}, advisory); err != nil {
return xerrors.Errorf("failed to save CBL-Mariner advisory detail: %w", err)
return xerrors.Errorf("failed to save %s advisory detail: %w", platformName, err)
}

severity, _ := types.NewSeverity(strings.ToUpper(entry.Metadata.Severity))
Expand All @@ -226,8 +265,8 @@ func (vs VulnSrc) commit(tx *bolt.Tx, platformName string, entries []Entry) erro
Description: entry.Metadata.Description,
References: []string{entry.Metadata.Reference.RefURL},
}
if err := vs.dbc.PutVulnerabilityDetail(tx, cveID, source.ID, vuln); err != nil {
return xerrors.Errorf("failed to save CBL-Mariner vulnerability detail: %w", err)
if err := vs.dbc.PutVulnerabilityDetail(tx, cveID, vs.source.ID, vuln); err != nil {
return xerrors.Errorf("failed to save %s vulnerability detail: %w", platformName, err)
}

if err := vs.dbc.PutVulnerabilityID(tx, cveID); err != nil {
Expand All @@ -238,10 +277,10 @@ func (vs VulnSrc) commit(tx *bolt.Tx, platformName string, entries []Entry) erro
}

func (vs VulnSrc) Get(release, pkgName string) ([]types.Advisory, error) {
bucket := fmt.Sprintf(platformFormat, release)
bucket := fmt.Sprintf(vs.platformFormat, release)
advisories, err := vs.dbc.GetAdvisories(bucket, pkgName)
if err != nil {
return nil, xerrors.Errorf("failed to get CBL-Marina advisories: %w", err)
return nil, xerrors.Errorf("failed to get %s advisories: %w", bucket, err)
}
return advisories, nil
}
Loading