Skip to content

Commit

Permalink
Extract all dpkg packages regardless of the Status field and store th…
Browse files Browse the repository at this point in the history
…e value of Status in the metadata.

PiperOrigin-RevId: 630407306
  • Loading branch information
Yousef Alowayed authored and copybara-github committed May 3, 2024
1 parent e83bfdd commit d9a5b66
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 23 deletions.
1 change: 1 addition & 0 deletions binary/proto/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func setProtoMetadata(meta any, i *spb.Inventory) {
DpkgMetadata: &spb.DPKGPackageMetadata{
PackageName: m.PackageName,
SourceName: m.SourceName,
Status: m.Status,
SourceVersion: m.SourceVersion,
PackageVersion: m.PackageVersion,
OsId: m.OSID,
Expand Down
2 changes: 2 additions & 0 deletions binary/proto/scan_result.proto
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ message APKPackageMetadata {
}

// The additional data found in DPKG packages.
// Next ID: 11
message DPKGPackageMetadata {
string package_name = 1;
string source_name = 2;
Expand All @@ -210,6 +211,7 @@ message DPKGPackageMetadata {
string os_version_id = 7;
string maintainer = 8;
string architecture = 9;
string status = 10;
}

// The additional data found in RPM packages.
Expand Down
24 changes: 2 additions & 22 deletions extractor/os/dpkg/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,7 @@ func (e Extractor) Extract(ctx context.Context, input *extractor.ScanInput) ([]*
return pkgs, err
}
}
// Distroless distributions have their packages in status.d, which does not contain the Status
// value.
if !strings.Contains(input.Path, "status.d") || h.Get("Status") != "" {
installed, err := statusInstalled(h.Get("Status"))
if err != nil {
return pkgs, fmt.Errorf("statusInstalled(%q): %w", h.Get("Status"), err)
}
if !installed {
continue
}
}

pkgName := h.Get("Package")
pkgVersion := h.Get("Version")
if pkgName == "" || pkgVersion == "" {
Expand All @@ -153,6 +143,7 @@ func (e Extractor) Extract(ctx context.Context, input *extractor.ScanInput) ([]*
Metadata: &Metadata{
PackageName: pkgName,
PackageVersion: pkgVersion,
Status: h.Get("Status"),
OSID: m["ID"],
OSVersionCodename: m["VERSION_CODENAME"],
OSVersionID: m["VERSION_ID"],
Expand All @@ -176,17 +167,6 @@ func (e Extractor) Extract(ctx context.Context, input *extractor.ScanInput) ([]*
return pkgs, nil
}

func statusInstalled(status string) (bool, error) {
// Status field format: "want flag status", e.g. "install ok installed"
// The package is currently installed if the status field is set to installed.
// Other fields just show the intent of the package manager but not the current state.
parts := strings.Split(status, " ")
if len(parts) != 3 {
return false, fmt.Errorf("invalid DPKG Status field %q", status)
}
return parts[2] == "installed", nil
}

func parseSourceNameVersion(source string) (string, string, error) {
if source == "" {
return "", "", nil
Expand Down
89 changes: 88 additions & 1 deletion extractor/os/dpkg/extractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "accountsservice",
PackageVersion: "22.08.8-6",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -105,6 +106,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "acl",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -120,6 +122,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "adduser",
PackageVersion: "3.131",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -135,6 +138,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "admin-session",
PackageVersion: "2023.06.26.c543406313-00",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -150,6 +154,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "attr",
PackageVersion: "1:2.5.1-4",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -166,6 +171,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "libacl1",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
SourceName: "acl",
OSID: "debian",
OSVersionCodename: "bookworm",
Expand All @@ -183,6 +189,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "util-linux-extra",
PackageVersion: "2.38.1-5+b1",
Status: "install ok installed",
SourceName: "util-linux",
SourceVersion: "2.38.1-5",
OSID: "debian",
Expand All @@ -207,6 +214,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "foo",
PackageVersion: "1.0",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -220,6 +228,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "bar",
PackageVersion: "2.0",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -240,6 +249,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "foo",
PackageVersion: "1.0",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -253,6 +263,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "bar",
PackageVersion: "2.0",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -273,6 +284,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "wantinstall_installed",
PackageVersion: "1.0",
Status: "install ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -286,6 +298,35 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "wantdeinstall_installed",
PackageVersion: "1.0",
Status: "deinstall reinstreq installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
},
Locations: []string{"testdata/statusfield"},
Extractor: dpkg.Name,
},
&extractor.Inventory{
Name: "wantdeinstall_configfiles",
Version: "1.0",
Metadata: &dpkg.Metadata{
PackageName: "wantdeinstall_configfiles",
PackageVersion: "1.0",
Status: "deinstall ok config-files",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
},
Locations: []string{"testdata/statusfield"},
Extractor: dpkg.Name,
},
&extractor.Inventory{
Name: "wantinstall_unpacked",
Version: "1.0",
Metadata: &dpkg.Metadata{
PackageName: "wantinstall_unpacked",
PackageVersion: "1.0",
Status: "install ok unpacked",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -299,6 +340,34 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "wantpurge_installed",
PackageVersion: "1.0",
Status: "purge ok installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
},
Locations: []string{"testdata/statusfield"},
Extractor: dpkg.Name,
},
&extractor.Inventory{
Name: "wantinstall_halfinstalled",
Version: "1.0",
Metadata: &dpkg.Metadata{
PackageName: "wantinstall_halfinstalled",
PackageVersion: "1.0",
Status: "install reinstreq half-installed",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
},
Locations: []string{"testdata/statusfield"},
Extractor: dpkg.Name,
},
&extractor.Inventory{
Name: "wantnostatus",
Version: "1.0",
Metadata: &dpkg.Metadata{
PackageName: "wantnostatus",
PackageVersion: "1.0",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
Expand All @@ -313,7 +382,6 @@ func TestExtract(t *testing.T) {
path: "testdata/empty",
osrelease: DebianBookworm,
wantInventory: []*extractor.Inventory{},
wantErr: cmpopts.AnyError,
},
{
name: "invalid",
Expand All @@ -334,6 +402,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "acl",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
OSID: "debian",
OSVersionID: "12",
Maintainer: "Guillem Jover <[email protected]>",
Expand All @@ -355,6 +424,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "acl",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
OSID: "debian",
Maintainer: "Guillem Jover <[email protected]>",
Architecture: "amd64",
Expand All @@ -375,6 +445,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "acl",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
OSVersionCodename: "bookworm",
Maintainer: "Guillem Jover <[email protected]>",
Architecture: "amd64",
Expand All @@ -398,6 +469,7 @@ func TestExtract(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "acl",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
OSID: "ubuntu",
OSVersionCodename: "jammy",
OSVersionID: "22.04",
Expand Down Expand Up @@ -438,6 +510,20 @@ func TestExtract(t *testing.T) {
Locations: []string{"testdata/status.d/foo"},
Extractor: dpkg.Name,
},
&extractor.Inventory{
Name: "bar",
Version: "1.0",
Metadata: &dpkg.Metadata{
PackageName: "bar",
PackageVersion: "1.0",
Status: "deinstall ok config-files",
OSID: "debian",
OSVersionCodename: "bookworm",
OSVersionID: "12",
},
Locations: []string{"testdata/status.d/foo"},
Extractor: dpkg.Name,
},
},
},
}
Expand Down Expand Up @@ -489,6 +575,7 @@ func TestExtractNonexistentOSRelease(t *testing.T) {
Metadata: &dpkg.Metadata{
PackageName: "acl",
PackageVersion: "2.3.1-3",
Status: "install ok installed",
OSID: "",
OSVersionID: "",
Maintainer: "Guillem Jover <[email protected]>",
Expand Down
1 change: 1 addition & 0 deletions extractor/os/dpkg/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package dpkg
// Metadata holds parsing information for an dpkg package.
type Metadata struct {
PackageName string
Status string
SourceName string
SourceVersion string
PackageVersion string
Expand Down
3 changes: 3 additions & 0 deletions extractor/os/dpkg/testdata/statusfield
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ Version: 1.0
Package: wantinstall_halfinstalled
Status: install reinstreq half-installed
Version: 1.0

Package: wantnostatus
Version: 1.0

0 comments on commit d9a5b66

Please sign in to comment.