Skip to content
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ import _ "github.com/git-pkgs/registries/all"

The Alpine URL is one repository architecture directory, such as `https://dl-cdn.alpinelinux.org/alpine/v3.23/main/x86_64`, or the `APKINDEX.tar.gz` inside it. The client doesn't pick a release, component or architecture from a mirror root. It reads v2 `APKINDEX.tar.gz` indexes. When a directory serves a v3 `Packages.adb`, the client returns an error rather than reading the v2 index.

The Debian URL is an archive root with the suites, components and architectures to read as query parameters, which archives don't use: `https://deb.debian.org/debian?suites=bookworm,bookworm-updates&components=main&architectures=amd64`. `components` defaults to `main`. An archive under another root, such as `https://deb.debian.org/debian-security` with suite `bookworm-security`, is a registry of its own. The client reads `dists/<suite>/InRelease`, falling back to `Release`, and checks each `Packages` index against its SHA256 there; a suite with neither is read unchecked. It tries `Packages.xz`, `Packages.gz` and `Packages` in that order, of those the Release lists, and decodes them as a stream. The signature on `InRelease` is not checked. Each artifact records `arch` and `distro`, the suite's codename without `-updates` or `-security`, for the `arch` and `distro` PURL qualifiers. Versions keep their epoch (`1:9.2p1-2+deb12u3`) and compare with the `deb` scheme. `fetch.Resolver` also takes a `distro` given as the release, as trivy and syft write it (`debian-12`, `debian-12.4`, `ubuntu-22.04`), and a version without its epoch, or with trivy's `epoch` qualifier; a number listed with two epochs needs the epoch, and is otherwise refused with `fetch.ErrAmbiguousVersion`. Source packages (`architectures=source`) are not supported. In a PURL, `repository_url` is this whole URL, query included, so its `?`, `=`, `&` and `,` are percent-encoded.
The Debian URL is an archive root with the suites, components and architectures to read as query parameters, which archives don't use: `https://deb.debian.org/debian?suites=bookworm,bookworm-updates&components=main&architectures=amd64`. `components` defaults to `main`. An archive under another root, such as `https://deb.debian.org/debian-security` with suite `bookworm-security`, is a registry of its own. The client reads `dists/<suite>/InRelease`, falling back to `Release`, and checks each `Packages` index against its SHA256 there; without a keyring (below), a suite with neither is read unchecked. It tries `Packages.xz`, `Packages.gz` and `Packages` in that order, of those the Release lists, and decodes them as a stream. Signatures are checked only for an archive given a keyring with `registries.WithDebianKeyring(archiveURL, keyring)`, a binary OpenPGP keyring such as `/usr/share/keyrings/debian-archive-keyring.gpg`, or one or more armored blocks: `InRelease` must then be clearsigned by one of its keys, a `Release` without `InRelease` needs a valid `Release.gpg`, and a suite with neither, or with a signed Release that lists no SHA256 sums or whose `Valid-Until` has passed, is refused. Of several signatures, one that verifies is enough, as in apt. Only the signed text is read. Without a keyring, nothing is verified, so the SHA256 checks only catch indexes that don't match the Release served with them. Each artifact records `arch` and `distro`, the suite's codename without `-updates` or `-security`, for the `arch` and `distro` PURL qualifiers. Versions keep their epoch (`1:9.2p1-2+deb12u3`) and compare with the `deb` scheme. `fetch.Resolver` also takes a `distro` given as the release, as trivy and syft write it (`debian-12`, `debian-12.4`, `ubuntu-22.04`), and a version without its epoch, or with trivy's `epoch` qualifier; a number listed with two epochs needs the epoch, and is otherwise refused with `fetch.ErrAmbiguousVersion`. Source packages (`architectures=source`) are not supported. In a PURL, `repository_url` is this whole URL, query included, so its `?`, `=`, `&` and `,` are percent-encoded.

