Skip to content
Open
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
29 changes: 12 additions & 17 deletions internal/archive/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,14 @@ import (
"github.com/canonical/chisel/internal/control"
"github.com/canonical/chisel/internal/deb"
"github.com/canonical/chisel/internal/pgputil"
"github.com/canonical/chisel/internal/pkgutil"
)

type Archive interface {
Options() *Options
Fetch(pkg string) (io.ReadSeekCloser, *PackageInfo, error)
Fetch(pkg string) (io.ReadSeekCloser, *pkgutil.Info, error)
Exists(pkg string) bool
Info(pkg string) (*PackageInfo, error)
}

type PackageInfo struct {
Name string
Version string
Arch string
SHA256 string
Info(pkg string) (*pkgutil.Info, error)
}

type Options struct {
Expand Down Expand Up @@ -133,7 +127,7 @@ func (a *ubuntuArchive) selectPackage(pkg string) (control.Section, *ubuntuIndex
return selectedSection, selectedIndex, nil
}

func (a *ubuntuArchive) Fetch(pkg string) (io.ReadSeekCloser, *PackageInfo, error) {
func (a *ubuntuArchive) Fetch(pkg string) (io.ReadSeekCloser, *pkgutil.Info, error) {
section, index, err := a.selectPackage(pkg)
if err != nil {
return nil, nil, err
Expand All @@ -148,7 +142,7 @@ func (a *ubuntuArchive) Fetch(pkg string) (io.ReadSeekCloser, *PackageInfo, erro
return reader, info, nil
}

func (a *ubuntuArchive) Info(pkg string) (*PackageInfo, error) {
func (a *ubuntuArchive) Info(pkg string) (*pkgutil.Info, error) {
section, _, err := a.selectPackage(pkg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -466,12 +460,13 @@ func (index *ubuntuIndex) fetch(path, digest string, flags fetchFlags) (io.ReadS
return index.archive.cache.Open(digestKind, writer.Digest())
}

func sectionPackageInfo(section control.Section) *PackageInfo {
return &PackageInfo{
Name: section.Get("Package"),
Version: section.Get("Version"),
Arch: section.Get("Architecture"),
SHA256: section.Get("SHA256"),
func sectionPackageInfo(section control.Section) *pkgutil.Info {
return &pkgutil.Info{
Name: section.Get("Package"),
Version: section.Get("Version"),
Arch: section.Get("Architecture"),
DigestKind: cache.SHA256,
Digest: section.Get("SHA256"),
}
}

Expand Down
81 changes: 45 additions & 36 deletions internal/archive/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

"github.com/canonical/chisel/internal/archive"
"github.com/canonical/chisel/internal/archive/testarchive"
"github.com/canonical/chisel/internal/cache"
"github.com/canonical/chisel/internal/pkgutil"
"github.com/canonical/chisel/internal/tarball"
"github.com/canonical/chisel/internal/testutil"
)
Expand Down Expand Up @@ -244,22 +246,24 @@ func (s *httpSuite) TestFetchPackage(c *C) {
// First on component main.
pkg, info, err := testArchive.Fetch("mypkg1")
c.Assert(err, IsNil)
c.Assert(info, DeepEquals, &archive.PackageInfo{
Name: "mypkg1",
Version: "1.1",
Arch: "amd64",
SHA256: "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05",
c.Assert(info, DeepEquals, &pkgutil.Info{
Name: "mypkg1",
Version: "1.1",
Arch: "amd64",
DigestKind: cache.SHA256,
Digest: "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05",
})
c.Assert(read(pkg), Equals, "mypkg1 1.1 data")

// Last on component universe.
pkg, info, err = testArchive.Fetch("mypkg4")
c.Assert(err, IsNil)
c.Assert(info, DeepEquals, &archive.PackageInfo{
Name: "mypkg4",
Version: "1.4",
Arch: "amd64",
SHA256: "54af70097b30b33cfcbb6911ad3d0df86c2d458928169e348fa7873e4fc678e4",
c.Assert(info, DeepEquals, &pkgutil.Info{
Name: "mypkg4",
Version: "1.4",
Arch: "amd64",
DigestKind: cache.SHA256,
Digest: "54af70097b30b33cfcbb6911ad3d0df86c2d458928169e348fa7873e4fc678e4",
})
c.Assert(read(pkg), Equals, "mypkg4 1.4 data")
}
Expand All @@ -286,22 +290,24 @@ func (s *httpSuite) TestFetchPortsPackage(c *C) {
// First on component main.
pkg, info, err := testArchive.Fetch("mypkg1")
c.Assert(err, IsNil)
c.Assert(info, DeepEquals, &archive.PackageInfo{
Name: "mypkg1",
Version: "1.1",
Arch: "arm64",
SHA256: "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05",
c.Assert(info, DeepEquals, &pkgutil.Info{
Name: "mypkg1",
Version: "1.1",
Arch: "arm64",
DigestKind: cache.SHA256,
Digest: "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05",
})
c.Assert(read(pkg), Equals, "mypkg1 1.1 data")

// Last on component universe.
pkg, info, err = testArchive.Fetch("mypkg4")
c.Assert(err, IsNil)
c.Assert(info, DeepEquals, &archive.PackageInfo{
Name: "mypkg4",
Version: "1.4",
Arch: "arm64",
SHA256: "54af70097b30b33cfcbb6911ad3d0df86c2d458928169e348fa7873e4fc678e4",
c.Assert(info, DeepEquals, &pkgutil.Info{
Name: "mypkg4",
Version: "1.4",
Arch: "arm64",
DigestKind: cache.SHA256,
Digest: "54af70097b30b33cfcbb6911ad3d0df86c2d458928169e348fa7873e4fc678e4",
})
c.Assert(read(pkg), Equals, "mypkg4 1.4 data")
}
Expand Down Expand Up @@ -335,21 +341,23 @@ func (s *httpSuite) TestFetchSecurityPackage(c *C) {

pkg, info, err := testArchive.Fetch("mypkg1")
c.Assert(err, IsNil)
c.Assert(info, DeepEquals, &archive.PackageInfo{
Name: "mypkg1",
Version: "1.1.2.2",
Arch: "amd64",
SHA256: "5448585bdd916e5023eff2bc1bc3b30bcc6ee9db9c03e531375a6a11ddf0913c",
c.Assert(info, DeepEquals, &pkgutil.Info{
Name: "mypkg1",
Version: "1.1.2.2",
Arch: "amd64",
DigestKind: cache.SHA256,
Digest: "5448585bdd916e5023eff2bc1bc3b30bcc6ee9db9c03e531375a6a11ddf0913c",
})
c.Assert(read(pkg), Equals, "package from jammy-security")

pkg, info, err = testArchive.Fetch("mypkg2")
c.Assert(err, IsNil)
c.Assert(info, DeepEquals, &archive.PackageInfo{
Name: "mypkg2",
Version: "1.2",
Arch: "amd64",
SHA256: "a4b4f3f3a8fa09b69e3ba23c60a41a1f8144691fd371a2455812572fd02e6f79",
c.Assert(info, DeepEquals, &pkgutil.Info{
Name: "mypkg2",
Version: "1.2",
Arch: "amd64",
DigestKind: cache.SHA256,
Digest: "a4b4f3f3a8fa09b69e3ba23c60a41a1f8144691fd371a2455812572fd02e6f79",
})
c.Assert(read(pkg), Equals, "mypkg2 1.2 data")
}
Expand Down Expand Up @@ -585,16 +593,17 @@ func (s *httpSuite) TestVerifyArchiveRelease(c *C) {
var packageInfoTests = []struct {
summary string
pkg string
info *archive.PackageInfo
info *pkgutil.Info
error string
}{{
summary: "Basic",
pkg: "mypkg1",
info: &archive.PackageInfo{
Name: "mypkg1",
Version: "1.1",
Arch: "amd64",
SHA256: "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05",
info: &pkgutil.Info{
Name: "mypkg1",
Version: "1.1",
Arch: "amd64",
DigestKind: cache.SHA256,
Digest: "1f08ef04cfe7a8087ee38a1ea35fa1810246648136c3c42d5a61ad6503d85e05",
},
}, {
summary: "Package not found in archive",
Expand Down
27 changes: 20 additions & 7 deletions internal/manifestutil/manifestutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
"strings"

"github.com/canonical/chisel/internal/apacheutil"
"github.com/canonical/chisel/internal/archive"
"github.com/canonical/chisel/internal/cache"
"github.com/canonical/chisel/internal/pkgutil"
"github.com/canonical/chisel/internal/setup"
"github.com/canonical/chisel/public/jsonwall"
"github.com/canonical/chisel/public/manifest"
Expand All @@ -35,7 +36,7 @@ func FindPaths(slices []*setup.Slice) map[string][]*setup.Slice {
}

type WriteOptions struct {
PackageInfo []*archive.PackageInfo
PackageInfo []*pkgutil.Info
Selection []*setup.Slice
Report *Report
}
Expand Down Expand Up @@ -69,13 +70,13 @@ func Write(options *WriteOptions, writer io.Writer) error {
return err
}

func manifestAddPackages(dbw *jsonwall.DBWriter, infos []*archive.PackageInfo) error {
func manifestAddPackages(dbw *jsonwall.DBWriter, infos []*pkgutil.Info) error {
for _, info := range infos {
err := dbw.Add(&manifest.Package{
Kind: "package",
Name: info.Name,
Version: info.Version,
Digest: info.SHA256,
Digest: info.Digest,
Arch: info.Arch,
})
if err != nil {
Expand Down Expand Up @@ -250,19 +251,31 @@ func validateReportEntry(entry *ReportEntry) (err error) {
return nil
}

func validatePackage(pkg *archive.PackageInfo) (err error) {
func validatePackage(pkg *pkgutil.Info) (err error) {
if pkg.Name == "" {
return fmt.Errorf("package name not set")
}
if pkg.Arch == "" {
return fmt.Errorf("package %q missing arch", pkg.Name)
}
if pkg.SHA256 == "" {
return fmt.Errorf("package %q missing sha256", pkg.Name)
// The manifest records the package digest as a SHA256 one. Fail rather than
// recording a digest of another kind under that name.
// TODO: record packages whose digest is not a SHA256 one, such as the ones
// coming from a store. This also requires recording the release unique
// package name instead of the source one, as slices are recorded with the
// former.
if pkg.DigestKind != cache.SHA256 {
return fmt.Errorf("package %q has unsupported digest kind %q", pkg.Name, pkg.DigestKind)
}
if pkg.Digest == "" {
return fmt.Errorf("package %q missing digest", pkg.Name)
}
if pkg.Version == "" {
return fmt.Errorf("package %q missing version", pkg.Name)
}
if pkg.Revision < 0 {
return fmt.Errorf("package %q has invalid revision", pkg.Name)
}
return nil
}

Expand Down
Loading
Loading