Skip to content

Commit

Permalink
rpm: change RepositoryHint to use URL formatting
Browse files Browse the repository at this point in the history
This makes things easier to reason about and removes the ad-hoc parsing
that used to happen.

Signed-off-by: Hank Donnay <[email protected]>
  • Loading branch information
hdonnay committed Apr 5, 2023
1 parent 7e08e21 commit 9284a22
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 209 deletions.
18 changes: 7 additions & 11 deletions rpm/native_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"fmt"
"io"
"net/url"
"runtime/trace"
"strings"

Expand Down Expand Up @@ -63,7 +64,7 @@ func packagesFromDB(ctx context.Context, pkgdb string, db nativeDB) ([]*claircor
p.Module = info.Module
}
p.Version = constructEVR(&b, &info)
p.RepositoryHint = constructHint(&b, &info)
p.RepositoryHint = constructHint(&info)

if s, ok := src[info.SourceNEVR]; ok {
p.Source = s
Expand Down Expand Up @@ -174,14 +175,12 @@ func constructEVR(b *strings.Builder, info *Info) string {
return b.String()
}

func constructHint(b *strings.Builder, info *Info) string {
b.Reset()
func constructHint(info *Info) string {
v := url.Values{}
if info.Digest != "" {
b.WriteString("hash:")
switch info.DigestAlgo {
case 8:
b.WriteString("sha256:")
b.WriteString(info.Digest)
v.Add("hash", fmt.Sprintf("sha256:%s", info.Digest))
}
}
if len(info.Signature) != 0 {
Expand All @@ -193,12 +192,9 @@ func constructHint(b *strings.Builder, info *Info) string {
if p.SigType != 0 {
continue
}
if b.Len() != 0 {
b.WriteByte('|')
}
fmt.Fprintf(b, "key:%016x", p.IssuerKeyId)
v.Add("key", fmt.Sprintf("%016x", p.IssuerKeyId))
}
}
}
return b.String()
return v.Encode()
}
33 changes: 32 additions & 1 deletion rpm/packagescanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package rpm

import (
"context"
"errors"
"fmt"
"io"
"io/fs"
"net/url"
"os"
"path"
"runtime/trace"
Expand All @@ -22,7 +24,7 @@ import (
const (
pkgName = "rpm"
pkgKind = "package"
pkgVersion = "9"
pkgVersion = "10"
)

var (
Expand Down Expand Up @@ -173,6 +175,35 @@ func (ps *Scanner) Scan(ctx context.Context, layer *claircore.Layer) ([]*clairco
}
pkgs = append(pkgs, ps...)
}
rm, err := repoMap(ctx, sys)
switch {
case errors.Is(err, nil) && len(rm) != 0: // OK
for _, pkg := range pkgs {
nerva := fmt.Sprintf("%s-%s.%s", pkg.Name, pkg.Version, pkg.Arch)
repoid, ok := rm[nerva]
if !ok {
// Packages not installed via dnf, which may happen during
// bootstrapping, aren't present in the dnf history database.
// This means the process shouldn't bail if the package is
// missing.
continue
}
v, err := url.ParseQuery(pkg.RepositoryHint)
if err != nil { // Shouldn't happen:
zlog.Warn(ctx).
AnErr("url.ParseQuery", err).
Msg("malformed RepositoryHint")
continue
}
v.Add("repoid", repoid)
pkg.RepositoryHint = v.Encode()
}
case errors.Is(err, nil) && len(rm) == 0: // nothing found
default: // some error
zlog.Warn(ctx).
AnErr("repoMap", err).
Msg("unable to open dnf history database")
}

return pkgs, nil
}
Expand Down
Loading

0 comments on commit 9284a22

Please sign in to comment.