Skip to content

Commit

Permalink
✨ Add origin version to debian packages
Browse files Browse the repository at this point in the history
Some packages contain the version information for the source package.
Where we can find it, we add it.

Signed-off-by: Christian Zunker <[email protected]>
  • Loading branch information
czunker committed Feb 26, 2024
1 parent 39df8dc commit 357468d
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 27 deletions.
3 changes: 3 additions & 0 deletions providers/os/resources/os.lr
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,9 @@ package @defaults("name version") {

// Package origin (optional)
origin() string
// Package origin version (optional)
originVersion() string


// Available version
available string
Expand Down
14 changes: 14 additions & 0 deletions providers/os/resources/os.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions providers/os/resources/os.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,8 @@ resources:
installed: {}
name: {}
origin: {}
originVersion:
min_mondoo_version: latest
outdated: {}
purl:
min_mondoo_version: latest
Expand Down
33 changes: 20 additions & 13 deletions providers/os/resources/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package resources

import (
"errors"
"go.mondoo.com/cnquery/v10/types"
"regexp"
"sync"

"go.mondoo.com/cnquery/v10/types"

"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v10/llx"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/plugin"
Expand Down Expand Up @@ -74,6 +75,7 @@ func initPackage(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[str
res.Arch.State = plugin.StateIsSet | plugin.StateIsNull
res.Format.State = plugin.StateIsSet | plugin.StateIsNull
res.Origin.State = plugin.StateIsSet | plugin.StateIsNull
res.OriginVersion.State = plugin.StateIsSet | plugin.StateIsNull
res.Status.State = plugin.StateIsSet | plugin.StateIsNull
res.Files.State = plugin.StateIsSet | plugin.StateIsNull
return nil, res, nil
Expand All @@ -94,6 +96,10 @@ func (p *mqlPackage) origin() (string, error) {
return "", nil
}

func (p *mqlPackage) originVersion() (string, error) {
return "", nil
}

func (p *mqlPackage) files() ([]interface{}, error) {
if p.filesState == packages.PkgFilesNotAvailable {
return nil, nil
Expand Down Expand Up @@ -189,18 +195,19 @@ func (x *mqlPackages) list() ([]interface{}, error) {
}

pkg, err := CreateResource(x.MqlRuntime, "package", map[string]*llx.RawData{
"name": llx.StringData(osPkg.Name),
"version": llx.StringData(osPkg.Version),
"available": llx.StringData(available),
"arch": llx.StringData(osPkg.Arch),
"status": llx.StringData(osPkg.Status),
"description": llx.StringData(osPkg.Description),
"format": llx.StringData(osPkg.Format),
"installed": llx.BoolData(true),
"origin": llx.StringData(osPkg.Origin),
"epoch": llx.StringData(osPkg.Epoch),
"purl": llx.StringData(osPkg.PUrl),
"cpes": llx.ArrayData(cpes, types.Resource("cpe")),
"name": llx.StringData(osPkg.Name),
"version": llx.StringData(osPkg.Version),
"available": llx.StringData(available),
"arch": llx.StringData(osPkg.Arch),
"status": llx.StringData(osPkg.Status),
"description": llx.StringData(osPkg.Description),
"format": llx.StringData(osPkg.Format),
"installed": llx.BoolData(true),
"origin": llx.StringData(osPkg.Origin),
"originVersion": llx.StringData(osPkg.OriginVersion),
"epoch": llx.StringData(osPkg.Epoch),
"purl": llx.StringData(osPkg.PUrl),
"cpes": llx.ArrayData(cpes, types.Resource("cpe")),
})
if err != nil {
return nil, err
Expand Down
18 changes: 12 additions & 6 deletions providers/os/resources/packages/dpkg_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ package packages
import (
"bufio"
"fmt"
"github.com/package-url/packageurl-go"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v10/providers/os/resources/cpe"
"go.mondoo.com/cnquery/v10/providers/os/resources/purl"
"io"
"os"
"regexp"
"strings"

"github.com/package-url/packageurl-go"
"go.mondoo.com/cnquery/v10/providers-sdk/v1/inventory"
"go.mondoo.com/cnquery/v10/providers/os/resources/cpe"
"go.mondoo.com/cnquery/v10/providers/os/resources/purl"

"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"go.mondoo.com/cnquery/v10/providers/os/connection/shared"
Expand All @@ -25,7 +26,8 @@ const (
)

var (
DPKG_REGEX = regexp.MustCompile(`^(.+):\s(.+)$`)
DPKG_REGEX = regexp.MustCompile(`^(.+):\s(.+)$`)
// e.g. source with version: samba (2:4.17.12+dfsg-0+deb12u1)
DPKG_ORIGIN_REGEX = regexp.MustCompile(`^\s*([^\(]*)(?:\((.*)\))?\s*$`)
)

Expand Down Expand Up @@ -80,11 +82,15 @@ func ParseDpkgPackages(pf *inventory.Platform, input io.Reader) ([]Package, erro
pkg.Status = strings.TrimSpace(m[2])
case key == "Source":
o := DPKG_ORIGIN_REGEX.FindStringSubmatch(m[2])
if o != nil && len(o) >= 1 {
if len(o) >= 1 {
pkg.Origin = strings.TrimSpace(o[1])
} else {
log.Error().Str("origin", m[2]).Msg("cannot parse dpkg origin")
}
// Some packages also have a version as part of the Source field
if len(o) >= 2 {
pkg.OriginVersion = strings.TrimSpace(o[2])
}
// description supports multi-line statements, start desc
case key == "Description":
pkg.Description = strings.TrimSpace(m[2])
Expand Down
13 changes: 7 additions & 6 deletions providers/os/resources/packages/dpkg_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ The sfdisk utility is mostly for automation and scripting uses.`,
assert.Equal(t, findPkg(m, p.Name), p, p.Name)

p = Package{
Name: "libaudit1",
Version: "1:2.4-1+b1",
Arch: "amd64",
Status: "install ok installed",
Origin: "audit",
Name: "libaudit1",
Version: "1:2.4-1+b1",
Arch: "amd64",
Status: "install ok installed",
Origin: "audit",
OriginVersion: "1:2.4-1",
Description: `Dynamic library for security auditing
The audit-libs package contains the dynamic libraries needed for
applications to use the audit framework. It is used to monitor systems for
Expand All @@ -71,7 +72,7 @@ security related events.`,
Format: "deb",
FilesAvailable: PkgFilesAsync,
}
assert.Equal(t, findPkg(m, p.Name), p, p.Name)
assert.Equal(t, p, findPkg(m, p.Name), p.Name)

p = Package{
Name: "libss2",
Expand Down
5 changes: 3 additions & 2 deletions providers/os/resources/packages/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ type Package struct {
// this may be the source package or an origin
// e.g. on alpine it is used for parent packages
// o Package Origin - https://wiki.alpinelinux.org/wiki/Apk_spec
Origin string `json:"origin"`
Format string `json:"format"`
Origin string `json:"origin"`
OriginVersion string `json:"originVersion"`
Format string `json:"format"`

// Package Url follows https://github.com/package-url/purl-spec
PUrl string `json:"purl,omitempty"`
Expand Down

0 comments on commit 357468d

Please sign in to comment.