Compare commits

...
64 Commits
Author SHA1 Message Date
Linus GasserandClaude Opus 5.5 9b712d23b8 docs: list the AFFECTED column commit in README_C4DT.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:14:58 +01:00
Linus GasserandClaude Opus 5.5 d19f174e2a feat(audit): show what is affected instead of the advisory ID
The ADVISORY column showed IDs such as CCA-2026-0002, which are local to
the advisories repository and say nothing at a glance. It is replaced by
AFFECTED, naming what is vulnerable from the condition that matched:
"nginx 1.31.8" for NGINX_VERSION=1.31.8, the image's version for a
version condition, the build date for created_after, the recipe and its
version for a recipe range, and the image otherwise.

The advisory ID, marked if it is a draft, and its CVEs move to the end of
each fix line: "(CCA-2026-0002, CVE-2026-123007 and 4 more)".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:14:57 +01:00
Linus GasserandClaude Opus 5.5 f5af00ae4c docs: list the audit action commit in README_C4DT.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:05:38 +01:00
Linus GasserandClaude Opus 5.5 371b5aadb7 feat(audit): say who has to act, and search the registry for fixed images
When no recipe release fixed a finding, the audit said that a new release
was needed, even when the image itself had no fix yet, or when upstream
had already published one under another tag (e.g.
lasuite/impress-frontend:v6.1.0-1 for v6.1.0).

Each finding now has an action, shown in a new ACTION column (replacing
STATUS) and at the start of its fix line: redeploy, upgrade, recipe must
update, upstream must update, check by hand, or follow advisory. Findings
the operator cannot fix alone are summarised by action at the end.

To tell "recipe must update" from "upstream must update", the registry
is searched for a fixed image: the repository's tags are listed, and
checked against the advisory in this order:

- the deployed tag, if the recipe pins an older digest of it;
- later builds of the same version under a suffixed tag (-1, -fix1, _r2).
  Suffixes are not parsed; per-arch copies, pre-releases and variants are
  left out by name, and a candidate must be built at least an hour after
  the tag's current image, which keeps out pre-releases built before the
  release. The latest-built fixed candidate wins;
- newer versions of the same form (v6.2.0 for v6.1.0), oldest fixed first.

A redeploy is also only suggested if it deploys a fixed image: when the
recipe pins a digest, the pinned image is checked, not the tag.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 19:05:37 +01:00
Linus GasserandClaude Opus 5.5 bb75ff4136 docs: list the COMPOSE_FILE checkout fix in README_C4DT.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:35:23 +01:00
Linus GasserandClaude Opus 5.5 82564ded64 fix(audit): read the chaos checkout through the app's COMPOSE_FILE
The checkout check read the recipe's compose*.yml files only. An app
override attached through COMPOSE_FILE, e.g. one that pins an affected
image, was ignored, so the audit could suggest a --chaos redeploy that
would still deploy the affected image.

It now loads what abra app deploy --chaos would deploy: the files from
GetComposeFiles, overrides outside the recipe directory included, merged
and filled in with the app's .env values by GetAppComposeConfig, the
same loader deploy uses. The advice for apps without a fix mentions the
override as a place for the fixed image.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:35:19 +01:00
Linus GasserandClaude Opus 5.5 b0aa0470f5 docs: mention the recipe checkout check of abra audit in README_C4DT.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:28:59 +01:00
Linus GasserandClaude Opus 5.5 527d2a6a86 feat(audit): check the recipe checkout of --chaos deployments
A --chaos deployment runs the local recipe checkout, not a release. When
a redeploy of the same images does not help, the audit now first reads
the compose*.yml files of the checkout as they are on disk, uncommitted
changes included, and checks their images against the advisory. If they
are fixed, it suggests abra app deploy <domain> --chaos; otherwise it
checks the releases as before.

The suggested chaos redeploy drops --force, which deploy does not need
together with --chaos.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:28:59 +01:00
Linus GasserandClaude Opus 5.5 e44ac846cf docs: describe the audit chaos handling and the table fix in README_C4DT.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:26:33 +01:00
Linus GasserandClaude Opus 5.5 cef4dc2e17 fix(audit): handle --chaos deployments and clarify fix messages
The audit read only the version label, which a --chaos deployment leaves
at the release it was based on. It now also reads the chaos and
chaos-version labels:

- the table has a VERSION column, showing the deployed commit marked
  "(chaos)" for chaos deployments, as abra app ls does;
- a suggested redeploy keeps --chaos: without it, abra app deploy would
  replace the chaos deployment with the release in the .env file;
- when no release fixes a finding, a chaos app is told to use a fixed
  image in its recipe checkout and redeploy with --chaos. Releases are
  only mentioned for it when a newer one exists.

