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
5 changes: 5 additions & 0 deletions internal/cli/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
19 changes: 19 additions & 0 deletions pkg/apk/apk/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}
Expand Down
31 changes: 31 additions & 0 deletions pkg/apk/apk/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
46 changes: 46 additions & 0 deletions pkg/build/build_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -30,6 +31,7 @@ import (
"path/filepath"
"runtime"
"slices"
"strings"
"sync"

"github.com/chainguard-dev/clog"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional: with a base image whose installed DB already has .apko-excluded (the base was built with a ! entry), this appends a second record with the same name and a different checksum. buildImage copies the base records in first, and the world also gets .apko-excluded=0-r0 twice. I tried that against apk-tools 2.14.13 with one record carrying D:!curl and the other D:!wget: apk add curl and apk add wget both succeeded, and apk upgrade -a replaced the package. So the derived image loses the base's block as well as its own. Merging the base record's ! dependencies into this one, and replacing that record rather than appending another, would fix it. baseimage is experimental, so I'm fine with this as a follow-up.

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))
}
9 changes: 9 additions & 0 deletions pkg/build/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := ""
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions pkg/build/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
6 changes: 6 additions & 0 deletions pkg/lock/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
20 changes: 20 additions & 0 deletions pkg/lock/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package lock

import (
"slices"
"testing"

"chainguard.dev/apko/pkg/build/types"
Expand All @@ -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)
}
}