diff --git a/internal/cli/lock.go b/internal/cli/lock.go index 356dc06e0..a43e61e9f 100644 --- a/internal/cli/lock.go +++ b/internal/cli/lock.go @@ -168,6 +168,11 @@ func LockCmd(ctx context.Context, output string, archs []types.Architecture, opt Keyrings: make([]pkglock.LockKeyring, 0, len(ic.Contents.Keyring)), }, } + for _, p := range ic.Contents.Packages { + if strings.HasPrefix(p, "!") { + lock.Contents.Excluded = append(lock.Contents.Excluded, p) + } + } explicitClient := &http.Client{} for _, keyring := range ic.Contents.Keyring { diff --git a/pkg/apk/apk/repo.go b/pkg/apk/apk/repo.go index 9824c8e02..ae3ff7170 100644 --- a/pkg/apk/apk/repo.go +++ b/pkg/apk/apk/repo.go @@ -729,10 +729,26 @@ func (p *PkgResolver) getPackagesWithDependencies(ctx context.Context, packages installTracked = map[string]*RepositoryPackage{} ) + for _, s := range constraints { + if s == "!" { + return nil, nil, fmt.Errorf("package list entry %q names no package", s) + } + } + if err := p.constrain(constraints, dq); err != nil { return nil, nil, fmt.Errorf("constraining initial packages: %w", err) } + // A !name entry excludes name, as in an apk world file. constrain has + // already disqualified its providers, so drop the entry. + constraints = slices.DeleteFunc(constraints, func(s string) bool { + if name, ok := strings.CutPrefix(s, "!"); ok { + conflicts = append(conflicts, name) + return true + } + return false + }) + for len(constraints) != 0 { next, err := p.nextPackage(constraints, dq) if err != nil { @@ -757,6 +773,9 @@ func (p *PkgResolver) getPackagesWithDependencies(ctx context.Context, packages // now get the dependencies for each package for _, pkgName := range packages { + if strings.HasPrefix(pkgName, "!") { + continue + } pkg, deps, confs, err := p.GetPackageWithDependencies(ctx, pkgName, dependenciesMap, dq) if err != nil { return toInstall, nil, &ConstraintError{pkgName, err} diff --git a/pkg/apk/apk/repo_test.go b/pkg/apk/apk/repo_test.go index 7cb452fa9..0bf68dc32 100644 --- a/pkg/apk/apk/repo_test.go +++ b/pkg/apk/apk/repo_test.go @@ -824,6 +824,37 @@ func TestExcludedDeps(t *testing.T) { } } +// TestWorldConflict checks that a !name entry excludes name and reports it +// as a conflict. +func TestWorldConflict(t *testing.T) { + providers := map[string][]string{ + "musl=1.23-r4": {"so:ld-linux-aarch64.so.1"}, + "ld-linux=2.38-r10": {"so:ld-linux-aarch64.so.1"}, + } + dependers := map[string][]string{ + "glibc=2.38-r10": {"so:ld-linux-aarch64.so.1"}, + } + + resolver := makeResolver(providers, dependers) + // Without the exclusion, glibc gets ld-linux. + pkgs, conflicts, err := resolver.GetPackagesWithDependencies(context.Background(), []string{"glibc", "!ld-linux"}, nil) + require.NoError(t, err) + + wantPkgs := []string{ + "musl-1.23-r4.apk", + "glibc-2.38-r10.apk", + } + require.Equal(t, []string{"ld-linux"}, conflicts) + + require.Len(t, pkgs, len(wantPkgs)) + for i, pkg := range pkgs { + require.Equal(t, wantPkgs[i], pkg.Filename()) + } + + _, _, err = resolver.GetPackagesWithDependencies(context.Background(), []string{"glibc", "!"}, nil) + require.ErrorContains(t, err, `"!" names no package`) +} + func TestSameProvidedVersion(t *testing.T) { providers := map[string][]string{ "ld-linux=2.38-r10": {"so:ld-linux-aarch64.so.1=1.0"}, diff --git a/pkg/build/build_implementation.go b/pkg/build/build_implementation.go index fe5b4497f..729925d8a 100644 --- a/pkg/build/build_implementation.go +++ b/pkg/build/build_implementation.go @@ -18,6 +18,7 @@ import ( "archive/tar" "bufio" "context" + "crypto/sha1" //nolint:gosec // apk package checksums are SHA-1 "crypto/sha256" "encoding/hex" "encoding/json" @@ -30,6 +31,7 @@ import ( "path/filepath" "runtime" "slices" + "strings" "sync" "github.com/chainguard-dev/clog" @@ -322,6 +324,10 @@ func (bc *Context) buildImage(ctx context.Context) ([]apk.InstalledDiff, error) } } + if err := bc.blockExcluded(ctx); err != nil { + return nil, fmt.Errorf("blocking excluded packages: %w", err) + } + // For now adding additional accounts is banned when using base image. On the other hand, we don't want to // wipe out the users set in base. // If one wants to add a support for adding additional users they would need to look into this piece of code. @@ -554,3 +560,43 @@ func (bc *Context) ResolveWithBase(ctx context.Context) ([]*apk.APKResolved, err func (bc *Context) InstalledPackages() ([]*apk.InstalledPackage, error) { return bc.apk.GetInstalled() } + +// excludedPackage is the installed package that conflicts with the !name +// entries of the world. +const excludedPackage = ".apko-excluded" + +// blockExcluded records excludedPackage with a !name dependency for each +// !name entry of the world, as glibc depends on !musl. A later apk add of +// name then fails. +func (bc *Context) blockExcluded(ctx context.Context) error { + world, err := bc.apk.GetWorld() + if err != nil { + return err + } + var excluded []string + for _, w := range world { + if strings.HasPrefix(w, "!") { + excluded = append(excluded, w) + } + } + if len(excluded) == 0 { + return nil + } + // apk-tools cannot rewrite its database for a package without a + // checksum, so the package gets one from its dependencies. + sum := sha1.Sum([]byte(strings.Join(excluded, " "))) //nolint:gosec // apk package checksums are SHA-1 + pkg := &apk.Package{ + Name: excludedPackage, + Version: "0-r0", + Arch: "noarch", + Description: "conflicts with the excluded packages of the image", + Dependencies: excluded, + Checksum: sum[:], + } + if _, err := bc.apk.AddInstalledPackage(pkg, nil); err != nil { + return err + } + // apk keeps a package that no repository has when the world pins its + // version, as for apk add --virtual. + return bc.apk.SetWorld(ctx, append(world, excludedPackage+"="+pkg.Version)) +} diff --git a/pkg/build/lock.go b/pkg/build/lock.go index fb448359b..7d88b2513 100644 --- a/pkg/build/lock.go +++ b/pkg/build/lock.go @@ -177,7 +177,14 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri byArch := map[string][]string{} + // A !name entry installs nothing. Keep it as is in every locked list. + var excluded []string + for _, orig := range originals { + if strings.HasPrefix(orig, "!") { + excluded = append(excluded, orig) + continue + } name := orig version := "" pinned := "" @@ -309,6 +316,7 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri } pl = append(pl, pkgName) } + pl = append(pl, excluded...) // Sort the package list explicitly with the `=` included. // This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1) // due to the presence or absence of the `=` character. @@ -327,6 +335,7 @@ func unify(originals []string, inputs []resolved) (map[string][]string, map[stri } pl = append(pl, pkgName) } + pl = append(pl, excluded...) // Sort the package list explicitly with the `=` included. // This is because (foo, foo-bar) sorts differently than (foo=1, foo-bar=1) // due to the presence or absence of the `=` character. diff --git a/pkg/build/lock_test.go b/pkg/build/lock_test.go index 376da6578..b78b63516 100644 --- a/pkg/build/lock_test.go +++ b/pkg/build/lock_test.go @@ -61,6 +61,18 @@ func TestUnify(t *testing.T) { "foo=1.2.3", }, }, + }, { + name: "excluded package", + originals: []string{"foo", "!bar"}, + inputs: []resolved{{ + arch: "amd64", + packages: sets.New("foo"), + versions: map[string]string{"foo": "1.2.3"}, + }}, + want: map[string][]string{ + "amd64": {"!bar", "foo=1.2.3"}, + "index": {"!bar", "foo=1.2.3"}, + }, }, { name: "locked versions", originals: []string{"foo=1.2.3", "bar=2.4.6", "baz=0.0.1"}, diff --git a/pkg/lock/lock.go b/pkg/lock/lock.go index 0148737df..39e99416e 100644 --- a/pkg/lock/lock.go +++ b/pkg/lock/lock.go @@ -30,6 +30,9 @@ type LockContents struct { Repositories []LockRepo `json:"repositories"` // Packages in order of installation -> for a single architecture. Packages []LockPkg `json:"packages"` + // Excluded holds the !name entries of the package list, for every + // architecture. + Excluded []string `json:"excluded,omitempty"` } type LockPkg struct { @@ -97,5 +100,8 @@ func (lock Lock) Arch2LockedPackages(archs []types.Architecture) map[string][]st ) } } + for arch, pkgs := range wantedPackages { + wantedPackages[arch] = append(pkgs, lock.Contents.Excluded...) + } return wantedPackages } diff --git a/pkg/lock/lock_test.go b/pkg/lock/lock_test.go index 4f6a26249..dc34fddd4 100644 --- a/pkg/lock/lock_test.go +++ b/pkg/lock/lock_test.go @@ -15,6 +15,7 @@ package lock import ( + "slices" "testing" "chainguard.dev/apko/pkg/build/types" @@ -40,3 +41,22 @@ func TestArch2LockedPackages(t *testing.T) { t.Errorf("wanted %d arch, got %d", want, got) } } + +func TestArch2LockedPackagesExcluded(t *testing.T) { + l := Lock{ + Contents: LockContents{ + Packages: []LockPkg{{ + Name: "glibc", + Version: "2.38-r10", + Architecture: "x86_64", + }}, + Excluded: []string{"!musl"}, + }, + } + + got := l.Arch2LockedPackages([]types.Architecture{types.ParseArchitecture("amd64")}) + want := []string{"glibc=2.38-r10", "!musl"} + if !slices.Equal(got["amd64"], want) { + t.Errorf("got %v, want %v", got["amd64"], want) + } +}