When nothing fixes a finding yet, the message now starts with "No fix
available yet", then gives the reasons and names the image that needs a
fixed version, so that the listed releases no longer read like a
solution. The summary line avoids "1 servers".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:26:29 +01:00
Linus GasserandClaude Opus 5.5 5c4ffaf279 fix(formatter): print tables when stdout is not a terminal
PrintTable read the width of stdin and returned the error when it was
not a terminal, so every table command failed in a pipe or cron job with
"operation not supported by device". Read the width of stdout, and print
the table without a width limit when there is none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 18:24:44 +01:00
Linus GasserandClaude Opus 5.5 46cee776f4 docs: add AGENTS.md with the c4dt branch and tag conventions
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 17:26:20 +01:00
Linus GasserandClaude Opus 5.5 59240028d9 audit: add --include-drafts
Apply draft advisories too, so a maintainer can try an advisory on real
servers before publishing it. Draft findings are marked "(draft)";
withdrawn advisories never apply.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 17:17:56 +01:00
Linus GasserandClaude Opus 5.5 ea1dd3e62c docs: describe the C4DT fork in README and README_C4DT.md
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 17:11:16 +01:00
Linus GasserandClaude Opus 5.5 9170b4d23a test(recipe): make the offline unfetched-branch test actually unfetched
SetupWithUpstream creates the feature branch before cloning, so the
clone already has origin/<branch> and EnsureVersionCtx succeeded
offline. Remove the remote-tracking ref first, as the test intends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 17:06:43 +01:00
Linus Gasser 5f80d5e85c feat(recipe): allow branch names in RECIPE=<name>:<version>
Fetch and resolve a branch name on demand (into refs/remotes/origin/*,
never refs/heads/*, to avoid corrupting default-branch detection),
checking it out detached at its tip as a chaos commit. Resolution order
stays tag -> hash -> branch so existing pins are unaffected. Adds
EnsureVersionCtx so offline mode fails clearly instead of hanging or
erroring opaquely when a branch hasn't been fetched yet.

This supersedes the previous behavior of rejecting branch names with a
clear error.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y6PrSttbx61PCQTie7HzLg
(cherry picked from commit e30f77caca)
2026-11-20 17:04:56 +01:00
Linus Gasser 836971d66b Ignore .devbox
(cherry picked from commit 33086eaebc)
2026-11-20 17:04:56 +01:00
Linus Gasser 47d9f73267 fix(app): keep recipe version on a single env field and label
Store the deployed version on TYPE (or the legacy RECIPE key when TYPE is
absent) instead of duplicating it across both. For git-URL recipes RECIPE
now stays a bare source pointer, so the two can never drift apart. Drops
the dead dirtyVersion branch and the fragile substring skip in
WriteRecipeVersion.

Also stamp the coop-cloud.<stack>.version label ourselves on upgrade and
rollback rather than trusting the value baked into the recipe's
compose.yml: git-URL/WIP recipes carry a stale or missing label, which
otherwise left the live deployment reporting the wrong version. Stop
getReleaseNotes and the [version] arg validation from fataling when the
deployed version is "unknown".

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
(cherry picked from commit 73801daae8)
2026-11-20 17:04:49 +01:00
Linus Gasser 0c91585eae feat(app): support git-URL recipes in 'abra app new'
Allow `abra app new <git-url>` to use a recipe from outside the
catalogue. On clone, a `.abra-source` sidecar records the canonical
host/path name (the on-disk directory escapes "/" and "." lossily), and
IsClean ignores it. When templating the app's .env, a `RECIPE=<canonical
name>` line is injected so a later `abra app deploy`, possibly on another
machine, re-fetches the recipe from the same git source. `recipe ls` now
shows a source column listing these external recipes alongside catalogue
ones.

(cherry picked from commit 5e8162bc66)
2026-11-20 17:04:49 +01:00
Linus Gasser 0a1578966a feat(recipe): normalize git URLs to canonical host/path names
Add NormalizeRecipeName to unify the various ways a recipe can be
referenced - https/http/ssh URLs, SCP-style git@host:path, already-
canonical host/path, and short catalogue names - to a single stable
"host/path" form, preserving any ":version" suffix. Wire it into
recipe.Get and ValidateRecipe so every entry point accepts git URLs, and
add Recipe.ShortName to recover the bare recipe name from a prefixed one.

(cherry picked from commit 6d9896fc46)
2026-11-20 17:04:49 +01:00
Linus Gasser 73227c9e58 fix(recipe): reject branch names as :version with a clear error
Passing a branch as the ":version" suffix previously failed with an
opaque "unable to resolve" error from go-git. Detect when the requested
revision matches a branch on a configured remote and fail with a message
explaining that ":<version>" only supports tags or commit hashes.

(cherry picked from commit 540842b76a)
2026-11-20 17:04:49 +01:00
Linus Gasser 2f0f3880e6 fix(formatter): guard ShortenID/SmallSHA against short input
ShortenID and SmallSHA sliced their input to a fixed length without
checking it was long enough, panicking on shorter strings. Return the
input unchanged when it is already shorter than the cut. Also replace the
blank Commit placeholder with an explicit "unknown-commit" sentinel.

(cherry picked from commit eeb4ea8b9c)
2026-11-20 17:04:49 +01:00
Linus GasserandClaude Opus 5.5 6fc26afad6 audit: check that fixes exist, short recipe names, list layout
- When a recipe upgrade is needed, search the recipe's newer releases for
  the oldest one whose images are fixed, and complain if there is none.
  Images are read from compose*.yml at each release tag; only tags are
  fetched, the local checkout is never changed.
- For recipe version ranges, check that the advisory's fixed version is
  a released tag.
- Show recipes relative to git.coopcloud.tech/coop-cloud/ (nextcloud,
  c4dt/nextcloud, or the full path). Advisories match recipes the same
  way, so forks are not matched by the canonical recipe's ranges.
- Replace the wide table by a narrow numbered table plus a numbered list
  of fixes wrapped to the terminal width; shorten SSH errors of
  unreachable servers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 16:54:57 +01:00
Linus GasserandClaude Opus 5.5 d691241414 Treat an empty servers directory as nothing to audit
LoadAppFiles("") fails on an empty servers directory; report "no apps
found" and exit 0 instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 16:33:30 +01:00
Linus GasserandClaude Opus 5.5 491ea47f50 Check the registry before suggesting a redeploy
A redeploy only fixes an image if its tag now points to a fixed image.
Patch-pinned tags such as nextcloud:35.0.2-fpm are not rebuilt once the
next patch is out, so suggesting a redeploy was wrong for them.

For findings with strategy refresh, read the tag's current image for
the server's platform and apply the advisory again. If it is still not
fixed, suggest a recipe upgrade instead; if the registry cannot be
reached, mark the suggestion as unchecked.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 16:32:47 +01:00
Linus GasserandClaude Opus 5.5 165a81de91 Fix abra audit not finding any apps
LoadAppFiles only scans all servers when it gets a single empty string;
called without arguments it scanned none, so audit always reported
"no apps found".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 16:30:49 +01:00
Linus GasserandClaude Opus 5.5 907a7eaa08 Fix panic on short commit hashes
SmallSHA, ShortenID and the version string sliced fixed lengths, so a
binary built without ldflags (commit = 7 spaces) panicked on every
command. Add formatter.Truncate and use it in all three places.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 16:09:53 +01:00
Linus Gasser d699e51cda First audit version 2026-11-20 16:05:57 +01:00
Linus Gasser 4e88ff9d34 Updating vendor directory 2026-11-20 16:05:35 +01:00
Linus GasserandClaude Opus 5.5 e07454ec46 Add design doc for abra audit
Proposes an `abra audit` command that checks deployed apps against a
list of advisories, covering mutable image tags, stale base images and
vulnerable recipe versions, plus `abra audit update` to draft
advisories from CVEs or minimum library versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-20 15:42:42 +01:00
Linus Gasser e5804d224d Allow digests in docker images
For the nextcloud update we need to be able to indicate the
digest of the docker image, as the docker tag is the same
for the vulnerable and the fixed docker image.
This avoids 'abra update' to re-install with a warning an
old image if the connection to docker hub fails.
2026-11-20 14:16:01 +01:00
renovate-bot 9023bb87c3 chore(deps): update nginx docker tag to v1.31.9 2026-11-18 17:01:13 +00:00
ChasquiLabo b15c039e06 chore: translation using Weblate (Spanish)
Currently translated at 91.5% (1056 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-13 02:01:19 +00:00
ChasquiLabo f3536cfaef chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-13 01:15:12 +00:00
ChasquiLabo 0f4c9f3899 chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-13 01:05:34 +00:00
ChasquiLabo caddaf8869 chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-13 01:02:01 +00:00
ChasquiLabo fcd3c10368 chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-13 00:15:36 +00:00
ChasquiLabo 0d5d2b67a3 chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-13 00:08:54 +00:00
ChasquiLabo e611b8644b chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 23:49:17 +00:00
ChasquiLabo 172182b9a5 chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 23:45:21 +00:00
ChasquiLabo 99ee157e8b chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 23:39:15 +00:00
ChasquiLabo 1f6d21f69d chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 23:25:51 +00:00
ChasquiLabo 94c710296d chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 23:06:42 +00:00
ChasquiLabo 53df7f876c chore: translation using Weblate (Spanish)
Currently translated at 91.3% (1054 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 22:51:26 +00:00
ChasquiLabo eb591f0843 chore: translation using Weblate (Spanish)
Currently translated at 91.1% (1052 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 22:06:33 +00:00
ChasquiLabo d9b3fa41cd chore: translation using Weblate (Spanish)
Currently translated at 91.1% (1052 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 19:38:22 +00:00
ChasquiLabo 6af3a2354d chore: translation using Weblate (Spanish)
Currently translated at 91.1% (1052 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 19:32:36 +00:00
ChasquiLabo 96c5a8406f chore: translation using Weblate (Spanish)
Currently translated at 91.0% (1051 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 18:50:28 +00:00
ChasquiLabo 2b374e9b08 chore: translation using Weblate (Spanish)
Currently translated at 91.0% (1051 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 18:48:42 +00:00
ChasquiLabo 0caf585969 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 17:35:19 +00:00
ChasquiLabo df7cc8048a chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 16:49:17 +00:00
ChasquiLabo f134b70d49 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-12 14:21:32 +00:00
ChasquiLabo 7517798a21 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 23:42:02 +00:00
ChasquiLabo 0f06c39a35 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 23:35:28 +00:00
ChasquiLabo 7c660b299a chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 23:13:05 +00:00
ChasquiLabo d1d9eab103 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 23:00:37 +00:00
ChasquiLabo 1696aced77 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 22:36:45 +00:00
ChasquiLabo 5773074dd6 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 22:32:03 +00:00
ChasquiLabo 533051d63e chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 20:45:44 +00:00
ChasquiLabo b4cb9b3920 chore: translation using Weblate (Spanish)
Currently translated at 90.9% (1050 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 20:45:06 +00:00
ChasquiLabo d3bd408fa6 chore: translation using Weblate (Spanish)
Currently translated at 90.6% (1046 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 20:14:08 +00:00
ChasquiLabo bdd8831137 chore: translation using Weblate (Spanish)
Currently translated at 90.6% (1046 of 1154 strings)

Translation: Co-op Cloud/abra
Translate-URL: https://translate.coopcloud.tech/projects/co-op-cloud/abra/es/
2026-11-11 19:24:33 +00:00
renovate-bot 6761f1efa5 chore(deps): update module golang.org/x/term to v0.48.0 2026-11-09 19:01:08 +00:00
renovate-bot 5a600ce656 chore(deps): update module golang.org/x/sys to v0.50.0 2026-11-09 13:01:02 +00:00
83 changed files with 10813 additions and 1307 deletions
+2
View File
@@ -8,3 +8,5 @@
dist/
tests/integration/.bats
tests/resources/path_to_repo
advisories
.devbox
+37
View File
@@ -0,0 +1,37 @@
# AGENTS.md
This is the C4DT fork of [`toolshed/abra`](https://git.coopcloud.tech/toolshed/abra), the Co-op Cloud command-line tool. What differs from upstream is described in [README_C4DT.md](README_C4DT.md); keep that file up to date when adding a change.
## Branches
| Branch | Content |
| --- | --- |
| `main` | Mirror of upstream `toolshed/abra` `main`. No C4DT commits. |
| `c4dt` | The fork's release line: upstream plus all C4DT changes. Release tags are made here. |
| feature branches, e.g. `audit_v1` | Work in progress, merged into `c4dt` when ready. |
- Start new work from `c4dt`, in a feature branch.
- To follow upstream, update `main` from `toolshed/abra`, then merge or rebase `c4dt` onto it. Update the base commit and date in `README.md` and `README_C4DT.md`.
- Changes taken from other forks are cherry-picked with `git cherry-pick -x`, so the original commit stays recorded.
## Tags
Releases are annotated tags on `c4dt`, named `<upstream version>-c4dt.<n>`, e.g. `0.13.0-beta-c4dt.1`:
- `<upstream version>` is the latest upstream tag the fork is based on.
- `<n>` counts the C4DT releases on that base, starting at 1, and restarts at 1 when the upstream version changes.
- Tags are never moved or deleted: users pin them, so a tag must always build the same code.
Build a release with the tag as version, so `abra --version` prints `<tag>-<commit>`:
```bash
go build -ldflags "-X main.Version=<tag> -X main.Commit=$(git rev-parse HEAD)" -o abra ./cmd/abra
```
## Development
- Toolchain: `devbox.json`. Use `devbox run build` and `devbox run test`; the test script sets a temporary `ABRA_DIR`, which the tests require.
- Dependencies are vendored: after changing `go.mod`, run `go mod vendor`.
- `abra audit` is designed in [`doc/DesignAudit.md`](doc/DesignAudit.md), which also lists what later versions will add. Keep the design and the code in sync.
- Advisories for `abra audit` live in a separate repository, [`c4dt/advisories`](https://git.coopcloud.tech/c4dt/advisories), under `advisories/`.
- User-facing strings are wrapped in `i18n.G`.
+4
View File
@@ -5,6 +5,10 @@
[![Go Reference](https://pkg.go.dev/badge/coopcloud.tech/abra.svg)](https://pkg.go.dev/coopcloud.tech/abra)
[![Translation status](https://translate.coopcloud.tech/widget/co-op-cloud/svg-badge.svg)](https://translate.coopcloud.tech/engage/co-op-cloud/)
> [!NOTE]
> This is an LLM-modified version of `abra`, based on upstream [`toolshed/abra`](https://git.coopcloud.tech/toolshed/abra) `main` of 2026-11-18 (`0.13.0-beta` + 163 commits, `9023bb87`).
> See [README_C4DT.md](README_C4DT.md) for the differences.
The Co-op Cloud utility belt 🎩🐇
<a href="https://github.com/egonelbre/gophers"><img align="right" width="150" src="https://github.com/egonelbre/gophers/raw/master/.thumb/sketch/adventure/poking-fire.png"/></a>
+39
View File
@@ -0,0 +1,39 @@
# C4DT changes to `abra`
This fork of [`toolshed/abra`](https://git.coopcloud.tech/toolshed/abra) is maintained by C4DT. The changes were made with the help of an LLM coding assistant.
It is based on upstream `main` at commit `9023bb87` of 2026-11-18, which is 163 commits after the `0.13.0-beta` tag.
Releases are tags on the `c4dt` branch, named `<upstream version>-c4dt.<n>`, e.g. `0.13.0-beta-c4dt.1`. Pin a tag rather than following a branch. See [AGENTS.md](AGENTS.md) for the branch and tag conventions.
## Overview
| Change | Summary |
| --- | --- |
| [Digest pinning](#digest-pinning) | Recipes can pin images to a digest; `abra recipe upgrade` keeps the pin up to date |
| [`abra audit`](#abra-audit) | Checks all deployed apps against reviewed advisories and suggests fixes that are verified to exist |
| [Type remote](#type-remote) | Recipes can come from any git URL, and be pinned to a branch, in `RECIPE=` |
| [Tables without a terminal](#tables-without-a-terminal) | Table output works in pipes and cron jobs instead of failing |
| [Devbox](#devbox) | `devbox.json` with the toolchain, and `build` and `test` scripts |
## Details
### Digest pinning
Some upstream images, such as `nextcloud`, are rebuilt under the same tag, so the tag alone cannot tell a vulnerable image from a fixed one. Recipes can now pin an image to a digest, for example `nextcloud:34.0.5-fpm@sha256:...`. `abra recipe upgrade` keeps the digest in sync: when it moves a pinned image to a new tag, it writes the new tag's digest, and when the current tag was re-published upstream, it warns and offers to update the pin. A pinned digest also makes a failed registry lookup at deploy time visible, instead of Docker silently keeping an old local image. To support this, `pkg/client/registry.go` gained `GetRegistryDigest`, which returns the index digest that Docker resolves `image:tag` to, and now requests OCI image indexes, which many multi-arch images on Docker Hub require. Commit `e5804d22`.
### `abra audit`
`abra audit` checks every deployed app on every server against the reviewed advisories in [`c4dt/advisories`](https://git.coopcloud.tech/c4dt/advisories), which it pulls on every run. It reads the recipe version and the image digests from each service spec, and the image configs (build time, environment such as `PHP_VERSION`, version labels) from the server's Docker daemon, or from the registry for the server's platform. Advisories match by recipe version range, environment version range, image version, build time, and known-good or known-bad digests. Before suggesting a fix, the audit checks that it works: a redeploy is only suggested if the tag's current image is fixed, and a recipe upgrade names the oldest release whose images are fixed. If no release fixes the problem yet, it says so and warns; recipe repositories are only read from their release tags, so local checkouts are never changed. The output is a short numbered table of failing apps followed by a numbered list of fixes, and the exit code is 1 when anything fails. Unlike the rest of abra, the audit needs an explicitly chosen abra directory (`$ABRA_DIR` or `abraDir` in `abra.yaml`) and never falls back to `~/.abra`. The design, including the planned later versions, is in [`doc/DesignAudit.md`](doc/DesignAudit.md). Apps deployed with `--chaos` are recognised: the table shows the deployed commit, a suggested redeploy keeps `--chaos` (without it, the deploy would replace the chaos deployment with the release in the `.env` file), what a `--chaos` redeploy would use (the app's `COMPOSE_FILE` with its overrides, loaded as deploy does) is checked for fixed images before the releases, and each finding has an action that says who has to act: redeploy, upgrade, recipe must update (a fixed image exists in the registry, found by listing the repository's tags and ordering rebuilds such as `v6.1.0-1` or `v6.1.0-fix1` by build time), upstream must update (no fixed image exists), check by hand, or follow advisory. A redeploy is only suggested if it deploys a fixed image, taking digests pinned in the recipe into account. The table shows what is affected (e.g. `nginx 1.31.8`) instead of the advisory ID, which moves to the end of the fix line together with the advisory's CVEs. Commits `e07454ec`, `d699e51c`, `165a81de`, `491ea47f`, `d6912414`, `6fc26afa`, `cef4dc2e`, `527d2a6a`, `82564ded`, `371b5aad`, `d19f174e`.
### Type remote
Recipes no longer have to come from the catalogue. `abra app new` accepts a git URL in any common form (`https://`, `ssh://`, `git@host:path`), which is normalized to a canonical `host/path` name, and the app's `.env` gets a `RECIPE=<host/path>` line so that a later deploy, possibly on another machine, fetches the recipe from the same source. The clone records its source in a `.abra-source` file, and `abra recipe ls` shows these external recipes in a source column. The `:<version>` suffix in `RECIPE=` may now also be a branch name: it is fetched on demand into `refs/remotes/origin/*` and checked out at its tip as a chaos commit, after tags and commit hashes, and fails clearly in offline mode if the branch was never fetched. The deployed version is now stored in a single `.env` field, and upgrade and rollback set the version label themselves, because git-URL recipes often carry a stale label in `compose.yml`. Cherry-picked from `ineiti/abra` branch `type_remote` (commits `73227c9e`, `0a157896`, `0c91585e`, `47d9f732`, `5f80d5e8`); `9170b4d2` fixes one of its tests, which did not test the unfetched case it was named for.
### Tables without a terminal
`formatter.PrintTable` read the width of stdin and failed with `operation not supported by device` when it was not a terminal, so `abra audit` and every other table command failed in a pipe or cron job. It now reads the width of stdout and prints the table without a width limit when there is no terminal. Commit `5c4ffaf2`.
### Devbox
A `devbox.json` pins the toolchain (Go 1.27, make, gettext): `devbox run build` builds abra, and `devbox run test` runs the tests with a temporary `ABRA_DIR`, which the existing tests require. Along with it, the `vendor/` directory was regenerated with `go mod vendor`, because it was out of sync with `go.mod` after an upstream dependency update and the default build failed. A binary built with a plain `go build`, without the `-ldflags` that set the commit, no longer panics: commit hashes are now truncated safely, and the default commit is `unknown-commit`. Commits `4e88ff9d`, `d699e51c`, `907a7eaa`, `2f0f3880`, `836971d6`.
+7 -1
View File
@@ -222,7 +222,13 @@ synchronize your local app environment values with what is deployed live.`),
mergedEnv[k] = v
}
if !strings.Contains(recipeEnvVar, ":") {
// The recipe version is always carried by TYPE (RECIPE, when present,
// is a bare source pointer). Stamp the deployed version onto TYPE and
// leave RECIPE untouched so the two never drift apart.
if t, ok := mergedEnv["TYPE"]; ok {
name, _, _ := strings.Cut(t, ":")
mergedEnv["TYPE"] = fmt.Sprintf("%s:%s", name, version)
} else {
mergedEnv[recipeKey] = fmt.Sprintf("%s:%s", mergedEnv[recipeKey], version)
}
+17 -1
View File
@@ -32,6 +32,18 @@ deploy <domain>" to do so.
You can see what recipes are available (i.e. values for the [recipe] argument)
by running "abra recipe ls".
In addition to short catalogue names, [recipe] also accepts arbitrary git
URLs to use a recipe from outside the catalogue (e.g. a fork or work in
progress). Any of these forms is accepted:
abra app new git.example.com/user/recipe
abra app new https://git.example.com/user/recipe
abra app new git@git.example.com:user/recipe
In that case a RECIPE=<canonical name> line is written to the app's .env
file so a subsequent "abra app deploy" (on this or another machine) will
re-fetch the recipe from the same git source.
Recipe commit hashes are supported values for "[version]".
Passing the "--secrets/-S" flag will automatically generate secrets for your
@@ -295,7 +307,7 @@ func ensureDomainFlag(recipe recipePkg.Recipe, server string) error {
if appDomain == "" && !internal.NoInput {
prompt := &survey.Input{
Message: i18n.G("Specify app domain"),
Default: fmt.Sprintf("%s.%s", recipe.Name, server),
Default: fmt.Sprintf("%s.%s", recipe.ShortName(), server),
}
if err := survey.AskOne(prompt, &appDomain); err != nil {
return err
@@ -306,6 +318,10 @@ func ensureDomainFlag(recipe recipePkg.Recipe, server string) error {
return errors.New(i18n.G("no domain provided"))
}
if strings.ContainsAny(appDomain, "/\\") {
return errors.New(i18n.G("invalid domain '%s': must not contain '/' or '\\'", appDomain))
}
return nil
}
+6
View File
@@ -188,6 +188,12 @@ beforehand. See "abra app backup" for more.`),
appPkg.SetChaosVersionLabel(compose, stackName, chosenDowngrade)
}
// NOTE: stamp the deployed version label ourselves rather than relying
// on the value baked into the recipe's compose.yml. Git-URL/WIP recipes
// often carry a stale or missing label, which would otherwise leave the
// live deployment reporting the wrong version after a rollback.
appPkg.SetVersionLabel(compose, stackName, chosenDowngrade)
// Gather secrets
secretInfo, err := deploy.GatherSecretsForDeploy(cl, app, internal.ShowUnchanged)
if err != nil {
+24 -4
View File
@@ -200,6 +200,12 @@ beforehand. See "abra app backup" for more.`),
appPkg.SetChaosVersionLabel(compose, stackName, chosenUpgrade)
}
// NOTE: stamp the deployed version label ourselves rather than relying
// on the value baked into the recipe's compose.yml. Git-URL/WIP recipes
// often carry a stale or missing label, which would otherwise leave the
// live deployment reporting the wrong version after an upgrade.
appPkg.SetVersionLabel(compose, stackName, chosenUpgrade)
envVars, err := appPkg.CheckEnv(app)
if err != nil {
log.Fatal(err)
@@ -346,9 +352,17 @@ func getReleaseNotes(
return errors.New(i18n.G("parsing chosen upgrade version failed: %s", err))
}
parsedDeployedVersion, err := tagcmp.Parse(deployMeta.Version)
if err != nil {
return errors.New(i18n.G("parsing deployment version failed: %s", err))
// The deployed version can be "unknown" (e.g. deployed without a version
// label, as is common for git-URL/work-in-progress recipes). In that case
// there is no lower bound to filter by, so we gather notes for every
// version below the chosen one instead of failing.
var parsedDeployedVersion tagcmp.Tag
haveDeployedVersion := deployMeta.Version != config.UNKNOWN_DEFAULT
if haveDeployedVersion {
parsedDeployedVersion, err = tagcmp.Parse(deployMeta.Version)
if err != nil {
return errors.New(i18n.G("parsing deployment version failed: %s", err))
}
}
for _, version := range internal.SortVersionsDesc(versions) {
@@ -357,7 +371,7 @@ func getReleaseNotes(
return errors.New(i18n.G("parsing recipe version failed: %s", err))
}
if parsedVersion.IsGreaterThan(parsedDeployedVersion) &&
if (!haveDeployedVersion || parsedVersion.IsGreaterThan(parsedDeployedVersion)) &&
parsedVersion.IsLessThan(parsedChosenUpgrade) {
note, err := app.Recipe.GetReleaseNotes(version, app.Domain)
if err != nil {
@@ -420,6 +434,12 @@ func validateUpgradeVersionArg(
return errors.New(i18n.G("'%s' is not a known version for %s", specificVersion, app.Recipe.Name))
}
// Without a known deployed version we cannot tell whether the requested
// version is an upgrade; trust the explicit choice rather than failing.
if deployMeta.Version == config.UNKNOWN_DEFAULT {
return nil
}
parsedDeployedVersion, err := tagcmp.Parse(deployMeta.Version)
if err != nil {
return errors.New(i18n.G("'%s' is not a known version", deployMeta.Version))
+186
View File
@@ -0,0 +1,186 @@
// Package audit implements the `abra audit` command.
package audit
import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"coopcloud.tech/abra/cli/internal"
appPkg "coopcloud.tech/abra/pkg/app"
auditPkg "coopcloud.tech/abra/pkg/audit"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"github.com/spf13/cobra"
)
// AuditCommand audits all deployed apps on all servers.
var AuditCommand = &cobra.Command{
// translators: `abra audit` command
Use: i18n.G("audit [flags]"),
// translators: Short description for `abra audit` command
Short: i18n.G("Audit deployed apps against known advisories"),
Long: i18n.G(`Check every deployed app on every server against a curated list of
advisories, and suggest how to fix what fails.
The advisories are pulled from %s into $ABRA_DIR/advisories
on every run. If the repository cannot be reached, the local copy is used.
Only published advisories are applied; use --include-drafts to also apply
drafts, e.g. to try a new advisory before publishing it.
The audit needs an explicitly chosen abra directory: set $ABRA_DIR, or set
abraDir in an abra.yaml file in the current directory or one of its parents.
Only failing apps are listed. The exit code is 0 when nothing fails, and 1
otherwise.`, auditPkg.DefaultAdvisoriesURL),
Example: i18n.G(` # audit all apps on all servers
ABRA_DIR=~/.abra abra audit
# also apply advisories that are still drafts
abra audit --include-drafts`),
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
os.Exit(run())
},
}
func run() int {
abraDir := config.ABRA_DIR_EXPLICIT
if abraDir == "" {
log.Fatal(i18n.G("abra audit needs an explicitly chosen abra directory: set $ABRA_DIR, or abraDir in an abra.yaml file"))
}
advDir := filepath.Join(abraDir, "advisories")
if err := auditPkg.SyncAdvisories(advDir, auditPkg.DefaultAdvisoriesURL, internal.Offline); err != nil {
log.Fatal(err)
}
advisories, warnings, err := auditPkg.LoadAdvisories(filepath.Join(advDir, "advisories"))
if err != nil {
log.Fatal(err)
}
for _, w := range warnings {
log.Warn(w)
}
evalOpts := auditPkg.EvaluateOptions{IncludeDrafts: includeDrafts}
var used, drafts int
for _, a := range advisories {
if evalOpts.Applies(a) {
used++
if a.Status == auditPkg.StatusDraft {
drafts++
}
}
}
log.Debug(i18n.G("loaded %d advisories, using %d", len(advisories), used))
if includeDrafts {
log.Info(i18n.G("including %d draft advisories, which have not been reviewed yet", drafts))
}
if entries, err := os.ReadDir(config.SERVERS_DIR); err == nil && len(entries) == 0 {
log.Info(i18n.G("no apps found in %s", config.SERVERS_DIR))
return 0
}
appFiles, err := appPkg.LoadAppFiles("") // "" means all servers
if err != nil {
log.Fatal(err)
}
apps, err := appPkg.GetApps(appFiles, "")
if err != nil {
log.Fatal(err)
}
if len(apps) == 0 {
log.Info(i18n.G("no apps found in %s", config.SERVERS_DIR))
return 0
}
states, serverErrs := auditPkg.CollectState(apps, auditPkg.CollectOptions{Offline: internal.Offline})
findings := auditPkg.Evaluate(states, advisories, evalOpts)
if !internal.Offline {
var recipes []recipe.Recipe
for _, app := range apps {
recipes = append(recipes, app.Recipe)
}
byName := make(map[string]appPkg.App)
for _, app := range apps {
byName[app.Name] = app
}
checker := auditPkg.FixChecker{
Fetch: auditPkg.RegistryFetch,
Tags: auditPkg.RegistryTags,
Recipes: auditPkg.NewGitRecipes(recipes, internal.Offline),
Checkout: func(name string) ([]string, error) {
return auditPkg.CheckoutImages(byName[name])
},
}
checker.CheckFixes(context.Background(), findings)
}
for _, f := range findings {
for _, r := range f.Reasons {
log.Debug(fmt.Sprintf("%s %s: %s", f.App, f.Advisory.ID, r))
}
}
sort.SliceStable(findings, func(i, j int) bool {
if findings[i].Server != findings[j].Server {
return findings[i].Server < findings[j].Server
}
return findings[i].App < findings[j].App
})
if len(findings) > 0 || len(serverErrs) > 0 {
if err := printReport(findings, serverErrs); err != nil {
log.Fatal(err)
}
}
servers := make(map[string]bool)
for _, st := range states {
servers[st.Server] = true
}
log.Info(i18n.G(
"audit done: deployed apps: %d, servers: %d, advisories: %d, findings: %d, unreachable servers: %d",
len(states), len(servers), used, len(findings), len(serverErrs),
))
// findings that the operator cannot fix alone, by who has to act
waiting := make(map[string][]string)
for _, f := range findings {
switch f.Action {
case auditPkg.ActionRecipe, auditPkg.ActionUpstream, auditPkg.ActionCheck:
waiting[f.Action] = append(waiting[f.Action], fmt.Sprintf("%s (%s)", f.App, auditPkg.ShortRecipeName(f.Recipe)))
}
}
for _, action := range []string{auditPkg.ActionUpstream, auditPkg.ActionRecipe, auditPkg.ActionCheck} {
if apps := waiting[action]; len(apps) > 0 {
log.Warn(fmt.Sprintf("%s: %s", actionLabel(action), strings.Join(apps, ", ")))
}
}
if len(findings) > 0 || len(serverErrs) > 0 {
return 1
}
return 0
}
var includeDrafts bool
func init() {
AuditCommand.Flags().BoolVar(
&includeDrafts,
i18n.G("include-drafts"),
false,
i18n.G("also apply draft advisories, to try them before publishing"),
)
}
+194
View File
@@ -0,0 +1,194 @@
package audit
import (
"fmt"
"os"
"strconv"
"strings"
auditPkg "coopcloud.tech/abra/pkg/audit"
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/i18n"
"golang.org/x/term"
)
// printReport prints a narrow overview table, followed by a numbered list
// with the fix for each row. Long texts do not fit in a table, so they go in
// the list, wrapped to the terminal width.
func printReport(findings []auditPkg.Finding, serverErrs []auditPkg.ServerError) error {
servers := make(map[string]bool)
for _, f := range findings {
servers[f.Server] = true
}
showServer := len(servers) > 1
if len(findings) > 0 {
table, err := formatter.CreateTable()
if err != nil {
return err
}
headers := []string{"#"}
if showServer {
headers = append(headers, i18n.G("SERVER"))
}
headers = append(headers, i18n.G("APP"), i18n.G("RECIPE"), i18n.G("VERSION"), i18n.G("AFFECTED"), i18n.G("SEVERITY"), i18n.G("ACTION"))
table.Headers(headers...)
for i, f := range findings {
row := []string{strconv.Itoa(i + 1)}
if showServer {
row = append(row, f.Server)
}
row = append(row, f.App, auditPkg.ShortRecipeName(f.Recipe), displayVersion(f), f.Affected, f.Advisory.Severity, displayAction(f))
table.Row(row...)
}
if err := formatter.PrintTable(table); err != nil {
return err
}
fmt.Println()
width := termWidth()
for i, f := range findings {
prefix := fmt.Sprintf("%d. ", i+1)
fmt.Println(wrap(prefix+f.App+": "+fixLine(f), width, strings.Repeat(" ", len(prefix))))
}
}
if len(serverErrs) > 0 {
if len(findings) > 0 {
fmt.Println()
}
width := termWidth()
for _, e := range serverErrs {
fmt.Println(wrap(i18n.G("unreachable: %s: %s", e.Server, shortError(e.Err)), width, " "))
}
}
return nil
}
// actionLabel translates a finding action.
func actionLabel(action string) string {
switch action {
case auditPkg.ActionRedeploy:
return i18n.G("redeploy")
case auditPkg.ActionUpgrade:
return i18n.G("upgrade")
case auditPkg.ActionRecipe:
return i18n.G("recipe must update")
case auditPkg.ActionUpstream:
return i18n.G("upstream must update")
case auditPkg.ActionCheck:
return i18n.G("check by hand")
case auditPkg.ActionAdvisory:
return i18n.G("follow advisory")
}
return action
}
// displayAction is the action shown in the table. A finding whose images
// could not all be checked (status unknown) needs a look first.
func displayAction(f auditPkg.Finding) string {
if f.Status == auditPkg.FindingUnknown {
return actionLabel(auditPkg.ActionCheck)
}
return actionLabel(f.Action)
}
// fixLine starts with the action, so that the first words say who acts,
// and ends with the references to look the advisory up.
func fixLine(f auditPkg.Finding) string {
line := actionLabel(f.Action) + ". " + f.Fix
if f.Status == auditPkg.FindingUnknown {
line = i18n.G("%s. Not every image could be checked (see abra audit -d). If it is affected: %s. %s",
actionLabel(auditPkg.ActionCheck), actionLabel(f.Action), f.Fix)
}
return line + " (" + advisoryRefs(f.Advisory) + ")"
}
// advisoryRefs names the advisory and its CVEs: "CCA-2026-0002,
// CVE-2026-123007 and 4 more". Drafts are marked.
func advisoryRefs(adv *auditPkg.Advisory) string {
refs := adv.ID
if adv.Status == auditPkg.StatusDraft {
refs += " " + i18n.G("(draft)")
}
var cves []string
for _, r := range adv.Refs {
if strings.HasPrefix(r, "CVE-") {
cves = append(cves, strings.Fields(r)[0])
}
}
switch len(cves) {
case 0:
case 1:
refs += ", " + cves[0]
default:
refs += ", " + i18n.G("%s and %d more", cves[0], len(cves)-1)
}
return refs
}
// displayVersion shows what is deployed: the recipe version, or for a
// --chaos deployment the commit it was deployed from, as abra app ls does.
func displayVersion(f auditPkg.Finding) string {
if !f.Chaos {
return f.Version
}
if f.ChaosVersion == "" {
return f.Version + " " + i18n.G("(chaos)")
}
return f.ChaosVersion + " " + i18n.G("(chaos)")
}
// termWidth returns the terminal width, or 0 (no wrapping) when stdout is not
// a terminal or ABRA_CI is set.
func termWidth() int {
if os.Getenv("ABRA_CI") == "1" {
return 0
}
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil || w < 40 {
return 0
}
return w - 1
}
// wrap breaks text into lines of at most width characters at spaces,
// indenting continuation lines. A width of 0 disables wrapping.
func wrap(text string, width int, indent string) string {
if width <= 0 || len(text) <= width {
return text
}
var lines []string
line := ""
for _, word := range strings.Fields(text) {
switch {
case line == "":
line = word
case len(line)+1+len(word) > width:
lines = append(lines, line)
line = indent + word
default:
line += " " + word
}
}
if line != "" {
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
// shortError keeps the useful part of docker/SSH connection errors, which
// otherwise contain several lines of boilerplate.
func shortError(err error) string {
msg := err.Error()
if i := strings.LastIndex(msg, "stderr="); i >= 0 {
msg = msg[i+len("stderr="):]
}
return strings.TrimSpace(msg)
}
+53
View File
@@ -0,0 +1,53 @@
package audit
import (
"errors"
"testing"
auditPkg "coopcloud.tech/abra/pkg/audit"
"github.com/stretchr/testify/assert"
)
func TestWrap(t *testing.T) {
assert.Equal(t, "short", wrap("short", 40, " "))
assert.Equal(t, "1. one two\n three\n four five", wrap("1. one two three four five", 12, " "))
assert.Equal(t, "no wrapping at all here", wrap("no wrapping at all here", 0, " "))
}
func TestShortError(t *testing.T) {
err := errors.New(`error during connect: Get "http://docker.example.com/v1.51/info": command [ssh -- host docker system dial-stdio] has exited with exit status 255, please make sure the URL is valid, and Docker 18.09 or later is installed on the remote host: stderr=ssh: connect to host srv port 22: No route to host
`)
assert.Equal(t, "ssh: connect to host srv port 22: No route to host", shortError(err))
assert.Equal(t, "plain", shortError(errors.New("plain")))
}
func TestDisplayVersion(t *testing.T) {
assert.Equal(t, "1.0.0+1.2.3", displayVersion(auditPkg.Finding{Version: "1.0.0+1.2.3"}))
assert.Equal(t, "1234abcd+U (chaos)", displayVersion(auditPkg.Finding{Version: "1.0.0+1.2.3", Chaos: true, ChaosVersion: "1234abcd+U"}))
assert.Equal(t, "1.0.0+1.2.3 (chaos)", displayVersion(auditPkg.Finding{Version: "1.0.0+1.2.3", Chaos: true}))
}
func TestFixLine(t *testing.T) {
adv := &auditPkg.Advisory{ID: "CCA-1", Status: auditPkg.StatusPublished}
f := auditPkg.Finding{Advisory: adv, Status: auditPkg.FindingAffected, Action: auditPkg.ActionUpstream, Fix: "No fixed image exists yet."}
assert.Equal(t, "upstream must update", displayAction(f))
assert.Equal(t, "upstream must update. No fixed image exists yet. (CCA-1)", fixLine(f))
// an image that could not be checked needs a look first
f = auditPkg.Finding{Advisory: adv, Status: auditPkg.FindingUnknown, Action: auditPkg.ActionRedeploy, Fix: "abra app deploy app --force"}
assert.Equal(t, "check by hand", displayAction(f))
assert.Equal(t, "check by hand. Not every image could be checked (see abra audit -d). If it is affected: redeploy. abra app deploy app --force (CCA-1)", fixLine(f))
}
func TestAdvisoryRefs(t *testing.T) {
adv := &auditPkg.Advisory{ID: "CCA-2026-0002", Status: auditPkg.StatusPublished}
assert.Equal(t, "CCA-2026-0002", advisoryRefs(adv))
adv.Refs = []string{"https://nginx.org/en/security_advisories.html", "CVE-2026-123007"}
assert.Equal(t, "CCA-2026-0002, CVE-2026-123007", advisoryRefs(adv))
adv.Refs = append(adv.Refs, "CVE-2026-113425", "CVE-2026-123633")
adv.Status = auditPkg.StatusDraft
assert.Equal(t, "CCA-2026-0002 (draft), CVE-2026-123007 and 2 more", advisoryRefs(adv))
}
+9 -3
View File
@@ -59,9 +59,15 @@ func ValidateRecipe(args []string, cmdName string) recipe.Recipe {
log.Fatal(i18n.G("no recipe name provided"))
}
if _, ok := knownRecipes[recipeName]; !ok {
if !strings.Contains(recipeName, "/") {
log.Fatal(i18n.G("no recipe '%s' exists?", recipeName))
recipeName = recipe.NormalizeRecipeName(recipeName)
lookupName := recipeName
if i := strings.LastIndex(lookupName, ":"); i >= 0 {
lookupName = lookupName[:i]
}
if _, ok := knownRecipes[lookupName]; !ok {
if !strings.Contains(lookupName, "/") {
log.Fatal(i18n.G("no recipe '%s' exists? pass a git URL (e.g. https://git.example.com/user/recipe) to use a recipe outside the catalogue", lookupName))
}
}
+45
View File
@@ -2,11 +2,13 @@ package recipe
import (
"fmt"
"path"
"sort"
"strconv"
"strings"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/pkg/config"
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
@@ -41,6 +43,7 @@ var RecipeListCommand = &cobra.Command{
headers := []string{
i18n.G("name"),
i18n.G("source"),
i18n.G("category"),
i18n.G("status"),
i18n.G("healthcheck"),
@@ -56,6 +59,7 @@ var RecipeListCommand = &cobra.Command{
for _, recipe := range recipes {
row := []string{
recipe.Name,
i18n.G("catalogue"),
recipe.Category,
strconv.Itoa(recipe.Features.Status),
recipe.Features.Healthcheck,
@@ -76,6 +80,23 @@ var RecipeListCommand = &cobra.Command{
}
}
externals := externalRecipes(catl)
sort.Strings(externals)
for _, name := range externals {
row := []string{
name,
i18n.G("external"),
"-", "-", "-", "-", "-", "-", "-",
}
if pattern != "" {
if !strings.Contains(name, pattern) {
continue
}
}
table.Row(row...)
rows = append(rows, row)
}
if len(rows) > 0 {
if internal.MachineReadable {
out, err := formatter.ToJSON(headers, rows)
@@ -93,6 +114,30 @@ var RecipeListCommand = &cobra.Command{
},
}
// externalRecipes returns canonical names of locally-cloned recipes that
// were sourced from an arbitrary git URL (i.e. they carry a .abra-source
// sidecar) and are not already listed in the catalogue.
func externalRecipes(catl recipe.RecipeCatalogue) []string {
dirs, err := recipe.GetRecipesLocal()
if err != nil {
log.Debug(i18n.G("can't read local recipes: %s", err))
return nil
}
var names []string
for _, dir := range dirs {
canonical := recipe.ReadRecipeSource(path.Join(config.RECIPES_DIR, dir))
if canonical == "" {
continue
}
if _, inCatalogue := catl[canonical]; inCatalogue {
continue
}
names = append(names, canonical)
}
return names
}
var (
pattern string
)
+51
View File
@@ -129,6 +129,9 @@ interface.`),
log.Fatal(err)
}
// digest pinned images shared between services are only checked once
digestChecked := make(map[string]bool)
for _, service := range config.Services {
img, err := reference.ParseNormalizedNamed(service.Image)
if err != nil {
@@ -178,6 +181,13 @@ interface.`),
sort.Sort(tagcmp.ByTagDesc(compatible))
if canonical, ok := img.(reference.Canonical); ok && !digestChecked[service.Image] {
digestChecked[service.Image] = true
if err := refreshDigest(recipe, img, image, canonical.Digest().String()); err != nil {
log.Fatal(err)
}
}
if len(compatible) == 0 && !allTags {
log.Info(i18n.G("no new versions available for %s, assuming %s is the latest (use -a/--all-tags to see all anyway)", image, tag))
continue // skip on to the next tag and don't update any compose files
@@ -364,6 +374,47 @@ interface.`),
},
}
// refreshDigest checks whether the current tag of a digest pinned image was
// re-published upstream with a new digest and offers to update the pin.
func refreshDigest(recipe recipePkg.Recipe, img reference.Named, image, digest string) error {
tag := img.(reference.NamedTagged).Tag()
registryDigest, err := client.GetRegistryDigest(img, tag)
if err != nil {
return err
}
if registryDigest == digest {
log.Debug(i18n.G("digest of %s:%s is up to date", image, tag))
return nil
}
log.Warn(i18n.G("%s:%s was re-published upstream (digest %s → %s)", image, tag, digest, registryDigest))
update := false
if !internal.NoInput {
prompt := &survey.Confirm{
Message: i18n.G("update digest of %s:%s?", image, tag),
Default: true,
}
if err := survey.AskOne(prompt, &update); err != nil {
return err
}
}
if !update {
return nil
}
if _, err := recipe.UpdateTag(image, tag); err != nil {
return err
}
log.Info(i18n.G("digest updated for %s:%s", image, tag))
return nil
}
var (
allTags bool
createCommit bool
+5 -1
View File
@@ -7,6 +7,7 @@ import (
"strings"
"coopcloud.tech/abra/cli/app"
"coopcloud.tech/abra/cli/audit"
"coopcloud.tech/abra/cli/catalogue"
"coopcloud.tech/abra/cli/internal"
"coopcloud.tech/abra/cli/recipe"
@@ -85,10 +86,12 @@ func Run(version, commit string) {
Config:
$ABRA_DIR: %s`, config.ABRA_DIR),
Version: fmt.Sprintf("%s-%s", version, commit[:7]),
Version: fmt.Sprintf("%s-%s", version, formatter.Truncate(commit, 7)),
ValidArgs: []string{
// translators: `abra app` command for autocompletion
i18n.G("app"),
// translators: `abra audit` command for autocompletion
i18n.G("audit"),
// translators: `abra autocomplete` command for autocompletion
i18n.G("autocomplete"),
// translators: `abra catalogue` command for autocompletion
@@ -254,6 +257,7 @@ Config:
AutocompleteCommand,
manCommand,
app.AppCommand,
audit.AuditCommand,
catalogue.CatalogueCommand,
server.ServerCommand,
recipe.RecipeCommand,
+1 -1
View File
@@ -16,7 +16,7 @@ func main() {
Version = "dev"
}
if Commit == "" {
Commit = " "
Commit = "unknown-commit"
}
cli.Run(Version, Commit)
+17
View File
@@ -0,0 +1,17 @@
{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/main/.schema/devbox.schema.json",
"packages": [
"go@1.27",
"gnumake@latest",
"gettext@latest",
"gnused@latest"
],
"shell": {
"scripts": {
"build": "make build",
"test": "export ABRA_DIR=\"$(mktemp -d)/abra\"; rc=0; make test || rc=$?; rm -rf \"$(dirname \"$ABRA_DIR\")\"; exit $rc",
"update-pot": "make update-pot",
"i18n": "make i18n"
}
}
}
+305
View File
@@ -0,0 +1,305 @@
{
"lockfile_version": "1",
"packages": {
"gettext@latest": {
"last_modified": "2026-10-20T14:23:57Z",
"resolved": "github:NixOS/nixpkgs/5adc50831c259f87faeb09ddf930d43597197473#gettext",
"source": "devbox-search",
"version": "1.0",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/36c168mc93w4mlmqknam03ffyi6qg0w6-gettext-1.0",
"default": true
},
{
"name": "man",
"path": "/nix/store/38mvyy6a3ga3xqbzzbzfhpmmd4dll8hl-gettext-1.0-man",
"default": true
},
{
"name": "doc",
"path": "/nix/store/4i2r8pmdirlpig74ymxmi03sgl0inyd4-gettext-1.0-doc"
},
{
"name": "info",
"path": "/nix/store/ml58bdjablvqamxwc28ws5y9222hdvpx-gettext-1.0-info"
}
],
"store_path": "/nix/store/36c168mc93w4mlmqknam03ffyi6qg0w6-gettext-1.0"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/qh35hki623iqrj2mqfxnzjqawxn93gk7-gettext-1.0",
"default": true
},
{
"name": "man",
"path": "/nix/store/v0arqmc3cx4ls1vwg4c62kz7apcn76ml-gettext-1.0-man",
"default": true
},
{
"name": "doc",
"path": "/nix/store/4ficj1yfkd8bn17mlv81jsib04hfyg8c-gettext-1.0-doc"
},
{
"name": "info",
"path": "/nix/store/w0slr4m7ywbkmlm8dbksjp22adcab935-gettext-1.0-info"
}
],
"store_path": "/nix/store/qh35hki623iqrj2mqfxnzjqawxn93gk7-gettext-1.0"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/9mxzw5vwn99vx2b74ipjzk8x4b784fjy-gettext-1.0",
"default": true
},
{
"name": "man",
"path": "/nix/store/r87hlf9pn8q3a8rnplzhy9lbgaadqcr8-gettext-1.0-man",
"default": true
},
{
"name": "doc",
"path": "/nix/store/xmssbi5i2w3fv67hrv6c29wdiv8gxywi-gettext-1.0-doc"
},
{
"name": "info",
"path": "/nix/store/j4lz4zqicd1mw2l5c272ialxhl0nkc0b-gettext-1.0-info"
}
],
"store_path": "/nix/store/9mxzw5vwn99vx2b74ipjzk8x4b784fjy-gettext-1.0"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/8qdw9shgr7dd3315jim8sfva2dnhq86c-gettext-1.0",
"default": true
},
{
"name": "man",
"path": "/nix/store/4acc3iagl3xlljb1aqn8j25vx5n400bc-gettext-1.0-man",
"default": true
},
{
"name": "doc",
"path": "/nix/store/dnz19fkr7spnpc7affh8ivcmk32g9xda-gettext-1.0-doc"
},
{
"name": "info",
"path": "/nix/store/v3z399ilsdjn461y0l2gi8h0z7jzsy6a-gettext-1.0-info"
}
],
"store_path": "/nix/store/8qdw9shgr7dd3315jim8sfva2dnhq86c-gettext-1.0"
}
}
},
"github:NixOS/nixpkgs/nixpkgs-unstable": {
"last_modified": "2026-10-20T14:23:57Z",
"resolved": "github:NixOS/nixpkgs/5adc50831c259f87faeb09ddf930d43597197473?lastModified=1792506237&narHash=sha256-Q%2F8KLB6jNfpaUN1HAedE%2Fu%2FEt9feE%2FzYSsutBLKrCFk%3D"
},
"gnumake@latest": {
"last_modified": "2026-10-20T14:23:57Z",
"resolved": "github:NixOS/nixpkgs/5adc50831c259f87faeb09ddf930d43597197473#gnumake",
"source": "devbox-search",
"version": "4.4.1",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/306s97mvw5slpgg7hnb26xy37dhy4xk0-gnumake-4.4.1",
"default": true
},
{
"name": "man",
"path": "/nix/store/g0hyk9rpf10di0l5x6gxjahh5rip30kn-gnumake-4.4.1-man",
"default": true
},
{
"name": "doc",
"path": "/nix/store/xl1nz3znzjb27wrig0vrf5fj3z7jqh5s-gnumake-4.4.1-doc"
},
{
"name": "info",
"path": "/nix/store/99bqn610vynhvhwg8srd9g2l990cfa7a-gnumake-4.4.1-info"
}
],
"store_path": "/nix/store/306s97mvw5slpgg7hnb26xy37dhy4xk0-gnumake-4.4.1"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/nk0i2hc4x8qpshj326jzn36hkbgi7j7z-gnumake-4.4.1",
"default": true
},
{
"name": "man",
"path": "/nix/store/nacivbyfzp1w0xydskbxrkapp2gn552s-gnumake-4.4.1-man",
"default": true
},
{
"name": "debug",
"path": "/nix/store/f7yag335k3y4ymn9hb5q1yzqc1njf1hi-gnumake-4.4.1-debug"
},
{
"name": "doc",
"path": "/nix/store/782lakmsm9nxf4hpnn8whmq541cphv6r-gnumake-4.4.1-doc"
},
{
"name": "info",
"path": "/nix/store/51cb4z2jrvqdnab9cc8hjf478c8f55hw-gnumake-4.4.1-info"
}
],
"store_path": "/nix/store/nk0i2hc4x8qpshj326jzn36hkbgi7j7z-gnumake-4.4.1"
},
"x86_64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/0bg5b97b9hbhz8907bfcrch1i116xppa-gnumake-4.4.1",
"default": true
},
{
"name": "man",
"path": "/nix/store/wb4yn3d7vmi1hr4f8pz38rjvcl7f70ma-gnumake-4.4.1-man",
"default": true
},
{
"name": "doc",
"path": "/nix/store/3b9cjf16jx4ncpp3b4k6m8q01nxsrw1n-gnumake-4.4.1-doc"
},
{
"name": "info",
"path": "/nix/store/rriz5p4q8a6sca7sw3p3js1m5dl09ia8-gnumake-4.4.1-info"
}
],
"store_path": "/nix/store/0bg5b97b9hbhz8907bfcrch1i116xppa-gnumake-4.4.1"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/vs2sa1msvkjxbb70h47819zq8a9srj21-gnumake-4.4.1",
"default": true
},
{
"name": "man",
"path": "/nix/store/ncw4l9vr02fh2prknq5dhmdycb00rdmr-gnumake-4.4.1-man",
"default": true
},
{
"name": "debug",
"path": "/nix/store/k7lz9zpj52al794kkw0d80hwj2v7pn9h-gnumake-4.4.1-debug"
},
{
"name": "doc",
"path": "/nix/store/7c3i6ivjqgc7hz21bchima12lxk8c1s4-gnumake-4.4.1-doc"
},
{
"name": "info",
"path": "/nix/store/9lbfzcvwx1c859fy6k0l9cbkp6fcxd9l-gnumake-4.4.1-info"
}
],
"store_path": "/nix/store/vs2sa1msvkjxbb70h47819zq8a9srj21-gnumake-4.4.1"
}
}
},
"gnused@latest": {
"last_modified": "2026-10-20T14:23:57Z",
"resolved": "github:NixOS/nixpkgs/5adc50831c259f87faeb09ddf930d43597197473#gnused",
"source": "devbox-search",
"version": "4.10",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/8fyr2rna4vqibkqsbzyfd4z6fna5c09p-gnused-4.10",
"default": true
},
{
"name": "info",
"path": "/nix/store/6c8gqzdibqfzgnp8876lp4zh1z9f13g1-gnused-4.10-info"
}
],
"store_path": "/nix/store/8fyr2rna4vqibkqsbzyfd4z6fna5c09p-gnused-4.10"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/03rgyjxclvhxnif2klf13nkr92rpxqqg-gnused-4.10",
"default": true
},
{
"name": "info",
"path": "/nix/store/sgxd46ij300v6jwrxj5cfjz7a8029jxy-gnused-4.10-info"
}
],
"store_path": "/nix/store/03rgyjxclvhxnif2klf13nkr92rpxqqg-gnused-4.10"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/9wc73n5izv0m7ppjbibi4hbr68rba1lj-gnused-4.10",
"default": true
},
{
"name": "info",
"path": "/nix/store/4gfax7m36hw70wn2cqy0dh3dr1r3rlr3-gnused-4.10-info"
}
],
"store_path": "/nix/store/9wc73n5izv0m7ppjbibi4hbr68rba1lj-gnused-4.10"
}
}
},
"go@1.27": {
"last_modified": "2026-10-20T14:23:57Z",
"resolved": "github:NixOS/nixpkgs/5adc50831c259f87faeb09ddf930d43597197473#go_1_27",
"source": "devbox-search",
"version": "1.27.3",
"systems": {
"aarch64-darwin": {
"outputs": [
{
"name": "out",
"path": "/nix/store/s5m10yz53f8778k4mi1irdqzy8iaqri1-go-1.27.3",
"default": true
}
],
"store_path": "/nix/store/s5m10yz53f8778k4mi1irdqzy8iaqri1-go-1.27.3"
},
"aarch64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/0940szmkaskjshd3z0gp2m2kfxcnc424-go-1.27.3",
"default": true
}
],
"store_path": "/nix/store/0940szmkaskjshd3z0gp2m2kfxcnc424-go-1.27.3"
},
"x86_64-linux": {
"outputs": [
{
"name": "out",
"path": "/nix/store/1fng2iqlx5y8rqvhjp9203mymzyny6cz-go-1.27.3",
"default": true
}
],
"store_path": "/nix/store/1fng2iqlx5y8rqvhjp9203mymzyny6cz-go-1.27.3"
}
}
}
}
}
+450
View File
@@ -0,0 +1,450 @@
# abra audit — design
2026-11-20 · Linus Gasser
## Summary
Add a top-level `abra audit` command. It checks every deployed app on every server against a curated list of advisories, and offers to fix what fails. A maintainer-side `abra audit update` turns upstream vulnerability sources into proposed advisories.
## Problem
Operators of Co-op Cloud servers cannot easily tell whether their apps are safe:
- **Mutable tags.** Images such as `php` or `nextcloud` are often rebuilt under the same tag. A server can run a vulnerable image while its compose file looks current.
- **Stale base images.** An image like `nextcloud` is built on `php`. When `php` is fixed, `nextcloud` is not fixed until upstream rebuilds it.
- **Old recipe versions.** Some recipe versions ship known-vulnerable upstream versions, and nothing tells the operator.
- **Unreliable redeploys.** A redeploy should pull the new image. If the registry is overloaded at deploy time, Docker falls back to the bare tag and each node may keep its old local image, with only a warning.
## Goals and non-goals
Goals:
- One command reports, for every deployed app, whether it matches a known advisory.
- Detect vulnerable images even when the tag has not changed, including vulnerable base images.
- Propose a fix for each finding and, on request, apply it and verify the result.
- Help advisory maintainers find new problems from upstream sources, without missing images that were fixed before anyone looked.
- Work on mixed amd64 and arm64 swarms.
Non-goals:
- A full CVE scanner. Deep scanning is delegated to Trivy or Grype as an option.
- Rebuilding upstream images. abra can detect a stale base image but not patch it.
- Publishing advisories automatically. A human reviews every advisory before operators see it.
## Commands
`abra audit` is a top-level command, because an audit spans servers, apps and recipes. `abra app check` already exists (it checks `.env` variables), so that name is not available.
| Command | Who | What it does |
| --- | --- | --- |
| `abra audit` | operator | Audit all deployed apps on all servers |
| `abra audit app <domain>` | operator | Audit one deployed app |
| `abra audit server <server>` | operator | Audit all apps on one server |
| `abra audit recipe <recipe>` | maintainer, CI | Static check of recipe versions and pinned images; nothing deployed needed |
| `abra audit list` | anyone | List advisories, filtered by `--recipe` or `--severity` |
| `abra audit show <id>` | anyone | Details and fix notes for one advisory |
| `abra audit update` | advisory maintainer | Propose new or changed advisories from a CVE, from minimum versions, or from upstream sources |
Two aliases are kept, because operators look under `app` and `server` first:
- `abra app audit <domain>` is the same as `abra audit app <domain>`.
- `abra server audit <server>` is the same as `abra audit server <server>`.
**Storage.** The audit needs an explicitly chosen abra directory. It is taken from, in order:
1. `$ABRA_DIR`;
2. the `abraDir` setting of an `abra.yaml` file in the current directory or one of its parents, as the rest of abra already supports.
If neither exists, all `abra audit` commands fail with a clear error. They do not fall back to `~/.abra`, unlike the rest of abra. Below, `$ABRA_DIR` means this directory. Everything the audit stores lives below it:
| Path | Contents |
| --- | --- |
| `$ABRA_DIR/servers` | Server and app `.env` files, as for the rest of abra |
| `$ABRA_DIR/advisories` | Checkout of the advisories repo: shared, reviewed data |
| `$ABRA_DIR/.cache/audit/images` | Local cache of image configs, by digest; from v2 on, safe to delete |
**Fetching.** There is no cache lifetime: every invocation checks whether the advisory list has changed, and pulls it if so. The list is a git repo, so this is a `git fetch` of a few KB when nothing changed. When the repo cannot be reached, the audit warns, says how old the local copy is, and uses it.
### Options for `audit`, `app` and `server`
```
-s, --server <name> only apps on this server
-r, --recipe <name> only apps of this recipe
-a, --advisories <url|path> advisory source (default: c4dt/advisories)
--severity <level> minimum severity: low, medium, high, critical
--include-drafts also apply draft advisories (marked as such)
-f, --fix propose fixes, apply after confirmation
-n, --no-input apply fixes without asking
--refresh-only only re-pull images, never upgrade the recipe
--retries <n> re-checks after a fix, with backoff (default 3)
-o, --offline no registry or advisory fetch; recipe checks only
--deep also scan running digests with Trivy or Grype
--report send unknown digests to the advisories repo (opt-in)
-m, --machine JSON output for cron and monitoring
```
`abra audit recipe` takes only `--machine`, so CI can fail on findings.
### Exit codes
The exit code describes the state **after** the run, so a finding that was fixed and verified no longer counts.
| Code | Meaning |
| --- | --- |
| 0 | No findings remain: none were found, or all were fixed and verified |
| 1 | Findings remain that were not fixed: no `--fix`, fix declined, or strategy `manual` |
| 2 | A fix was tried but could not be verified, for example because the registry was unreachable |
With several apps, the highest code wins. The report lists every finding with its final status (`fixed`, `affected`, `unknown`, `unverified`), so fixed findings stay visible in the output and in `--machine` JSON.
## Advisory format
Advisories are YAML files, one per advisory, in a separate git repo under `advisories/`. They are kept apart from the recipe catalogue because they change on a different schedule and need their own review. The repo starts as `c4dt/advisories` and can move to `coop-cloud/advisories` once the format has settled.
```yaml
id: CCA-2026-0012
title: PHP 8.3 CGI argument injection
severity: critical # low | medium | high | critical
status: published # draft | published | withdrawn
refs: [CVE-2026-12345, https://example.org/advisory]
published: 2026-10-20
# Recipe versions: semver range on the recipe part of the tag
recipes:
nextcloud:
affected: "<7.0.3+30.0.1"
fixed: "7.0.3+30.0.1"
# Images: an image is fixed if ANY condition holds
images:
- match: "nextcloud:30*-apache"
fixed_from: sha256:2222... # this digest and all first seen after it
affected_digests: [sha256:1111...]
- match: "*" # any image...
base: "php:8.3*" # ...built on php 8.3
env: { PHP_VERSION: "<8.3.14" } # read from the image config
fix:
strategy: refresh # upgrade | refresh | manual
notes: "Upstream images rebuilt on 2026-10-18; a redeploy is enough."
```
### Image conditions
Digests are hashes and have no order, so "newer than digest X" cannot be computed from the digest itself. Instead, the format uses what the image says about itself, in order of preference:
| Condition | Field | Reliability | Notes |
| --- | --- | --- | --- |
| Version in the image | `env`, `version` | Best | Tests the fix itself, per platform, without pulling layers |
| Build time | `created_after` | Good, if the image records its real build time | Covers rebuilds where no version changes; see below |
| Base ancestry | `base`, `base_digest_from` | Good | Docker Official Images record their base in OCI annotations |
| Allowlist | `fixed_digests` | Exact but closed | Later rebuilds need the list updated |
| Denylist | `affected_digests` | Exact but incomplete | For one known-bad digest |
| Digest history | `fixed_from` | Good | Fallback for images whose build time is unusable; later version |
An image that matches `match` but satisfies no condition is reported as **unknown**, never as OK.
How the conditions combine, for an image that matches `match`:
1. A digest in `affected_digests` is affected; a digest in `fixed_digests` is fixed.
2. `env` both selects and tests. If the image lacks one of the variables, the entry does not apply, because the image is not built on that runtime. Otherwise the ranges are **affected** ranges: if every variable is in its range, `env` says affected, else fixed. Write `">=8.3.0 <8.3.14"` to limit the entry to one release series.
3. `version` is an affected range on `org.opencontainers.image.version`.
4. `created_after` says fixed for images built at or after that time, and affected for older ones, unless the build time is unusable.
5. If any condition says fixed, the image is fixed. Otherwise, if one says affected, it is affected. Otherwise it is unknown.
`match` is a glob where `*` matches any characters. A pattern with a `:` is matched against `name:tag`, otherwise against the name only, using Docker's short names (`nextcloud`, not `docker.io/library/nextcloud`). Version ranges are space- or comma-separated constraints (`<`, `<=`, `>`, `>=`, `=`, `!=`) that must all hold, with `||` between alternatives. A recipe range without an upstream part (`<7.0.3`) only compares the recipe part of the tag.
### Ordering images by version and build time
Most fixes change a version that the image shows, so no digest bookkeeping is needed:
- **Application version.** `NEXTCLOUD_VERSION` in the config, or the annotation `org.opencontainers.image.version` (for example `35.0.3-apache`).
- **Inherited runtime version.** Images inherit environment variables from their base, so `nextcloud:apache` also carries `PHP_VERSION=8.5.13`. A nextcloud rebuilt on a fixed php shows the new `PHP_VERSION`, even though `NEXTCLOUD_VERSION` is unchanged.
That leaves rebuilds where every visible version stays the same, typically a Debian package such as openssl patched in the base image. For these, the build time decides: `created_after` is the `Created` time of the first fixed image, and any image of that tag built later counts as fixed.
**Is the build time monotonic?** Per tag, in practice yes: every rebuild is a new build run with a later timestamp. On Docker Hub on 2026-11-20, `php:8.3-apache` recorded its real build time (2026-11-19 14:40 UTC), and `nextcloud:apache` was built 33 minutes later on top of it. Both also carry the annotations `org.opencontainers.image.base.name` and `base.digest`. The exceptions are known:
| Case | Effect | Handling |
| --- | --- | --- |
| Reproducible builds (`SOURCE_DATE_EPOCH`, ko, Nix, Bazel) set `Created` to a commit date or to 1970 | A base-only rebuild keeps the same `Created`, so old and new cannot be told apart | Detect it, then report unknown |
| Upstream moves a tag back to an older image | The older `Created` is reported as affected | Correct as is |
| Clock skew between build machines | Minutes | Irrelevant next to fix dates days apart |
The audit treats `Created` as unusable, and reports unknown, when:
- it is before 2000, which catches the 1970 epoch;
- it is earlier than the `Created` of the base image named in `base.digest`, because an image cannot be built before its base;
- two different digests of the same tag have the same `Created`.
The advisory can then fall back to `fixed_digests`, or later to the digest history.
### Fix strategies
| Strategy | Meaning | What `--fix` does |
| --- | --- | --- |
| `upgrade` | A newer recipe version fixes it | `abra app upgrade <domain> <fixed version>` |
| `refresh` | The same tag now points to a fixed image | Redeploy the same version, then verify the running digest |
| `manual` | No fixed image exists yet | Report only, with the notes and options such as pinning a digest |
## Digest history (fallback, later version)
The digest history is only needed for images whose build time is unusable, as listed above. It is deferred to v4, and built only if such images turn out to matter.
The advisories repo would keep an append-only history per image tag, recording when each digest was first seen:
```yaml
# digests/nextcloud/30-apache.yml (append-only)
- digest: sha256:1111...
first_seen: 2026-10-02T08:00:00Z
config: { created: 2026-10-01, NEXTCLOUD_VERSION: 30.0.3, PHP_VERSION: 8.3.12 }
- digest: sha256:2222...
first_seen: 2026-10-18T14:00:00Z
config: { created: 2026-10-18, NEXTCLOUD_VERSION: 30.0.3, PHP_VERSION: 8.3.14 }
```
It would be filled from three sources:
1. **`abra audit update`** runs regularly from CI and appends every new digest it sees, with its image config. The config is stored because registries may delete old digests once they are untagged.
2. **Backfill from `docker-library/repo-info`**, which keeps years of per-tag digests for official images in git.
3. **Running servers.** With `--report`, `abra audit` sends digests it finds that are not in the history. This is opt-in.
## Images fixed before anyone looked
Suppose a CVE is published and upstream pushes a fixed image under the same tag before `update` runs. `update` never sees the vulnerable image, but old installations still run it. This is handled because advisories are driven by the CVE, not by the current image:
- The CVE creates the advisory, whether or not today's image is clean.
- A clean current image only sets the strategy to `refresh`. Its versions become the `env` condition, or its `Created` time becomes `created_after`.
- On a server, an image with older versions or an earlier build time is reported as affected. Both lead to a refresh and a verification.
`update` only needs to see the fixed image, and that is the one available when it runs.
## Audit and fix
`abra audit` checks each app in the same way, and a fix counts only after the running digest has been verified.
1. **Read state.** From the service spec, read the recipe version label and each image reference `image:tag@sha256:...`. For per-platform checks, read the node's platform and fetch that image config from the registry.
2. **Match advisories.** First recipe ranges, then image conditions.
3. **Classify** each finding as affected or unknown. No finding means clean.
4. **Fix** (with `--fix`) according to the advisory's strategy, then verify.
```mermaid
flowchart TD
read[Read service spec] --> match[Match advisories]
match --> finding{Finding?}
finding -- no --> clean[Clean, exit 0]
finding -- affected or unknown --> strategy[Pick fix strategy]
strategy -- manual --> report[Report, exit 1]
strategy -- upgrade or refresh --> apply[Upgrade / refresh]
apply --> verified{Digest fixed?}
verified -- yes --> fixed[Fixed, exit 0]
verified -- no --> retry[Retry, backoff]
retry -- within --retries --> apply
retry -- retries used up --> unverified[Unverified, exit 2]
```
Verification waits for the service to converge, then reads the spec again. It fails if the spec holds a bare tag (the registry lookup failed) or a digest that still matches the advisory. Retries use backoff, for example 30 s, 2 min and 10 min, before giving up with exit code 2.
## `abra audit update`
`abra audit update` is a maintainer tool. It compares the recipes in the catalogue with a vulnerability, and proposes new or changed advisories for a human to review. It has three modes; the first two come first (see [Versions](#versions)).
```
abra audit update --cve <CVE-ID> # one known vulnerability
abra audit update --require <lib>=<v>[,<v>...] # minimum patched versions
abra audit update --sources [recipe...] # scan upstream sources (later)
--repo <path> local checkout of the advisories repo (default $ABRA_DIR/advisories)
--source <list> with --sources: endoflife,osv,ghsa,kev,repo-info,trivy (default: all but trivy)
--since <date> with --sources: only upstream changes after this date (default: last run)
-n, --no-input write all proposals as drafts without asking
--pr commit accepted proposals on a branch and open a PR
```
### Sources
| Source | Gives | Becomes |
| --- | --- | --- |
| [endoflife.date](https://endoflife.date) | End-of-life dates for runtimes such as php and postgres | `medium` advisory, strategy `upgrade` |
| [OSV.dev](https://osv.dev) and [GitHub Advisory Database](https://github.com/advisories) | CVEs with affected and fixed upstream versions | Recipe ranges, mapped through the `+UPSTREAM` part of recipe tags |
| [CISA KEV](https://www.cisa.gov/known-exploited-vulnerabilities-catalog) | CVEs exploited in the wild | Severity raised to `critical` |
| [docker-library/repo-info](https://github.com/docker-library/repo-info) | Digest and package history of official images | Digest history; `fixed_from` for rebuilt images |
| Trivy (opt-in, slow) | CVEs found in current catalogue images | Proposals the other sources missed |
### Steps
1. **Inventory.** List every recipe version in the catalogue and its images. Fetch each image config for runtime versions (`PHP_VERSION`, `NODE_VERSION`) and find its base image.
2. **Record digests.** Append new digests and their configs to the digest history.
3. **Query sources** for the upstream apps and runtimes found in step 1.
4. **Classify.** For each CVE, write a condition that separates affected from fixed images. Look at the current image only to choose the strategy: `refresh` if the current image is clean, `manual` if it is not.
5. **Deduplicate.** Match on CVE ID and affected recipe or image. If an advisory already covers the finding, propose a change (wider range, new fixed digest, higher severity) instead of a new entry.
6. **Review.** Show each proposal as a YAML diff: accept, edit, skip, or ignore permanently. Ignored findings go to `ignore.yml` with a reason, so they do not return.
7. **Write** accepted proposals with `status: draft`, optionally on a branch with a PR. Only a human sets `status: published`.
A human is needed because the sources say what is vulnerable upstream. Only a maintainer can judge whether it matters for the recipe's configuration and which fix strategy applies.
## Multiple platforms
abra already works on mixed amd64 and arm64 swarms, because it leaves the platform choice to Docker. The audit must compare digests at the right level.
**Current behaviour.** abra has no platform-specific code. `deploy`, `upgrade` and `rollback` use `ResolveImageAlways` (`cli/app/deploy.go:157`), so Docker resolves `image:tag` to the index digest that covers all platforms. Each node then pulls its own variant. `GetRegistryDigest` in `pkg/client/registry.go` also returns the index digest, so pinned `image:tag@sha256:...` references work on every platform.
| Question | Compare | Where to read it |
| --- | --- | --- |
| Has the tag moved? | Index digest | Service spec, and a registry HEAD request |
| Which PHP version is inside? | Per-platform image config | Pick the node's platform from the index (`docker node inspect` gives `Description.Platform`) |
| Is it built on the fixed base? | Per-platform layers | Same per-platform manifest |
Pitfalls:
- **A new index digest does not prove the fix for your platform.** Any platform's rebuild changes the index digest. Some upstreams rebuild arm64 later or not at all, so important fixes are checked per platform.
- **Copied per-platform digests.** The Docker Hub web page shows per-platform digests most prominently. A maintainer who pins one breaks other platforms with `no matching manifest`. `abra recipe lint` should warn when a pinned digest is not an index but the image is multi-arch.
- **Containers show the image ID.** `docker inspect` on a container gives the config digest, not the manifest digest. Use the service spec for comparisons.
## What Docker allows
abra can detect almost any stale or vulnerable layer, but it can only fix an image that upstream has already rebuilt.
| Possible | How |
| --- | --- |
| Know exactly what is running | Swarm stores `image:tag@sha256:...` in the service spec |
| Tell whether a tag has moved | Registry HEAD request for the tag's manifest digest |
| Read versions without pulling | Fetch the image config (a few KB): `Created` and `Env`, which often inherits `PHP_VERSION` or `NGINX_VERSION` from the base |
| Find the base image | OCI annotations `org.opencontainers.image.base.name` and `base.digest` where set; otherwise layer-prefix matching against the base's layers |
| Go deeper, to libc or openssl | SBOM and provenance attestations, which Docker Official Images publish |
| Not possible | Consequence |
| --- | --- |
| Patch a base layer of someone else's image | If upstream has not rebuilt, the options are waiting, pinning a known-good older digest, or having the recipe build its own image |
| Trust a redeploy without checking | On registry failure Docker deploys the bare tag; every refresh must be verified |
**Undeploy is not needed.** `undeploy` then `deploy` has the same registry problem as a plain redeploy, and adds downtime. A redeploy of the same version with `ResolveImageAlways` already asks the registry again.
**Pinned digests make failures visible.** With `image: nextcloud:30-apache@sha256:...` in the recipe, a failed registry lookup fails the deploy instead of running a stale image. The `allow_digest` branch adds this support to `abra recipe upgrade`.
## Versions
The feature is built in four versions. Each one is usable on its own, and is tried on real servers before the next one starts.
| Version | Adds | Changes servers? |
| --- | --- | --- |
| v1 | `abra audit`: report failing apps and suggest commands | No |
| v2 | `abra audit update --cve` and `--require`: draft advisories for one vulnerability | No |
| v3 | Subcommands, options, `--fix` with verification, deploy warnings | Yes, with `--fix` |
| v4 | Automatic advisory proposals from upstream sources, digest history | No |
### v1: basic audit
Answers "which of my apps need attention, and what do I run?" using only a hand-written advisory list.
- **Command.** `abra audit`, without arguments. It goes through every server in `$ABRA_DIR/servers` and every deployed app on it, using the same data as `abra app ls -S`.
- **Advisories.** Hand-written YAML in `c4dt/advisories`, pulled on every run. The first advisory is the nextcloud same-tag rebuild. Only `published` advisories apply; `--include-drafts` also applies `draft` ones, marked "(draft)" in the output, so a maintainer can try an advisory on real servers before publishing it. `withdrawn` advisories never apply.
- **Conditions.** Only recipe ranges (`affected`, `fixed`), `env`, `version`, `created_after` (with the checks for unusable build times), `fixed_digests` and `affected_digests`. Fields v1 does not know are skipped with a warning, so newer advisories do not break older abra versions.
- **Reads only.** Service specs (version label and image digests) and image configs. On a single-node server, the common case, the image config is read from the node itself with `docker image inspect`, so no registry is needed.
- **Output.** Only failing apps. A narrow numbered table lists app, recipe, deployed version, what is affected, severity and action, with a server column only when several servers are audited. For an app deployed with `--chaos`, the version is the commit it was deployed from, marked `(chaos)`, as in `abra app ls`. The affected column names what is vulnerable, from the condition that matched: the runtime and its version for an `env` condition (`NGINX_VERSION=1.31.8` gives `nginx 1.31.8`), the image's version for a `version` condition, the build date for `created_after`, the recipe and its version for a recipe range, and the image otherwise. Advisory IDs such as `CCA-2026-0002` are local to the advisories repository and mean nothing at a glance, so they are not in the table. Below it, a numbered list gives the fix for each row, starting with the action and ending with the references to look the advisory up: its ID, marked if it is a draft, and its CVEs (`CCA-2026-0002, CVE-2026-123007 and 4 more`). The list is wrapped to the terminal width, because fixes are too long for a table column. Unreachable servers follow, with only the useful part of the SSH error. The findings that the operator cannot fix alone are summarised at the end, grouped by action. The action says who has to act:
| Action | Meaning | Fix shown |
| --- | --- | --- |
| redeploy | What a redeploy deploys is fixed: the tag's current image, or the recipe checkout of a `--chaos` deployment | `abra app deploy <domain> --force && abra audit`; for a `--chaos` deployment `abra app deploy <domain> --chaos`, because without `--chaos` the deploy would replace the chaos deployment with the release in the `.env` file |
| upgrade | A released recipe version uses fixed images | `abra app upgrade <domain> <version>` |
| recipe must update | A fixed image exists in the registry, but no recipe release uses it | The fixed image; for a `--chaos` deployment, use it in the checkout or the app's override and redeploy with `--chaos` |
| upstream must update | No fixed image exists yet | Why: the tag was not rebuilt, or rebuilt but still affected, and no later tag is fixed; plus a pointer to the advisory's mitigations |
| check by hand | The audit could not decide: an image could not be checked (status `unknown`), the registry could not be searched, or build times are unusable | The reason, and the fix that would apply otherwise |
| follow advisory | Strategy `manual` | The advisory's notes |
- **Fix check.** Every suggestion is checked before it is shown:
- A redeploy only helps if what it deploys is fixed. That is the tag's current image in the registry, unless the recipe pins a digest (`image:tag@sha256:...`): then a redeploy keeps the pinned image, which is checked instead. Upstreams stop rebuilding patch-pinned tags such as `nextcloud:35.0.2-fpm` once the next patch is out. If a redeploy does not help, an upgrade is looked for.
- A `--chaos` deployment runs the local checkout, so for it the audit first loads what `abra app deploy --chaos` would deploy: the files of the app's `COMPOSE_FILE` (`GetComposeFiles`), overrides outside the recipe directory included, as they are on disk with uncommitted changes, merged and filled in with the app's `.env` values by the same loader as deploy. If those images are fixed, the action is a `--chaos` redeploy. Images already deployed from an override are checked like any other deployed image.
- For an upgrade, the newer releases of the recipe are searched for the oldest one whose images are fixed. Images are read from the `compose*.yml` files of each release tag, without changing the local checkout.
- If no release is fixed, the registry is searched for a fixed image, to tell "recipe must update" from "upstream must update". The tags of the image's repository are listed, and candidates are checked in this order:
1. The deployed tag itself, if its current image is fixed but a redeploy does not pick it up because the recipe pins an older digest.
2. Later builds of the same version under a suffixed tag. Upstreams mark such rebuilds in many ways (`v6.1.0-1`, `v6.1.0-fix1`, `v6.1.0_r2`), and in semver `v6.1.0-1` even sorts before `v6.1.0`, so suffixes are not parsed. A candidate is the deployed tag, a separator, and a suffix containing a number; per-architecture copies (`-amd64`), pre-releases (`-preprod`, `-rc1`) and variants (`-fpm`, `-alpine3`) are left out by name. The build time then decides: a candidate must be built at least an hour after the registry's current image of the deployed tag. A pre-release is built before the release, and a variant in the same CI run as it, so neither passes; comparing with the deployed image instead would let a pre-release pushed after an old deployment through. Of the fixed candidates, the latest built is chosen. If the build times are unusable (see [Ordering images by version and build time](#ordering-images-by-version-and-build-time)), the action is "check by hand".
3. Newer versions of the same form: `v6.1.1` or `v6.2.0` for `v6.1.0`, but not `v6`, `latest` or `main`. As for releases, the newest is checked first; if it is fixed, the oldest fixed one is chosen.
Every candidate is checked against the advisory: whether a tag looks newer never decides whether it is fixed. The search is bounded to 20 rebuild and 10 newer-version candidates per image.
- For a recipe version range, the advisory's `fixed` version must be a released tag; otherwise the recipe must update.
- If the registry or the recipe cannot be read, the suggestion is kept and marked as unchecked.
- **Recipe names.** Recipes are shown relative to `git.coopcloud.tech/coop-cloud/`: `nextcloud` for the canonical recipe, `c4dt/nextcloud` for a fork on the same server, and the full path elsewhere. Advisories name recipes the same way, so `nextcloud` in an advisory does not match the fork `c4dt/nextcloud`, which has its own versions.
- **Exit code.** 0 when nothing fails, 1 otherwise.
- **Errors.** An unreachable server is listed as such and the run continues.
**Test.** On a test server, deploy one old nextcloud recipe version and one app with a stale image under a current tag. Write an advisory for each. Check that both are reported, that following the suggestions fixes them, and that a second `abra audit` is clean.
### v2: `abra audit update` for one vulnerability
Turns a single vulnerability into draft advisories, so the maintainer does not have to check every recipe by hand. There are two ways to describe the vulnerability.
**By CVE: `abra audit update --cve CVE-2026-12345`.** The CVE is looked up in [OSV.dev](https://osv.dev), which also covers GitHub advisories and package ecosystems such as Packagist, npm, PyPI, Debian and Alpine. This gives the affected packages and their fixed versions. Recipes are then matched in two ways:
- applications, such as nextcloud, through the `+UPSTREAM` part of the recipe tags;
- runtimes and libraries, such as php, through the image config (next paragraph).
**By minimum versions: `abra audit update --require php=8.5.12,8.4.27,8.3.36,8.2.35`.** One minimum version per release series. For every image in the catalogue that contains the library:
| Image contains | Result |
| --- | --- |
| A listed series, below its minimum (php 8.4.20) | Affected |
| A listed series, at or above its minimum (php 8.4.27) | Fixed |
| A series that is not listed (php 8.1.x) | Unsupported, proposed as a separate end-of-life advisory |
Libraries are found through environment variables in the image config, which images inherit from their base:
| Library | Variable |
| --- | --- |
| php | `PHP_VERSION` |
| node | `NODE_VERSION` |
| python | `PYTHON_VERSION` |
| nginx | `NGINX_VERSION` |
| postgres | `PG_VERSION` |
| redis | `REDIS_VERSION` |
An unknown library is an error that lists the known ones. Finding libraries through SBOMs comes later.
**Which recipe versions.** Every published version of each recipe that uses the library is checked, so the proposal gets a correct `affected` range. Image configs are a few KB and are cached by digest in `$ABRA_DIR/.cache/audit/images/`, so this is cheap after the first run. A digest never changes its content, so this cache never expires.
**Strategy.** For each affected recipe, the current image under the same tag is checked too:
- a newer recipe version uses a fixed image → `upgrade` to that version;
- the same tag now points to a fixed image → `refresh`, with the current digest in `fixed_digests`;
- nothing is fixed yet → `manual`.
**Output.** Draft advisory files in the local checkout, each shown as a diff to accept, edit or skip. v2 does not commit or open pull requests.
**Test.** Run `--require` with the versions of a recent php security release, and compare the proposals with a list made by hand. Run `--cve` with a recent nextcloud CVE and check the recipe range.
### v3: arguments and fixes
Makes the audit usable for single apps and for automation, and lets it apply fixes.
- **Subcommands.** `abra audit app`, `server`, `recipe`, `list` and `show`, plus the aliases `abra app audit` and `abra server audit`.
- **Options.** `--server`, `--recipe`, `--severity`, `--machine`, `--offline` and `--advisories`.
- **Fixes.** `--fix`, `--refresh-only`, `--no-input` and `--retries`, with verification after each fix and exit code 2 when verification fails.
- **Deploy warnings.** `abra app deploy` and `abra app upgrade` warn, without refusing, when the target version matches an advisory.
- **Platforms.** `env` conditions are checked per platform on mixed amd64 and arm64 swarms.
**Test.** Run `abra audit --fix` against the v1 test setup, with the registry blocked for one attempt (for example with a firewall rule), and check that the retry succeeds and that the exit code is right in each case.
### v4: more automatic updates
Moves advisory maintenance from "react to a CVE" to "be told about new problems".
- `abra audit update --sources`, run on a schedule in CI, with endoflife.date, OSV, GHSA, CISA KEV and docker-library/repo-info.
- Deduplication against existing advisories, `ignore.yml`, and `--pr`.
- Digest history and `fixed_from`, only if images with unusable build times (reproducible builds) turn out to matter.
- Optional extras: `--deep` scanning with Trivy or Grype, the lint rule for per-platform digests, a status column in `abra app list`, and `--report`.
- Moving the advisories repo to `coop-cloud/advisories`.
## Decisions
| Question | Decision |
| --- | --- |
| Who maintains the advisories, and how fast? | Linus Gasser, manually, in `c4dt/advisories`, for now |
| Should `--report` exist? | Not in the first versions |
| How long to cache the advisory list? | No cache: every invocation checks for the latest list |
| What does `abra app deploy` do when the target version matches an advisory? | It warns, and does not refuse |
## Open questions
- [ ] Do the registries we depend on keep untagged digests long enough to read their config later? This matters only for the digest history (v4), because the config of a running image can be read from the node. To find out, take old `php` digests from docker-library/repo-info and try to fetch their manifests from Docker Hub.
- [ ] Which images used by recipes are built reproducibly, so that their `Created` time is unusable? `abra audit update` could list them while scanning the catalogue.
+4 -4
View File
@@ -21,12 +21,14 @@ require (
github.com/leonelquinteros/gotext v1.8.0
github.com/moby/sys/signal v0.7.1
github.com/moby/term v0.5.2
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.1
github.com/pkg/errors v0.9.1
github.com/schollz/progressbar/v3 v3.19.1
github.com/spf13/cobra v1.10.1
github.com/stretchr/testify v1.12.1
golang.org/x/sys v0.49.0
golang.org/x/term v0.47.0
golang.org/x/sys v0.50.0
golang.org/x/term v0.48.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.2
)
@@ -104,8 +106,6 @@ require (
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.1 // indirect
github.com/opencontainers/runc v1.1.13 // indirect
github.com/opencontainers/runtime-spec v1.1.0 // indirect
github.com/pjbgf/sha1cd v0.7.0 // indirect
+4
View File
@@ -1138,11 +1138,15 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
golang.org/x/term v0.48.0 h1:G6UU1eiSB8nl3DzeUGt7Mg+2SctI8tPS6Z5/mBt6cOk=
golang.org/x/term v0.48.0/go.mod h1:BRDLHCZr2SUjIeBHHpRyQeC9xzSAVbzY2rvKgafnHf4=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+111 -51
View File
@@ -260,12 +260,30 @@ func ReadAppEnvFile(appFile AppFile, name AppName) (App, error) {
func NewApp(env envfile.AppEnv, name string, appFile AppFile) (App, error) {
domain := env["DOMAIN"]
recipeName, exists := env["RECIPE"]
if !exists {
recipeName, exists = env["TYPE"]
if !exists {
return App{}, errors.New(i18n.G("%s is missing the TYPE env var?", name))
}
typeVar, hasType := env["TYPE"]
recipeVar, hasRecipe := env["RECIPE"]
if !hasType && !hasRecipe {
return App{}, errors.New(i18n.G("%s is missing the TYPE env var?", name))
}
// The recipe identity is taken from RECIPE when present (it records the
// canonical git source for externally-sourced recipes), otherwise from
// TYPE. The deployed version is always carried by TYPE when a TYPE line
// exists; only legacy apps that set RECIPE alone carry it there. RECIPE
// itself must stay a bare source pointer, never a versioned value.
identity := typeVar
if hasRecipe {
identity = recipeVar
}
versionSource := identity
if hasType {
versionSource = typeVar
}
recipeName, _, _ := strings.Cut(identity, ":")
if _, version, found := strings.Cut(versionSource, ":"); found && version != "" {
recipeName = fmt.Sprintf("%s:%s", recipeName, version)
}
return App{
@@ -392,11 +410,15 @@ func TemplateAppEnvSample(r recipe.Recipe, appName, server, domain string) error
newContents := strings.Replace(
string(read),
fmt.Sprintf("%s.example.com", r.Name),
fmt.Sprintf("%s.example.com", r.ShortName()),
domain,
-1,
)
if strings.Contains(r.Name, "/") {
newContents = injectRecipeLine(newContents, r.Name)
}
err = os.WriteFile(appEnvPath, []byte(newContents), 0)
if err != nil {
return err
@@ -407,6 +429,37 @@ func TemplateAppEnvSample(r recipe.Recipe, appName, server, domain string) error
return nil
}
// injectRecipeLine ensures the env file carries a RECIPE=<canonical name>
// line so a downstream `abra app deploy` (potentially on another machine)
// can re-fetch the recipe from its original git source. If a RECIPE= line
// already exists in the copied .env.sample it is replaced; otherwise a new
// line is inserted immediately after the TYPE= line, or appended at the
// end if no TYPE= line is present.
func injectRecipeLine(contents, canonicalName string) string {
lines := strings.Split(contents, "\n")
for i, line := range lines {
trimmed := strings.TrimLeft(line, " \t")
if strings.HasPrefix(trimmed, "RECIPE=") {
lines[i] = "RECIPE=" + canonicalName
return strings.Join(lines, "\n")
}
}
for i, line := range lines {
trimmed := strings.TrimLeft(line, " \t")
if strings.HasPrefix(trimmed, "TYPE=") {
out := make([]string, 0, len(lines)+1)
out = append(out, lines[:i+1]...)
out = append(out, "RECIPE="+canonicalName)
out = append(out, lines[i+1:]...)
return strings.Join(out, "\n")
}
}
if contents != "" && !strings.HasSuffix(contents, "\n") {
contents += "\n"
}
return contents + "RECIPE=" + canonicalName + "\n"
}
// SanitiseAppName makes a app name usable with Docker by replacing illegal
// characters.
func SanitiseAppName(name string) string {
@@ -514,10 +567,22 @@ func ExposeAllEnv(
_, exists := service.Environment[k]
if !exists {
value := v
if k == "TYPE" || k == "RECIPE" {
switch k {
case "TYPE":
// NOTE(d1): don't use the wrong version from the app env
// since we are deploying a new version
value = toDeployVersion
// since we are deploying a new version. Keep the
// recipe name already recorded in TYPE and only
// swap in the version being deployed.
name, _, _ := strings.Cut(v, ":")
if _, version, found := strings.Cut(toDeployVersion, ":"); found {
value = fmt.Sprintf("%s:%s", name, version)
} else {
value = name
}
case "RECIPE":
// RECIPE records the canonical git source only; the
// version is carried by TYPE, so keep it bare here.
value, _, _ = strings.Cut(toDeployVersion, ":")
}
service.Environment[k] = &value
log.Debug(i18n.G("%s: %s: %s", stackName, k, value))
@@ -642,60 +707,55 @@ func (a App) WriteRecipeVersion(version string, dryRun bool) error {
if err != nil {
return err
}
defer file.Close()
var (
dirtyVersion string
skipped bool
lines []string
scanner = bufio.NewScanner(file)
lines []string
hasType bool
scanner = bufio.NewScanner(file)
)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "RECIPE=") && !strings.HasPrefix(line, "TYPE=") {
lines = append(lines, line)
continue
}
if strings.HasPrefix(line, "#") {
lines = append(lines, line)
continue
}
if strings.Contains(line, version) && !a.Recipe.Dirty && !strings.HasSuffix(line, config.DIRTY_DEFAULT) {
skipped = true
lines = append(lines, line)
continue
}
splitted := strings.Split(line, ":")
line = fmt.Sprintf("%s:%s", splitted[0], version)
lines = append(lines, line)
if strings.HasPrefix(line, "TYPE=") {
hasType = true
}
}
if err := scanner.Err(); err != nil {
file.Close()
log.Fatal(err)
}
file.Close()
// The recipe version is carried by a single field: TYPE when present (the
// standard recipe key), otherwise the legacy RECIPE key. When both exist
// (git-URL recipes) RECIPE stays a bare source pointer and only TYPE is
// versioned, so the two can never drift apart.
targetPrefix := "RECIPE="
if hasType {
targetPrefix = "TYPE="
}
for i, line := range lines {
if !strings.HasPrefix(line, targetPrefix) || strings.HasPrefix(line, "#") {
continue
}
name, _, _ := strings.Cut(line, ":")
lines[i] = fmt.Sprintf("%s:%s", name, version)
}
if dryRun {
log.Debug(i18n.G("skipping writing version %s because dry run", version))
return nil
}
if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil {
log.Fatal(err)
}
if a.Recipe.Dirty && dirtyVersion != "" {
version = dirtyVersion
}
if !dryRun {
if err := os.WriteFile(a.Path, []byte(strings.Join(lines, "\n")), os.ModePerm); err != nil {
log.Fatal(err)
}
} else {
log.Debug(i18n.G("skipping writing version %s because dry run", version))
}
if !skipped {
log.Debug(i18n.G("version %s saved to %s.env", version, a.Domain))
} else {
log.Debug(i18n.G("skipping version %s write as already exists in %s.env", version, a.Domain))
}
log.Debug(i18n.G("version %s saved to %s.env", version, a.Domain))
return nil
}
+56
View File
@@ -3,6 +3,8 @@ package app_test
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"reflect"
"testing"
@@ -271,6 +273,60 @@ func TestWriteRecipeVersionOverwrite(t *testing.T) {
assert.Equal(t, "foo", app.Recipe.EnvVersion)
}
// TestWriteRecipeVersionSingleField ensures the version is written to exactly
// one field: TYPE when present (even alongside a RECIPE source pointer, as with
// git-URL recipes), otherwise the legacy RECIPE field. RECIPE must stay bare
// when a TYPE line exists so the two can never drift apart.
func TestWriteRecipeVersionSingleField(t *testing.T) {
cases := []struct {
name string
input string
want string
}{
{
name: "git recipe: TYPE and RECIPE both present",
input: "TYPE=myrecipe\nRECIPE=git.example.com/user/myrecipe\nDOMAIN=x\n",
want: "TYPE=myrecipe:1.2.3+4.5.6\nRECIPE=git.example.com/user/myrecipe\nDOMAIN=x",
},
{
name: "git recipe: re-versioning replaces only TYPE",
input: "TYPE=myrecipe:0.1.0+1.0.0\nRECIPE=git.example.com/user/myrecipe\nDOMAIN=x\n",
want: "TYPE=myrecipe:1.2.3+4.5.6\nRECIPE=git.example.com/user/myrecipe\nDOMAIN=x",
},
{
name: "legacy recipe: only RECIPE present",
input: "RECIPE=test_recipe\nDOMAIN=x\n",
want: "RECIPE=test_recipe:1.2.3+4.5.6\nDOMAIN=x",
},
{
name: "catalogue recipe: only TYPE present",
input: "TYPE=nextcloud\nDOMAIN=x\n",
want: "TYPE=nextcloud:1.2.3+4.5.6\nDOMAIN=x",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := filepath.Join(t.TempDir(), "app.env")
if err := os.WriteFile(p, []byte(tc.input), 0o644); err != nil {
t.Fatal(err)
}
app := appPkg.App{Path: p, Domain: "x"}
if err := app.WriteRecipeVersion("1.2.3+4.5.6", false); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.want, string(got))
})
}
}
func TestWriteRecipeVersionUnknown(t *testing.T) {
test.Setup()
t.Cleanup(func() { test.Teardown() })
+30
View File
@@ -0,0 +1,30 @@
package app
import "testing"
func TestInjectRecipeLineAfterType(t *testing.T) {
in := "DOMAIN=example.com\nTYPE=foo\nVERSION=1.0\n"
want := "DOMAIN=example.com\nTYPE=foo\nRECIPE=org/foo\nVERSION=1.0\n"
got := injectRecipeLine(in, "org/foo")
if got != want {
t.Errorf("injectRecipeLine inserted RECIPE in wrong position\nwant:\n%q\ngot:\n%q", want, got)
}
}
func TestInjectRecipeLineReplacesExisting(t *testing.T) {
in := "TYPE=foo\nRECIPE=old\nDOMAIN=example.com\n"
want := "TYPE=foo\nRECIPE=org/foo\nDOMAIN=example.com\n"
got := injectRecipeLine(in, "org/foo")
if got != want {
t.Errorf("injectRecipeLine should replace existing RECIPE line\nwant:\n%q\ngot:\n%q", want, got)
}
}
func TestInjectRecipeLineNoTypeAppends(t *testing.T) {
in := "DOMAIN=example.com\n"
want := "DOMAIN=example.com\nRECIPE=org/foo\n"
got := injectRecipeLine(in, "org/foo")
if got != want {
t.Errorf("injectRecipeLine should append when TYPE is missing\nwant:\n%q\ngot:\n%q", want, got)
}
}
+310
View File
@@ -0,0 +1,310 @@
// Package audit checks deployed apps against a curated list of advisories.
package audit
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"time"
"coopcloud.tech/abra/pkg/i18n"
"gopkg.in/yaml.v3"
)
// Advisory statuses.
const (
StatusDraft = "draft"
StatusPublished = "published"
StatusWithdrawn = "withdrawn"
)
// Fix strategies.
const (
StrategyUpgrade = "upgrade"
StrategyRefresh = "refresh"
StrategyManual = "manual"
)
var severities = []string{"low", "medium", "high", "critical"}
// Advisory describes one known problem and which recipe versions and images
// it affects.
type Advisory struct {
ID string `yaml:"id"`
Title string `yaml:"title"`
Severity string `yaml:"severity"`
Status string `yaml:"status"`
Refs []string `yaml:"refs"`
Published string `yaml:"published"`
Recipes map[string]RecipeRange `yaml:"recipes"`
Images []ImageRule `yaml:"images"`
Fix Fix `yaml:"fix"`
// Path is the file the advisory was read from.
Path string `yaml:"-"`
}
// RecipeRange gives the affected recipe versions of one recipe.
type RecipeRange struct {
Affected string `yaml:"affected"`
Fixed string `yaml:"fixed"`
affected Range
}
// ImageRule describes which images are affected. An image that matches
// Match is fixed if ANY of the fixing conditions holds. See Evaluate.
type ImageRule struct {
Match string `yaml:"match"`
Env map[string]string `yaml:"env"`
Version string `yaml:"version"`
CreatedAfter string `yaml:"created_after"`
FixedDigests []string `yaml:"fixed_digests"`
AffectedDigests []string `yaml:"affected_digests"`
// Skipped lists the fields this version of abra does not understand.
Skipped []string `yaml:"-"`
env map[string]Range
version *Range
createdAfter *time.Time
}
// Fix describes how to fix the advisory.
type Fix struct {
Strategy string `yaml:"strategy"`
Notes string `yaml:"notes"`
}
var (
knownTopLevel = []string{"id", "title", "severity", "status", "refs", "published", "recipes", "images", "fix"}
knownRecipe = []string{"affected", "fixed"}
knownImage = []string{"match", "env", "version", "created_after", "fixed_digests", "affected_digests"}
knownFix = []string{"strategy", "notes"}
)
// LoadAdvisories reads every *.yml / *.yaml file below dir. Advisories that
// cannot be parsed are skipped and reported as warnings, so that one broken
// file does not stop the audit. Fields that this version of abra does not
// know are skipped with a warning, so newer advisories do not break older
// abra versions.
func LoadAdvisories(dir string) ([]Advisory, []string, error) {
var advisories []Advisory
var warnings []string
if _, err := os.Stat(dir); err != nil {
return nil, nil, errors.New(i18n.G("no advisories found: %s", err))
}
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
ext := filepath.Ext(path)
if ext != ".yml" && ext != ".yaml" {
return nil
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
adv, warns, err := ParseAdvisory(data)
rel, _ := filepath.Rel(dir, path)
for _, w := range warns {
warnings = append(warnings, fmt.Sprintf("%s: %s", rel, w))
}
if err != nil {
warnings = append(warnings, i18n.G("%s: skipping advisory: %s", rel, err))
return nil
}
adv.Path = path
advisories = append(advisories, adv)
return nil
})
if err != nil {
return nil, warnings, err
}
sort.Slice(advisories, func(i, j int) bool { return advisories[i].ID < advisories[j].ID })
return advisories, warnings, nil
}
// ParseAdvisory parses and validates one advisory.
func ParseAdvisory(data []byte) (Advisory, []string, error) {
var adv Advisory
var warnings []string
var root yaml.Node
if err := yaml.Unmarshal(data, &root); err != nil {
return adv, nil, err
}
if len(root.Content) == 0 {
return adv, nil, errors.New(i18n.G("empty file"))
}
doc := root.Content[0]
if doc.Kind != yaml.MappingNode {
return adv, nil, errors.New(i18n.G("advisory is not a mapping"))
}
unknown := func(n *yaml.Node, known []string, where string) []string {
var skipped []string
if n == nil || n.Kind != yaml.MappingNode {
return nil
}
for i := 0; i+1 < len(n.Content); i += 2 {
key := n.Content[i].Value
if !slices.Contains(known, key) {
skipped = append(skipped, key)
warnings = append(warnings, i18n.G("unknown field %s%s skipped", where, key))
}
}
return skipped
}
unknown(doc, knownTopLevel, "")
if n := mapValue(doc, "recipes"); n != nil && n.Kind == yaml.MappingNode {
for i := 0; i+1 < len(n.Content); i += 2 {
unknown(n.Content[i+1], knownRecipe, fmt.Sprintf("recipes.%s.", n.Content[i].Value))
}
}
if n := mapValue(doc, "fix"); n != nil {
unknown(n, knownFix, "fix.")
}
if err := doc.Decode(&adv); err != nil {
return adv, warnings, err
}
if n := mapValue(doc, "images"); n != nil && n.Kind == yaml.SequenceNode {
for i, img := range n.Content {
if i < len(adv.Images) {
adv.Images[i].Skipped = unknown(img, knownImage, fmt.Sprintf("images[%d].", i))
}
}
}
if err := adv.validate(); err != nil {
return adv, warnings, err
}
return adv, warnings, nil
}
func mapValue(n *yaml.Node, key string) *yaml.Node {
if n == nil || n.Kind != yaml.MappingNode {
return nil
}
for i := 0; i+1 < len(n.Content); i += 2 {
if n.Content[i].Value == key {
return n.Content[i+1]
}
}
return nil
}
func (a *Advisory) validate() error {
if a.ID == "" {
return errors.New(i18n.G("missing id"))
}
a.Severity = strings.ToLower(a.Severity)
if a.Severity != "" && !slices.Contains(severities, a.Severity) {
return errors.New(i18n.G("%s: unknown severity %s", a.ID, a.Severity))
}
a.Status = strings.ToLower(a.Status)
if a.Status == "" {
a.Status = StatusDraft
}
if !slices.Contains([]string{StatusDraft, StatusPublished, StatusWithdrawn}, a.Status) {
return errors.New(i18n.G("%s: unknown status %s", a.ID, a.Status))
}
a.Fix.Strategy = strings.ToLower(a.Fix.Strategy)
if a.Fix.Strategy != "" && !slices.Contains([]string{StrategyUpgrade, StrategyRefresh, StrategyManual}, a.Fix.Strategy) {
return errors.New(i18n.G("%s: unknown fix strategy %s", a.ID, a.Fix.Strategy))
}
if len(a.Recipes) == 0 && len(a.Images) == 0 {
return errors.New(i18n.G("%s: neither recipes nor images given", a.ID))
}
for name, rr := range a.Recipes {
raw := rr.Affected
if raw == "" {
if rr.Fixed == "" {
return errors.New(i18n.G("%s: recipe %s needs affected or fixed", a.ID, name))
}
raw = "<" + rr.Fixed
}
r, err := ParseRange(raw)
if err != nil {
return errors.New(i18n.G("%s: recipe %s: %s", a.ID, name, err))
}
rr.affected = r
a.Recipes[name] = rr
}
for i := range a.Images {
img := &a.Images[i]
if img.Match == "" {
return errors.New(i18n.G("%s: images[%d] has no match", a.ID, i))
}
if len(img.Env) > 0 {
img.env = make(map[string]Range)
for k, v := range img.Env {
r, err := ParseRange(v)
if err != nil {
return errors.New(i18n.G("%s: images[%d].env.%s: %s", a.ID, i, k, err))
}
img.env[k] = r
}
}
if img.Version != "" {
r, err := ParseRange(img.Version)
if err != nil {
return errors.New(i18n.G("%s: images[%d].version: %s", a.ID, i, err))
}
img.version = &r
}
if img.CreatedAfter != "" {
t, err := parseTime(img.CreatedAfter)
if err != nil {
return errors.New(i18n.G("%s: images[%d].created_after: %s", a.ID, i, err))
}
img.createdAfter = &t
}
for _, d := range append(slices.Clone(img.FixedDigests), img.AffectedDigests...) {
if !strings.HasPrefix(d, "sha256:") {
return errors.New(i18n.G("%s: images[%d]: digest %s must start with sha256:", a.ID, i, d))
}
}
}
return nil
}
func parseTime(s string) (time.Time, error) {
layouts := []string{time.RFC3339Nano, "2006-01-02T15:04:05", "2006-01-02 15:04:05", "2006-01-02"}
for _, l := range layouts {
if t, err := time.Parse(l, s); err == nil {
return t, nil
}
}
return time.Time{}, errors.New(i18n.G("unable to parse time %s", s))
}
+282
View File
@@ -0,0 +1,282 @@
package audit
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRange(t *testing.T) {
tests := []struct {
rng string
version string
want bool
}{
{"<7.0.3+30.0.1", "7.0.2+30.0.1", true},
{"<7.0.3+30.0.1", "7.0.3+30.0.1", false},
{"<7.0.3+30.0.1", "7.0.3+30.0.0", true},
{"<7.0.3", "7.0.2+31.0.0", true},
{"<7.0.3", "7.0.3+29.0.0", false},
{"<8.3.14", "8.3.12", true},
{">=8.3.0 <8.3.14", "8.2.20", false},
{">=8.3.0, <8.3.14", "8.3.13", true},
{">=8.3.0 <8.3.14 || >=8.4.0 <8.4.2", "8.4.1", true},
{">=8.3.0 <8.3.14 || >=8.4.0 <8.4.2", "8.4.2", false},
{"< 35.0.4", "35.0.3-apache", true},
{"8.3", "8.3.0", true},
{"!=1.2.3", "1.2.3", false},
{"<=v1.2", "1.2.0", true},
}
for _, tc := range tests {
r, err := ParseRange(tc.rng)
require.NoError(t, err, tc.rng)
v, err := ParseVersion(tc.version)
require.NoError(t, err, tc.version)
assert.Equal(t, tc.want, r.Contains(v), "%s in %s", tc.version, tc.rng)
}
for _, bad := range []string{"", "<", "<foo", "1.2 || "} {
_, err := ParseRange(bad)
assert.Error(t, err, bad)
}
}
const sampleAdvisory = `
id: CCA-2026-0012
title: PHP 8.3 CGI argument injection
severity: critical
status: published
refs: [CVE-2026-12345]
published: 2026-10-20
future_field: whatever
recipes:
nextcloud:
affected: "<7.0.3+30.0.1"
fixed: "7.0.3+30.0.1"
images:
- match: "nextcloud:30*-apache"
created_after: 2026-10-18
affected_digests: [sha256:1111111111111111111111111111111111111111111111111111111111111111]
fixed_digests: [sha256:2222222222222222222222222222222222222222222222222222222222222222]
- match: "*"
base: "php:8.3*"
env: { PHP_VERSION: ">=8.3.0 <8.3.14" }
fix:
strategy: refresh
notes: "Upstream images rebuilt on 2026-10-18; a redeploy is enough."
`
func TestParseAdvisory(t *testing.T) {
adv, warns, err := ParseAdvisory([]byte(sampleAdvisory))
require.NoError(t, err)
assert.Equal(t, "CCA-2026-0012", adv.ID)
assert.Equal(t, StatusPublished, adv.Status)
assert.Equal(t, "2026-10-20", adv.Published)
assert.Equal(t, "7.0.3+30.0.1", adv.Recipes["nextcloud"].Fixed)
require.Len(t, adv.Images, 2)
require.NotNil(t, adv.Images[0].createdAfter)
assert.Equal(t, time.Date(2026, 10, 18, 0, 0, 0, 0, time.UTC), *adv.Images[0].createdAfter)
assert.Equal(t, []string{"base"}, adv.Images[1].Skipped)
joined := strings.Join(warns, "\n")
assert.Contains(t, joined, "future_field")
assert.Contains(t, joined, "images[1].base")
}
func TestParseAdvisoryInvalid(t *testing.T) {
for name, doc := range map[string]string{
"no id": "status: published\nrecipes: {a: {fixed: 1.0.0}}",
"bad severity": "id: X\nseverity: extreme\nrecipes: {a: {fixed: 1.0.0}}",
"no conditions": "id: X",
"bad range": "id: X\nrecipes: {a: {affected: '<foo'}}",
"bad digest": "id: X\nimages: [{match: a, fixed_digests: [abc]}]",
"bad strategy": "id: X\nrecipes: {a: {fixed: 1.0.0}}\nfix: {strategy: pray}",
"image no match": "id: X\nimages: [{env: {A: '<1'}}]",
} {
_, _, err := ParseAdvisory([]byte(doc))
assert.Error(t, err, name)
}
}
func TestLoadAdvisories(t *testing.T) {
dir := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(dir, "2026"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(dir, "2026", "good.yml"), []byte(sampleAdvisory), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "broken.yaml"), []byte("id: [\n"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(dir, "README.md"), []byte("# hi"), 0o644))
advs, warns, err := LoadAdvisories(dir)
require.NoError(t, err)
require.Len(t, advs, 1)
assert.Contains(t, strings.Join(warns, "\n"), "broken.yaml")
}
func mustImage(t *testing.T, service, ref string, info *ImageInfo) ImageRef {
t.Helper()
img, err := ParseImageRef(service, ref)
require.NoError(t, err)
if info != nil {
cp := *info
img.Info = &cp
}
return img
}
func at(s string) *time.Time {
tm, err := time.Parse(time.RFC3339, s)
if err != nil {
panic(err)
}
return &tm
}
func TestEvaluate(t *testing.T) {
adv, _, err := ParseAdvisory([]byte(sampleAdvisory))
require.NoError(t, err)
advs := []Advisory{adv}
newImg := &ImageInfo{Created: at("2026-10-19T10:00:00Z"), Env: map[string]string{"PHP_VERSION": "8.3.14"}}
oldImg := &ImageInfo{Created: at("2026-10-01T10:00:00Z"), Env: map[string]string{"PHP_VERSION": "8.3.12"}}
noPHP := &ImageInfo{Created: at("2026-10-01T10:00:00Z"), Env: map[string]string{}}
states := []AppState{
{
// clean: fixed recipe, rebuilt image
Server: "s1", Name: "clean.example.com", Recipe: "nextcloud", Version: "7.0.3+30.0.1",
Images: []ImageRef{
mustImage(t, "app", "nextcloud:30.0.1-apache@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", newImg),
mustImage(t, "db", "postgres:15@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", noPHP),
},
},
{
// old recipe version
Server: "s1", Name: "old.example.com", Recipe: "nextcloud", Version: "7.0.2+30.0.0",
Images: []ImageRef{mustImage(t, "app", "nextcloud:30.0.0-apache@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", newImg)},
},
{
// current recipe, stale image under the same tag
Server: "s1", Name: "stale.example.com", Recipe: "nextcloud", Version: "7.0.3+30.0.1",
Images: []ImageRef{mustImage(t, "app", "nextcloud:30.0.1-apache@sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc", oldImg)},
},
{
// some other recipe built on old php
Server: "s2", Name: "wp.example.com", Recipe: "wordpress", Version: "1.0.0+6.0.0",
Images: []ImageRef{mustImage(t, "app", "wordpress:6@sha256:dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", oldImg)},
},
{
// known bad digest
Server: "s2", Name: "bad.example.com", Recipe: "nextcloud", Version: "7.0.3+30.0.1",
Images: []ImageRef{mustImage(t, "app", "nextcloud:30.0.1-apache@sha256:1111111111111111111111111111111111111111111111111111111111111111",
&ImageInfo{Created: at("2026-10-20T10:00:00Z"), Env: map[string]string{"PHP_VERSION": "8.3.14"}})},
},
{
// no image config and no digest: unknown
Server: "s2", Name: "nocfg.example.com", Recipe: "nextcloud", Version: "7.0.3+30.0.1",
Images: []ImageRef{mustImage(t, "app", "nextcloud:30.0.1-apache", nil)},
},
{
// reproducible build: unusable build time, php fixed by env
Server: "s2", Name: "repro.example.com", Recipe: "nextcloud", Version: "7.0.3+30.0.1",
Images: []ImageRef{mustImage(t, "app", "nextcloud:30.0.1-apache@sha256:eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
&ImageInfo{Created: at("1970-01-01T00:00:00Z"), Env: map[string]string{"PHP_VERSION": "8.3.13"}})},
},
}
markUnusableCreated(states)
findings := Evaluate(states, advs, EvaluateOptions{})
got := map[string]Finding{}
for _, f := range findings {
got[f.App] = f
}
assert.NotContains(t, got, "clean.example.com")
require.Contains(t, got, "old.example.com")
assert.Equal(t, FindingAffected, got["old.example.com"].Status)
assert.Equal(t, "abra app upgrade old.example.com 7.0.3+30.0.1", got["old.example.com"].Fix)
require.Contains(t, got, "stale.example.com")
assert.Equal(t, FindingAffected, got["stale.example.com"].Status)
assert.Equal(t, "abra app deploy stale.example.com --force && abra audit", got["stale.example.com"].Fix)
require.Contains(t, got, "wp.example.com")
assert.Equal(t, FindingAffected, got["wp.example.com"].Status)
require.Contains(t, got, "bad.example.com")
assert.Equal(t, FindingAffected, got["bad.example.com"].Status)
require.Contains(t, got, "nocfg.example.com")
assert.Equal(t, FindingUnknown, got["nocfg.example.com"].Status)
require.Contains(t, got, "repro.example.com")
assert.Equal(t, FindingAffected, got["repro.example.com"].Status, "env still says affected")
assert.Contains(t, strings.Join(got["repro.example.com"].Reasons, " "), "PHP_VERSION=8.3.13")
// drafts are ignored, unless asked for
advs[0].Status = StatusDraft
assert.Empty(t, Evaluate(states, advs, EvaluateOptions{}))
assert.Len(t, Evaluate(states, advs, EvaluateOptions{IncludeDrafts: true}), len(findings))
// withdrawn advisories are always ignored
advs[0].Status = StatusWithdrawn
assert.Empty(t, Evaluate(states, advs, EvaluateOptions{IncludeDrafts: true}))
}
func TestCreatedUnusable(t *testing.T) {
adv, _, err := ParseAdvisory([]byte(`
id: X
status: published
images:
- match: "app:1"
created_after: 2026-10-18
fix: {strategy: refresh}
`))
require.NoError(t, err)
same := at("2026-10-01T00:00:00Z")
states := []AppState{
{Server: "a", Name: "a1", Images: []ImageRef{mustImage(t, "app", "app:1@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &ImageInfo{Created: same})}},
{Server: "b", Name: "b1", Images: []ImageRef{mustImage(t, "app", "app:1@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", &ImageInfo{Created: same})}},
{Server: "c", Name: "c1", Images: []ImageRef{mustImage(t, "app", "app:1@sha256:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc",
&ImageInfo{Created: at("2026-10-01T00:00:00Z"), BaseCreated: at("2026-10-05T00:00:00Z")})}},
{Server: "d", Name: "d1", Images: []ImageRef{mustImage(t, "app", "app:1@sha256:dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd", &ImageInfo{Created: at("2026-10-02T00:00:00Z")})}},
{Server: "e", Name: "e1", Images: []ImageRef{mustImage(t, "app", "app:1@sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", &ImageInfo{Created: at("2026-10-20T00:00:00Z")})}},
}
markUnusableCreated(states)
got := map[string]string{}
for _, f := range Evaluate(states, []Advisory{adv}, EvaluateOptions{}) {
got[f.App] = f.Status
}
assert.Equal(t, map[string]string{
"a1": FindingUnknown, // shares build time with b1
"b1": FindingUnknown, // shares build time with a1
"c1": FindingUnknown, // built before its base
"d1": FindingAffected, // usable and old
}, got)
}
func TestGlobMatch(t *testing.T) {
img, err := ParseImageRef("app", "git.coopcloud.tech/coop-cloud/foo:1.2-apache@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
require.NoError(t, err)
assert.True(t, globMatch("*", img))
assert.True(t, globMatch("git.coopcloud.tech/coop-cloud/foo", img))
assert.True(t, globMatch("*/foo:1.*-apache", img))
assert.False(t, globMatch("foo:*", img))
img, err = ParseImageRef("app", "docker.io/library/nextcloud:30.0.1-apache")
require.NoError(t, err)
assert.Equal(t, "nextcloud", img.Name)
assert.True(t, globMatch("nextcloud:30*-apache", img))
}
+415
View File
@@ -0,0 +1,415 @@
package audit
import (
"fmt"
"regexp"
"slices"
"strings"
"coopcloud.tech/abra/pkg/i18n"
)
// Finding statuses.
const (
FindingAffected = "affected"
FindingUnknown = "unknown"
)
// Finding actions say who has to act, and how.
const (
// ActionRedeploy: redeploying the app picks up a fixed image.
ActionRedeploy = "redeploy"
// ActionUpgrade: a released recipe version fixes it.
ActionUpgrade = "upgrade"
// ActionRecipe: a fixed image exists, but no recipe release uses it.
ActionRecipe = "recipe must update"
// ActionUpstream: no fixed image exists yet.
ActionUpstream = "upstream must update"
// ActionCheck: the audit could not decide.
ActionCheck = "check by hand"
// ActionAdvisory: the advisory's notes say what to do.
ActionAdvisory = "follow advisory"
)
// Finding is one advisory that applies to one deployed app.
type Finding struct {
Server string `json:"server"`
App string `json:"app"`
Recipe string `json:"recipe"`
Version string `json:"version"`
// Chaos and ChaosVersion are copied from AppState.
Chaos bool `json:"chaos"`
ChaosVersion string `json:"chaosVersion,omitempty"`
Advisory *Advisory `json:"-"`
Status string `json:"status"`
// Affected names what is vulnerable, e.g. "nginx 1.31.8" or
// "lasuite-docs 2.0.0+v6.0.4", from the conditions that matched.
Affected string `json:"affected"`
// Reasons explains, per recipe or image, why the finding was raised.
Reasons []string `json:"reasons"`
// Fix is a suggested command, or the advisory's notes for manual fixes.
Fix string `json:"fix"`
// Action says who has to act, one of the Action constants.
Action string `json:"action"`
// NoFix is set when no recipe release fixes the finding yet.
NoFix bool `json:"noFix"`
recipeAffected bool
// images that are affected or unknown, for VerifyRefresh
images []ImageRef
os, arch string
}
// verdict of one image rule for one image.
type verdict int
const (
notApplicable verdict = iota
fixed
affected
unknown
)
// EvaluateOptions configures Evaluate.
type EvaluateOptions struct {
// IncludeDrafts also applies advisories with status draft, so that a
// maintainer can try an advisory before publishing it.
IncludeDrafts bool
}
// Applies reports whether an advisory is used: published advisories always,
// drafts only on request, withdrawn advisories never.
func (o EvaluateOptions) Applies(adv Advisory) bool {
switch adv.Status {
case StatusPublished:
return true
case StatusDraft:
return o.IncludeDrafts
}
return false
}
// Evaluate matches every app against every advisory that applies.
func Evaluate(states []AppState, advisories []Advisory, opts EvaluateOptions) []Finding {
var findings []Finding
for _, st := range states {
for i := range advisories {
adv := &advisories[i]
if !opts.Applies(*adv) {
continue
}
if f, ok := evaluateApp(st, adv); ok {
findings = append(findings, f)
}
}
}
return findings
}
func evaluateApp(st AppState, adv *Advisory) (Finding, bool) {
f := Finding{
Server: st.Server,
App: st.Name,
Recipe: st.Recipe,
Version: st.Version,
Chaos: st.Chaos,
ChaosVersion: st.ChaosVersion,
Advisory: adv,
}
var gotAffected, gotUnknown bool
var affectedLabels []string
// 1. recipe ranges
if rr, ok := recipeRange(adv, st.Recipe); ok && st.Version != "" {
v, err := ParseVersion(st.Version)
if err == nil && rr.affected.Contains(v) {
gotAffected = true
f.recipeAffected = true
f.Reasons = append(f.Reasons, i18n.G("recipe version %s is in %s", st.Version, rr.affected.Raw))
affectedLabels = appendUnique(affectedLabels, ShortRecipeName(st.Recipe)+" "+st.Version)
}
}
// 2. image conditions
for _, img := range st.Images {
for _, rule := range adv.Images {
if !globMatch(rule.Match, img) {
continue
}
v, reason := evaluateImage(rule, img)
reason = fmt.Sprintf("%s (%s): %s", img.Service, img.NameTag(), reason)
switch v {
case affected:
gotAffected = true
case unknown:
gotUnknown = true
default:
continue
}
if !slices.Contains(f.Reasons, reason) {
f.Reasons = append(f.Reasons, reason)
}
affectedLabels = appendUnique(affectedLabels, affectedLabel(rule, img))
if !slices.ContainsFunc(f.images, func(i ImageRef) bool { return i.Ref == img.Ref }) {
f.images = append(f.images, img)
}
}
}
switch {
case gotAffected:
f.Status = FindingAffected
case gotUnknown:
f.Status = FindingUnknown
default:
return f, false
}
f.Affected = strings.Join(affectedLabels, ", ")
f.Action, f.Fix = suggestFix(f, st)
f.os, f.arch = st.OS, st.Arch
return f, true
}
// affectedLabel names what makes img affected under rule, in a few words:
// the runtime and its version for env conditions ("nginx 1.31.8"), the
// image's own version for version conditions, its build date for
// created_after, and the image itself otherwise.
func affectedLabel(rule ImageRule, img ImageRef) string {
info := img.Info
if info == nil || (img.Digest != "" && slices.Contains(rule.AffectedDigests, img.Digest)) {
return img.NameTag()
}
if len(rule.env) > 0 {
var keys []string
for k := range rule.env {
if _, ok := info.Env[k]; ok {
keys = append(keys, k)
}
}
if len(keys) == len(rule.env) {
slices.Sort(keys)
var parts []string
for _, k := range keys {
parts = append(parts, componentName(k)+" "+info.Env[k])
}
return strings.Join(parts, ", ")
}
}
if rule.version != nil && info.Version() != "" {
name := img.Name
if i := strings.LastIndex(name, "/"); i >= 0 {
name = name[i+1:]
}
return name + " " + info.Version()
}
if rule.createdAfter != nil && info.Created != nil && info.createdUnusable == "" {
return i18n.G("%s built %s", img.NameTag(), info.Created.UTC().Format("2006-01-02"))
}
return img.NameTag()
}
// componentName turns a version variable into the name of what it
// versions: NGINX_VERSION -> nginx, PHP_VERSION -> php.
func componentName(envVar string) string {
name := strings.TrimSuffix(envVar, "_VERSION")
return strings.ReplaceAll(strings.ToLower(name), "_", "-")
}
// evaluateImage applies one rule to one image that matches it:
//
// 1. a digest in affected_digests is affected;
// 2. a digest in fixed_digests is fixed;
// 3. the env block selects images: if a variable is missing, the rule does
// not apply (the image is not built on that runtime). If every variable
// is in its range, env votes affected, otherwise fixed;
// 4. version is an affected range on the image version, and votes the same way;
// 5. created_after votes fixed for images built at or after that time, and
// affected for older images, but only when the build time is usable.
//
// The image is fixed if any condition votes fixed, affected if one votes
// affected, and unknown otherwise: an image that matches but satisfies no
// condition is never reported as OK.
func evaluateImage(rule ImageRule, img ImageRef) (verdict, string) {
if img.Digest != "" && slices.Contains(rule.AffectedDigests, img.Digest) {
return affected, i18n.G("digest %s is known to be affected", shortDigest(img.Digest))
}
if img.Digest != "" && slices.Contains(rule.FixedDigests, img.Digest) {
return fixed, ""
}
needsConfig := len(rule.env) > 0 || rule.version != nil || rule.createdAfter != nil
info := img.Info
if needsConfig && info == nil {
if img.InfoErr != nil {
return unknown, i18n.G("image config unavailable: %s", img.InfoErr)
}
return unknown, i18n.G("image config unavailable")
}
var votedAffected []string
var notes []string
if len(rule.env) > 0 {
for k := range rule.env {
if _, ok := info.Env[k]; !ok {
return notApplicable, ""
}
}
allIn := true
var vals []string
for k, r := range rule.env {
val := info.Env[k]
vals = append(vals, fmt.Sprintf("%s=%s", k, val))
v, err := ParseVersion(val)
if err != nil {
allIn = false
notes = append(notes, i18n.G("unable to parse %s=%s", k, val))
continue
}
if !r.Contains(v) {
return fixed, ""
}
}
if allIn {
slices.Sort(vals)
votedAffected = append(votedAffected, strings.Join(vals, ", "))
}
}
if rule.version != nil {
raw := info.Version()
if raw == "" {
notes = append(notes, i18n.G("image has no version"))
} else if v, err := ParseVersion(raw); err != nil {
notes = append(notes, i18n.G("unable to parse image version %s", raw))
} else if rule.version.Contains(v) {
votedAffected = append(votedAffected, i18n.G("version %s is in %s", raw, rule.version.Raw))
} else {
return fixed, ""
}
}
if rule.createdAfter != nil {
switch {
case info.Created == nil:
notes = append(notes, i18n.G("image has no build time"))
case info.createdUnusable != "":
notes = append(notes, info.createdUnusable)
case !info.Created.Before(*rule.createdAfter):
return fixed, ""
default:
votedAffected = append(votedAffected, i18n.G("built %s, before %s", info.Created.UTC().Format("2006-01-02 15:04"), rule.createdAfter.UTC().Format("2006-01-02 15:04")))
}
}
if len(votedAffected) > 0 {
return affected, strings.Join(votedAffected, "; ")
}
if img.Digest == "" {
notes = append(notes, i18n.G("service spec has no digest (registry lookup failed at deploy?)"))
}
if len(rule.Skipped) > 0 {
notes = append(notes, i18n.G("conditions not supported by this abra: %s", strings.Join(rule.Skipped, ", ")))
}
if len(notes) == 0 {
notes = append(notes, i18n.G("no condition proves digest %s fixed", shortDigest(img.Digest)))
}
return unknown, strings.Join(notes, "; ")
}
// recipeRange looks up the advisory's range for a recipe. Advisories name
// recipes in the short form of ShortRecipeName, so "nextcloud" covers
// git.coopcloud.tech/coop-cloud/nextcloud but not a fork such as
// git.coopcloud.tech/c4dt/nextcloud, which has its own versions.
func recipeRange(adv *Advisory, recipe string) (RecipeRange, bool) {
if rr, ok := adv.Recipes[recipe]; ok {
return rr, true
}
rr, ok := adv.Recipes[ShortRecipeName(recipe)]
return rr, ok
}
func upgradeCommand(adv *Advisory, app, recipe string) string {
if rr, ok := recipeRange(adv, recipe); ok && rr.Fixed != "" {
return fmt.Sprintf("abra app upgrade %s %s", app, rr.Fixed)
}
return fmt.Sprintf("abra app upgrade %s", app)
}
// suggestFix returns the action and fix for a finding as the advisory
// suggests it. FixChecker then verifies the fix and may change both.
func suggestFix(f Finding, st AppState) (string, string) {
adv := f.Advisory
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 {
return ActionUpgrade, upgrade()
}
switch adv.Fix.Strategy {
case StrategyUpgrade:
return ActionUpgrade, upgrade()
case StrategyRefresh:
// without --chaos, deploy would replace the chaos deployment with
// the release in the .env file
if st.Chaos {
return ActionRedeploy, fmt.Sprintf("abra app deploy %s --chaos && abra audit", st.Name)
}
return ActionRedeploy, fmt.Sprintf("abra app deploy %s --force && abra audit", st.Name)
default:
if adv.Fix.Notes != "" {
return ActionAdvisory, strings.TrimSpace(adv.Fix.Notes)
}
return ActionAdvisory, i18n.G("No fix is known yet.")
}
}
// globMatch matches an advisory pattern against an image. "*" matches any
// sequence of characters, "?" a single one. Patterns with a ":" are matched
// against "name:tag", others against the name only.
func globMatch(pattern string, img ImageRef) bool {
target := img.Name
if strings.Contains(pattern, ":") {
target = img.NameTag()
}
return globToRegexp(pattern).MatchString(target)
}
func globToRegexp(pattern string) *regexp.Regexp {
var b strings.Builder
b.WriteString("^")
for _, r := range pattern {
switch r {
case '*':
b.WriteString(".*")
case '?':
b.WriteString(".")
default:
b.WriteString(regexp.QuoteMeta(string(r)))
}
}
b.WriteString("$")
return regexp.MustCompile(b.String())
}
func shortDigest(d string) string {
if d == "" {
return i18n.G("(none)")
}
d = strings.TrimPrefix(d, "sha256:")
if len(d) > 12 {
d = d[:12]
}
return "sha256:" + d
}
+442
View File
@@ -0,0 +1,442 @@
package audit
import (
"context"
"fmt"
"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)
// RegistryFetch is the default FetchFunc.
func RegistryFetch(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo, error) {
return FetchFromRegistry(ctx, img, os, arch)
}
// TagsFunc lists the tags of an image's repository.
type TagsFunc func(ctx context.Context, img ImageRef) ([]string, error)
// FixChecker checks that the suggested fixes actually exist, and decides
// who has to act on each finding (see the Action constants).
type FixChecker struct {
Fetch FetchFunc
// Tags lists repository tags, to find fixed images that no recipe
// release uses yet. If nil, no such search is made.
Tags TagsFunc
Recipes RecipeSource
// Checkout returns the images that abra app deploy --chaos would deploy
// for an app, see CheckoutImages. If nil, checkouts are not checked.
Checkout func(app string) ([]string, error)
cache map[string]fetchResult
tagsCache map[string]tagsResult
}
type fetchResult struct {
img *ImageRef
err error
}
type tagsResult struct {
tags []string
err error
}
// CheckFixes verifies the suggested fix of each finding, and sets its
// action:
//
// - a redeploy only helps if what it deploys is fixed: the tag's current
// image in the registry, or the digest the recipe pins. Upstreams stop
// rebuilding patch-pinned tags such as nextcloud:35.0.2-fpm once the
// next patch is out. If a redeploy does not help, an upgrade is looked
// for instead;
// - for an app deployed with --chaos, the recipe checkout and the app's
// overrides are checked first: if they use fixed images, a --chaos
// redeploy is the fix;
// - for an upgrade, the newer releases of the recipe are searched for the
// oldest one whose images are fixed;
// - if no release is fixed, the registry is searched for a fixed image
// (see findFixedTag): if one exists, the recipe must update, otherwise
// upstream must;
// - for a recipe version range, the advisory's fixed version must be a
// released tag of the recipe.
//
// When the registry or the recipe cannot be read, the suggestion is kept and
// marked as unchecked.
func (c *FixChecker) CheckFixes(ctx context.Context, findings []Finding) {
if c.cache == nil {
c.cache = make(map[string]fetchResult)
}
if c.tagsCache == nil {
c.tagsCache = make(map[string]tagsResult)
}
for i := range findings {
f := &findings[i]
switch {
case f.recipeAffected:
c.checkRecipeRange(f)
case len(f.images) == 0:
case f.Advisory.Fix.Strategy == StrategyRefresh:
c.checkRefresh(ctx, f)
case f.Advisory.Fix.Strategy == StrategyUpgrade:
c.checkUpgrade(ctx, f, f.images)
}
}
}
func (c *FixChecker) checkRecipeRange(f *Finding) {
rr, ok := recipeRange(f.Advisory, f.Recipe)
if !ok || rr.Fixed == "" || c.Recipes == nil {
return
}
versions, err := c.Recipes.Versions(f.Recipe)
if err != nil {
log.Debug(i18n.G("%s: unable to list versions of %s: %s", f.App, f.Recipe, err))
f.Fix += " " + i18n.G("(unchecked: %s)", err)
return
}
want, _ := ParseVersion(rr.Fixed)
for _, v := range versions {
if pv, err := ParseVersion(v); err == nil && pv.Compare(want) >= 0 {
return
}
}
short := ShortRecipeName(f.Recipe)
f.Action = ActionRecipe
f.Fix = i18n.G("The advisory names %s %s as fixed, but the latest release is %s. A release of %s with the fix is needed.",
short, rr.Fixed, lastOr(versions, i18n.G("none")), short)
f.NoFix = true
}
func (c *FixChecker) checkRefresh(ctx context.Context, f *Finding) {
if f.arch == "" {
f.Fix += " " + i18n.G("(unchecked: platform unknown)")
return
}
var stale []ImageRef
var fetchErr error
for _, img := range f.images {
// a redeploy deploys the digest the recipe pins, if any, otherwise
// whatever the tag points to now
target, byDigest := img, false
if d := c.pinnedDigest(f, img); d != "" {
target.Digest, byDigest = d, true
} else if img.Tag == "" {
stale = append(stale, img)
continue
}
cur, err := c.current(ctx, target, f.os, f.arch, byDigest)
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) {
if byDigest {
f.Reasons = append(f.Reasons, i18n.G("redeploy would not help: the recipe pins %s to %s", img.NameTag(), shortDigest(target.Digest)))
} else {
f.Reasons = append(f.Reasons, i18n.G("redeploy would not help: the current image of %s (%s) is not fixed either", img.NameTag(), shortDigest(cur.Digest)))
}
stale = append(stale, img)
}
}
switch {
case len(stale) > 0:
c.checkUpgrade(ctx, f, stale)
case fetchErr != nil:
f.Fix += " " + i18n.G("(unchecked: registry unreachable)")
default:
var names []string
for _, img := range f.images {
names = appendUnique(names, img.NameTag())
}
f.Fix = i18n.G("The registry's %s now has a fixed image: %s", strings.Join(names, ", "), f.Fix)
}
}
// pinnedDigest returns the digest that a redeploy of the app would use for
// the image, if its compose files pin one: those of the deployed release,
// or for a --chaos deployment those of the checkout.
func (c *FixChecker) pinnedDigest(f *Finding, img ImageRef) string {
var refs []string
var err error
switch {
case f.Chaos && c.Checkout != nil:
refs, err = c.Checkout(f.App)
case !f.Chaos && c.Recipes != nil && f.Version != "":
refs, err = c.Recipes.Images(f.Recipe, f.Version)
default:
return ""
}
if err != nil {
return ""
}
for _, ref := range refs {
if r, err := ParseImageRef("", ref); err == nil && r.Name == img.Name && r.Digest != "" {
return r.Digest
}
}
return ""
}
// checkUpgrade looks for the oldest recipe release newer than the deployed
// one whose images are all fixed. stale are the images a redeploy does not
// fix.
func (c *FixChecker) checkUpgrade(ctx context.Context, f *Finding, stale []ImageRef) {
f.Action = ActionUpgrade
generic := upgradeCommand(f.Advisory, f.App, f.Recipe)
if c.Recipes == nil || f.arch == "" {
f.Fix = generic + " " + i18n.G("(unchecked: no fixed release looked up)")
return
}
names := make(map[string]bool)
for _, img := range stale {
names[img.Name] = true
}
// A chaos deployment runs the recipe checkout: if the checkout already
// uses fixed images, redeploying it is the fix.
if f.Chaos && c.Checkout != nil {
if fixedImgs, ok := c.checkoutFixed(ctx, f, names); ok {
f.Action = ActionRedeploy
f.Fix = i18n.G("The recipe checkout already uses %s, which is fixed: %s",
strings.Join(fixedImgs, ", "), fmt.Sprintf("abra app deploy %s --chaos && abra audit", f.App))
return
}
}
versions, err := c.Recipes.Versions(f.Recipe)
if err != nil {
log.Debug(i18n.G("%s: unable to list versions of %s: %s", f.App, f.Recipe, err))
f.Fix = generic + " " + i18n.G("(unchecked: %s)", err)
return
}
current, curErr := ParseVersion(f.Version)
var newer []string
for _, v := range versions {
pv, _ := ParseVersion(v)
if curErr != nil || pv.Compare(current) > 0 {
newer = append(newer, v)
}
}
if len(newer) == 0 {
f.Reasons = append(f.Reasons, i18n.G("no newer release of %s exists", ShortRecipeName(f.Recipe)))
c.noRelease(ctx, f, stale)
return
}
// Check the latest release first: if it is not fixed, no release is.
fixedAt := func(v string) (bool, error) { return c.versionFixed(ctx, f, v, names) }
latest := newer[len(newer)-1]
ok, err := fixedAt(latest)
if err != nil {
f.Fix = generic + " " + i18n.G("(unchecked: %s)", err)
return
}
if !ok {
f.Reasons = append(f.Reasons, i18n.G("the latest release of %s, %s, is not fixed", ShortRecipeName(f.Recipe), latest))
c.noRelease(ctx, f, stale)
return
}
best := latest
for _, v := range newer[:len(newer)-1] {
if ok, err := fixedAt(v); err == nil && ok {
best = v
break
}
}
f.Fix = i18n.G("Release %s uses fixed images: %s", best, fmt.Sprintf("abra app upgrade %s %s", f.App, best))
}
// noRelease handles a finding that no recipe release fixes: it searches the
// registry for a fixed image of each stale image. If every one has a fixed
// image, the recipe must update, otherwise upstream must.
func (c *FixChecker) noRelease(ctx context.Context, f *Finding, stale []ImageRef) {
f.NoFix = true
short := ShortRecipeName(f.Recipe)
var found, missing []string
var searchErr error
for _, img := range stale {
res, err := c.findFixedTag(ctx, f, img)
switch {
case err != nil:
log.Debug(i18n.G("%s: unable to search for a fixed %s: %s", f.App, img.Name, err))
searchErr = err
case res.tag == "":
missing = append(missing, upstreamState(img, res.current))
case res.sameTag:
found = append(found, i18n.G("%s (its current image)", img.Name+":"+res.tag))
default:
found = append(found, img.Name+":"+res.tag)
}
}
switch {
case len(missing) > 0:
f.Action = ActionUpstream
f.Fix = i18n.G("No fixed image exists yet: %s.", strings.Join(missing, "; "))
if f.Advisory.Fix.Notes != "" {
f.Fix += " " + i18n.G("Until then, see the advisory's mitigations.")
}
case searchErr != nil:
f.Action = ActionCheck
f.Fix = i18n.G("No release of %s fixes it, and the registry could not be searched for a fixed image: %s", short, searchErr)
case f.Chaos:
f.Action = ActionRecipe
checkout := i18n.G("the recipe checkout")
if f.ChaosVersion != "" {
checkout += " (" + i18n.G("deployed with --chaos from %s", f.ChaosVersion) + ")"
}
f.Fix = i18n.G("Upstream published a fixed image, %s. Use it in %s or the app's override, then run abra app deploy %s --chaos.",
strings.Join(found, ", "), checkout, f.App)
default:
f.Action = ActionRecipe
f.Fix = i18n.G("Upstream published a fixed image, %s, but no release of %s uses it yet. Release %s with it, then run abra app upgrade %s.",
strings.Join(found, ", "), short, short, f.App)
}
}
// upstreamState says why there is no fixed image of img: its tag was not
// rebuilt, or rebuilt but still affected.
func upstreamState(img ImageRef, current *ImageRef) string {
if current != nil && current.Digest != img.Digest {
if current.Info != nil && current.Info.Created != nil && current.Info.createdUnusable == "" {
return i18n.G("%s was rebuilt on %s but is still affected, and no later tag is fixed",
img.NameTag(), current.Info.Created.UTC().Format(time.DateOnly))
}
return i18n.G("%s was rebuilt but is still affected, and no later tag is fixed", img.NameTag())
}
return i18n.G("%s has not been rebuilt with a fix, and no later tag is fixed", img.NameTag())
}
// versionFixed reports whether every image of the recipe version that has
// one of the given names is fixed. An image that is no longer used counts as
// fixed.
func (c *FixChecker) versionFixed(ctx context.Context, f *Finding, version string, names map[string]bool) (bool, error) {
refs, err := c.Recipes.Images(f.Recipe, version)
if err != nil {
return false, err
}
ok, _, err := c.imagesFixed(ctx, f, version, refs, names)
return ok, err
}
// checkoutFixed reports whether a --chaos redeploy of the app, from its
// recipe checkout and overrides, would use fixed images, and which. A
// checkout that drops the affected images is not reported: that is more
// likely an unrelated change than a fix.
func (c *FixChecker) checkoutFixed(ctx context.Context, f *Finding, names map[string]bool) ([]string, bool) {
refs, err := c.Checkout(f.App)
if err != nil {
log.Debug(i18n.G("%s: unable to read the recipe checkout of %s: %s", f.App, f.Recipe, err))
return nil, false
}
ok, used, err := c.imagesFixed(ctx, f, i18n.G("checkout"), refs, names)
if err != nil {
log.Debug(i18n.G("%s: unable to check the recipe checkout of %s: %s", f.App, f.Recipe, err))
return nil, false
}
return used, ok && len(used) > 0
}
// imagesFixed reports whether every image in refs that has one of the given
// names is fixed, and returns those images. where names the recipe version
// for debug messages.
func (c *FixChecker) imagesFixed(ctx context.Context, f *Finding, where string, refs []string, names map[string]bool) (bool, []string, error) {
var used []string
for _, ref := range refs {
img, err := ParseImageRef("", ref)
if err != nil || !names[img.Name] {
continue
}
used = append(used, img.NameTag())
cur, err := c.current(ctx, img, f.os, f.arch, true)
if err != nil {
return false, used, err
}
if !fixedByAdvisory(f.Advisory, *cur) {
log.Debug(i18n.G("%s %s: %s is not fixed", ShortRecipeName(f.Recipe), where, img.NameTag()))
return false, used, nil
}
}
return true, used, nil
}
// current reads the image a reference points to now: by digest if byDigest
// is set and the reference has one, by tag otherwise.
func (c *FixChecker) current(ctx context.Context, img ImageRef, os, arch string, byDigest bool) (*ImageRef, error) {
ref := img
if !byDigest {
ref.Digest = ""
}
key := strings.Join([]string{ref.NameTag(), ref.Digest, os, arch}, "|")
if r, ok := c.cache[key]; ok {
return r.img, r.err
}
ref.Info, ref.InfoErr = nil, nil
fctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
info, err := c.Fetch(fctx, ref, os, arch)
if err != nil {
c.cache[key] = fetchResult{err: err}
return nil, err
}
info.createdUnusable = createdUnusable(info)
if info.Digest != "" {
ref.Digest = info.Digest
}
ref.Info = info
c.cache[key] = fetchResult{img: &ref}
return &ref, nil
}
// 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
}
func appendUnique(list []string, s string) []string {
if containsString(list, s) {
return list
}
return append(list, s)
}
func lastOr(list []string, def string) string {
if len(list) == 0 {
return def
}
return list[len(list)-1]
}
+332
View File
@@ -0,0 +1,332 @@
package audit
import (
"context"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"time"
appPkg "coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/recipe"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const phpAdvisory = `
id: PHP
status: published
recipes:
ranged: {fixed: 2.0.0+1.0.0}
images:
- match: "*"
env: { PHP_VERSION: ">=8.5.0 <8.5.13" }
fix: {strategy: refresh}
`
// fakeRecipes maps recipe -> version -> images.
type fakeRecipes map[string]map[string][]string
func (f fakeRecipes) Versions(name string) ([]string, error) {
vs, ok := f[name]
if !ok {
return nil, errors.New("no such recipe")
}
var out []string
for v := range vs {
out = append(out, v)
}
sortVersions(out)
return out, nil
}
func (f fakeRecipes) Images(name, version string) ([]string, error) {
return f[name][version], nil
}
func digestOf(c string) string { return "sha256:" + strings.Repeat(c, 64) }
func TestCheckFixes(t *testing.T) {
adv, _, err := ParseAdvisory([]byte(phpAdvisory))
require.NoError(t, err)
old := &ImageInfo{Env: map[string]string{"PHP_VERSION": "8.5.12"}}
app := func(name, recipe, version, image string, platform bool) AppState {
st := AppState{Server: "s", Name: name, Recipe: recipe, Version: version,
Images: []ImageRef{mustImage(t, "app", image, old)}}
if platform {
st.OS, st.Arch = "linux", "amd64"
}
return st
}
states := []AppState{
app("floating", "nc", "1.0.0+35.0.2", "nextcloud:35-fpm@"+digestOf("a"), true),
app("pinned", "nc", "1.0.0+35.0.2", "nextcloud:35.0.2-fpm@"+digestOf("b"), true),
app("fork", "git.coopcloud.tech/c4dt/nextcloud", "16.2.2+35.0.2-fpm", "nextcloud:35.0.2-fpm@"+digestOf("b"), true),
app("stale", "stale", "1.0.0+35.0.1", "nextcloud:35.0.1-fpm@"+digestOf("e"), true),
app("offline", "nc", "1.0.0+35.0.2", "other:1@"+digestOf("c"), true),
app("noplatform", "nc", "1.0.0+35.0.2", "nextcloud:35-fpm@"+digestOf("a"), false),
app("ranged", "ranged", "1.0.0+1.0.0", "postgres:15@"+digestOf("f"), true),
app("upstream", "up", "1.0.0+1.0.0", "frozen:1.0.0@"+digestOf("9"), true),
app("digestpin", "dp", "1.0.0+35", "nextcloud:35-fpm@"+digestOf("a"), true),
app("unsearchable", "us", "1.0.0+1.0.0", "private:1.0.0@"+digestOf("8"), true),
}
// the same apps, deployed with --chaos
for _, name := range []string{"floating", "fork", "stale", "pinned"} {
for _, st := range states {
if st.Name == name {
st.Name, st.Chaos, st.ChaosVersion = "chaos-"+name, true, "1234abcd+U"
states = append(states, st)
break
}
}
}
recipes := fakeRecipes{
"nc": {
"1.0.0+35.0.2": {"nextcloud:35.0.2-fpm", "postgres:15"},
"1.0.1+35.0.3": {"nextcloud:35.0.3-fpm", "postgres:15"},
"1.1.0+35.0.4": {"nextcloud:35.0.4-fpm", "postgres:15"},
},
"git.coopcloud.tech/c4dt/nextcloud": {
"16.2.1+35.0.2-fpm": {"nextcloud:35.0.2-fpm"},
"16.2.2+35.0.2-fpm": {"nextcloud:35.0.2-fpm"},
},
"stale": {
"1.0.0+35.0.1": {"nextcloud:35.0.1-fpm"},
"1.0.1+35.0.2": {"nextcloud:35.0.2-fpm"},
},
"ranged": {"1.0.0+1.0.0": {"postgres:15"}},
"up": {"1.0.0+1.0.0": {"frozen:1.0.0"}},
// the recipe pins the old digest of a tag that has been rebuilt
"dp": {"1.0.0+35": {"nextcloud:35-fpm@" + digestOf("a")}},
"us": {"1.0.0+1.0.0": {"private:1.0.0"}},
}
// PHP version per tag; a pinned digest a is the old 35-fpm image
php := map[string]string{
"nextcloud:35-fpm": "8.5.13",
"nextcloud:35.0.2-fpm": "8.5.12",
"nextcloud:35.0.1-fpm": "8.5.12",
"nextcloud:35.0.3-fpm": "8.5.13",
"nextcloud:35.0.4-fpm": "8.5.13",
"frozen:1.0.0": "8.5.12",
"frozen:1.0.1": "8.5.12",
"private:1.0.0": "8.5.12",
}
var fetched []string
fetch := func(_ context.Context, img ImageRef, os, arch string) (*ImageInfo, error) {
fetched = append(fetched, img.NameTag()+"@"+img.Digest)
if img.Digest == digestOf("a") {
return &ImageInfo{Digest: digestOf("a"), Env: map[string]string{"PHP_VERSION": "8.5.12"}}, nil
}
v, ok := php[img.NameTag()]
if !ok {
return nil, errors.New("registry down")
}
d := digestOf("d")
switch img.NameTag() {
case "nextcloud:35.0.2-fpm":
d = digestOf("b")
case "frozen:1.0.0":
d = digestOf("9")
case "private:1.0.0":
d = digestOf("8")
}
return &ImageInfo{Digest: d, Env: map[string]string{"PHP_VERSION": v}}, nil
}
tags := func(_ context.Context, img ImageRef) ([]string, error) {
switch img.Name {
case "nextcloud":
return []string{"35-fpm", "35.0.1-fpm", "35.0.2-fpm", "35.0.3-fpm", "35.0.4-fpm", "latest"}, nil
case "frozen":
return []string{"1.0.0", "1.0.1"}, nil
}
return nil, errors.New("unauthorized")
}
// what a --chaos redeploy would use: chaos-fork's checkout already moved
// to a fixed image, chaos-stale's still uses the deployed one, and
// chaos-pinned's cannot be read
checkout := func(app string) ([]string, error) {
switch app {
case "chaos-fork":
return []string{"nextcloud:35.0.3-fpm"}, nil
case "chaos-stale":
return []string{"nextcloud:35.0.1-fpm"}, nil
case "chaos-floating":
return []string{"nextcloud:35-fpm"}, nil
}
return nil, errors.New("no checkout")
}
findings := Evaluate(states, []Advisory{adv}, EvaluateOptions{})
require.Len(t, findings, 14)
checker := FixChecker{Fetch: fetch, Tags: tags, Recipes: recipes, Checkout: checkout}
checker.CheckFixes(context.Background(), findings)
got := map[string]Finding{}
for _, f := range findings {
got[f.App] = f
}
check := func(app, action, fix string) {
t.Helper()
assert.Equal(t, action, got[app].Action, app)
assert.Equal(t, fix, got[app].Fix, app)
assert.Equal(t, action == ActionRecipe || action == ActionUpstream || action == ActionCheck, got[app].NoFix, app)
}
check("floating", ActionRedeploy, "The registry's nextcloud:35-fpm now has a fixed image: abra app deploy floating --force && abra audit")
check("pinned", ActionUpgrade, "Release 1.0.1+35.0.3 uses fixed images: abra app upgrade pinned 1.0.1+35.0.3")
check("fork", ActionRecipe, "Upstream published a fixed image, nextcloud:35.0.3-fpm, but no release of c4dt/nextcloud uses it yet. Release c4dt/nextcloud with it, then run abra app upgrade fork.")
// 35.0.2-fpm is newer but not fixed: the oldest fixed tag is suggested
check("stale", ActionRecipe, "Upstream published a fixed image, nextcloud:35.0.3-fpm, but no release of stale uses it yet. Release stale with it, then run abra app upgrade stale.")
check("offline", ActionRedeploy, "abra app deploy offline --force && abra audit (unchecked: registry unreachable)")
check("noplatform", ActionRedeploy, "abra app deploy noplatform --force && abra audit (unchecked: platform unknown)")
check("ranged", ActionRecipe, "The advisory names ranged 2.0.0+1.0.0 as fixed, but the latest release is 1.0.0+1.0.0. A release of ranged with the fix is needed.")
check("upstream", ActionUpstream, "No fixed image exists yet: frozen:1.0.0 has not been rebuilt with a fix, and no later tag is fixed.")
check("digestpin", ActionRecipe, "Upstream published a fixed image, nextcloud:35-fpm (its current image), but no release of dp uses it yet. Release dp with it, then run abra app upgrade digestpin.")
check("unsearchable", ActionCheck, "No release of us fixes it, and the registry could not be searched for a fixed image: unauthorized")
// a chaos deployment is redeployed with --chaos, and fixed in the checkout
check("chaos-floating", ActionRedeploy, "The registry's nextcloud:35-fpm now has a fixed image: abra app deploy chaos-floating --chaos && abra audit")
check("chaos-fork", ActionRedeploy, "The recipe checkout already uses nextcloud:35.0.3-fpm, which is fixed: abra app deploy chaos-fork --chaos && abra audit")
// without a readable checkout, the releases are checked as usual
check("chaos-pinned", ActionUpgrade, "Release 1.0.1+35.0.3 uses fixed images: abra app upgrade chaos-pinned 1.0.1+35.0.3")
check("chaos-stale", ActionRecipe, "Upstream published a fixed image, nextcloud:35.0.3-fpm. Use it in the recipe checkout (deployed with --chaos from 1234abcd+U) or the app's override, then run abra app deploy chaos-stale --chaos.")
// each image is fetched once, by tag or by digest
seen := map[string]int{}
for _, f := range fetched {
seen[f]++
}
for img, n := range seen {
if !strings.HasPrefix(img, "other:1@") {
assert.Equal(t, 1, n, img)
}
}
}
func TestShortRecipeName(t *testing.T) {
for in, want := range map[string]string{
"nextcloud": "nextcloud",
"git.coopcloud.tech/coop-cloud/nextcloud": "nextcloud",
"git.coopcloud.tech/c4dt/nextcloud": "c4dt/nextcloud",
"https://git.coopcloud.tech/c4dt/nextcloud.git": "c4dt/nextcloud",
"github.com/foo/nextcloud": "github.com/foo/nextcloud",
} {
assert.Equal(t, want, ShortRecipeName(in), in)
}
}
func TestRecipeRangeForks(t *testing.T) {
adv, _, err := ParseAdvisory([]byte("id: X\nstatus: published\nrecipes: {nextcloud: {fixed: 7.0.0}}\n"))
require.NoError(t, err)
_, ok := recipeRange(&adv, "nextcloud")
assert.True(t, ok)
_, ok = recipeRange(&adv, "git.coopcloud.tech/coop-cloud/nextcloud")
assert.True(t, ok)
_, ok = recipeRange(&adv, "git.coopcloud.tech/c4dt/nextcloud")
assert.False(t, ok, "a fork has its own versions")
}
func TestComposeImages(t *testing.T) {
assert.Equal(t, []string{"nextcloud:35.0.2-fpm", "nginx:1.29", "postgres:15"}, composeImages(`
services:
app: {image: nextcloud:35.0.2-fpm}
web: {image: "${WEB_IMAGE:-nginx:1.29}"}
db: {image: postgres:15}
custom: {image: "${CUSTOM_IMAGE}"}
build: {build: .}
`))
}
func TestGitRecipes(t *testing.T) {
dir := t.TempDir()
repo, err := git.PlainInit(dir, false)
require.NoError(t, err)
sig := &object.Signature{Name: "t", Email: "t@example.com", When: time.Now()}
release := func(version, image string) {
commitFile(t, repo, dir, "compose.yml", "services:\n app:\n image: "+image+"\n")
head, err := repo.Head()
require.NoError(t, err)
_, err = repo.CreateTag(version, head.Hash(), &git.CreateTagOptions{Tagger: sig, Message: version})
require.NoError(t, err)
}
release("1.0.0+35.0.2-fpm", "nextcloud:35.0.2-fpm")
release("1.0.1+35.0.3-fpm", "nextcloud:35.0.3-fpm")
// a local edit must survive
require.NoError(t, os.WriteFile(filepath.Join(dir, "compose.yml"), []byte("local edit\n"), 0o644))
g := NewGitRecipes([]recipe.Recipe{{Name: "nc", Dir: dir}}, true)
versions, err := g.Versions("nc")
require.NoError(t, err)
assert.Equal(t, []string{"1.0.0+35.0.2-fpm", "1.0.1+35.0.3-fpm"}, versions)
images, err := g.Images("nc", "1.0.0+35.0.2-fpm")
require.NoError(t, err)
assert.Equal(t, []string{"nextcloud:35.0.2-fpm"}, images)
data, err := os.ReadFile(filepath.Join(dir, "compose.yml"))
require.NoError(t, err)
assert.Equal(t, "local edit\n", string(data))
}
func TestCheckoutImages(t *testing.T) {
root := t.TempDir()
recipeDir := filepath.Join(root, "recipes", "nc")
serverDir := filepath.Join(root, "servers", "srv")
require.NoError(t, os.MkdirAll(recipeDir, 0o755))
require.NoError(t, os.MkdirAll(serverDir, 0o755))
write := func(file, content string) {
require.NoError(t, os.WriteFile(file, []byte(content), 0o644))
}
write(filepath.Join(recipeDir, "compose.yml"), `version: "3.8"
services:
app:
image: nextcloud:35.0.4-fpm
web:
image: nginx:${NGINX_TAG:-1.31.8}
db:
image: postgres:15
`)
write(filepath.Join(recipeDir, "compose.smtp.yml"), `version: "3.8"
services:
smtp:
image: postfix:3
`)
// an override outside the recipe directory pins the app image
write(filepath.Join(serverDir, "nc.override.yaml"), `version: "3.8"
services:
app:
image: nextcloud:35.0.2-fpm
`)
app := appPkg.App{
Name: "nc.example.com",
Recipe: recipe.Recipe{Name: "nc", Dir: recipeDir,
ComposePath: filepath.Join(recipeDir, "compose.yml")},
Env: map[string]string{
"COMPOSE_FILE": "compose.yml:../../servers/srv/nc.override.yaml",
"NGINX_TAG": "1.31.9",
},
}
images, err := CheckoutImages(app)
require.NoError(t, err)
assert.Equal(t, []string{"nextcloud:35.0.2-fpm", "nginx:1.31.9", "postgres:15"}, images)
app.Env = map[string]string{"COMPOSE_FILE": "compose.yml:missing.yml"}
_, err = CheckoutImages(app)
assert.Error(t, err)
}
+326
View File
@@ -0,0 +1,326 @@
package audit
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
"time"
"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"
dockerClient "github.com/docker/docker/client"
"github.com/opencontainers/go-digest"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
)
// OCI annotations and labels read by the audit.
const (
AnnotationVersion = "org.opencontainers.image.version"
AnnotationBaseName = "org.opencontainers.image.base.name"
AnnotationBaseDigest = "org.opencontainers.image.base.digest"
)
// ImageRef is one image as referenced in a service spec.
type ImageRef struct {
Service string `json:"service"`
// Ref is the reference as stored in the service spec, typically
// image:tag@sha256:...
Ref string `json:"ref"`
// Name is the familiar image name, e.g. "nextcloud" or "git.coopcloud.tech/coop-cloud/foo".
Name string `json:"name"`
Tag string `json:"tag"`
Digest string `json:"digest"`
Info *ImageInfo `json:"-"`
InfoErr error `json:"-"`
named reference.Named
}
// 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
Annotations map[string]string
// BaseCreated is the build time of the base image named in the
// base.digest annotation, if it could be read.
BaseCreated *time.Time
// Source is "node" or "registry".
Source string
// createdUnusable is set when the build time cannot be trusted.
createdUnusable string
}
// ParseImageRef parses an image reference from a service spec.
func ParseImageRef(service, ref string) (ImageRef, error) {
img := ImageRef{Service: service, Ref: ref}
named, err := reference.ParseNormalizedNamed(ref)
if err != nil {
return img, err
}
img.named = named
img.Name = reference.FamiliarName(named)
if t, ok := named.(reference.Tagged); ok {
img.Tag = t.Tag()
}
if d, ok := named.(reference.Digested); ok {
img.Digest = d.Digest().String()
}
return img, nil
}
// NameTag returns "name:tag", the string matched against advisory patterns.
func (i ImageRef) NameTag() string {
if i.Tag == "" {
return i.Name
}
return i.Name + ":" + i.Tag
}
// Version returns the image's version from its label or annotation.
func (i *ImageInfo) Version() string {
if v := i.Labels[AnnotationVersion]; v != "" {
return v
}
return i.Annotations[AnnotationVersion]
}
// InspectOnNode reads the image config from the docker daemon of the server.
// This needs no registry, and works on single-node servers where the running
// image is always present.
func InspectOnNode(ctx context.Context, cl *dockerClient.Client, img ImageRef) (*ImageInfo, error) {
ref := img.Ref
if img.Digest != "" && img.named != nil {
ref = fmt.Sprintf("%s@%s", reference.TrimNamed(img.named).String(), img.Digest)
}
resp, err := cl.ImageInspect(ctx, ref)
if err != nil {
return nil, err
}
info := &ImageInfo{Source: "node", Env: map[string]string{}, Labels: map[string]string{}}
if resp.Created != "" {
if t, err := time.Parse(time.RFC3339Nano, resp.Created); err == nil {
info.Created = &t
}
}
if resp.Config != nil {
info.Env = parseEnv(resp.Config.Env)
if resp.Config.Labels != nil {
info.Labels = resp.Config.Labels
}
}
return info, nil
}
// manifestDoc covers the fields needed from image indexes, Docker manifest
// lists and image manifests.
type manifestDoc struct {
MediaType string `json:"mediaType"`
Annotations map[string]string `json:"annotations"`
Config *struct {
Digest string `json:"digest"`
MediaType string `json:"mediaType"`
Size int64 `json:"size"`
} `json:"config"`
Manifests []struct {
Digest string `json:"digest"`
Annotations map[string]string `json:"annotations"`
Platform *struct {
Architecture string `json:"architecture"`
OS string `json:"os"`
Variant string `json:"variant"`
} `json:"platform"`
} `json:"manifests"`
}
// FetchFromRegistry reads the image config for the given platform from the
// registry. Only a few KB are transferred; no layers are pulled. When the
// image records its base in the base.digest annotation, the base image's
// build time is read as well.
func FetchFromRegistry(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo, error) {
info, err := fetchConfig(ctx, img, os, arch)
if err != nil {
return nil, err
}
baseName := info.Annotations[AnnotationBaseName]
baseDigest := info.Annotations[AnnotationBaseDigest]
if baseName != "" && baseDigest != "" {
if base, err := ParseImageRef("", fmt.Sprintf("%s@%s", stripTagDigest(baseName), baseDigest)); err == nil {
if bi, err := fetchConfig(ctx, base, os, arch); err == nil {
info.BaseCreated = bi.Created
}
}
}
return info, nil
}
// RegistryTags lists the tags of the image's repository.
func RegistryTags(ctx context.Context, img ImageRef) ([]string, error) {
if img.named == nil {
return nil, errors.New(i18n.G("invalid image reference %s", img.Ref))
}
ref, err := docker.ParseReference("//" + reference.TrimNamed(img.named).String())
if err != nil {
return nil, err
}
return docker.GetRepositoryTags(ctx, &types.SystemContext{}, ref)
}
func stripTagDigest(name string) string {
named, err := reference.ParseNormalizedNamed(name)
if err != nil {
return name
}
return reference.TrimNamed(named).String()
}
func fetchConfig(ctx context.Context, img ImageRef, os, arch string) (*ImageInfo, error) {
if img.named == nil {
return nil, errors.New(i18n.G("invalid image reference %s", img.Ref))
}
// NOTE: containers/image refuses references with both tag and digest
refStr := reference.TrimNamed(img.named).String()
if img.Digest != "" {
refStr += "@" + img.Digest
} else if img.Tag != "" {
refStr += ":" + img.Tag
}
ref, err := docker.ParseReference("//" + refStr)
if err != nil {
return nil, err
}
src, err := ref.NewImageSource(ctx, &types.SystemContext{})
if err != nil {
return nil, err
}
defer src.Close()
raw, _, err := src.GetManifest(ctx, nil)
if err != nil {
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
}
annotations := map[string]string{}
for k, v := range doc.Annotations {
annotations[k] = v
}
if len(doc.Manifests) > 0 {
var chosen string
for _, m := range doc.Manifests {
if m.Platform != nil && m.Platform.OS == os && m.Platform.Architecture == arch {
chosen = m.Digest
for k, v := range m.Annotations {
annotations[k] = v
}
break
}
}
if chosen == "" {
return nil, errors.New(i18n.G("%s has no image for %s/%s", img.Ref, os, arch))
}
d := digest.Digest(chosen)
raw, _, err = src.GetManifest(ctx, &d)
if err != nil {
return nil, err
}
doc = manifestDoc{}
if err := json.Unmarshal(raw, &doc); err != nil {
return nil, err
}
for k, v := range doc.Annotations {
annotations[k] = v
}
}
if doc.Config == nil || doc.Config.Digest == "" {
return nil, errors.New(i18n.G("%s: unsupported manifest type %s", img.Ref, doc.MediaType))
}
blob, _, err := src.GetBlob(ctx, types.BlobInfo{Digest: digest.Digest(doc.Config.Digest), Size: doc.Config.Size}, none.NoCache)
if err != nil {
return nil, err
}
defer blob.Close()
cfgBytes, err := io.ReadAll(io.LimitReader(blob, 4<<20))
if err != nil {
return nil, err
}
var cfg imgspecv1.Image
if err := json.Unmarshal(cfgBytes, &cfg); err != nil {
return nil, err
}
info := &ImageInfo{
Source: "registry",
Digest: topDigest.String(),
Created: cfg.Created,
Env: parseEnv(cfg.Config.Env),
Labels: cfg.Config.Labels,
Annotations: annotations,
}
if info.Labels == nil {
info.Labels = map[string]string{}
}
return info, nil
}
func parseEnv(env []string) map[string]string {
out := make(map[string]string, len(env))
for _, e := range env {
k, v, _ := strings.Cut(e, "=")
out[k] = v
}
return out
}
// NormaliseArch maps the architecture names reported by the docker daemon
// (uname -m style) to OCI platform names.
func NormaliseArch(arch string) string {
switch arch {
case "x86_64", "amd64":
return "amd64"
case "aarch64", "arm64":
return "arm64"
case "armv7l", "armv7", "armhf", "arm":
return "arm"
case "i386", "i686", "386":
return "386"
}
return arch
}
+256
View File
@@ -0,0 +1,256 @@
package audit
import (
"errors"
"path"
"regexp"
"sort"
"strings"
appPkg "coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/recipe"
"coopcloud.tech/abra/pkg/upstream/stack"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"gopkg.in/yaml.v3"
)
const canonicalRecipePrefix = "git.coopcloud.tech/coop-cloud/"
// ShortRecipeName returns the shortest unambiguous form of a recipe name,
// relative to the canonical git.coopcloud.tech/coop-cloud/ location:
//
// nextcloud -> nextcloud
// git.coopcloud.tech/coop-cloud/nextcloud -> nextcloud
// git.coopcloud.tech/c4dt/nextcloud -> c4dt/nextcloud
// github.com/foo/nextcloud -> github.com/foo/nextcloud
func ShortRecipeName(name string) string {
name = strings.TrimPrefix(name, "https://")
name = strings.TrimSuffix(name, ".git")
if strings.HasPrefix(name, canonicalRecipePrefix) {
return strings.TrimPrefix(name, canonicalRecipePrefix)
}
if strings.HasPrefix(name, "git.coopcloud.tech/") {
return strings.TrimPrefix(name, "git.coopcloud.tech/")
}
return name
}
// RecipeSource lists the released versions of a recipe and the images each
// version uses.
type RecipeSource interface {
Versions(recipeName string) ([]string, error)
Images(recipeName, version string) ([]string, error)
}
// GitRecipes reads recipe versions from the local recipe checkouts. It never
// changes the working tree: tags are fetched into the repository, and compose
// files are read from the tag's tree.
type GitRecipes struct {
Offline bool
recipes map[string]recipe.Recipe
repos map[string]*git.Repository
errs map[string]error
}
// NewGitRecipes prepares access to the recipes of the given recipes.
func NewGitRecipes(recipes []recipe.Recipe, offline bool) *GitRecipes {
g := &GitRecipes{
Offline: offline,
recipes: make(map[string]recipe.Recipe),
repos: make(map[string]*git.Repository),
errs: make(map[string]error),
}
for _, r := range recipes {
g.recipes[r.Name] = r
}
return g
}
func (g *GitRecipes) open(name string) (*git.Repository, error) {
if repo, ok := g.repos[name]; ok {
return repo, nil
}
if err, ok := g.errs[name]; ok {
return nil, err
}
repo, err := g.openUncached(name)
if err != nil {
g.errs[name] = err
return nil, err
}
g.repos[name] = repo
return repo, nil
}
func (g *GitRecipes) openUncached(name string) (*git.Repository, error) {
r, ok := g.recipes[name]
if !ok {
return nil, errors.New(i18n.G("unknown recipe %s", name))
}
repo, err := git.PlainOpen(r.Dir)
if err != nil {
if g.Offline {
return nil, err
}
if err := r.EnsureExists(); err != nil {
return nil, err
}
if repo, err = git.PlainOpen(r.Dir); err != nil {
return nil, err
}
}
if !g.Offline {
err := repo.Fetch(&git.FetchOptions{RemoteName: "origin", Tags: git.AllTags})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
log.Debug(i18n.G("%s: unable to fetch tags, using local tags: %s", name, err))
}
}
return repo, nil
}
// Versions returns the parseable release tags, oldest first.
func (g *GitRecipes) Versions(name string) ([]string, error) {
repo, err := g.open(name)
if err != nil {
return nil, err
}
iter, err := repo.Tags()
if err != nil {
return nil, err
}
var tags []string
_ = iter.ForEach(func(ref *plumbing.Reference) error {
tag := ref.Name().Short()
if _, err := ParseVersion(tag); err == nil {
tags = append(tags, tag)
}
return nil
})
sortVersions(tags)
return tags, nil
}
// Images returns the images used by a recipe version, read from the
// compose.yml and compose.*.yml files of that tag.
func (g *GitRecipes) Images(name, version string) ([]string, error) {
repo, err := g.open(name)
if err != nil {
return nil, err
}
hash, err := repo.ResolveRevision(plumbing.Revision("refs/tags/" + version))
if err != nil {
return nil, err
}
commit, err := repo.CommitObject(*hash)
if err != nil {
return nil, err
}
tree, err := commit.Tree()
if err != nil {
return nil, err
}
var images []string
err = tree.Files().ForEach(func(f *object.File) error {
if strings.Contains(f.Name, "/") {
return nil
}
if ok, _ := path.Match("compose*yml", f.Name); !ok {
return nil
}
content, err := f.Contents()
if err != nil {
return err
}
for _, img := range composeImages(content) {
if !containsString(images, img) {
images = append(images, img)
}
}
return nil
})
return images, err
}
// CheckoutImages returns the images that abra app deploy --chaos would
// deploy for the app: the compose files of its COMPOSE_FILE, read from the
// recipe checkout as they are on disk, merged and with the app's .env
// values filled in, exactly as deploy does. Overrides outside the recipe
// directory are included.
func CheckoutImages(app appPkg.App) ([]string, error) {
files, err := app.Recipe.GetComposeFiles(app.Env)
if err != nil {
return nil, err
}
compose, err := appPkg.GetAppComposeConfig(app.Name, stack.Deploy{Composefiles: files}, app.Env)
if err != nil {
return nil, err
}
var images []string
for _, svc := range compose.Services {
if svc.Image != "" && !containsString(images, svc.Image) {
images = append(images, svc.Image)
}
}
sort.Strings(images)
return images, nil
}
var envDefault = regexp.MustCompile(`\$\{[A-Za-z0-9_]+:?-([^}]*)\}`)
// composeImages extracts services.*.image from a compose file. Variables with
// a default (${FOO:-bar}) are replaced by the default; images with other
// variables are skipped.
func composeImages(content string) []string {
var doc struct {
Services map[string]struct {
Image string `yaml:"image"`
} `yaml:"services"`
}
if err := yaml.Unmarshal([]byte(content), &doc); err != nil {
return nil
}
var images []string
for _, svc := range doc.Services {
img := envDefault.ReplaceAllString(svc.Image, "$1")
if img == "" || strings.Contains(img, "$") {
continue
}
images = append(images, img)
}
sort.Strings(images)
return images
}
func sortVersions(tags []string) {
sort.SliceStable(tags, func(i, j int) bool {
a, _ := ParseVersion(tags[i])
b, _ := ParseVersion(tags[j])
return a.Compare(b) < 0
})
}
func containsString(list []string, s string) bool {
for _, l := range list {
if l == s {
return true
}
}
return false
}
+33
View File
@@ -0,0 +1,33 @@
package audit
import (
"context"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestFetchFromRegistry talks to Docker Hub, so it only runs with
// ABRA_TEST_NETWORK=1.
func TestFetchFromRegistry(t *testing.T) {
if os.Getenv("ABRA_TEST_NETWORK") != "1" {
t.Skip("set ABRA_TEST_NETWORK=1 to run")
}
for _, arch := range []string{"amd64", "arm64"} {
img, err := ParseImageRef("app", "nextcloud:apache")
require.NoError(t, err)
info, err := FetchFromRegistry(context.Background(), img, "linux", arch)
require.NoError(t, err)
assert.NotEmpty(t, info.Env["PHP_VERSION"], arch)
assert.NotEmpty(t, info.Env["NEXTCLOUD_VERSION"], arch)
require.NotNil(t, info.Created, arch)
t.Logf("%s: created=%s php=%s nextcloud=%s base=%s baseCreated=%v",
arch, info.Created, info.Env["PHP_VERSION"], info.Env["NEXTCLOUD_VERSION"],
info.Annotations[AnnotationBaseName], info.BaseCreated)
}
}
+114
View File
@@ -0,0 +1,114 @@
package audit
import (
"errors"
"os"
"path/filepath"
"time"
gitPkg "coopcloud.tech/abra/pkg/git"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
"github.com/go-git/go-git/v5"
)
// DefaultAdvisoriesURL is the git repository holding the reviewed advisories.
const DefaultAdvisoriesURL = "https://git.coopcloud.tech/c4dt/advisories.git"
// fetchMarker is touched after each successful fetch, so the age of the local
// copy can be reported when the remote cannot be reached.
const fetchMarker = "abra-audit-last-fetch"
// SyncAdvisories makes sure dir holds an up-to-date checkout of the
// advisories repository. There is no cache lifetime: every call checks the
// remote. When the remote cannot be reached, a warning with the age of the
// local copy is logged and the local copy is used.
//
// A directory that is not a git checkout is used as is, which is handy for
// testing advisories locally.
func SyncAdvisories(dir, url string, offline bool) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if offline {
return errors.New(i18n.G("no local advisories in %s and offline mode is on", dir))
}
log.Info(i18n.G("fetching advisories from %s", url))
if err := gitPkg.Clone(dir, url); err != nil {
_ = os.RemoveAll(dir)
return errors.New(i18n.G("unable to fetch advisories from %s: %s", url, err))
}
if _, err := git.PlainOpen(dir); err != nil {
_ = os.RemoveAll(dir)
return errors.New(i18n.G("unable to fetch advisories from %s: %s", url, err))
}
touchMarker(dir)
return nil
}
repo, err := git.PlainOpen(dir)
if err != nil {
log.Warn(i18n.G("%s is not a git checkout, using it as is", dir))
return nil
}
if offline {
log.Info(i18n.G("offline mode: using local advisories (%s)", localAge(dir, repo)))
return nil
}
wt, err := repo.Worktree()
if err != nil {
return err
}
err = wt.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
log.Warn(i18n.G("unable to update advisories: %s", err))
log.Warn(i18n.G("using local advisories (%s)", localAge(dir, repo)))
return nil
}
if err == nil {
log.Debug(i18n.G("advisories updated"))
} else {
log.Debug(i18n.G("advisories already up to date"))
}
touchMarker(dir)
return nil
}
func touchMarker(dir string) {
p := filepath.Join(dir, ".git", fetchMarker)
now := time.Now()
if err := os.WriteFile(p, []byte(now.UTC().Format(time.RFC3339)+"\n"), 0o644); err != nil {
log.Debug(i18n.G("unable to write %s: %s", p, err))
}
}
// localAge describes how old the local copy is: the time of the last
// successful fetch, or of the last commit if abra never fetched it.
func localAge(dir string, repo *git.Repository) string {
if st, err := os.Stat(filepath.Join(dir, ".git", fetchMarker)); err == nil {
return i18n.G("last fetched %s ago", humanAge(time.Since(st.ModTime())))
}
if head, err := repo.Head(); err == nil {
if c, err := repo.CommitObject(head.Hash()); err == nil {
return i18n.G("last commit %s ago", humanAge(time.Since(c.Committer.When)))
}
}
return i18n.G("age unknown")
}
func humanAge(d time.Duration) string {
switch {
case d < time.Hour:
return i18n.G("%d minutes", int(d.Minutes()))
case d < 48*time.Hour:
return i18n.G("%d hours", int(d.Hours()))
default:
return i18n.G("%d days", int(d.Hours()/24))
}
}
+59
View File
@@ -0,0 +1,59 @@
package audit
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func commitFile(t *testing.T, repo *git.Repository, dir, name, content string) {
t.Helper()
require.NoError(t, os.MkdirAll(filepath.Dir(filepath.Join(dir, name)), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(dir, name), []byte(content), 0o644))
wt, err := repo.Worktree()
require.NoError(t, err)
_, err = wt.Add(name)
require.NoError(t, err)
_, err = wt.Commit("add "+name, &git.CommitOptions{
Author: &object.Signature{Name: "test", Email: "test@example.com", When: time.Now()},
})
require.NoError(t, err)
}
func TestSyncAdvisories(t *testing.T) {
upstream := t.TempDir()
repo, err := git.PlainInit(upstream, false)
require.NoError(t, err)
commitFile(t, repo, upstream, "advisories/a.yml", "id: A\nrecipes: {x: {fixed: 1.0.0}}\n")
local := filepath.Join(t.TempDir(), "advisories")
// offline without a local copy fails
require.Error(t, SyncAdvisories(local, upstream, true))
// first run clones
require.NoError(t, SyncAdvisories(local, upstream, false))
assert.FileExists(t, filepath.Join(local, "advisories", "a.yml"))
assert.FileExists(t, filepath.Join(local, ".git", fetchMarker))
// next run pulls new advisories
commitFile(t, repo, upstream, "advisories/b.yml", "id: B\nrecipes: {x: {fixed: 1.0.0}}\n")
require.NoError(t, SyncAdvisories(local, upstream, false))
assert.FileExists(t, filepath.Join(local, "advisories", "b.yml"))
// already up to date
require.NoError(t, SyncAdvisories(local, upstream, false))
// unreachable remote: warn and use the local copy
require.NoError(t, os.RemoveAll(upstream))
require.NoError(t, SyncAdvisories(local, upstream, false))
advs, _, err := LoadAdvisories(filepath.Join(local, "advisories"))
require.NoError(t, err)
assert.Len(t, advs, 2)
}
+273
View File
@@ -0,0 +1,273 @@
package audit
import (
"context"
"fmt"
"sort"
"sync"
"time"
appPkg "coopcloud.tech/abra/pkg/app"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
"coopcloud.tech/abra/pkg/upstream/convert"
"coopcloud.tech/abra/pkg/upstream/stack"
dockerClient "github.com/docker/docker/client"
)
// AppState is what is deployed for one app.
type AppState struct {
Server string `json:"server"`
Name string `json:"name"`
Recipe string `json:"recipe"`
Version string `json:"version"`
// Chaos is set for apps deployed with --chaos. Version is then the
// release the checkout was based on, and ChaosVersion the commit.
Chaos bool `json:"chaos"`
ChaosVersion string `json:"chaosVersion,omitempty"`
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.
type ServerError struct {
Server string
Err error
}
// CollectOptions configures CollectState.
type CollectOptions struct {
// Offline disables registry lookups; image configs are then only read
// from the server's docker daemon.
Offline bool
}
// CollectState reads the deployed state of all given apps. Servers are
// queried in parallel. Apps that are not deployed are left out. A server that
// cannot be reached is returned as a ServerError, and the other servers are
// still audited.
func CollectState(apps []appPkg.App, opts CollectOptions) ([]AppState, []ServerError) {
byServer := make(map[string][]appPkg.App)
for _, app := range apps {
byServer[app.Server] = append(byServer[app.Server], app)
}
var (
mu sync.Mutex
wg sync.WaitGroup
states []AppState
errs []ServerError
)
for server, serverApps := range byServer {
wg.Add(1)
go func(server string, serverApps []appPkg.App) {
defer wg.Done()
s, err := collectServer(server, serverApps, opts)
mu.Lock()
defer mu.Unlock()
if err != nil {
errs = append(errs, ServerError{Server: server, Err: err})
return
}
states = append(states, s...)
}(server, serverApps)
}
wg.Wait()
sort.Slice(states, func(i, j int) bool {
if states[i].Server != states[j].Server {
return states[i].Server < states[j].Server
}
return states[i].Name < states[j].Name
})
sort.Slice(errs, func(i, j int) bool { return errs[i].Server < errs[j].Server })
markUnusableCreated(states)
return states, errs
}
func collectServer(server string, apps []appPkg.App, opts CollectOptions) ([]AppState, error) {
cl, err := client.New(server, client.WithTimeout(10))
if err != nil {
return nil, err
}
status := stack.GetAllDeployedServices(cl, server)
if status.Err != nil {
return nil, status.Err
}
ctx := context.Background()
var platformOS, platformArch string
if !opts.Offline {
if info, err := cl.Info(ctx); err == nil {
platformOS = info.OSType
platformArch = NormaliseArch(info.Architecture)
} else {
log.Debug(i18n.G("%s: unable to read platform: %s", server, err))
}
}
byStack := make(map[string]*AppState)
var order []string
for _, app := range apps {
byStack[app.StackName()] = &AppState{
Server: server,
Name: app.Name,
Recipe: app.Recipe.Name,
OS: platformOS,
Arch: platformArch,
}
}
deployed := make(map[string]bool)
cache := make(map[string]imageResult)
for _, svc := range status.Services {
stackName := svc.Spec.Labels[convert.LabelNamespace]
st, ok := byStack[stackName]
if !ok {
continue
}
if !deployed[stackName] {
deployed[stackName] = true
order = append(order, stackName)
}
if v, ok := svc.Spec.Labels[fmt.Sprintf("coop-cloud.%s.version", stackName)]; ok && v != "" {
st.Version = v
}
if c := svc.Spec.Labels[fmt.Sprintf("coop-cloud.%s.chaos", stackName)]; c == "true" {
st.Chaos = true
}
if v := svc.Spec.Labels[fmt.Sprintf("coop-cloud.%s.chaos-version", stackName)]; v != "" {
st.ChaosVersion = v
}
if svc.Spec.TaskTemplate.ContainerSpec == nil {
continue
}
ref := svc.Spec.TaskTemplate.ContainerSpec.Image
serviceName := svc.Spec.Name
if ns := stackName + "_"; len(serviceName) > len(ns) && serviceName[:len(ns)] == ns {
serviceName = serviceName[len(ns):]
}
img, err := ParseImageRef(serviceName, ref)
if err != nil {
img.InfoErr = err
st.Images = append(st.Images, img)
continue
}
res, ok := cache[ref]
if !ok {
res = inspectImage(ctx, cl, img, platformOS, platformArch, opts)
cache[ref] = res
}
img.Info, img.InfoErr = res.info, res.err
st.Images = append(st.Images, img)
}
var states []AppState
for _, stackName := range order {
st := byStack[stackName]
sort.Slice(st.Images, func(i, j int) bool { return st.Images[i].Service < st.Images[j].Service })
states = append(states, *st)
}
return states, nil
}
type imageResult struct {
info *ImageInfo
err error
}
func inspectImage(ctx context.Context, cl *dockerClient.Client, img ImageRef, platformOS, platformArch string, opts CollectOptions) imageResult {
nodeCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
info, nodeErr := InspectOnNode(nodeCtx, cl, img)
if nodeErr == nil {
return imageResult{info: info}
}
log.Debug(i18n.G("%s: not found on node: %s", img.Ref, nodeErr))
if opts.Offline || platformArch == "" {
return imageResult{err: nodeErr}
}
regCtx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
info, err := FetchFromRegistry(regCtx, img, platformOS, platformArch)
if err != nil {
return imageResult{err: fmt.Errorf("%s", i18n.G("node: %s; registry: %s", nodeErr, err))}
}
return imageResult{info: info}
}
// markUnusableCreated flags build times that cannot be trusted:
// - before 2000, which catches reproducible builds using the 1970 epoch;
// - earlier than the build time of the base image;
// - shared by two different digests of the same tag.
func markUnusableCreated(states []AppState) {
// tag -> created -> digests
seen := make(map[string]map[time.Time]map[string]bool)
for _, st := range states {
for _, img := range st.Images {
if img.Info == nil || img.Info.Created == nil || img.Digest == "" {
continue
}
c := img.Info.Created.UTC()
if seen[img.NameTag()] == nil {
seen[img.NameTag()] = make(map[time.Time]map[string]bool)
}
if seen[img.NameTag()][c] == nil {
seen[img.NameTag()][c] = make(map[string]bool)
}
seen[img.NameTag()][c][img.Digest] = true
}
}
for _, st := range states {
for _, img := range st.Images {
info := img.Info
if info == nil || info.Created == nil {
continue
}
c := info.Created.UTC()
switch {
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 ""
}
+290
View File
@@ -0,0 +1,290 @@
package audit
import (
"context"
"errors"
"regexp"
"sort"
"strconv"
"strings"
"time"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
)
// Limits for the search of fixed tags, to keep registry requests bounded.
const (
maxRebuildCandidates = 20
maxNewerCandidates = 10
// rebuildMargin is how much later than the current image a rebuild must
// be built. Variants of one version (-fpm, -alpine) usually come out of
// the same CI run within minutes; rebuilds come hours or days later.
rebuildMargin = time.Hour
)
// tagSearch is the result of findFixedTag.
type tagSearch struct {
// tag is a fixed tag, or "" if none was found.
tag string
// sameTag is set when the deployed tag itself now points to a fixed
// image, which a redeploy does not pick up because the recipe pins an
// older digest.
sameTag bool
// current is the registry's current image of the deployed tag.
current *ImageRef
}
// findFixedTag searches the registry for a fixed image of img, in this
// order:
//
// 1. the deployed tag itself, if its current image is fixed;
// 2. later builds of the same version under a suffixed tag, such as
// v6.1.0-1 or v6.1.0-fix1 for v6.1.0. Suffixes are not parsed, since
// upstreams use many schemes. A candidate must be built at least
// rebuildMargin after the tag's current image: this keeps out
// pre-releases such as v6.1.0-preprod, built before the release, and
// variants built along with it. The latest-built fixed one is chosen;
// 3. newer versions of the same form, e.g. v6.1.1 or v6.2.0, but not
// v6 or latest: the oldest fixed one is chosen.
//
// Every candidate is checked against the advisory: whether a tag looks newer
// never decides whether it is fixed.
func (c *FixChecker) findFixedTag(ctx context.Context, f *Finding, img ImageRef) (tagSearch, error) {
var res tagSearch
if c.Tags == nil {
return res, errors.New(i18n.G("no tag search"))
}
if img.Tag == "" {
return res, errors.New(i18n.G("%s has no tag", img.Name))
}
cur, err := c.current(ctx, img, f.os, f.arch, false)
if err != nil {
return res, err
}
res.current = cur
if cur.Digest != img.Digest && fixedByAdvisory(f.Advisory, *cur) {
res.tag, res.sameTag = img.Tag, true
return res, nil
}
tags, err := c.tags(ctx, img)
if err != nil {
return res, err
}
tag, err := c.fixedRebuild(ctx, f, img, cur, tags)
if err != nil || tag != "" {
res.tag = tag
return res, err
}
res.tag = c.fixedNewer(ctx, f, img, tags)
return res, nil
}
// fixedRebuild returns the latest-built fixed rebuild of img's version.
func (c *FixChecker) fixedRebuild(ctx context.Context, f *Finding, img ImageRef, cur *ImageRef, tags []string) (string, error) {
var candidates []string
for _, t := range tags {
if isRebuildTag(img.Tag, t) {
candidates = append(candidates, t)
}
}
if len(candidates) == 0 {
return "", nil
}
if cur.Info == nil || cur.Info.Created == nil || cur.Info.createdUnusable != "" {
return "", errors.New(i18n.G("the build time of %s is unusable, so %s cannot be checked", img.NameTag(), strings.Join(candidates, ", ")))
}
after := cur.Info.Created.Add(rebuildMargin)
sort.Strings(candidates)
if len(candidates) > maxRebuildCandidates {
log.Debug(i18n.G("%s: checking only %d of %d rebuild tags", img.Name, maxRebuildCandidates, len(candidates)))
candidates = candidates[len(candidates)-maxRebuildCandidates:]
}
var best string
var bestTime time.Time
var unusable []string
for _, t := range candidates {
ref, err := ParseImageRef("", img.Name+":"+t)
if err != nil {
continue
}
cand, err := c.current(ctx, ref, f.os, f.arch, false)
if err != nil {
log.Debug(i18n.G("%s: skipping %s: %s", f.App, ref.NameTag(), err))
continue
}
info := cand.Info
switch {
case info.Created == nil || info.createdUnusable != "":
unusable = append(unusable, t)
continue
case info.Created.Before(after):
log.Debug(i18n.G("%s: skipping %s: built before %s", f.App, ref.NameTag(), img.NameTag()))
continue
case !fixedByAdvisory(f.Advisory, *cand):
log.Debug(i18n.G("%s: %s is not fixed", f.App, ref.NameTag()))
continue
}
if best == "" || info.Created.After(bestTime) {
best, bestTime = t, *info.Created
}
}
if best == "" && len(unusable) > 0 {
return "", errors.New(i18n.G("the build times of %s are unusable", strings.Join(unusable, ", ")))
}
return best, nil
}
// fixedNewer returns the oldest fixed newer version of img's tag, of the same
// form. As for recipe releases, the newest is checked first: if it is not
// fixed, none is.
func (c *FixChecker) fixedNewer(ctx context.Context, f *Finding, img ImageRef, tags []string) string {
base, ok := parseTagForm(img.Tag)
if !ok {
return ""
}
var newer []tagForm
for _, t := range tags {
if tf, ok := parseTagForm(t); ok && tf.sameForm(base) && tf.compare(base) > 0 {
newer = append(newer, tf)
}
}
if len(newer) == 0 {
return ""
}
sort.Slice(newer, func(i, j int) bool { return newer[i].compare(newer[j]) < 0 })
fixedAt := func(tf tagForm) bool {
ref, err := ParseImageRef("", img.Name+":"+tf.raw)
if err != nil {
return false
}
cand, err := c.current(ctx, ref, f.os, f.arch, false)
if err != nil {
log.Debug(i18n.G("%s: skipping %s: %s", f.App, ref.NameTag(), err))
return false
}
return fixedByAdvisory(f.Advisory, *cand)
}
latest := newer[len(newer)-1]
if !fixedAt(latest) {
return ""
}
for i, tf := range newer[:len(newer)-1] {
if i >= maxNewerCandidates {
break
}
if fixedAt(tf) {
return tf.raw
}
}
return latest.raw
}
func (c *FixChecker) tags(ctx context.Context, img ImageRef) ([]string, error) {
if r, ok := c.tagsCache[img.Name]; ok {
return r.tags, r.err
}
tctx, cancel := context.WithTimeout(ctx, 60*time.Second)
defer cancel()
tags, err := c.Tags(tctx, img)
c.tagsCache[img.Name] = tagsResult{tags: tags, err: err}
return tags, err
}
var (
// suffix words of pre-releases and of variants, which are not rebuilds
notRebuildWord = regexp.MustCompile(`^(?i)(pre|preprod|rc|beta|alpha|dev|devel|test|snapshot|nightly|edge|canary|pr|alpine|slim|fpm|apache|debian|ubuntu|bookworm|bullseye|trixie|windowsservercore|nanoserver)\d*$`)
archWord = regexp.MustCompile(`^(?i)(amd64|arm64|arm64v8|armv7|armv6|arm|386|i386|ppc64le|s390x|riscv64|x86_64|aarch64)$`)
digits = regexp.MustCompile(`\d`)
)
// isRebuildTag reports whether tag looks like a later build of base, such as
// v6.1.0-1 or v6.1.0-fix1 for v6.1.0: base, a separator, and a suffix with a
// number in it. Patch versions (1.2.3 for 1.2), pre-releases, variants and
// per-architecture tags are not rebuilds.
func isRebuildTag(base, tag string) bool {
if len(tag) < len(base)+2 || !strings.HasPrefix(tag, base) {
return false
}
sep, suffix := tag[len(base)], tag[len(base)+1:]
if !strings.ContainsRune("-_.+", rune(sep)) {
return false
}
if sep == '.' && suffix[0] >= '0' && suffix[0] <= '9' {
return false
}
if !digits.MatchString(suffix) {
return false
}
words := strings.FieldsFunc(suffix, func(r rune) bool { return strings.ContainsRune("-_.+", r) })
for _, w := range words {
if notRebuildWord.MatchString(w) {
return false
}
}
return !archWord.MatchString(words[len(words)-1])
}
// tagForm splits a tag such as v6.1.0 or 35.0.2-fpm into prefix ("v"),
// version numbers and the rest ("-fpm").
type tagForm struct {
raw string
prefix string
nums []int
rest string
}
var tagFormRe = regexp.MustCompile(`^([vV]?)(\d+(?:\.\d+)*)(.*)$`)
func parseTagForm(tag string) (tagForm, bool) {
m := tagFormRe.FindStringSubmatch(tag)
if m == nil {
return tagForm{}, false
}
tf := tagForm{raw: tag, prefix: m[1], rest: m[3]}
for _, p := range strings.Split(m[2], ".") {
n, err := strconv.Atoi(p)
if err != nil {
return tagForm{}, false
}
tf.nums = append(tf.nums, n)
}
return tf, true
}
// sameForm reports whether two tags differ only in their version numbers,
// with as many numbers: v6.1.1 has the form of v6.1.0, but v6.1 and
// v6.1.0-1 do not.
func (t tagForm) sameForm(o tagForm) bool {
return t.prefix == o.prefix && t.rest == o.rest && len(t.nums) == len(o.nums)
}
func (t tagForm) compare(o tagForm) int {
for i := range t.nums {
if i >= len(o.nums) {
return 1
}
if t.nums[i] != o.nums[i] {
if t.nums[i] < o.nums[i] {
return -1
}
return 1
}
}
if len(t.nums) < len(o.nums) {
return -1
}
return 0
}
+206
View File
@@ -0,0 +1,206 @@
package audit
import (
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsRebuildTag(t *testing.T) {
for tag, want := range map[string]bool{
"v6.1.0-1": true,
"v6.1.0-fix1": true,
"v6.1.0_r2": true,
"v6.1.0+build3": true,
"v6.1.0-1-amd64": false, // per-architecture copy
"v6.1.0-arm64": false,
"v6.1.0-preprod": false, // pre-release
"v6.1.0-rc1": false,
"v6.1.0-fpm": false, // variant, no number
"v6.1.0-alpine3": false, // variant
"v6.1.0.1": false, // a newer version, not a rebuild
"v6.1.0": false,
"v6.1.01": false,
"v6.1.1-1": false, // rebuild of another version
"v6.1.0-": false,
} {
assert.Equal(t, want, isRebuildTag("v6.1.0", tag), tag)
}
}
func TestTagForm(t *testing.T) {
base, ok := parseTagForm("35.0.2-fpm")
require.True(t, ok)
for tag, want := range map[string]bool{
"35.0.3-fpm": true,
"36.0.0-fpm": true,
"35.0.3-apache": false,
"35-fpm": false,
"v35.0.3-fpm": false,
} {
tf, ok := parseTagForm(tag)
require.True(t, ok, tag)
assert.Equal(t, want, tf.sameForm(base), tag)
}
_, ok = parseTagForm("latest")
assert.False(t, ok)
}
const nginxAdvisory = `
id: NGINX
status: published
images:
- match: "*"
env: { NGINX_VERSION: "<1.31.9" }
fix: {strategy: refresh}
`
// fakeImage is one tag in the fake registry.
type fakeImage struct {
created time.Time
nginx string
}
func TestFindFixedTag(t *testing.T) {
adv, _, err := ParseAdvisory([]byte(nginxAdvisory))
require.NoError(t, err)
release := time.Date(2026, 11, 13, 13, 0, 0, 0, time.UTC)
day := 24 * time.Hour
search := func(t *testing.T, deployed string, registry map[string]fakeImage) (tagSearch, error) {
t.Helper()
fetch := func(_ context.Context, img ImageRef, os, arch string) (*ImageInfo, error) {
fi, ok := registry[img.Tag]
if !ok {
return nil, errors.New("not found")
}
created := fi.created
return &ImageInfo{Digest: digestOf(string(rune('a' + len(img.Tag)%6))), Created: &created,
Env: map[string]string{"NGINX_VERSION": fi.nginx}}, nil
}
tags := func(_ context.Context, img ImageRef) ([]string, error) {
var out []string
for tag := range registry {
out = append(out, tag)
}
return out, nil
}
c := FixChecker{Fetch: fetch, Tags: tags, cache: map[string]fetchResult{}, tagsCache: map[string]tagsResult{}}
f := &Finding{App: "app", Advisory: &adv, os: "linux", arch: "amd64"}
img := mustImage(t, "app", "lasuite/frontend:"+deployed+"@"+digestOf("0"), nil)
return c.findFixedTag(context.Background(), f, img)
}
t.Run("latest-built fixed rebuild", func(t *testing.T) {
res, err := search(t, "v6.1.0", map[string]fakeImage{
"v6.1.0": {release, "1.31.8"},
"v6.1.0-preprod": {release.Add(-day), "1.31.9"}, // built before the release
"v6.1.0-1": {release.Add(6 * day), "1.31.9"}, // fixed rebuild
"v6.1.0-1-amd64": {release.Add(6 * day), "1.31.9"}, // per-arch copy
"v6.1.0-fix2": {release.Add(7 * day), "1.31.9"}, // later fixed rebuild
"v6.1.0-fix3": {release.Add(8 * day), "1.31.8"}, // later, but not fixed
"v6.1.0-r1": {release.Add(10 * time.Minute), "1.31.9"}, // same CI run as the release
})
require.NoError(t, err)
assert.Equal(t, "v6.1.0-fix2", res.tag)
assert.False(t, res.sameTag)
})
t.Run("rebuild preferred to newer version", func(t *testing.T) {
res, err := search(t, "v6.1.0", map[string]fakeImage{
"v6.1.0": {release, "1.31.8"},
"v6.1.0-1": {release.Add(6 * day), "1.31.9"},
"v6.2.0": {release.Add(7 * day), "1.31.9"},
})
require.NoError(t, err)
assert.Equal(t, "v6.1.0-1", res.tag)
})
t.Run("oldest fixed newer version", func(t *testing.T) {
res, err := search(t, "v6.1.0", map[string]fakeImage{
"v6.1.0": {release, "1.31.8"},
"v6.1.1": {release.Add(day), "1.31.8"},
"v6.2.0": {release.Add(2 * day), "1.31.9"},
"v7.0.0": {release.Add(3 * day), "1.31.9"},
"v6": {release.Add(3 * day), "1.31.9"},
"latest": {release.Add(3 * day), "1.31.9"},
"main": {release.Add(4 * day), "1.31.9"},
})
require.NoError(t, err)
assert.Equal(t, "v6.2.0", res.tag)
})
t.Run("no fixed image", func(t *testing.T) {
res, err := search(t, "v6.1.0", map[string]fakeImage{
"v6.1.0": {release, "1.31.8"},
"v6.1.0-preprod": {release.Add(-day), "1.31.9"},
"v6.1.1": {release.Add(day), "1.31.8"},
"main": {release.Add(day), "1.31.9"},
})
require.NoError(t, err)
assert.Equal(t, "", res.tag)
require.NotNil(t, res.current)
})
t.Run("unusable build time of the release", func(t *testing.T) {
_, err := search(t, "v6.1.0", map[string]fakeImage{
"v6.1.0": {time.Unix(0, 0).UTC(), "1.31.8"},
"v6.1.0-1": {release.Add(6 * day), "1.31.9"},
})
assert.ErrorContains(t, err, "build time of lasuite/frontend:v6.1.0 is unusable")
})
t.Run("unusable build time of the rebuild", func(t *testing.T) {
_, err := search(t, "v6.1.0", map[string]fakeImage{
"v6.1.0": {release, "1.31.8"},
"v6.1.0-1": {time.Unix(0, 0).UTC(), "1.31.9"},
})
assert.ErrorContains(t, err, "build times of v6.1.0-1 are unusable")
})
}
func TestAffectedLabel(t *testing.T) {
adv, _, err := ParseAdvisory([]byte(`
id: X
status: published
recipes:
nc: {affected: "<2.0.0"}
images:
- match: "lasuite/*"
env: { NGINX_VERSION: "<1.31.9" }
- match: "grafana/*"
version: "<12.0.0"
- match: "debian:*"
created_after: "2026-11-01T00:00:00Z"
`))
require.NoError(t, err)
built := time.Date(2026, 10, 1, 0, 0, 0, 0, time.UTC)
states := []AppState{
{Name: "nginx", Recipe: "x", Images: []ImageRef{mustImage(t, "app", "lasuite/frontend:v6.1.0@"+digestOf("a"),
&ImageInfo{Env: map[string]string{"NGINX_VERSION": "1.31.8"}})}},
{Name: "version", Recipe: "x", Images: []ImageRef{mustImage(t, "app", "grafana/grafana:11.0@"+digestOf("b"),
&ImageInfo{Labels: map[string]string{AnnotationVersion: "11.0.1"}})}},
{Name: "built", Recipe: "x", Images: []ImageRef{mustImage(t, "app", "debian:12@"+digestOf("c"),
&ImageInfo{Created: &built})}},
{Name: "noinfo", Recipe: "x", Images: []ImageRef{mustImage(t, "app", "lasuite/backend:v6.1.0@"+digestOf("d"), nil)}},
{Name: "recipe", Recipe: "git.coopcloud.tech/coop-cloud/nc", Version: "1.0.0+30.0.1"},
}
got := map[string]string{}
for _, f := range Evaluate(states, []Advisory{adv}, EvaluateOptions{}) {
got[f.App] = f.Affected
}
assert.Equal(t, map[string]string{
"nginx": "nginx 1.31.8",
"version": "grafana 11.0.1",
"built": "debian:12 built 2026-10-01",
"noinfo": "lasuite/backend:v6.1.0",
"recipe": "nc 1.0.0+30.0.1",
}, got)
}
+169
View File
@@ -0,0 +1,169 @@
package audit
import (
"errors"
"regexp"
"strconv"
"strings"
"coopcloud.tech/abra/pkg/i18n"
)
// Version is a loosely parsed version. Recipe tags have the form
// RECIPE+UPSTREAM (e.g. 7.0.3+30.0.1), so a version is a list of groups
// separated by "+", each group a list of numbers. Anything after the leading
// numeric part of a group (e.g. "-apache" in "35.0.3-apache") is ignored.
type Version struct {
Raw string
Groups [][]int
}
var numericPrefix = regexp.MustCompile(`^[vV]?(\d+(?:\.\d+)*)`)
// ParseVersion parses a version such as "8.3.14", "v1.2", "35.0.3-apache" or
// "7.0.3+30.0.1".
func ParseVersion(raw string) (Version, error) {
v := Version{Raw: raw}
raw = strings.TrimSpace(raw)
if raw == "" {
return v, errors.New(i18n.G("empty version"))
}
for _, group := range strings.Split(raw, "+") {
m := numericPrefix.FindStringSubmatch(group)
if m == nil {
return v, errors.New(i18n.G("unable to parse version %s", raw))
}
var nums []int
for _, part := range strings.Split(m[1], ".") {
n, err := strconv.Atoi(part)
if err != nil {
return v, errors.New(i18n.G("unable to parse version %s", raw))
}
nums = append(nums, n)
}
v.Groups = append(v.Groups, nums)
}
return v, nil
}
// Compare returns -1, 0 or 1. Only the groups present in both versions are
// compared, so "7.0.3" equals "7.0.3+30.0.1": a constraint without an
// upstream part only constrains the recipe part. Missing numbers inside a
// group count as 0.
func (v Version) Compare(o Version) int {
groups := min(len(v.Groups), len(o.Groups))
for g := 0; g < groups; g++ {
a, b := v.Groups[g], o.Groups[g]
for i := 0; i < max(len(a), len(b)); i++ {
var x, y int
if i < len(a) {
x = a[i]
}
if i < len(b) {
y = b[i]
}
if x < y {
return -1
}
if x > y {
return 1
}
}
}
return 0
}
type constraint struct {
op string
version Version
}
// Range is a set of version constraints. Constraints separated by spaces or
// commas must all hold; alternatives are separated by "||". For example
// ">=8.3.0 <8.3.14 || >=8.4.0 <8.4.2".
type Range struct {
Raw string
alts [][]constraint
}
var constraintRe = regexp.MustCompile(`^(<=|>=|==|!=|<|>|=)?\s*(.+)$`)
// ParseRange parses a version range.
func ParseRange(raw string) (Range, error) {
r := Range{Raw: raw}
if strings.TrimSpace(raw) == "" {
return r, errors.New(i18n.G("empty version range"))
}
for _, alt := range strings.Split(raw, "||") {
// NOTE: allow "< 1.2" by gluing a lone operator to the next token
fields := strings.FieldsFunc(alt, func(r rune) bool { return r == ' ' || r == ',' || r == '\t' })
var tokens []string
for i := 0; i < len(fields); i++ {
if constraintRe.MatchString(fields[i]) && strings.Trim(fields[i], "<>=!") == "" && i+1 < len(fields) {
tokens = append(tokens, fields[i]+fields[i+1])
i++
continue
}
tokens = append(tokens, fields[i])
}
if len(tokens) == 0 {
return r, errors.New(i18n.G("empty alternative in version range %s", raw))
}
var cs []constraint
for _, tok := range tokens {
m := constraintRe.FindStringSubmatch(tok)
if m == nil {
return r, errors.New(i18n.G("unable to parse version range %s", raw))
}
op := m[1]
if op == "" || op == "==" {
op = "="
}
v, err := ParseVersion(m[2])
if err != nil {
return r, errors.New(i18n.G("unable to parse version range %s: %s", raw, err))
}
cs = append(cs, constraint{op: op, version: v})
}
r.alts = append(r.alts, cs)
}
return r, nil
}
// Contains reports whether the version satisfies the range.
func (r Range) Contains(v Version) bool {
for _, alt := range r.alts {
ok := true
for _, c := range alt {
cmp := v.Compare(c.version)
switch c.op {
case "<":
ok = cmp < 0
case "<=":
ok = cmp <= 0
case ">":
ok = cmp > 0
case ">=":
ok = cmp >= 0
case "=":
ok = cmp == 0
case "!=":
ok = cmp != 0
}
if !ok {
break
}
}
if ok {
return true
}
}
return false
}
+52 -1
View File
@@ -4,18 +4,35 @@ import (
"context"
"errors"
"fmt"
"slices"
"coopcloud.tech/abra/pkg/i18n"
"github.com/containers/image/docker"
"github.com/containers/image/manifest"
"github.com/containers/image/types"
"github.com/distribution/reference"
imgspecv1 "github.com/opencontainers/image-spec/specs-go/v1"
)
func init() {
// NOTE: containers/image does not request OCI image indexes by default.
// Many multi-arch images (e.g. on Docker Hub) are published as OCI indexes
// and the registry refuses to serve them without the matching Accept header.
if !slices.Contains(manifest.DefaultRequestedManifestMIMETypes, imgspecv1.MediaTypeImageIndex) {
manifest.DefaultRequestedManifestMIMETypes = append(
manifest.DefaultRequestedManifestMIMETypes,
imgspecv1.MediaTypeImageIndex,
)
}
}
// GetRegistryTags retrieves all tags of an image from a container registry.
func GetRegistryTags(img reference.Named) ([]string, error) {
var tags []string
ref, err := docker.ParseReference(fmt.Sprintf("//%s", img))
// NOTE: strip tag and digest, containers/image refuses references
// which have both (e.g. nextcloud:34.0.5-fpm@sha256:...)
ref, err := docker.ParseReference(fmt.Sprintf("//%s", reference.TrimNamed(img)))
if err != nil {
return tags, errors.New(i18n.G("failed to parse image %s, saw: %s", img, err.Error()))
}
@@ -28,3 +45,37 @@ func GetRegistryTags(img reference.Named) ([]string, error) {
return tags, nil
}
// GetRegistryDigest retrieves the manifest digest of an image tag from a
// container registry. For multi-arch images, this is the digest of the image
// index, which is what Docker resolves "image:tag" to.
func GetRegistryDigest(img reference.Named, tag string) (string, error) {
tagged, err := reference.WithTag(reference.TrimNamed(img), tag)
if err != nil {
return "", errors.New(i18n.G("failed to parse image %s, saw: %s", img, err.Error()))
}
ref, err := docker.ParseReference(fmt.Sprintf("//%s", tagged))
if err != nil {
return "", errors.New(i18n.G("failed to parse image %s, saw: %s", tagged, err.Error()))
}
ctx := context.Background()
src, err := ref.NewImageSource(ctx, &types.SystemContext{})
if err != nil {
return "", err
}
defer src.Close()
manifestBytes, _, err := src.GetManifest(ctx, nil)
if err != nil {
return "", err
}
digest, err := manifest.Digest(manifestBytes)
if err != nil {
return "", err
}
return digest.String(), nil
}
+13 -2
View File
@@ -73,6 +73,17 @@ type Abra struct {
// 2. check if abraDir was set in a config file
// 3. use $HOME/.abra when above two options failed
func (a Abra) GetAbraDir() string {
if dir := a.GetExplicitAbraDir(); dir != "" {
return dir
}
log.Debug(i18n.G("using default abra dir"))
return os.ExpandEnv("$HOME/.abra")
}
// GetExplicitAbraDir returns the abra dir only when it was explicitly chosen,
// either through $ABRA_DIR or through the abraDir setting of a config file.
// It returns an empty string otherwise, and never falls back to $HOME/.abra.
func (a Abra) GetExplicitAbraDir() string {
if dir, exists := os.LookupEnv("ABRA_DIR"); exists && dir != "" {
log.Debug(i18n.G("read abra dir from $ABRA_DIR"))
return dir
@@ -85,8 +96,7 @@ func (a Abra) GetAbraDir() string {
// Make the path absolute
return path.Join(a.configPath, a.AbraDir)
}
log.Debug(i18n.G("using default abra dir"))
return os.ExpandEnv("$HOME/.abra")
return ""
}
func (a Abra) GetServersDir() string { return path.Join(a.GetAbraDir(), "servers") }
@@ -98,6 +108,7 @@ var config = LoadAbraConfig()
var (
ABRA_DIR = config.GetAbraDir()
ABRA_DIR_EXPLICIT = config.GetExplicitAbraDir()
SERVERS_DIR = config.GetServersDir()
RECIPES_DIR = config.GetRecipesDir()
LOGS_DIR = config.GetLogsDir()
+18 -8
View File
@@ -26,12 +26,24 @@ var BoldUnderlineStyle = lipgloss.NewStyle().
Bold(true).
Underline(true)
// ShortenID returns the first 12 characters of an ID, or the whole ID if it
// is shorter.
func ShortenID(str string) string {
return str[:12]
return Truncate(str, 12)
}
// SmallSHA returns the first 8 characters of a hash, or the whole hash if it
// is shorter (e.g. the placeholder commit of a build without ldflags).
func SmallSHA(hash string) string {
return hash[:8]
return Truncate(hash, 8)
}
// Truncate returns the first n characters of str, or str if it is shorter.
func Truncate(str string, n int) string {
if len(str) <= n {
return str
}
return str[:n]
}
// RemoveSha remove image sha from a string that are added in some docker outputs
@@ -84,12 +96,10 @@ func PrintTable(t *table.Table) error {
tWidth, _ := lipgloss.Size(t.String())
width, _, err := term.GetSize(0)
if err != nil {
return err
}
if tWidth > width {
// Without a terminal (a pipe, cron), there is no width to fit: print the
// table as it is instead of failing.
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err == nil && tWidth > width {
t.Width(width - 10)
}
+50
View File
@@ -1,6 +1,9 @@
package formatter
import (
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@@ -9,3 +12,50 @@ import (
func TestBoldDirtyDefault(t *testing.T) {
assert.Equal(t, "foo", BoldDirtyDefault("foo"))
}
func TestSmallSHA(t *testing.T) {
assert.Equal(t, "d699e51c", SmallSHA("d699e51c4c2a8f0e"))
assert.Equal(t, " ", SmallSHA(" "))
assert.Equal(t, "", SmallSHA(""))
}
func TestShortenID(t *testing.T) {
assert.Equal(t, "0123456789ab", ShortenID("0123456789abcdef"))
assert.Equal(t, "abc", ShortenID("abc"))
}
func TestTruncate(t *testing.T) {
assert.Equal(t, "abc", Truncate("abcdef", 3))
assert.Equal(t, "ab", Truncate("ab", 7))
}
func TestPrintTableWithoutTerminal(t *testing.T) {
t.Setenv("ABRA_CI", "")
// stdout is a pipe, not a terminal
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
stdout := os.Stdout
os.Stdout = w
defer func() { os.Stdout = stdout }()
table, err := CreateTable()
if err != nil {
t.Fatal(err)
}
table.Headers("A", "B")
table.Row("one", "two")
err = PrintTable(table)
w.Close()
out, _ := io.ReadAll(r)
if err != nil {
t.Fatalf("PrintTable without a terminal: %s", err)
}
if !strings.Contains(string(out), "one") {
t.Fatalf("table not printed: %q", out)
}
}
+5
View File
@@ -36,6 +36,11 @@ func IsClean(repoPath string) (bool, error) {
return false, err
}
// Ignore the abra-managed sidecar file that records the canonical
// source URL for externally-cloned recipes; it lives alongside the
// recipe but is not part of the upstream tree.
patterns = append(patterns, gitignore.ParsePattern(".abra-source", nil))
if len(patterns) > 0 {
worktree.Excludes = append(patterns, worktree.Excludes...)
}
+36
View File
@@ -2,6 +2,8 @@ package git
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/go-git/go-git/v5"
@@ -13,3 +15,37 @@ func TestIsClean(t *testing.T) {
assert.Equal(t, isClean, false)
assert.True(t, errors.Is(err, git.ErrRepositoryNotExists))
}
// TestIsCleanIgnoresAbraSource confirms that the .abra-source sidecar
// file written by abra next to externally-cloned recipes does not cause
// IsClean to report the worktree as dirty.
func TestIsCleanIgnoresAbraSource(t *testing.T) {
dir := t.TempDir()
if _, err := git.PlainInit(dir, false); err != nil {
t.Fatalf("git init failed: %s", err)
}
sidecar := filepath.Join(dir, ".abra-source")
if err := os.WriteFile(sidecar, []byte("git.example.com/u/recipe\n"), 0o644); err != nil {
t.Fatalf("writing sidecar failed: %s", err)
}
isClean, err := IsClean(dir)
if err != nil {
t.Fatalf("IsClean returned error: %s", err)
}
assert.True(t, isClean, "expected worktree with only .abra-source to be reported clean")
// Sanity check: an unrelated untracked file should still mark it dirty.
other := filepath.Join(dir, "random.txt")
if err := os.WriteFile(other, []byte("hello"), 0o644); err != nil {
t.Fatalf("writing extra file failed: %s", err)
}
isClean, err = IsClean(dir)
if err != nil {
t.Fatalf("IsClean returned error: %s", err)
}
assert.False(t, isClean, "expected worktree with unrelated untracked file to be reported dirty")
}
+87 -62
View File
@@ -7,13 +7,13 @@
msgid ""
msgstr "Project-Id-Version: \n"
"Report-Msgid-Bugs-To: EMAIL\n"
"POT-Creation-Date: 2026-10-14 10:32-0400\n"
"POT-Creation-Date: 2026-10-22 21:25+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
#: ./cli/app/cp.go:38
@@ -219,7 +219,7 @@ msgstr ""
msgid "%s created (version: %s)"
msgstr ""
#: ./cli/recipe/upgrade.go:335
#: ./cli/recipe/upgrade.go:345
#, c-format
msgid "%s currently has these unstaged changes 👇"
msgstr ""
@@ -304,7 +304,7 @@ msgstr ""
msgid "%s has no secrets to generate, skipping..."
msgstr ""
#: ./pkg/recipe/compose.go:107
#: ./pkg/recipe/compose.go:109
#, c-format
msgid "%s has no version label? try running \"abra recipe sync %s\" first?"
msgstr ""
@@ -324,12 +324,12 @@ msgstr ""
msgid "%s is already fetched"
msgstr ""
#: ./pkg/recipe/compose.go:233
#: ./pkg/recipe/compose.go:277
#, c-format
msgid "%s is already set, nothing to do?"
msgstr ""
#: ./pkg/recipe/compose.go:72
#: ./pkg/recipe/compose.go:74
#, c-format
msgid "%s is missing a compose.yml or compose.*.yml file?"
msgstr ""
@@ -379,7 +379,7 @@ msgstr ""
msgid "%s must first be deployed on %s before moving"
msgstr ""
#: ./cli/recipe/upgrade.go:150
#: ./cli/recipe/upgrade.go:153
#, c-format
msgid "%s not considered semver-like"
msgstr ""
@@ -549,6 +549,11 @@ msgstr ""
msgid "%s: waiting %d seconds before next retry"
msgstr ""
#: ./cli/recipe/upgrade.go:392
#, c-format
msgid "%s:%s was re-published upstream (digest %s → %s)"
msgstr ""
#: ./cli/app/upgrade.go:425
#, c-format
msgid "'%s' is not a known version"
@@ -638,17 +643,17 @@ msgstr ""
msgid "CHAOS DEPLOY"
msgstr ""
#: ./pkg/recipe/compose.go:47
#: ./pkg/recipe/compose.go:49
#, c-format
msgid "COMPOSE_FILE (=\"%s\") parsing failed?"
msgstr ""
#: ./pkg/recipe/compose.go:58
#: ./pkg/recipe/compose.go:60
#, c-format
msgid "COMPOSE_FILE detected (%s), loading %s"
msgstr ""
#: ./pkg/recipe/compose.go:38
#: ./pkg/recipe/compose.go:40
#, c-format
msgid "COMPOSE_FILE detected, loading %s"
msgstr ""
@@ -1687,7 +1692,7 @@ msgctxt "recipe fetch"
msgid "a"
msgstr ""
#: ./cli/recipe/upgrade.go:408
#: ./cli/recipe/upgrade.go:459
msgctxt "recipe upgrade"
msgid "a"
msgstr ""
@@ -1737,7 +1742,7 @@ msgstr ""
msgid "accepts at most 2 args with --local/-l"
msgstr ""
#: ./pkg/recipe/compose.go:249
#: ./pkg/recipe/compose.go:293
#, c-format
msgid "add '- \"%s\"' manually to the 'app' service in %s"
msgstr ""
@@ -1780,7 +1785,7 @@ msgstr ""
msgid "all-services"
msgstr ""
#: ./cli/recipe/upgrade.go:407
#: ./cli/recipe/upgrade.go:458
msgid "all-tags"
msgstr ""
@@ -1919,7 +1924,7 @@ msgstr ""
msgid "c"
msgstr ""
#: ./cli/recipe/upgrade.go:416
#: ./cli/recipe/upgrade.go:467
msgctxt "recipe upgrade"
msgid "c"
msgstr ""
@@ -1928,7 +1933,7 @@ msgstr ""
msgid "can not insert from file and read from stdin"
msgstr ""
#: ./cli/recipe/upgrade.go:323
#: ./cli/recipe/upgrade.go:333
#, c-format
msgid "can upgrade service: %s, image: %s, tag: %s ::"
msgstr ""
@@ -2108,7 +2113,7 @@ msgstr ""
msgid "chore: publish new catalogue release changes"
msgstr ""
#: ./cli/recipe/upgrade.go:352
#: ./cli/recipe/upgrade.go:362
msgid "chore: update image tags"
msgstr ""
@@ -2170,19 +2175,19 @@ msgstr ""
msgid "commandconn: starting %s with %v"
msgstr ""
#: ./cli/recipe/upgrade.go:415
#: ./cli/recipe/upgrade.go:466
msgid "commit"
msgstr ""
#: ./cli/recipe/upgrade.go:418
#: ./cli/recipe/upgrade.go:469
msgid "commit changes"
msgstr ""
#: ./cli/recipe/upgrade.go:342
#: ./cli/recipe/upgrade.go:352
msgid "commit changes?"
msgstr ""
#: ./cli/recipe/upgrade.go:356
#: ./cli/recipe/upgrade.go:366
#, c-format
msgid "committed changes as '%s'"
msgstr ""
@@ -2230,12 +2235,12 @@ msgstr ""
msgid "connection timed out for %s"
msgstr ""
#: ./pkg/recipe/compose.go:191
#: ./pkg/recipe/compose.go:235
#, c-format
msgid "considering %s config(s) for label update"
msgstr ""
#: ./pkg/recipe/compose.go:123
#: ./pkg/recipe/compose.go:132
#, c-format
msgid "considering %s config(s) for tag update"
msgstr ""
@@ -2258,7 +2263,7 @@ msgstr ""
msgid "context lacks Docker endpoint"
msgstr ""
#: ./pkg/recipe/compose.go:229
#: ./pkg/recipe/compose.go:273
#, c-format
msgid "coop-cloud.${STACK_NAME}.version=%s"
msgstr ""
@@ -2465,12 +2470,12 @@ msgstr ""
msgid "detected ABRA_CI=1"
msgstr ""
#: ./cli/recipe/upgrade.go:204
#: ./cli/recipe/upgrade.go:214
#, c-format
msgid "detected compatible upgradable tags %s for %s"
msgstr ""
#: ./cli/recipe/upgrade.go:177
#: ./cli/recipe/upgrade.go:180
#, c-format
msgid "detected potential upgradable tags %s for %s"
msgstr ""
@@ -2509,6 +2514,16 @@ msgstr ""
msgid "different versions for secret '%s', '%s' and %s'"
msgstr ""
#: ./cli/recipe/upgrade.go:388
#, c-format
msgid "digest of %s:%s is up to date"
msgstr ""
#: ./cli/recipe/upgrade.go:413
#, c-format
msgid "digest updated for %s:%s"
msgstr ""
#: ./pkg/recipe/recipe.go:202
#, c-format
msgid "dir: %s, "
@@ -2658,7 +2673,7 @@ msgstr ""
msgid "ensuring env version %s"
msgstr ""
#: ./cli/recipe/upgrade.go:285
#: ./cli/recipe/upgrade.go:295
msgid "enter / return to confirm, choose 'skip' to not upgrade this tag, vim mode is enabled"
msgstr ""
@@ -2839,7 +2854,7 @@ msgstr ""
msgid "failed to migrate app config: %s"
msgstr ""
#: ./pkg/client/registry.go:20
#: ./pkg/client/registry.go:37 ./pkg/client/registry.go:55 ./pkg/client/registry.go:60
#, c-format
msgid "failed to parse image %s, saw: %s"
msgstr ""
@@ -3291,15 +3306,15 @@ msgstr ""
msgid "including VOLUMES=%v in backupbot exec invocation"
msgstr ""
#: ./cli/recipe/release.go:634 ./cli/recipe/upgrade.go:378
#: ./cli/recipe/release.go:634 ./cli/recipe/upgrade.go:429
msgid "increase the major part of the version"
msgstr ""
#: ./cli/recipe/release.go:642 ./cli/recipe/upgrade.go:386
#: ./cli/recipe/release.go:642 ./cli/recipe/upgrade.go:437
msgid "increase the minor part of the version"
msgstr ""
#: ./cli/recipe/release.go:650 ./cli/recipe/upgrade.go:394
#: ./cli/recipe/release.go:650 ./cli/recipe/upgrade.go:445
msgid "increase the patch part of the version"
msgstr ""
@@ -3464,7 +3479,7 @@ msgstr ""
msgid "list [flags]"
msgstr ""
#: ./cli/recipe/upgrade.go:410
#: ./cli/recipe/upgrade.go:461
msgid "list all tags, not just upgrades"
msgstr ""
@@ -3534,11 +3549,11 @@ msgstr ""
#. with no spaces in between
#. translators: `abra man` aliases. use a comma separated list of aliases
#. with no spaces in between
#: ./cli/app/list.go:319 ./cli/app/move.go:34 ./cli/app/ps.go:205 ./cli/app/secret.go:553 ./cli/app/secret.go:649 ./cli/recipe/list.go:104 ./cli/recipe/upgrade.go:400 ./cli/recipe/version.go:139 ./cli/run.go:152 ./cli/server/list.go:106
#: ./cli/app/list.go:319 ./cli/app/move.go:34 ./cli/app/ps.go:205 ./cli/app/secret.go:553 ./cli/app/secret.go:649 ./cli/recipe/list.go:104 ./cli/recipe/upgrade.go:451 ./cli/recipe/version.go:139 ./cli/run.go:152 ./cli/server/list.go:106
msgid "m"
msgstr ""
#: ./cli/app/list.go:318 ./cli/app/ps.go:204 ./cli/app/secret.go:552 ./cli/app/secret.go:648 ./cli/recipe/list.go:103 ./cli/recipe/upgrade.go:399 ./cli/recipe/version.go:138 ./cli/server/list.go:105
#: ./cli/app/list.go:318 ./cli/app/ps.go:204 ./cli/app/secret.go:552 ./cli/app/secret.go:648 ./cli/recipe/list.go:103 ./cli/recipe/upgrade.go:450 ./cli/recipe/version.go:138 ./cli/server/list.go:105
msgid "machine"
msgstr ""
@@ -3547,7 +3562,7 @@ msgstr ""
msgid "main app service version for %s is empty?"
msgstr ""
#: ./cli/internal/recipe.go:48 ./cli/internal/recipe.go:66 ./cli/internal/recipe.go:80 ./cli/recipe/release.go:631 ./cli/recipe/upgrade.go:375
#: ./cli/internal/recipe.go:48 ./cli/internal/recipe.go:66 ./cli/internal/recipe.go:80 ./cli/recipe/release.go:631 ./cli/recipe/upgrade.go:426
msgid "major"
msgstr ""
@@ -3576,7 +3591,7 @@ msgstr ""
msgid "migrating app config from %s to %s"
msgstr ""
#: ./cli/internal/recipe.go:48 ./cli/internal/recipe.go:68 ./cli/internal/recipe.go:82 ./cli/recipe/release.go:639 ./cli/recipe/upgrade.go:383
#: ./cli/internal/recipe.go:48 ./cli/internal/recipe.go:68 ./cli/internal/recipe.go:82 ./cli/recipe/release.go:639 ./cli/recipe/upgrade.go:434
msgid "minor"
msgstr ""
@@ -3705,7 +3720,7 @@ msgstr ""
msgid "no .git directory in %s?"
msgstr ""
#: ./pkg/recipe/compose.go:29
#: ./pkg/recipe/compose.go:31
#, c-format
msgid "no COMPOSE_FILE detected, loading default: %s"
msgstr ""
@@ -3731,7 +3746,7 @@ msgstr ""
msgid "no changes discovered in %s, nothing to publish?"
msgstr ""
#: ./cli/recipe/upgrade.go:361
#: ./cli/recipe/upgrade.go:371
msgid "no changes, skip creating commit"
msgstr ""
@@ -3756,7 +3771,7 @@ msgstr ""
msgid "no domain provided"
msgstr ""
#: ./pkg/recipe/compose.go:248
#: ./pkg/recipe/compose.go:292
msgid "no existing label found, automagic insertion not supported yet"
msgstr ""
@@ -3765,7 +3780,7 @@ msgstr ""
msgid "no git tags found for %s"
msgstr ""
#: ./cli/recipe/upgrade.go:182
#: ./cli/recipe/upgrade.go:192
#, c-format
msgid "no new versions available for %s, assuming %s is the latest (use -a/--all-tags to see all anyway)"
msgstr ""
@@ -3897,12 +3912,12 @@ msgstr ""
msgid "not requesting a remote TTY"
msgstr ""
#: ./cli/recipe/upgrade.go:305
#: ./cli/recipe/upgrade.go:315
#, c-format
msgid "not upgrading %s, skipping as requested"
msgstr ""
#: ./cli/recipe/upgrade.go:244
#: ./cli/recipe/upgrade.go:254
#, c-format
msgid "not upgrading from %s to %s for %s, because the upgrade type is more serious than what user wants"
msgstr ""
@@ -3956,12 +3971,12 @@ msgstr ""
msgid "p"
msgstr ""
#: ./cli/recipe/upgrade.go:163
#: ./cli/recipe/upgrade.go:166
#, c-format
msgid "parsed %s for %s"
msgstr ""
#: ./pkg/recipe/compose.go:159
#: ./pkg/recipe/compose.go:178
#, c-format
msgid "parsed %s from %s"
msgstr ""
@@ -3999,7 +4014,7 @@ msgstr ""
msgid "pass command not found on $PATH, is it installed?"
msgstr ""
#: ./cli/internal/recipe.go:48 ./cli/internal/recipe.go:70 ./cli/internal/recipe.go:84 ./cli/recipe/release.go:647 ./cli/recipe/upgrade.go:391
#: ./cli/internal/recipe.go:48 ./cli/internal/recipe.go:70 ./cli/internal/recipe.go:84 ./cli/recipe/release.go:647 ./cli/recipe/upgrade.go:442
msgid "patch"
msgstr ""
@@ -4053,7 +4068,7 @@ msgstr ""
msgid "prefer offline & filesystem access"
msgstr ""
#: ./cli/app/list.go:321 ./cli/app/ps.go:207 ./cli/app/secret.go:555 ./cli/app/secret.go:651 ./cli/recipe/list.go:106 ./cli/recipe/upgrade.go:402 ./cli/recipe/version.go:141 ./cli/server/list.go:108
#: ./cli/app/list.go:321 ./cli/app/ps.go:207 ./cli/app/secret.go:555 ./cli/app/secret.go:651 ./cli/recipe/list.go:106 ./cli/recipe/upgrade.go:453 ./cli/recipe/version.go:141 ./cli/server/list.go:108
msgid "print machine-readable output"
msgstr ""
@@ -4436,7 +4451,7 @@ msgstr ""
msgid "retrieved %s as target container on %s"
msgstr ""
#: ./pkg/recipe/compose.go:59
#: ./pkg/recipe/compose.go:61
#, c-format
msgid "retrieved %s configs for %s"
msgstr ""
@@ -4446,7 +4461,7 @@ msgstr ""
msgid "retrieved %s for %s"
msgstr ""
#: ./cli/recipe/upgrade.go:144
#: ./cli/recipe/upgrade.go:147
#, c-format
msgid "retrieved %s from remote registry for %s"
msgstr ""
@@ -4723,12 +4738,12 @@ msgstr ""
msgid "serverAdd: cleanUp: unable to list files in %s: %s"
msgstr ""
#: ./cli/recipe/upgrade.go:227
#: ./cli/recipe/upgrade.go:237
#, c-format
msgid "service %s is at version %s, but pinned to %s, please correct your compose.yml file manually!"
msgstr ""
#: ./cli/recipe/upgrade.go:223
#: ./cli/recipe/upgrade.go:233
#, c-format
msgid "service %s, image %s pinned to %s, no compatible upgrade found"
msgstr ""
@@ -5007,7 +5022,7 @@ msgstr ""
msgid "sync [flags]"
msgstr ""
#: ./pkg/recipe/compose.go:243
#: ./pkg/recipe/compose.go:287
#, c-format
msgid "synced label %s to service %s"
msgstr ""
@@ -5025,7 +5040,7 @@ msgstr ""
msgid "tag at commit %s is unannotated or otherwise broken"
msgstr ""
#: ./cli/recipe/upgrade.go:301
#: ./cli/recipe/upgrade.go:311
#, c-format
msgid "tag upgraded from %s to %s for %s"
msgstr ""
@@ -5212,7 +5227,7 @@ msgstr ""
msgid "unable to determine server of app %s, please pass --server/-s"
msgstr ""
#: ./cli/recipe/upgrade.go:252
#: ./cli/recipe/upgrade.go:262
#, c-format
msgid "unable to determine versioning semantics of %s, listing all tags"
msgstr ""
@@ -5286,12 +5301,12 @@ msgstr ""
msgid "unable to open worktree of %s: %s"
msgstr ""
#: ./cli/recipe/upgrade.go:159
#: ./cli/recipe/upgrade.go:162
#, c-format
msgid "unable to parse %s, error was: %s, skipping upgrade for %s"
msgstr ""
#: ./pkg/recipe/compose.go:153
#: ./pkg/recipe/compose.go:167
#, c-format
msgid "unable to parse %s, skipping"
msgstr ""
@@ -5331,7 +5346,7 @@ msgstr ""
msgid "unable to read remotes in %s: %s"
msgstr ""
#: ./cli/recipe/upgrade.go:153
#: ./cli/recipe/upgrade.go:156
#, c-format
msgid "unable to read tag for image %s, is it missing? skipping upgrade for %s"
msgstr ""
@@ -5381,6 +5396,11 @@ msgstr ""
msgid "unable to retrieve container for %s: %s"
msgstr ""
#: ./pkg/recipe/compose.go:193
#, c-format
msgid "unable to retrieve digest for %s: %s"
msgstr ""
#: ./cli/app/list.go:157
#, c-format
msgid "unable to retrieve tags for %s: %s"
@@ -5513,12 +5533,17 @@ msgstr ""
msgid "up"
msgstr ""
#: ./cli/recipe/upgrade.go:397
#, c-format
msgid "update digest of %s:%s?"
msgstr ""
#: ./pkg/upstream/stack/stack.go:477
#, c-format
msgid "updating %s"
msgstr ""
#: ./pkg/recipe/compose.go:171 ./pkg/recipe/compose.go:237
#: ./pkg/recipe/compose.go:213 ./pkg/recipe/compose.go:281
#, c-format
msgid "updating %s to %s in %s"
msgstr ""
@@ -5543,17 +5568,17 @@ msgstr ""
msgid "upgrade [flags]"
msgstr ""
#: ./cli/recipe/upgrade.go:248
#: ./cli/recipe/upgrade.go:258
#, c-format
msgid "upgrade to which tag? (service: %s, image: %s, tag: %s)"
msgstr ""
#: ./cli/recipe/upgrade.go:254
#: ./cli/recipe/upgrade.go:264
#, c-format
msgid "upgrade to which tag? (service: %s, tag: %s)"
msgstr ""
#: ./cli/recipe/upgrade.go:221
#: ./cli/recipe/upgrade.go:231
#, c-format
msgid "upgrading service %s from %s to %s (pinned tag: %s)"
msgstr ""
@@ -5806,11 +5831,11 @@ msgstr ""
msgid "writing recipe version failed: %s"
msgstr ""
#: ./cli/recipe/release.go:632 ./cli/recipe/upgrade.go:376
#: ./cli/recipe/release.go:632 ./cli/recipe/upgrade.go:427
msgid "x"
msgstr ""
#: ./cli/recipe/release.go:640 ./cli/recipe/upgrade.go:384
#: ./cli/recipe/release.go:640 ./cli/recipe/upgrade.go:435
msgid "y"
msgstr ""
@@ -5822,7 +5847,7 @@ msgstr ""
msgid "you can only use one version flag: --major, --minor or --patch"
msgstr ""
#: ./cli/recipe/release.go:648 ./cli/recipe/upgrade.go:392
#: ./cli/recipe/release.go:648 ./cli/recipe/upgrade.go:443
msgid "z"
msgstr ""
Binary file not shown.
+155 -708
View File
File diff suppressed because one or more lines are too long
+49 -5
View File
@@ -6,8 +6,10 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"coopcloud.tech/abra/pkg/client"
"coopcloud.tech/abra/pkg/formatter"
"coopcloud.tech/abra/pkg/i18n"
"coopcloud.tech/abra/pkg/log"
@@ -110,7 +112,14 @@ func (r Recipe) GetVersionLabelLocal() (string, error) {
return label, nil
}
// getRegistryDigest is a variable so that it can be stubbed in tests.
var getRegistryDigest = client.GetRegistryDigest
// UpdateTag updates an image tag in-place on file system local compose files.
// Images which are pinned to a digest (e.g. "nextcloud:34.0.5-fpm@sha256:...")
// have their digest updated to the one of the new tag, as retrieved from the
// container registry. Passing the current tag of a digest pinned image
// refreshes the digest, e.g. when upstream re-published the same tag.
func (r Recipe) UpdateTag(image, tag string) (bool, error) {
fullPattern := fmt.Sprintf("%s/compose**yml", r.Dir)
image = formatter.StripTagMeta(image)
@@ -122,6 +131,11 @@ func (r Recipe) UpdateTag(image, tag string) (bool, error) {
log.Debug(i18n.G("considering %s config(s) for tag update", strings.Join(composeFiles, ", ")))
var (
updated bool
newDigest string
)
for _, composeFile := range composeFiles {
opts := stack.Deploy{Composefiles: []string{composeFile}}
@@ -140,7 +154,7 @@ func (r Recipe) UpdateTag(image, tag string) (bool, error) {
continue // may be a compose.$optional.yml file
}
img, _ := reference.ParseNormalizedNamed(service.Image)
img, err := reference.ParseNormalizedNamed(service.Image)
if err != nil {
return false, err
}
@@ -154,30 +168,60 @@ func (r Recipe) UpdateTag(image, tag string) (bool, error) {
continue
}
var composeDigest string
if canonical, ok := img.(reference.Canonical); ok {
composeDigest = canonical.Digest().String()
}
composeImage := formatter.StripTagMeta(reference.Path(img))
log.Debug(i18n.G("parsed %s from %s", composeTag, service.Image))
if image == composeImage {
bytes, err := ioutil.ReadFile(composeFile)
bytes, err := os.ReadFile(composeFile)
if err != nil {
return false, err
}
old := fmt.Sprintf("%s:%s", composeImage, composeTag)
new := fmt.Sprintf("%s:%s", composeImage, tag)
replacedBytes := strings.Replace(string(bytes), old, new, -1)
if composeDigest != "" {
if newDigest == "" {
newDigest, err = getRegistryDigest(img, tag)
if err != nil {
return false, errors.New(i18n.G("unable to retrieve digest for %s: %s", new, err))
}
}
old = fmt.Sprintf("%s@%s", old, composeDigest)
new = fmt.Sprintf("%s@%s", new, newDigest)
}
if old == new {
continue
}
// NOTE: only match the full image reference, so that "foo:1.2" does
// not match "foo:1.2.3" or the "foo:1.2" part of "foo:1.2@sha256:..."
pattern := regexp.MustCompile(regexp.QuoteMeta(old) + `([^@\w.\-]|$)`)
replaced := pattern.ReplaceAllString(string(bytes), new+"${1}")
if replaced == string(bytes) {
continue
}
log.Debug(i18n.G("updating %s to %s in %s", old, new, compose.Filename))
if err := os.WriteFile(compose.Filename, []byte(replacedBytes), 0o764); err != nil {
if err := os.WriteFile(compose.Filename, []byte(replaced), 0o764); err != nil {
return false, err
}
updated = true
}
}
}
return false, nil
return updated, nil
}
// UpdateLabel updates a label in-place on file system local compose files.
+127
View File
@@ -0,0 +1,127 @@
package recipe
import (
"os"
"path"
"testing"
"github.com/distribution/reference"
"github.com/stretchr/testify/assert"
)
const (
oldDigest = "sha256:1111111111111111111111111111111111111111111111111111111111111111"
newDigest = "sha256:2222222222222222222222222222222222222222222222222222222222222222"
)
func setupUpdateTagRecipe(t *testing.T, compose string) (Recipe, string) {
t.Helper()
dir := t.TempDir()
composePath := path.Join(dir, "compose.yml")
envPath := path.Join(dir, ".env.sample")
assert.NoError(t, os.WriteFile(composePath, []byte(compose), 0o644))
assert.NoError(t, os.WriteFile(envPath, []byte("TYPE=foo\n"), 0o644))
return Recipe{
Name: "foo",
Dir: dir,
ComposePath: composePath,
SampleEnvPath: envPath,
}, composePath
}
func stubRegistryDigest(t *testing.T, digest string) *[]string {
t.Helper()
var calls []string
orig := getRegistryDigest
getRegistryDigest = func(img reference.Named, tag string) (string, error) {
calls = append(calls, reference.Path(img)+":"+tag)
return digest, nil
}
t.Cleanup(func() { getRegistryDigest = orig })
return &calls
}
func TestUpdateTag(t *testing.T) {
testcases := []struct {
name string
compose string
image string
tag string
expected string
updated bool
lookups int
}{
{
name: "plain tag",
compose: "version: \"3.8\"\nservices:\n app:\n image: nginx:1.31.7\n",
image: "nginx",
tag: "1.31.8",
expected: "version: \"3.8\"\nservices:\n app:\n image: nginx:1.31.8\n",
updated: true,
},
{
name: "shorter tag does not mangle longer tag",
compose: "version: \"3.8\"\nservices:\n app:\n image: nginx:1.31\n web:\n image: nginx:1.31.7\n",
image: "nginx",
tag: "1.32",
expected: "version: \"3.8\"\nservices:\n app:\n image: nginx:1.32\n web:\n image: nginx:1.32\n",
updated: true,
},
{
name: "digest is updated with tag",
compose: "version: \"3.8\"\nservices:\n app:\n image: nextcloud:34.0.5-fpm@" + oldDigest + "\n",
image: "nextcloud",
tag: "34.0.6-fpm",
expected: "version: \"3.8\"\nservices:\n app:\n image: nextcloud:34.0.6-fpm@" + newDigest + "\n",
updated: true,
lookups: 1,
},
{
name: "digest is refreshed for same tag",
compose: "version: \"3.8\"\nservices:\n app:\n image: \"nextcloud:34.0.5-fpm@" + oldDigest + "\"\n",
image: "nextcloud",
tag: "34.0.5-fpm",
expected: "version: \"3.8\"\nservices:\n app:\n image: \"nextcloud:34.0.5-fpm@" + newDigest + "\"\n",
updated: true,
lookups: 1,
},
{
name: "same tag without digest is a no-op",
compose: "version: \"3.8\"\nservices:\n app:\n image: nextcloud:34.0.5-fpm\n",
image: "nextcloud",
tag: "34.0.5-fpm",
expected: "version: \"3.8\"\nservices:\n app:\n image: nextcloud:34.0.5-fpm\n",
updated: false,
},
{
name: "pinned and unpinned services sharing an image",
compose: "version: \"3.8\"\nservices:\n app:\n image: nextcloud:34.0.5-fpm@" + oldDigest + "\n cron:\n image: nextcloud:34.0.5-fpm\n",
image: "nextcloud",
tag: "34.0.6-fpm",
expected: "version: \"3.8\"\nservices:\n app:\n image: nextcloud:34.0.6-fpm@" + newDigest + "\n cron:\n image: nextcloud:34.0.6-fpm\n",
updated: true,
lookups: 1,
},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
r, composePath := setupUpdateTagRecipe(t, tc.compose)
calls := stubRegistryDigest(t, newDigest)
updated, err := r.UpdateTag(tc.image, tc.tag)
assert.NoError(t, err)
assert.Equal(t, tc.updated, updated)
assert.Len(t, *calls, tc.lookups)
content, err := os.ReadFile(composePath)
assert.NoError(t, err)
assert.Equal(t, tc.expected, string(content))
})
}
}
+102 -10
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path"
"slices"
"sort"
"strings"
@@ -20,6 +21,11 @@ import (
"github.com/go-git/go-git/v5/plumbing"
)
// SourceFile is the sidecar file written next to an externally-cloned
// recipe so the canonical "host/path" name can be recovered later (the
// on-disk directory name escapes "/" and "." to "_", which is lossy).
const SourceFile = ".abra-source"
type EnsureContext struct {
Chaos bool
Offline bool
@@ -59,7 +65,7 @@ func (r Recipe) Ensure(ctx EnsureContext) error {
return errors.New(i18n.G(`cannot redeploy previous chaos version (%s), did you mean to use "--chaos"?`))
}
if _, err := r.EnsureVersion(r.EnvVersion); err != nil {
if _, err := r.EnsureVersionCtx(ctx, r.EnvVersion); err != nil {
return err
}
@@ -79,6 +85,12 @@ func (r Recipe) EnsureExists() error {
if err := gitPkg.Clone(r.Dir, r.GitURL); err != nil {
return err
}
if strings.Contains(r.Name, "/") {
sidecar := path.Join(r.Dir, SourceFile)
if err := os.WriteFile(sidecar, []byte(r.Name+"\n"), 0o644); err != nil {
log.Debug(i18n.G("failed to write recipe source sidecar %s: %s", sidecar, err))
}
}
}
if err := gitPkg.EnsureGitRepo(r.Dir); err != nil {
@@ -88,6 +100,18 @@ func (r Recipe) EnsureExists() error {
return nil
}
// ReadRecipeSource returns the canonical name recorded in the .abra-source
// sidecar inside the given recipe directory, or the empty string if no
// sidecar exists. This lets callers recover the unescaped "host/path"
// form for externally-cloned recipes.
func ReadRecipeSource(recipeDir string) string {
data, err := os.ReadFile(path.Join(recipeDir, SourceFile))
if err != nil {
return ""
}
return strings.TrimSpace(string(data))
}
// IsChaosCommit determines if a version sttring is a chaos commit or not.
func (r Recipe) IsChaosCommit(version string) (bool, error) {
isChaosCommit := false
@@ -125,6 +149,13 @@ func (r Recipe) IsChaosCommit(version string) (bool, error) {
// EnsureVersion checks whether a specific version exists for a recipe.
func (r Recipe) EnsureVersion(version string) (bool, error) {
return r.EnsureVersionCtx(EnsureContext{}, version)
}
// EnsureVersionCtx is like EnsureVersion but is aware of the surrounding
// EnsureContext, so that e.g. offline mode can skip network fetches when
// resolving a branch name that isn't already available locally.
func (r Recipe) EnsureVersionCtx(ctx EnsureContext, version string) (bool, error) {
isChaosCommit := false
if err := gitPkg.EnsureGitRepo(r.Dir); err != nil {
@@ -159,30 +190,46 @@ func (r Recipe) EnsureVersion(version string) (bool, error) {
}
var opts *git.CheckoutOptions
var checkedOutRef string
if tagRef.String() == "" {
log.Debug(i18n.G("attempting to checkout '%s' as chaos commit", version))
hash, err := repo.ResolveRevision(plumbing.Revision(version))
if err != nil {
log.Fatal(i18n.G("unable to resolve '%s': %s", version, err))
}
hash, hashErr := repo.ResolveRevision(plumbing.Revision(version))
if hashErr != nil {
branchHash, found, branchErr := resolveBranch(repo, version, ctx.Offline)
if errors.Is(branchErr, errOfflineBranchUnresolved) {
return isChaosCommit, errors.New(i18n.G("'%s' is not available locally and cannot be fetched in offline mode; retry without --offline", version))
}
if branchErr != nil {
return isChaosCommit, branchErr
}
if !found {
return isChaosCommit, errors.New(i18n.G("unable to resolve '%s': %s", version, hashErr))
}
opts = &git.CheckoutOptions{Hash: *hash, Create: false, Force: true}
isChaosCommit = true
opts = &git.CheckoutOptions{Hash: branchHash, Create: false, Force: true}
isChaosCommit = true
checkedOutRef = version
} else {
opts = &git.CheckoutOptions{Hash: *hash, Create: false, Force: true}
isChaosCommit = true
checkedOutRef = hash.String()
}
} else {
opts = &git.CheckoutOptions{Branch: tagRef, Create: false, Force: true}
checkedOutRef = tagRef.Short()
}
worktree, err := repo.Worktree()
if err != nil {
return isChaosCommit, nil
return isChaosCommit, err
}
if err := worktree.Checkout(opts); err != nil {
return isChaosCommit, nil
return isChaosCommit, err
}
log.Debug(i18n.G("successfully checked %s out to %s in %s", r.Name, tagRef.Short(), r.Dir))
log.Debug(i18n.G("successfully checked %s out to %s in %s", r.Name, checkedOutRef, r.Dir))
return isChaosCommit, nil
}
@@ -485,6 +532,51 @@ func (r Recipe) GetRecipeVersions() (RecipeVersions, []string, error) {
return versions, uniqueWarnings, nil
}
// resolveBranch resolves name as a branch on the "origin" remote, fetching
// it on demand into a remote-tracking ref (never into refs/heads/*, which
// would corrupt GetDefaultBranch's local-branch-name heuristic). It returns
// (hash, true, nil) if name is a branch, (_, false, nil) if name is
// confirmed not to be a branch, and a non-nil error for any other failure
// (e.g. network/auth issues) that should not be silently treated as "not a
// branch". When offline is true, no fetch is attempted and only an
// already-fetched remote-tracking ref is consulted.
func resolveBranch(repo *git.Repository, name string, offline bool) (plumbing.Hash, bool, error) {
remoteTrackingRef := plumbing.NewRemoteReferenceName("origin", name)
if !offline {
refSpec := gitCfg.RefSpec(fmt.Sprintf("+refs/heads/%s:%s", name, remoteTrackingRef))
err := repo.Fetch(&git.FetchOptions{
RemoteName: "origin",
RefSpecs: []gitCfg.RefSpec{refSpec},
Force: true,
})
if err != nil {
var noMatch git.NoMatchingRefSpecError
switch {
case errors.Is(err, git.NoErrAlreadyUpToDate):
case errors.As(err, &noMatch):
return plumbing.ZeroHash, false, nil
default:
return plumbing.ZeroHash, false, errors.New(i18n.G("unable to fetch branch '%s': %s", name, err))
}
}
}
ref, err := repo.Reference(remoteTrackingRef, true)
if err != nil {
if offline {
return plumbing.ZeroHash, false, errOfflineBranchUnresolved
}
return plumbing.ZeroHash, false, errors.New(i18n.G("unable to resolve fetched branch '%s': %s", name, err))
}
return ref.Hash(), true, nil
}
// errOfflineBranchUnresolved is returned by resolveBranch when offline mode
// prevents fetching a branch that isn't already available locally.
var errOfflineBranchUnresolved = errors.New(i18n.G("not fetched and cannot resolve offline"))
// Head retrieves latest HEAD metadata.
func (r Recipe) Head() (*plumbing.Reference, error) {
repo, err := git.PlainOpen(r.Dir)
+131
View File
@@ -6,6 +6,8 @@ import (
"testing"
"coopcloud.tech/abra/pkg/test"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/stretchr/testify/assert"
)
@@ -36,3 +38,132 @@ func TestIsDirty(t *testing.T) {
assert.True(t, dirty)
}
func TestEnsureVersionBranch(t *testing.T) {
test.SetupWithUpstream()
t.Cleanup(func() { test.Teardown() })
upstream, err := git.PlainOpen(test.UpstreamRecipeDir)
if err != nil {
t.Fatal(err)
}
featureRef, err := upstream.Reference(plumbing.NewBranchReferenceName(test.FeatureBranch), true)
if err != nil {
t.Fatal(err)
}
r := Get(test.RecipeName)
isChaosCommit, err := r.EnsureVersion(test.FeatureBranch)
if err != nil {
t.Fatal(err)
}
assert.True(t, isChaosCommit)
head, err := r.Head()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, featureRef.Hash(), head.Hash())
}
func TestEnsureVersionBranchRefetchesOnRedeploy(t *testing.T) {
test.SetupWithUpstream()
t.Cleanup(func() { test.Teardown() })
r := Get(test.RecipeName)
if _, err := r.EnsureVersion(test.FeatureBranch); err != nil {
t.Fatal(err)
}
if err := test.CreateBranchWithCommit(test.UpstreamRecipeDir, test.FeatureBranch, "feature2.txt", "more feature work"); err != nil {
t.Fatal(err)
}
upstream, err := git.PlainOpen(test.UpstreamRecipeDir)
if err != nil {
t.Fatal(err)
}
featureRef, err := upstream.Reference(plumbing.NewBranchReferenceName(test.FeatureBranch), true)
if err != nil {
t.Fatal(err)
}
if _, err := r.EnsureVersion(test.FeatureBranch); err != nil {
t.Fatal(err)
}
head, err := r.Head()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, featureRef.Hash(), head.Hash())
}
func TestEnsureVersionOfflineUnfetchedBranch(t *testing.T) {
test.SetupWithUpstream()
t.Cleanup(func() { test.Teardown() })
r := Get(test.RecipeName)
// SetupWithUpstream creates the branch before cloning, so the clone
// already has it: remove the remote-tracking ref to make it unfetched.
repo, err := git.PlainOpen(r.Dir)
if err != nil {
t.Fatal(err)
}
if err := repo.Storer.RemoveReference(plumbing.NewRemoteReferenceName("origin", test.FeatureBranch)); err != nil {
t.Fatal(err)
}
headBefore, err := r.Head()
if err != nil {
t.Fatal(err)
}
_, err = r.EnsureVersionCtx(EnsureContext{Offline: true}, test.FeatureBranch)
assert.ErrorContains(t, err, "offline")
headAfter, err := r.Head()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, headBefore.Hash(), headAfter.Hash())
}
func TestEnsureVersionUnknownNameErrors(t *testing.T) {
test.SetupWithUpstream()
t.Cleanup(func() { test.Teardown() })
r := Get(test.RecipeName)
_, err := r.EnsureVersion("does-not-exist-anywhere")
assert.Error(t, err)
}
func TestEnsureBranchPinnedRecipe(t *testing.T) {
test.SetupWithUpstream()
t.Cleanup(func() { test.Teardown() })
upstream, err := git.PlainOpen(test.UpstreamRecipeDir)
if err != nil {
t.Fatal(err)
}
featureRef, err := upstream.Reference(plumbing.NewBranchReferenceName(test.FeatureBranch), true)
if err != nil {
t.Fatal(err)
}
r := Get(test.RecipeName + ":" + test.FeatureBranch)
if err := r.Ensure(EnsureContext{}); err != nil {
t.Fatal(err)
}
head, err := r.Head()
if err != nil {
t.Fatal(err)
}
assert.Equal(t, featureRef.Hash(), head.Hash())
}
+88
View File
@@ -8,6 +8,7 @@ import (
"net/url"
"os"
"path"
"regexp"
"sort"
"strconv"
"strings"
@@ -121,7 +122,84 @@ type Features struct {
SSO string `json:"sso"`
}
// scpURLPattern matches SCP-style git URLs like git@host:path or git@host:port/path.
// Captures: 1=host(:port)?, 2=path.
var scpURLPattern = regexp.MustCompile(`^[\w.-]+@([\w.-]+(?::\d+)?):(.+)$`)
// NormalizeRecipeName canonicalizes a recipe identifier to a stable
// "host/path" form (or returns short catalog names unchanged). Accepts:
//
// - https://host/path[.git][:version]
// - http://host/path[.git][:version]
// - ssh://git@host[:port]/path[.git][:version]
// - git@host:path[.git][:version] (SCP-style)
// - host/path[.git][:version] (already canonical)
// - short-name[:version] (catalog recipe, pass through)
//
// The optional trailing :version suffix is preserved verbatim. The .git
// suffix and trailing slashes on the path are stripped so the four URL
// forms of the same repository collapse to one canonical value.
func NormalizeRecipeName(input string) string {
input = strings.TrimSpace(input)
// Split off the version suffix first, but only if it's not part of a
// scheme (https://) or SCP-style git@host: prefix. The simplest way is
// to detect those prefixes and treat the rest as the path-with-version.
var (
body = input
version string
)
switch {
case strings.HasPrefix(input, "https://") || strings.HasPrefix(input, "http://") || strings.HasPrefix(input, "ssh://"):
u, err := url.Parse(input)
if err != nil {
return input
}
host := u.Hostname() // strip port to keep canonical form colon-free
p := strings.TrimPrefix(u.Path, "/")
p, version = splitVersion(p)
body = host + "/" + p
case scpURLPattern.MatchString(input):
m := scpURLPattern.FindStringSubmatch(input)
host := m[1]
if i := strings.Index(host, ":"); i >= 0 {
host = host[:i] // strip port
}
p, v := splitVersion(m[2])
body = host + "/" + p
version = v
default:
body, version = splitVersion(input)
}
body = strings.TrimSuffix(body, "/")
body = strings.TrimSuffix(body, ".git")
if version != "" {
return body + ":" + version
}
return body
}
// splitVersion separates a trailing ":version" suffix from a recipe path.
// It only treats the final colon-separated segment as a version when it is
// non-empty and contains no slash (a slash means the colon belonged to the
// path, e.g. a host:port or scheme separator).
func splitVersion(s string) (string, string) {
idx := strings.LastIndex(s, ":")
if idx < 0 {
return s, ""
}
candidate := s[idx+1:]
if candidate == "" || strings.Contains(candidate, "/") {
return s, ""
}
return s[:idx], candidate
}
func Get(name string) Recipe {
name = NormalizeRecipeName(name)
version := ""
versionRaw := ""
if strings.Contains(name, ":") {
@@ -209,6 +287,16 @@ func (r Recipe) String() string {
return out
}
// ShortName returns the final path segment of the recipe name, i.e. the
// bare recipe name without any "host/org/" prefix carried by externally
// sourced (git URL) recipes. For catalogue recipes it returns Name unchanged.
func (r Recipe) ShortName() string {
if i := strings.LastIndex(r.Name, "/"); i >= 0 {
return r.Name[i+1:]
}
return r.Name
}
func escapeRecipeName(recipeName string) string {
recipeName = strings.ReplaceAll(recipeName, "/", "_")
recipeName = strings.ReplaceAll(recipeName, ".", "_")
+125
View File
@@ -88,6 +88,88 @@ func TestGet(t *testing.T) {
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
{
name: "https://mygit.org/myorg/cool-recipe",
recipe: Recipe{
Name: "mygit.org/myorg/cool-recipe",
Dir: path.Join(cfg.GetAbraDir(), "/recipes/mygit_org_myorg_cool-recipe"),
GitURL: "https://mygit.org/myorg/cool-recipe.git",
SSHURL: "ssh://git@mygit.org/myorg/cool-recipe.git",
ComposePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/compose.yml"),
ReadmePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/README.md"),
SampleEnvPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/.env.sample"),
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
{
name: "https://mygit.org/myorg/cool-recipe.git",
recipe: Recipe{
Name: "mygit.org/myorg/cool-recipe",
Dir: path.Join(cfg.GetAbraDir(), "/recipes/mygit_org_myorg_cool-recipe"),
GitURL: "https://mygit.org/myorg/cool-recipe.git",
SSHURL: "ssh://git@mygit.org/myorg/cool-recipe.git",
ComposePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/compose.yml"),
ReadmePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/README.md"),
SampleEnvPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/.env.sample"),
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
{
name: "https://mygit.org/myorg/cool-recipe.git:1.2.4",
recipe: Recipe{
Name: "mygit.org/myorg/cool-recipe",
EnvVersion: "1.2.4",
EnvVersionRaw: "1.2.4",
Dir: path.Join(cfg.GetAbraDir(), "/recipes/mygit_org_myorg_cool-recipe"),
GitURL: "https://mygit.org/myorg/cool-recipe.git",
SSHURL: "ssh://git@mygit.org/myorg/cool-recipe.git",
ComposePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/compose.yml"),
ReadmePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/README.md"),
SampleEnvPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/.env.sample"),
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
{
name: "ssh://git@mygit.org/myorg/cool-recipe.git",
recipe: Recipe{
Name: "mygit.org/myorg/cool-recipe",
Dir: path.Join(cfg.GetAbraDir(), "/recipes/mygit_org_myorg_cool-recipe"),
GitURL: "https://mygit.org/myorg/cool-recipe.git",
SSHURL: "ssh://git@mygit.org/myorg/cool-recipe.git",
ComposePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/compose.yml"),
ReadmePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/README.md"),
SampleEnvPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/.env.sample"),
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
{
name: "git@mygit.org:myorg/cool-recipe.git",
recipe: Recipe{
Name: "mygit.org/myorg/cool-recipe",
Dir: path.Join(cfg.GetAbraDir(), "/recipes/mygit_org_myorg_cool-recipe"),
GitURL: "https://mygit.org/myorg/cool-recipe.git",
SSHURL: "ssh://git@mygit.org/myorg/cool-recipe.git",
ComposePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/compose.yml"),
ReadmePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/README.md"),
SampleEnvPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/.env.sample"),
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
{
name: "git@mygit.org:myorg/cool-recipe:1.2.4",
recipe: Recipe{
Name: "mygit.org/myorg/cool-recipe",
EnvVersion: "1.2.4",
EnvVersionRaw: "1.2.4",
Dir: path.Join(cfg.GetAbraDir(), "/recipes/mygit_org_myorg_cool-recipe"),
GitURL: "https://mygit.org/myorg/cool-recipe.git",
SSHURL: "ssh://git@mygit.org/myorg/cool-recipe.git",
ComposePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/compose.yml"),
ReadmePath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/README.md"),
SampleEnvPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/.env.sample"),
AbraShPath: path.Join(cfg.GetAbraDir(), "recipes/mygit_org_myorg_cool-recipe/abra.sh"),
},
},
}
for _, tc := range testcases {
@@ -101,6 +183,49 @@ func TestGet(t *testing.T) {
}
}
func TestNormalizeRecipeName(t *testing.T) {
cases := []struct {
in, want string
}{
// catalog short names pass through
{"foo", "foo"},
{"foo:1.2.3", "foo:1.2.3"},
// bare host/path form
{"mygit.org/myorg/cool-recipe", "mygit.org/myorg/cool-recipe"},
{"mygit.org/myorg/cool-recipe.git", "mygit.org/myorg/cool-recipe"},
{"mygit.org/myorg/cool-recipe.git:1.2.3", "mygit.org/myorg/cool-recipe:1.2.3"},
{"mygit.org/myorg/cool-recipe/", "mygit.org/myorg/cool-recipe"},
// https://
{"https://mygit.org/myorg/cool-recipe", "mygit.org/myorg/cool-recipe"},
{"https://mygit.org/myorg/cool-recipe.git", "mygit.org/myorg/cool-recipe"},
{"https://mygit.org/myorg/cool-recipe.git:1.2.3", "mygit.org/myorg/cool-recipe:1.2.3"},
{"http://mygit.org/myorg/cool-recipe", "mygit.org/myorg/cool-recipe"},
// ssh://, with and without port
{"ssh://git@mygit.org/myorg/cool-recipe.git", "mygit.org/myorg/cool-recipe"},
{"ssh://git@mygit.org:2222/myorg/cool-recipe.git", "mygit.org/myorg/cool-recipe"},
// SCP-style git@host:path
{"git@mygit.org:myorg/cool-recipe", "mygit.org/myorg/cool-recipe"},
{"git@mygit.org:myorg/cool-recipe.git", "mygit.org/myorg/cool-recipe"},
{"git@mygit.org:myorg/cool-recipe:1.2.3", "mygit.org/myorg/cool-recipe:1.2.3"},
// whitespace
{" https://mygit.org/myorg/cool-recipe ", "mygit.org/myorg/cool-recipe"},
}
for _, tc := range cases {
t.Run(tc.in, func(t *testing.T) {
got := NormalizeRecipeName(tc.in)
if got != tc.want {
t.Errorf("NormalizeRecipeName(%q) = %q, want %q", tc.in, got, tc.want)
}
})
}
}
func TestGetVersionLabelLocalDoesNotUseTimeoutLabel(t *testing.T) {
test.Setup()
t.Cleanup(func() { test.Teardown() })
+1 -1
View File
@@ -3,7 +3,7 @@ version: "3.8"
services:
app:
image: nginx:1.31.8
image: nginx:1.31.9
secrets:
- test_pass_one
- test_pass_two
+123
View File
@@ -8,6 +8,9 @@ import (
gitPkg "coopcloud.tech/abra/pkg/git"
"git.coopcloud.tech/toolshed/godotenv"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
var (
@@ -76,6 +79,126 @@ func Setup() {
}
}
// UpstreamRecipeDir is the "remote" repo used by SetupWithUpstream, kept
// outside recipes/ so Teardown's blanket removal of $ABRA_DIR still cleans
// it up.
var UpstreamRecipeDir = os.ExpandEnv("$ABRA_DIR/_upstream/test_recipe")
// FeatureBranch is the non-default branch created by SetupWithUpstream.
const FeatureBranch = "feature"
// SetupWithUpstream is like Setup, but the recipe under $ABRA_DIR/recipes
// is a real clone of a separate "upstream" repo (UpstreamRecipeDir) rather
// than a git-inited copy of the fixture. The upstream repo additionally has
// a FeatureBranch with one extra commit not present on main, so tests can
// exercise resolving/fetching a non-default branch as a recipe :version.
func SetupWithUpstream() {
Teardown()
if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR"), 0764); err != nil {
if !os.IsExist(err) {
log.Fatal(err)
}
}
if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR/servers"), 0700); err != nil {
if !os.IsExist(err) {
log.Fatal(err)
}
}
if err := os.Mkdir(os.ExpandEnv("$ABRA_DIR/recipes"), 0764); err != nil {
if !os.IsExist(err) {
log.Fatal(err)
}
}
serverSrcDir := os.ExpandEnv("$PWD/../../tests/resources/test_server")
serverDestDir := os.ExpandEnv("$ABRA_DIR/servers/test_server")
if err := os.CopyFS(serverDestDir, os.DirFS(serverSrcDir)); err != nil {
log.Fatal(err)
}
if err := os.MkdirAll(path.Dir(UpstreamRecipeDir), 0764); err != nil {
log.Fatal(err)
}
recipeSrcDir := os.ExpandEnv("$PWD/../../tests/resources/test_recipe")
if err := os.CopyFS(UpstreamRecipeDir, os.DirFS(recipeSrcDir)); err != nil {
log.Fatal(err)
}
if err := os.WriteFile(path.Join(UpstreamRecipeDir, ".env.sample"), []byte("TYPE=test_recipe\n"), 0o644); err != nil {
log.Fatal(err)
}
if err := gitPkg.Init(UpstreamRecipeDir, true, "tester", "helo@coopcloud.tech"); err != nil {
log.Fatal(err)
}
if err := CreateBranchWithCommit(UpstreamRecipeDir, FeatureBranch, "feature.txt", "feature work"); err != nil {
log.Fatal(err)
}
recipeDestDir := os.ExpandEnv("$ABRA_DIR/recipes/test_recipe")
if err := gitPkg.Clone(recipeDestDir, UpstreamRecipeDir); err != nil {
log.Fatal(err)
}
}
// CreateBranchWithCommit checks out branchName in repoPath (creating it off
// the current HEAD if it doesn't exist yet, or extending it if it does),
// writes fileName with contents, commits it, then returns to the repo's
// default branch (leaving the new commit reachable only via the branch's
// ref, matching how a contributor would push a feature branch upstream
// without switching the repo's HEAD away from main).
func CreateBranchWithCommit(repoPath, branchName, fileName, contents string) error {
repo, err := git.PlainOpen(repoPath)
if err != nil {
return err
}
head, err := repo.Head()
if err != nil {
return err
}
worktree, err := repo.Worktree()
if err != nil {
return err
}
branchRef := plumbing.NewBranchReferenceName(branchName)
create := true
if _, err := repo.Reference(branchRef, true); err == nil {
create = false
}
checkoutOpts := &git.CheckoutOptions{Branch: branchRef, Create: create}
if create {
checkoutOpts.Hash = head.Hash()
}
if err := worktree.Checkout(checkoutOpts); err != nil {
return err
}
if err := os.WriteFile(path.Join(repoPath, fileName), []byte(contents), 0o644); err != nil {
return err
}
if _, err := worktree.Add(fileName); err != nil {
return err
}
if _, err := worktree.Commit(fileName, &git.CommitOptions{
Author: &object.Signature{Name: "tester", Email: "helo@coopcloud.tech"},
}); err != nil {
return err
}
return worktree.Checkout(&git.CheckoutOptions{Branch: head.Name()})
}
func AddEnv(envKey, envValue string) error {
filePath := os.ExpandEnv(fmt.Sprintf("$ABRA_DIR/servers/%s/%s.env", ServerName, AppName))
+1 -1
View File
@@ -3,7 +3,7 @@ version: "3.8"
services:
app:
image: nginx:1.31.8
image: nginx:1.31.9
networks:
- proxy
deploy:
+3 -3
View File
@@ -3,16 +3,16 @@ kind: pipeline
name: coopcloud.tech/tagcmp
steps:
- name: gofmt
image: golang:1.27
image: golang:1.26
commands:
- test -z "$(gofmt -l .)"
- name: go build
image: golang:1.27
image: golang:1.26
commands:
- go build -v .
- name: go test
image: golang:1.27
image: golang:1.26
commands:
- go test . -cover
+1 -1
View File
@@ -1,4 +1,4 @@
FROM golang:1.27@sha256:820153096fda4de6ac1b7194d12e416b7dd8f4e9a33d17e0345b111b945162d8
FROM golang:1.26@sha256:f96cc555eb8db430159a3aa6797cd5bae561945b7b0fe7d0e284c63a3b291609
ENV GOOS=linux
ENV GOARCH=arm
+1 -1
View File
@@ -1,4 +1,4 @@
FROM golang:1.27@sha256:820153096fda4de6ac1b7194d12e416b7dd8f4e9a33d17e0345b111b945162d8
FROM golang:1.26@sha256:f96cc555eb8db430159a3aa6797cd5bae561945b7b0fe7d0e284c63a3b291609
ENV GOOS=linux
ENV GOARCH=arm64
+31
View File
@@ -0,0 +1,31 @@
// Package require implements the same assertions as the assert package but
// stops test execution when a test fails.
//
// # Example Usage
//
// The following is a complete example using require in a standard test function:
//
// import (
// "testing"
// "github.com/stretchr/testify/require"
// )
//
// func TestSomething(t *testing.T) {
//
// var a string = "Hello"
// var b string = "Hello"
//
// require.Equal(t, a, b, "The two words should be the same.")
//
// }
//
// # Assertions
//
// The require package have same global functions as in the assert package,
// but instead of returning a boolean result they call [testing.T.FailNow].
// A consequence of this is that it must be called from the goroutine running
// the test function, not from other goroutines created during the test.
//
// Every assertion function also takes an optional string message as the final argument,
// allowing custom error messages to be appended to the message the assertion method outputs.
package require
+16
View File
@@ -0,0 +1,16 @@
package require
// Assertions provides assertion methods around the
// TestingT interface.
type Assertions struct {
t TestingT
}
// New makes a new Assertions object for the specified TestingT.
func New(t TestingT) *Assertions {
return &Assertions{
t: t,
}
}
//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=require -template=require_forward.go.tmpl -include-format-funcs"
File diff suppressed because it is too large Load Diff
+6
View File
@@ -0,0 +1,6 @@
{{.CommentRequire}}
func {{.DocInfo.Name}}(t TestingT, {{.Params}}) {
if h, ok := t.(tHelper); ok { h.Helper() }
if assert.{{.DocInfo.Name}}(t, {{.ForwardedParams}}) { return }
t.FailNow()
}
File diff suppressed because it is too large Load Diff
+5
View File
@@ -0,0 +1,5 @@
{{.CommentRequireWithoutT "a"}}
func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) {
if h, ok := a.t.(tHelper); ok { h.Helper() }
{{.DocInfo.Name}}(a.t, {{.ForwardedParams}})
}
+29
View File
@@ -0,0 +1,29 @@
package require
// TestingT is an interface wrapper around *testing.T
type TestingT interface {
Errorf(format string, args ...interface{})
FailNow()
}
type tHelper = interface {
Helper()
}
// ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful
// for table driven tests.
type ComparisonAssertionFunc = func(TestingT, interface{}, interface{}, ...interface{})
// ValueAssertionFunc is a common function prototype when validating a single value. Can be useful
// for table driven tests.
type ValueAssertionFunc = func(TestingT, interface{}, ...interface{})
// BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful
// for table driven tests.
type BoolAssertionFunc = func(TestingT, bool, ...interface{})
// ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful
// for table driven tests.
type ErrorAssertionFunc = func(TestingT, error, ...interface{})
//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=require -template=require.go.tmpl -include-format-funcs"
+1 -1
View File
@@ -1,4 +1,4 @@
# This is a renovate-friendly source of Docker images.
FROM python:3.13.6-slim-bullseye@sha256:e98b521460ee75bca92175c16247bdf7275637a8faaeb2bcfa19d879ae5c4b9a AS python
FROM otel/weaver:v0.28.0@sha256:5857bb81d8cc7ed608cdcae37c4b459eb020f21dd4acade35f7933cd6762fbcc AS weaver
FROM otel/weaver:v0.21.2@sha256:2401de985c38bdb98b43918e2f43aa36b2afed4aa5669ac1c1de0a17301cd36d AS weaver
FROM avtodev/markdown-lint:v1@sha256:6aeedc2f49138ce7a1cd0adffc1b1c0321b841dc2102408967d9301c031949ee AS markdown
+17
View File
@@ -0,0 +1,17 @@
// Copyright 2026 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build wasm32
package cpu
// We're compiling the cpu package for an unknown (software-abstracted) CPU.
// Make CacheLinePad an empty struct and hope that the usual struct alignment
// rules are good enough.
const cacheLineSize = 0
func initOptions() {}
func archInit() {}
+1 -1
View File
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build 386 || amd64 || amd64p32 || alpha || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || nios2 || ppc64le || riscv || riscv64 || sh || wasm
//go:build 386 || amd64 || amd64p32 || alpha || arm || arm64 || loong64 || mipsle || mips64le || mips64p32le || nios2 || ppc64le || riscv || riscv64 || sh || wasm || wasm32
package cpu
+1 -1
View File
@@ -43,6 +43,6 @@ var (
func isProcessorFeaturePresent(ProcessorFeature uint32) (ret bool) {
r0, _, _ := syscall.SyscallN(procIsProcessorFeaturePresent.Addr(), uintptr(ProcessorFeature))
ret = r0 != 0
ret = uint32(r0) != 0
return
}
+8 -8
View File
@@ -14,13 +14,13 @@
// The runtime may know about them.
TEXT ·Syscall(SB),NOSPLIT,$0-56
MOVD $syscall·Syscall(SB), R31
MOVD R31, CTR
MOVD $syscall·Syscall(SB), R12
MOVD R12, CTR
JMP (CTR)
TEXT ·Syscall6(SB),NOSPLIT,$0-80
MOVD $syscall·Syscall6(SB), R31
MOVD R31, CTR
MOVD $syscall·Syscall6(SB), R12
MOVD R12, CTR
JMP (CTR)
TEXT ·SyscallNoError(SB),NOSPLIT,$0-48
@@ -39,13 +39,13 @@ TEXT ·SyscallNoError(SB),NOSPLIT,$0-48
RET
TEXT ·RawSyscall(SB),NOSPLIT,$0-56
MOVD $syscall·RawSyscall(SB), R31
MOVD R31, CTR
MOVD $syscall·RawSyscall(SB), R12
MOVD R12, CTR
JMP (CTR)
TEXT ·RawSyscall6(SB),NOSPLIT,$0-80
MOVD $syscall·RawSyscall6(SB), R31
MOVD R31, CTR
MOVD $syscall·RawSyscall6(SB), R12
MOVD R12, CTR
JMP (CTR)
TEXT ·RawSyscallNoError(SB),NOSPLIT,$0-48
+8 -2
View File
@@ -6,7 +6,10 @@
package unix
import "unsafe"
import (
"runtime"
"unsafe"
)
// readInt returns the size-bytes unsigned integer in native byte order at offset off.
func readInt(b []byte, off, size uintptr) (u uint64, ok bool) {
@@ -75,7 +78,10 @@ func ParseDirent(buf []byte, max int, names []string) (consumed int, count int,
if !ok {
break
}
if ino == 0 { // File absent in directory.
// Some filesystems on Linux (old XFS, FUSE) can return valid
// files with zero inodes. On other platforms, a zero inode
// indicates a file absent from the directory.
if ino == 0 && runtime.GOOS != "linux" {
continue
}
const namoff = uint64(unsafe.Offsetof(Dirent{}.Name))
+6
View File
@@ -23,6 +23,12 @@ func IoctlGetUint32(fd int, req uint) (uint32, error) {
return value, err
}
func IoctlGetUint64(fd int, req uint) (uint64, error) {
var value uint64
err := ioctlPtr(fd, req, unsafe.Pointer(&value))
return value, err
}
func IoctlGetRTCTime(fd int) (*RTCTime, error) {
var value RTCTime
err := ioctlPtr(fd, RTC_RD_TIME, unsafe.Pointer(&value))
+3
View File
@@ -31,6 +31,9 @@ func ParseUnixCredentials(m *SocketControlMessage) (*Ucred, error) {
if m.Header.Type != SCM_CREDENTIALS {
return nil, EINVAL
}
if len(m.Data) < SizeofUcred {
return nil, EINVAL
}
ucred := *(*Ucred)(unsafe.Pointer(&m.Data[0]))
return &ucred, nil
}
+7 -1
View File
@@ -49,7 +49,13 @@ func sendfile(outfd int, infd int, offset *int64, count int) (written int, err e
var writtenOut uint64 = 0
_, _, e1 := Syscall9(SYS_SENDFILE, uintptr(infd), uintptr(outfd), uintptr(*offset), uintptr((*offset)>>32), uintptr(count), 0, uintptr(unsafe.Pointer(&writtenOut)), 0, 0)
written = int(writtenOut)
// For some reason on the freebsd-386 builder writtenOut
// is modified when the system call returns EINVAL.
// The man page says that the value is only written for
// success, EINTR, or EAGAIN, so only use those cases.
if e1 == 0 || e1 == EINTR || e1 == EAGAIN {
written = int(writtenOut)
}
if e1 != 0 {
err = e1
-11
View File
@@ -35,14 +35,3 @@ const (
QUOTA_LIMITS_HARDWS_MAX_DISABLE = 0x00000008
QUOTA_LIMITS_HARDWS_MAX_ENABLE = 0x00000004
)
type MemoryBasicInformation struct {
BaseAddress uintptr
AllocationBase uintptr
AllocationProtect uint32
PartitionId uint16
RegionSize uintptr
State uint32
Protect uint32
Type uint32
}
+14 -14
View File
@@ -53,56 +53,56 @@ var (
func regConnectRegistry(machinename *uint16, key syscall.Handle, result *syscall.Handle) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegConnectRegistryW.Addr(), uintptr(unsafe.Pointer(machinename)), uintptr(key), uintptr(unsafe.Pointer(result)))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
func regCreateKeyEx(key syscall.Handle, subkey *uint16, reserved uint32, class *uint16, options uint32, desired uint32, sa *syscall.SecurityAttributes, result *syscall.Handle, disposition *uint32) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegCreateKeyExW.Addr(), uintptr(key), uintptr(unsafe.Pointer(subkey)), uintptr(reserved), uintptr(unsafe.Pointer(class)), uintptr(options), uintptr(desired), uintptr(unsafe.Pointer(sa)), uintptr(unsafe.Pointer(result)), uintptr(unsafe.Pointer(disposition)))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
func regDeleteKey(key syscall.Handle, subkey *uint16) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegDeleteKeyW.Addr(), uintptr(key), uintptr(unsafe.Pointer(subkey)))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
func regDeleteValue(key syscall.Handle, name *uint16) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegDeleteValueW.Addr(), uintptr(key), uintptr(unsafe.Pointer(name)))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
func regEnumValue(key syscall.Handle, index uint32, name *uint16, nameLen *uint32, reserved *uint32, valtype *uint32, buf *byte, buflen *uint32) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegEnumValueW.Addr(), uintptr(key), uintptr(index), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(nameLen)), uintptr(unsafe.Pointer(reserved)), uintptr(unsafe.Pointer(valtype)), uintptr(unsafe.Pointer(buf)), uintptr(unsafe.Pointer(buflen)))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
func regLoadMUIString(key syscall.Handle, name *uint16, buf *uint16, buflen uint32, buflenCopied *uint32, flags uint32, dir *uint16) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegLoadMUIStringW.Addr(), uintptr(key), uintptr(unsafe.Pointer(name)), uintptr(unsafe.Pointer(buf)), uintptr(buflen), uintptr(unsafe.Pointer(buflenCopied)), uintptr(flags), uintptr(unsafe.Pointer(dir)))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
func regSetValueEx(key syscall.Handle, valueName *uint16, reserved uint32, vtype uint32, buf *byte, bufsize uint32) (regerrno error) {
r0, _, _ := syscall.SyscallN(procRegSetValueExW.Addr(), uintptr(key), uintptr(unsafe.Pointer(valueName)), uintptr(reserved), uintptr(vtype), uintptr(unsafe.Pointer(buf)), uintptr(bufsize))
if r0 != 0 {
regerrno = syscall.Errno(r0)
if uint32(r0) != 0 {
regerrno = syscall.Errno(uint32(r0))
}
return
}
+9 -9
View File
@@ -507,7 +507,7 @@ func CurrentThread() Handle { return Handle(^uintptr(2 - 1)) }
// GetProcAddressByOrdinal retrieves the address of the exported
// function from module by ordinal.
func GetProcAddressByOrdinal(module Handle, ordinal uintptr) (proc uintptr, err error) {
r0, _, e1 := syscall.Syscall(procGetProcAddress.Addr(), 2, uintptr(module), ordinal, 0)
r0, _, e1 := syscall.SyscallN(procGetProcAddress.Addr(), uintptr(module), ordinal)
proc = uintptr(r0)
if proc == 0 {
err = errnoErr(e1)
@@ -1206,7 +1206,7 @@ func LoadConnectEx() error {
}
func connectEx(s Handle, name unsafe.Pointer, namelen int32, sendBuf *byte, sendDataLen uint32, bytesSent *uint32, overlapped *Overlapped) (err error) {
r1, _, e1 := syscall.Syscall9(connectExFunc.addr, 7, uintptr(s), uintptr(name), uintptr(namelen), uintptr(unsafe.Pointer(sendBuf)), uintptr(sendDataLen), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), 0, 0)
r1, _, e1 := syscall.SyscallN(connectExFunc.addr, uintptr(s), uintptr(name), uintptr(namelen), uintptr(unsafe.Pointer(sendBuf)), uintptr(sendDataLen), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)))
if r1 == 0 {
if e1 != 0 {
err = error(e1)
@@ -1271,7 +1271,7 @@ func WSASendMsg(fd Handle, msg *WSAMsg, flags uint32, bytesSent *uint32, overlap
if err != nil {
return err
}
r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.sendAddr, 6, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(flags), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)))
r1, _, e1 := syscall.SyscallN(sendRecvMsgFunc.sendAddr, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(flags), uintptr(unsafe.Pointer(bytesSent)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)))
if r1 == socket_error {
err = errnoErr(e1)
}
@@ -1283,7 +1283,7 @@ func WSARecvMsg(fd Handle, msg *WSAMsg, bytesReceived *uint32, overlapped *Overl
if err != nil {
return err
}
r1, _, e1 := syscall.Syscall6(sendRecvMsgFunc.recvAddr, 5, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(unsafe.Pointer(bytesReceived)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)), 0)
r1, _, e1 := syscall.SyscallN(sendRecvMsgFunc.recvAddr, uintptr(fd), uintptr(unsafe.Pointer(msg)), uintptr(unsafe.Pointer(bytesReceived)), uintptr(unsafe.Pointer(overlapped)), uintptr(unsafe.Pointer(croutine)))
if r1 == socket_error {
err = errnoErr(e1)
}
@@ -1805,7 +1805,7 @@ type PSAPI_WORKING_SET_EX_BLOCK uint64
// Valid returns the validity of this page.
// If this bit is 1, the subsequent members are valid; otherwise they should be ignored.
func (b PSAPI_WORKING_SET_EX_BLOCK) Valid() bool {
return (b & 1) == 1
return (b & 1) != 0
}
// ShareCount is the number of processes that share this page. The maximum value of this member is 7.
@@ -1822,7 +1822,7 @@ func (b PSAPI_WORKING_SET_EX_BLOCK) Win32Protection() uint64 {
// Shared returns the shared status of this page.
// If this bit is 1, the page can be shared.
func (b PSAPI_WORKING_SET_EX_BLOCK) Shared() bool {
return (b & (1 << 15)) == 1
return (b & (1 << 15)) != 0
}
// Node is the NUMA node. The maximum value of this member is 63.
@@ -1833,19 +1833,19 @@ func (b PSAPI_WORKING_SET_EX_BLOCK) Node() uint64 {
// Locked returns the locked status of this page.
// If this bit is 1, the virtual page is locked in physical memory.
func (b PSAPI_WORKING_SET_EX_BLOCK) Locked() bool {
return (b & (1 << 22)) == 1
return (b & (1 << 22)) != 0
}
// LargePage returns the large page status of this page.
// If this bit is 1, the page is a large page.
func (b PSAPI_WORKING_SET_EX_BLOCK) LargePage() bool {
return (b & (1 << 23)) == 1
return (b & (1 << 23)) != 0
}
// Bad returns the bad status of this page.
// If this bit is 1, the page is has been reported as bad.
func (b PSAPI_WORKING_SET_EX_BLOCK) Bad() bool {
return (b & (1 << 31)) == 1
return (b & (1 << 31)) != 0
}
// intField extracts an integer field in the PSAPI_WORKING_SET_EX_BLOCK union.
+10
View File
@@ -33,3 +33,13 @@ type JOBOBJECT_BASIC_LIMIT_INFORMATION struct {
SchedulingClass uint32
_ uint32 // pad to 8 byte boundary
}
type MemoryBasicInformation struct {
BaseAddress uintptr
AllocationBase uintptr
AllocationProtect uint32
RegionSize uintptr
State uint32
Protect uint32
Type uint32
}
+11
View File
@@ -32,3 +32,14 @@ type JOBOBJECT_BASIC_LIMIT_INFORMATION struct {
PriorityClass uint32
SchedulingClass uint32
}
type MemoryBasicInformation struct {
BaseAddress uintptr
AllocationBase uintptr
AllocationProtect uint32
PartitionId uint16
RegionSize uintptr
State uint32
Protect uint32
Type uint32
}
+11
View File
@@ -32,3 +32,14 @@ type JOBOBJECT_BASIC_LIMIT_INFORMATION struct {
PriorityClass uint32
SchedulingClass uint32
}
type MemoryBasicInformation struct {
BaseAddress uintptr
AllocationBase uintptr
AllocationProtect uint32
PartitionId uint16
RegionSize uintptr
State uint32
Protect uint32
Type uint32
}
+388 -388
View File
File diff suppressed because it is too large Load Diff
Generated Vendored
+3 -2
View File
@@ -547,6 +547,7 @@ github.com/stretchr/testify/assert
github.com/stretchr/testify/assert/yaml
github.com/stretchr/testify/internal/difflib
github.com/stretchr/testify/internal/spew
github.com/stretchr/testify/require
# github.com/theupdateframework/notary v0.7.0
## explicit; go 1.12
# github.com/xanzy/ssh-agent v0.3.3
@@ -694,7 +695,7 @@ golang.org/x/net/internal/socks
golang.org/x/net/internal/timeseries
golang.org/x/net/proxy
golang.org/x/net/trace
# golang.org/x/sys v0.49.0
# golang.org/x/sys v0.50.0
## explicit; go 1.26.0
golang.org/x/sys/cpu
golang.org/x/sys/execabs
@@ -702,7 +703,7 @@ golang.org/x/sys/plan9
golang.org/x/sys/unix
golang.org/x/sys/windows
golang.org/x/sys/windows/registry
# golang.org/x/term v0.47.0
# golang.org/x/term v0.48.0
## explicit; go 1.26.0
golang.org/x/term
# golang.org/x/text v0.41.0