The RPM URL is one repository, the directory that holds `repodata/`, such as `https://dl.fedoraproject.org/pub/fedora/linux/releases/43/Everything/x86_64/os`. The client doesn't pick a release, variant or architecture from a mirror root, and doesn't follow mirror lists or metalinks. It reads `repodata/repomd.xml`, follows its `primary` entry, checks that file against the checksum and size listed there, and decodes `.zst`, `.xz`, `.gz` or plain XML as a stream. Repository metadata doesn't name its distribution, so the `distro` to record on artifacts is given as a query parameter: `.../x86_64/os?distro=fedora-43`. Each artifact records `arch`, `epoch` and that `distro`, with the package's own checksum as integrity (`sha256-...`). The version number is `version-release`, as in the `rpm` PURL type, where the epoch is a qualifier; versions are ordered by epoch, version and release. Dependencies are the requirements that name packages, leaving out file paths, sonames and capabilities in parentheses such as `rpmlib(...)`; a constraint on a package is listed once per distinct constraint. A virtual capability such as `webserver` can't be told from a package name and is listed as one. In a PURL, `repository_url` is the whole URL, `distro` parameter included, so its `?` and `=` are percent-encoded.

Expand Down
22 changes: 22 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ type Client struct {
// MaxStreamSize limits bodies read through Stream and StreamJSON; zero
// means 512 MiB.
MaxStreamSize int64
// DebianKeyrings maps a Debian archive root to the OpenPGP keyring,
// armored or binary, its Release files must be signed with. See
// WithDebianKeyring.
DebianKeyrings map[string][]byte
}

// DefaultClient returns a client with sensible defaults.
Expand Down Expand Up @@ -427,6 +431,24 @@ func WithMaxStreamSize(n int64) Option {
}
}

// WithDebianKeyring makes the deb registry for archiveURL, the archive
// root such as https://deb.debian.org/debian, check its Release files
// against keyring, a binary OpenPGP keyring or one or more armored
// blocks. InRelease must then carry a valid signature by one of its keys,
// a Release without InRelease a valid Release.gpg, and a suite without
// either, or a Release whose Valid-Until has passed, is refused.
// Archives without a keyring are read without checking signatures.
func WithDebianKeyring(archiveURL string, keyring []byte) Option {
return func(c *Client) {
keyrings := make(map[string][]byte, len(c.DebianKeyrings)+1)
for k, v := range c.DebianKeyrings {
keyrings[k] = v
}
keyrings[archiveURL] = keyring
c.DebianKeyrings = keyrings
}
}

