diff --git a/cli/audit/audit.go b/cli/audit/audit.go index ec4ba2d2..54363c7f 100644 --- a/cli/audit/audit.go +++ b/cli/audit/audit.go @@ -2,6 +2,7 @@ package audit import ( + "context" "fmt" "os" "path/filepath" @@ -86,6 +87,9 @@ func run() int { states, serverErrs := auditPkg.CollectState(apps, auditPkg.CollectOptions{Offline: internal.Offline}) findings := auditPkg.Evaluate(states, advisories) + if !internal.Offline { + auditPkg.VerifyRefresh(context.Background(), findings, auditPkg.RegistryFetch) + } for _, f := range findings { for _, r := range f.Reasons { diff --git a/doc/DesignAudit.md b/doc/DesignAudit.md index e4f34e9c..f0840166 100644 --- a/doc/DesignAudit.md +++ b/doc/DesignAudit.md @@ -345,6 +345,7 @@ Answers "which of my apps need attention, and what do I run?" using only a hand- - upgrade: `abra app upgrade ` - refresh: `abra app deploy --force`, followed by `abra audit` to check the result - manual: the advisory's notes +- **Refresh check.** Before suggesting a redeploy, the audit reads the tag's current image from the registry and applies the advisory to it. Upstreams stop rebuilding patch-pinned tags such as `nextcloud:35.0.2-fpm` once the next patch is out, so if the current image is not fixed either, the suggestion becomes a recipe upgrade. If the registry cannot be reached, the redeploy suggestion is marked as unchecked. - **Exit code.** 0 when nothing fails, 1 otherwise. - **Errors.** An unreachable server is listed as such and the run continues. diff --git a/pkg/audit/evaluate.go b/pkg/audit/evaluate.go index c8c2dfa7..aa850a15 100644 --- a/pkg/audit/evaluate.go +++ b/pkg/audit/evaluate.go @@ -29,6 +29,9 @@ type Finding struct { Fix string `json:"fix"` recipeAffected bool + // images that are affected or unknown, for VerifyRefresh + images []ImageRef + os, arch string } // verdict of one image rule for one image. @@ -98,6 +101,9 @@ func evaluateApp(st AppState, adv *Advisory) (Finding, bool) { if !slices.Contains(f.Reasons, reason) { f.Reasons = append(f.Reasons, reason) } + if !slices.ContainsFunc(f.images, func(i ImageRef) bool { return i.Ref == img.Ref }) { + f.images = append(f.images, img) + } } } @@ -111,6 +117,7 @@ func evaluateApp(st AppState, adv *Advisory) (Finding, bool) { } f.Fix = suggestFix(f, st) + f.os, f.arch = st.OS, st.Arch return f, true } @@ -220,14 +227,16 @@ func evaluateImage(rule ImageRule, img ImageRef) (verdict, string) { return unknown, strings.Join(notes, "; ") } +func upgradeCommand(adv *Advisory, app, recipe string) string { + if rr, ok := adv.Recipes[recipe]; ok && rr.Fixed != "" { + return fmt.Sprintf("abra app upgrade %s %s", app, rr.Fixed) + } + return fmt.Sprintf("abra app upgrade %s", app) +} + func suggestFix(f Finding, st AppState) string { adv := f.Advisory - upgrade := func() string { - if rr, ok := adv.Recipes[st.Recipe]; ok && rr.Fixed != "" { - return fmt.Sprintf("abra app upgrade %s %s", st.Name, rr.Fixed) - } - return fmt.Sprintf("abra app upgrade %s", st.Name) - } + upgrade := func() string { return upgradeCommand(adv, st.Name, st.Recipe) } // a recipe version in the affected range is only fixed by upgrading if f.recipeAffected { diff --git a/pkg/audit/image.go b/pkg/audit/image.go index e8b681fa..92a93747 100644 --- a/pkg/audit/image.go +++ b/pkg/audit/image.go @@ -11,6 +11,7 @@ import ( "coopcloud.tech/abra/pkg/i18n" "github.com/containers/image/docker" + "github.com/containers/image/manifest" "github.com/containers/image/pkg/blobinfocache/none" "github.com/containers/image/types" "github.com/distribution/reference" @@ -45,6 +46,8 @@ type ImageRef struct { // ImageInfo holds what the image says about itself. type ImageInfo struct { + // Digest is the manifest (index) digest, set when read from a registry. + Digest string Created *time.Time Env map[string]string Labels map[string]string @@ -206,6 +209,11 @@ func fetchConfig(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo return nil, err } + topDigest, err := manifest.Digest(raw) + if err != nil { + return nil, err + } + var doc manifestDoc if err := json.Unmarshal(raw, &doc); err != nil { return nil, err @@ -267,6 +275,7 @@ func fetchConfig(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo info := &ImageInfo{ Source: "registry", + Digest: topDigest.String(), Created: cfg.Created, Env: parseEnv(cfg.Config.Env), Labels: cfg.Config.Labels, diff --git a/pkg/audit/refresh.go b/pkg/audit/refresh.go new file mode 100644 index 00000000..b96c66df --- /dev/null +++ b/pkg/audit/refresh.go @@ -0,0 +1,121 @@ +package audit + +import ( + "context" + "fmt" + "slices" + "strings" + "time" + + "coopcloud.tech/abra/pkg/i18n" + "coopcloud.tech/abra/pkg/log" +) + +// FetchFunc reads the config of an image for a platform from a registry. +type FetchFunc func(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo, error) + +// VerifyRefresh checks findings whose suggested fix is a redeploy. A redeploy +// only helps if the tag now points to a fixed image: upstreams stop +// rebuilding patch-pinned tags such as nextcloud:35.0.2-fpm once the next +// patch release is out. For each such finding, the tag's current image is +// read from the registry and the advisory is applied to it again: +// +// - fixed: the redeploy suggestion stays; +// - still affected or unknown: the suggestion becomes a recipe upgrade; +// - registry unreachable: the suggestion stays, marked as unchecked. +func VerifyRefresh(ctx context.Context, findings []Finding, fetch FetchFunc) { + type result struct { + img *ImageRef + err error + } + cache := make(map[string]result) + + current := func(img ImageRef, os, arch string) (*ImageRef, error) { + key := strings.Join([]string{img.NameTag(), os, arch}, "|") + if r, ok := cache[key]; ok { + return r.img, r.err + } + + tagged := img + tagged.Digest = "" + tagged.Info, tagged.InfoErr = nil, nil + + fctx, cancel := context.WithTimeout(ctx, 60*time.Second) + defer cancel() + info, err := fetch(fctx, tagged, os, arch) + if err == nil { + info.createdUnusable = createdUnusable(info) + tagged.Digest = info.Digest + tagged.Info = info + cache[key] = result{img: &tagged} + return &tagged, nil + } + cache[key] = result{err: err} + return nil, err + } + + for i := range findings { + f := &findings[i] + if f.recipeAffected || f.Advisory.Fix.Strategy != StrategyRefresh || len(f.images) == 0 { + continue + } + + if f.arch == "" { + f.Fix += " " + i18n.G("(unchecked: platform unknown)") + continue + } + + var stale []string + var fetchErr error + for _, img := range f.images { + if img.Tag == "" { + stale = append(stale, img.Name) + continue + } + + cur, err := current(img, f.os, f.arch) + if err != nil { + fetchErr = err + log.Debug(i18n.G("%s: unable to read current %s from registry: %s", f.App, img.NameTag(), err)) + continue + } + + if cur.Digest == img.Digest || !fixedByAdvisory(f.Advisory, *cur) { + log.Debug(i18n.G("%s: current %s (%s) is not fixed", f.App, img.NameTag(), shortDigest(cur.Digest))) + if !slices.Contains(stale, img.NameTag()) { + stale = append(stale, img.NameTag()) + } + } + } + + switch { + case len(stale) > 0: + f.Fix = i18n.G("%s not rebuilt with a fix; upgrade to a recipe version with a fixed image: %s", + strings.Join(stale, ", "), upgradeCommand(f.Advisory, f.App, f.Recipe)) + f.Reasons = append(f.Reasons, i18n.G("redeploy would not help: the current image of %s is not fixed either", strings.Join(stale, ", "))) + case fetchErr != nil: + f.Fix += " " + i18n.G("(unchecked: registry unreachable)") + } + } +} + +// fixedByAdvisory reports whether no image rule of the advisory flags img. +func fixedByAdvisory(adv *Advisory, img ImageRef) bool { + for _, rule := range adv.Images { + if !globMatch(rule.Match, img) { + continue + } + if v, _ := evaluateImage(rule, img); v == affected || v == unknown { + return false + } + } + return true +} + +// RegistryFetch is the default FetchFunc. +func RegistryFetch(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo, error) { + if img.named == nil { + return nil, fmt.Errorf("%s", i18n.G("invalid image reference %s", img.Ref)) + } + return FetchFromRegistry(ctx, img, os, arch) +} diff --git a/pkg/audit/refresh_test.go b/pkg/audit/refresh_test.go new file mode 100644 index 00000000..ef2d9baf --- /dev/null +++ b/pkg/audit/refresh_test.go @@ -0,0 +1,64 @@ +package audit + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const phpAdvisory = ` +id: PHP +status: published +images: + - match: "*" + env: { PHP_VERSION: ">=8.5.0 <8.5.13" } +fix: {strategy: refresh} +` + +func TestVerifyRefresh(t *testing.T) { + adv, _, err := ParseAdvisory([]byte(phpAdvisory)) + require.NoError(t, err) + + old := &ImageInfo{Env: map[string]string{"PHP_VERSION": "8.5.12"}} + states := []AppState{ + {Server: "s", Name: "floating", Recipe: "nc", OS: "linux", Arch: "amd64", + Images: []ImageRef{mustImage(t, "app", "nextcloud:35-fpm@sha256:"+strings.Repeat("a", 64), old)}}, + {Server: "s", Name: "pinned", Recipe: "nc", OS: "linux", Arch: "amd64", + Images: []ImageRef{mustImage(t, "app", "nextcloud:35.0.2-fpm@sha256:"+strings.Repeat("b", 64), old)}}, + {Server: "s", Name: "offline", Recipe: "nc", OS: "linux", Arch: "amd64", + Images: []ImageRef{mustImage(t, "app", "other:1@sha256:"+strings.Repeat("c", 64), old)}}, + {Server: "s", Name: "noplatform", Recipe: "nc", + Images: []ImageRef{mustImage(t, "app", "nextcloud:35-fpm@sha256:"+strings.Repeat("a", 64), old)}}, + } + + var fetched []string + fetch := func(_ context.Context, img ImageRef, os, arch string) (*ImageInfo, error) { + fetched = append(fetched, img.Ref+" "+os+"/"+arch) + assert.Empty(t, img.Digest, "must fetch by tag") + switch img.NameTag() { + case "nextcloud:35-fpm": + return &ImageInfo{Digest: "sha256:" + strings.Repeat("d", 64), Env: map[string]string{"PHP_VERSION": "8.5.13"}}, nil + case "nextcloud:35.0.2-fpm": + return &ImageInfo{Digest: "sha256:" + strings.Repeat("b", 64), Env: map[string]string{"PHP_VERSION": "8.5.12"}}, nil + } + return nil, errors.New("registry down") + } + + findings := Evaluate(states, []Advisory{adv}) + require.Len(t, findings, 4) + VerifyRefresh(context.Background(), findings, fetch) + + got := map[string]string{} + for _, f := range findings { + got[f.App] = f.Fix + } + assert.Equal(t, "abra app deploy floating --force && abra audit", got["floating"]) + assert.Equal(t, "nextcloud:35.0.2-fpm not rebuilt with a fix; upgrade to a recipe version with a fixed image: abra app upgrade pinned", got["pinned"]) + assert.Equal(t, "abra app deploy offline --force && abra audit (unchecked: registry unreachable)", got["offline"]) + assert.Equal(t, "abra app deploy noplatform --force && abra audit (unchecked: platform unknown)", got["noplatform"]) + assert.Len(t, fetched, 3) +} diff --git a/pkg/audit/state.go b/pkg/audit/state.go index da4962aa..c9db5f75 100644 --- a/pkg/audit/state.go +++ b/pkg/audit/state.go @@ -24,6 +24,11 @@ type AppState struct { Version string `json:"version"` Chaos bool `json:"chaos"` Images []ImageRef `json:"images"` + + // OS and Arch of the server, used to read per-platform image configs. + // Empty when unknown or offline. + OS string `json:"-"` + Arch string `json:"-"` } // ServerError records a server that could not be audited. @@ -115,6 +120,8 @@ func collectServer(server string, apps []appPkg.App, opts CollectOptions) ([]App Server: server, Name: app.Name, Recipe: app.Recipe.Name, + OS: platformOS, + Arch: platformArch, } } @@ -208,8 +215,6 @@ func inspectImage(ctx context.Context, cl *dockerClient.Client, img ImageRef, pl // - earlier than the build time of the base image; // - shared by two different digests of the same tag. func markUnusableCreated(states []AppState) { - epoch := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC) - // tag -> created -> digests seen := make(map[string]map[time.Time]map[string]bool) for _, st := range states { @@ -236,13 +241,27 @@ func markUnusableCreated(states []AppState) { } c := info.Created.UTC() switch { - case c.Before(epoch): - info.createdUnusable = i18n.G("build time %s is before 2000 (reproducible build?)", c.Format(time.RFC3339)) - case info.BaseCreated != nil && c.Before(*info.BaseCreated): - info.createdUnusable = i18n.G("build time %s is earlier than its base image", c.Format(time.RFC3339)) + case createdUnusable(info) != "": + info.createdUnusable = createdUnusable(info) case len(seen[img.NameTag()][c]) > 1: info.createdUnusable = i18n.G("several digests of %s share the build time %s", img.NameTag(), c.Format(time.RFC3339)) } } } } + +// createdUnusable runs the checks that need only the image itself: a build +// time before 2000, or earlier than the base image's. +func createdUnusable(info *ImageInfo) string { + if info.Created == nil { + return "" + } + c := info.Created.UTC() + switch { + case c.Before(time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)): + return i18n.G("build time %s is before 2000 (reproducible build?)", c.Format(time.RFC3339)) + case info.BaseCreated != nil && c.Before(*info.BaseCreated): + return i18n.G("build time %s is earlier than its base image", c.Format(time.RFC3339)) + } + return "" +}