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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ packages = registries.BulkFetchPackagesWithConcurrency(ctx, purls, nil, 5)
| Terraform | `pkg:terraform/hashicorp/consul/aws@0.11.0` (module), `pkg:terraform/hashicorp/random@3.6.0` (provider) |
| GitHub Actions | `pkg:githubactions/actions/checkout@v4` |
| VS Code extension | `pkg:vscode-extension/redhat/vscode-yaml@1.15.0` |
| Arch Linux | `pkg:alpm/arch/pacman@7.1.0.r9.g54d9411-4?arch=x86_64&repository_url=https://geo.mirror.pkgbuild.com/core/os/x86_64/core.db` |
| Alpine | `pkg:apk/alpine/curl@8.22.0-r0?arch=x86_64&repository_url=https://dl-cdn.alpinelinux.org/alpine/v3.23/main/x86_64` |
| Debian | `pkg:deb/debian/curl@7.88.1-10+deb12u14?arch=amd64&distro=bookworm&repository_url=https://deb.debian.org/debian%3Fsuites%3Dbookworm%2Cbookworm-updates%26architectures%3Damd64` |
| RPM | `pkg:rpm/fedora/curl@8.15.0-4.fc43?arch=x86_64&distro=fedora-43&repository_url=https://dl.fedoraproject.org/pub/fedora/linux/releases/43/Everything/x86_64/os%3Fdistro%3Dfedora-43` |
Expand Down Expand Up @@ -198,13 +199,16 @@ import _ "github.com/git-pkgs/registries/all"
| Deno | `deno` | https://apiland.deno.dev |
| Terraform | `terraform` | https://registry.terraform.io |
| Swift | `swift` | URL required |
| Arch Linux | `alpm` | URL required |
| Alpine | `apk` | URL required |
| Debian | `deb` | URL required |
| RPM | `rpm` | URL required |
| GitHub Actions | `githubactions` | https://api.github.com |
| VS Code extension | `vscode-extension` | https://open-vsx.org |
| APM | None | URL required |

The Arch Linux URL is one pacman sync database, such as `https://geo.mirror.pkgbuild.com/core/os/x86_64/core.db`, which names the repository and architecture; the client doesn't pick either from a mirror root. It reads the database's tar compressed with gzip, zstd or xz, or uncompressed, telling them apart by their first bytes, and caps it at 1 GiB decompressed. A path that is absolute, climbs out with `..` or nests below a package's directory, a `NAME` or `VERSION` that disagrees with the directory name, or a `FILENAME` holding a path makes the whole database an error. Each version's artifact is its `FILENAME` beside the database, with `SHA256SUM` as integrity (`sha256-...`), `CSIZE` as size and the metadata `arch` (`x86_64`, `any`, …), so `URLs().Download` is empty and `fetch.Resolver` reads the database. Versions keep their epoch and package release (`1:1.4-1`) and compare with the `alpm` scheme. `DEPENDS` are runtime dependencies and `OPTDEPENDS` optional ones, without their descriptions. A name another package provides, such as `libcurl.so=4-64`, is listed as that package, the first by name of those whose provided version meets the constraint; one nothing in the database satisfies is kept as written. Make, check, conflict, replace and provide relations, groups, the base package and the packager are in the version's metadata. `FetchMaintainers` reads `PACKAGER`, splitting `Name <email>`. Through a PURL, the `arch` namespace is kept as `Package.Namespace`.

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; 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.
Expand Down
3 changes: 2 additions & 1 deletion all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
//
// // Now all ecosystems are available
// ecosystems := registries.SupportedEcosystems()
// // ["apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deb", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "rpm", "swift", "terraform", "vscode-extension"]
// // ["alpm", "apk", "apm", "brew", "cargo", "clojars", "cocoapods", "composer", "conda", "cpan", "cran", "deb", "deno", "dub", "elm", "gem", "githubactions", "golang", "hackage", "haxelib", "helm", "hex", "julia", "luarocks", "maven", "nimble", "npm", "nuget", "pub", "pypi", "rpm", "swift", "terraform", "vscode-extension"]
package all

import (
_ "github.com/git-pkgs/registries/internal/alpm"
_ "github.com/git-pkgs/registries/internal/apk"
_ "github.com/git-pkgs/registries/internal/apm"
_ "github.com/git-pkgs/registries/internal/cargo"
Expand Down
78 changes: 78 additions & 0 deletions fetch/alpm_resolver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//go:build !tinygo

package fetch

import (
"archive/tar"
"bytes"
"compress/gzip"
"context"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/git-pkgs/registries"
_ "github.com/git-pkgs/registries/internal/alpm"
"github.com/git-pkgs/registries/safehttp"
)

// alpmDatabase serves a sync database holding pacman for x86_64, over TLS:
// the resolver only hands out https download URLs.
func alpmDatabase(t *testing.T) (string, *registries.Client) {
t.Helper()
desc := "%FILENAME%\npacman-7.1.0-4-x86_64.pkg.tar.zst\n\n%NAME%\npacman\n\n%VERSION%\n7.1.0-4\n\n" +
"%CSIZE%\n993248\n\n%SHA256SUM%\ne7b847d2989de38ec17ce9e2ca2a7b585ca61604dda0c73bc285b44005b234b1\n\n%ARCH%\nx86_64\n"
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
tw := tar.NewWriter(gz)
_ = tw.WriteHeader(&tar.Header{Name: "pacman-7.1.0-4/desc", Mode: 0o644, Size: int64(len(desc))})
_, _ = tw.Write([]byte(desc))
_ = tw.Close()
_ = gz.Close()

server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/core/os/x86_64/core.db" {
http.NotFound(w, r)
return
}
_, _ = w.Write(buf.Bytes())
}))
t.Cleanup(server.Close)
client := registries.DefaultClient()
client.HTTPClient = server.Client()
return server.URL + "/core/os/x86_64", client
}

func TestResolveALPM(t *testing.T) {
dir, client := alpmDatabase(t)
reg, err := registries.New("alpm", dir+"/core.db", client)
if err != nil {
t.Fatal(err)
}
r := NewResolver(WithAddressPolicy(safehttp.Options{AllowLoopback: true}))
r.RegisterRegistry(reg)

for _, qualifiers := range []map[string]string{nil, {"arch": "x86_64"}} {
info, err := r.ResolveWithOptions(context.Background(), "alpm", "arch/pacman", "7.1.0-4",
ResolveOptions{Qualifiers: qualifiers})
if err != nil {
t.Fatalf("Resolve with %v: %v", qualifiers, err)
}
want := ArtifactInfo{
URL: dir + "/pacman-7.1.0-4-x86_64.pkg.tar.zst",
Filename: "pacman-7.1.0-4-x86_64.pkg.tar.zst",
Integrity: "sha256-e7b847d2989de38ec17ce9e2ca2a7b585ca61604dda0c73bc285b44005b234b1",
Size: 993248,
}
if *info != want {
t.Errorf("info with %v = %+v, want %+v", qualifiers, info, want)
}
}

_, err = r.ResolveWithOptions(context.Background(), "alpm", "arch/pacman", "7.1.0-4",
ResolveOptions{Qualifiers: map[string]string{"arch": "aarch64"}})
if !errors.Is(err, ErrNoMatchingArtifact) {
t.Errorf("aarch64 against x86_64: err = %v, want ErrNoMatchingArtifact", err)
}
}
Loading