// NewClient creates a new client with the given options.
func NewClient(opts ...Option) *Client {
c := DefaultClient()
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.27
toolchain go1.27.3

require (
github.com/ProtonMail/go-crypto v1.5.2
github.com/cenk/backoff v2.2.1+incompatible
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a
github.com/git-pkgs/artifacts v0.2.1
Expand All @@ -21,8 +22,11 @@ require (
)

require (
github.com/cloudflare/circl v1.6.3 // indirect
github.com/github/go-spdx/v2 v2.8.0 // indirect
github.com/package-url/packageurl-go v0.1.7 // indirect
github.com/peterbourgon/g2s v0.0.0-20170223122336-d4e7ad98afea // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.35.0 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
github.com/ProtonMail/go-crypto v1.5.2 h1:cucYnvqcY7UOXVD//mSyjeaPY0SSN3v5cDkYPxumINk=
github.com/ProtonMail/go-crypto v1.5.2/go.mod h1:/RaSu30DaKO4RY+XdV/ACcCcZkGr7AhUIduq5sjzzCo=
github.com/cenk/backoff v2.2.1+incompatible h1:djdFT7f4gF2ttuzRKPbMOWgZajgesItGLwG5FTQKmmE=
github.com/cenk/backoff v2.2.1+incompatible/go.mod h1:7FtoeaSnHoZnmZzz47cM35Y9nSW7tNyaidugnHTaFDE=
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/facebookgo/clock v0.0.0-20150410010913-600d898af40a h1:yDWHCSQ40h88yih2JAcL6Ls/kVkSE8GFACTGVnMPruw=
Expand Down Expand Up @@ -36,8 +40,12 @@ github.com/ulikunitz/xz v0.5.17 h1:flR0y/x1hgM8EGV1AW3Xll6T413G0glV8UfBwR617V4=
github.com/ulikunitz/xz v0.5.17/go.mod h1:H9Rt/W6/Qj27PGauhQc6nfCDy7vHpzsOThBSaYDoEhw=
go.yaml.in/yaml/v3 v3.0.6 h1:zcDfvjXEqTQmpjniUBLc91xYJMB2c4QOFxEF/LCEckM=
go.yaml.in/yaml/v3 v3.0.6/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.41.0 h1:WKYxWedPGCTVVl5+WHSSrOBT0O8lx32+zxmHxijgXp4=
golang.org/x/crypto v0.41.0/go.mod h1:pO5AFd7FA68rFak7rOAGVuygIISepHftHnr8dr6+sUc=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 12 additions & 3 deletions internal/deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"sync"

"github.com/ProtonMail/go-crypto/openpgp"
"github.com/git-pkgs/purl"
"github.com/git-pkgs/registries/internal/core"
"github.com/git-pkgs/registries/internal/xzlimit"
Expand Down Expand Up @@ -76,6 +77,8 @@ type Registry struct {
configErr error
client *core.Client
urls *URLs
// keyring, when set, is what the Release files must be signed with.
keyring openpgp.EntityList

maxIndexSize int64

Expand All @@ -89,6 +92,9 @@ func New(baseURL string, client *core.Client) *Registry {
}
r := &Registry{client: client, maxIndexSize: maxIndexSize}
r.configErr = r.configure(baseURL)
if r.configErr == nil {
r.keyring, r.configErr = keyringFor(client, r.archiveURL)
}
r.urls = &URLs{repositoryURL: r.repositoryURL()}
return r
}
Expand All @@ -110,8 +116,7 @@ func (r *Registry) configure(baseURL string) error {
return fmt.Errorf("%w: %q is not an archive URL", ErrInvalidConfig, baseURL)
}
q := u.Query()
u.RawQuery, u.Fragment = "", ""
r.archiveURL = strings.TrimSuffix(u.String(), "/")
r.archiveURL = archiveRoot(u)
r.suites = splitList(q.Get("suites"))
r.components = splitList(q.Get("components"))
r.architectures = splitList(q.Get("architectures"))
Expand Down Expand Up @@ -247,8 +252,12 @@ func (r *Registry) readSuite(ctx context.Context, suite string, add func(record)
}

// fetchRelease reads InRelease, falling back to Release. It returns nil
// when the suite has neither, and its indexes are read unchecked.
// when the suite has neither, and its indexes are read unchecked. With a
// keyring, the Release must be signed (see fetchSignedRelease).
func (r *Registry) fetchRelease(ctx context.Context, suite string) (*release, error) {
if r.keyring != nil {
return r.fetchSignedRelease(ctx, suite)
}
for _, name := range []string{"InRelease", "Release"} {
body, err := r.client.GetBody(ctx, r.archiveURL+"/dists/"+suite+"/"+name)
if isNotFound(err) {
Expand Down
10 changes: 10 additions & 0 deletions internal/deb/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ type fileSum struct {
type release struct {
codename string
suite string
// validUntil is the Valid-Until field as written, checked only for a
// signed Release.
validUntil string
// files maps a path under dists/<suite>/ to its SHA256 entry.
files map[string]fileSum
}
Expand All @@ -165,6 +168,11 @@ func parseRelease(data []byte) (*release, error) {
if err != nil {
return nil, err
}
return parseReleaseText(text)
}

// parseReleaseText reads the text of a Release file.
func parseReleaseText(text []byte) (*release, error) {
rel := &release{files: map[string]fileSum{}}
scanner := bufio.NewScanner(bytes.NewReader(text))
scanner.Buffer(nil, maxLineSize)
Expand Down Expand Up @@ -196,6 +204,8 @@ func parseRelease(data []byte) (*release, error) {
rel.codename = strings.TrimSpace(value)
case "suite":
rel.suite = strings.TrimSpace(value)
case "valid-until":
rel.validUntil = strings.TrimSpace(value)
}
}
if err := scanner.Err(); err != nil {
Expand Down
Loading