eBPF-based security platform
bpm: Put signed indexes to the package namespace
Michi Mutsuzakic99a694d1f22
diff --git a/Documentation/concepts/background/oci.rst b/Documentation/concepts/background/oci.rstindex a130cbef..0952a524 100644--- a/Documentation/concepts/background/oci.rst+++ b/Documentation/concepts/background/oci.rst@@ -127,7 +127,7 @@ manifest with shirudo's own media types:- The signing certificate as ``application/pkix-cert``. No``shirudo bpm`` command reads it.* - Repository index- - ``shirudo/bpm-index:stable``, one per registry host+ - ``<namespace>/bpm-index:stable``, one per repository namespace- ``application/vnd.shirudo.bpm.index.v1+json`` and its detachedCMS signature,``application/vnd.shirudo.bpm.index.signature.v1``.diff --git a/Documentation/concepts/glossary.rst b/Documentation/concepts/glossary.rstindex 905f9569..973c644e 100644--- a/Documentation/concepts/glossary.rst+++ b/Documentation/concepts/glossary.rst@@ -373,7 +373,7 @@ it.the target's differs. See :doc:`/reference/cli/version`.signed index- The JSON document at ``shirudo/bpm-index:stable`` in a registry that+ The JSON document at ``<namespace>/bpm-index:stable`` in a registry thatlists each package's name, version, manifest digest and packagedigest, with a detached CMS signature. ``shirudo bpm push`` updatesit and refuses to list a version again with different content, anddiff --git a/Documentation/concepts/upatches-and-packages.rst b/Documentation/concepts/upatches-and-packages.rstindex 4456c437..49f6de2c 100644--- a/Documentation/concepts/upatches-and-packages.rst+++ b/Documentation/concepts/upatches-and-packages.rst@@ -144,7 +144,7 @@ registry protocol.``shirudo bpm push`` uploads a package as an OCI artifact. Given thesigning key, it also adds the package to the registry's index at-``shirudo/bpm-index:stable``: a JSON document that lists each package's+``<namespace>/bpm-index:stable``: a JSON document that lists each package'sname, version, manifest digest and package digest, with a detached CMSsignature in a second layer. ``push`` refuses to list a version againwith different content, so a version published through the index does notdiff --git a/Documentation/contribute/code/develop-locally.rst b/Documentation/contribute/code/develop-locally.rstindex e6b66829..663e0ea4 100644--- a/Documentation/contribute/code/develop-locally.rst+++ b/Documentation/contribute/code/develop-locally.rst@@ -189,7 +189,7 @@ from the config the target runs, and activate it... code-block:: consoleoperator$ mkdir -p "$DEV/config-root/etc/shirudo/bpm/registries.d" "$DEV/config-root/etc/shirudo/bpm/keys"- operator$ printf '%s\n' 'endpoint = oci://10.0.2.2:5000' 'key_file = /etc/shirudo/bpm/keys/dev.der' > "$DEV/config-root/etc/shirudo/bpm/registries.d/dev.conf"+ operator$ printf '%s\n' 'endpoint = oci://10.0.2.2:5000' 'index_repository = dev/bpm-index' 'key_file = /etc/shirudo/bpm/keys/dev.der' > "$DEV/config-root/etc/shirudo/bpm/registries.d/dev.conf"operator$ cp "$KEYS/cert.der" "$DEV/config-root/etc/shirudo/bpm/keys/dev.der"5. Build and sign the config image, and verify it:diff --git a/Documentation/images/operate-package-delivery.svg b/Documentation/images/operate-package-delivery.svgindex dcc459e4..15ca052a 100644--- a/Documentation/images/operate-package-delivery.svg+++ b/Documentation/images/operate-package-delivery.svg@@ -25,7 +25,7 @@<text x="110" y="64" font-size="13">Operator host</text><text x="350" y="64" font-size="13">OCI registry</text><text x="350" y="82">one endpoint per repository</text>- <text x="350" y="119">shirudo/bpm-index:stable</text>+ <text x="350" y="119"><namespace>/bpm-index:stable</text><text x="350" y="137">index and its signature</text><text x="350" y="188">packages (.bee)</text><text x="350" y="241">upatch tree</text>diff --git a/Documentation/operate/plan/scalability.rst b/Documentation/operate/plan/scalability.rstindex 30092cb0..52128f05 100644--- a/Documentation/operate/plan/scalability.rst+++ b/Documentation/operate/plan/scalability.rst@@ -81,7 +81,7 @@ Staged rollouts~~~~~~~~~~~~~~~- A repository is one registry endpoint, ``oci://<host>[:<port>]`` without- a path, and its index is always ``shirudo/bpm-index:stable`` on it.+ a path, and its index is at ``<namespace>/bpm-index:stable`` on it.There is no mirror or fallback list. To stage a package, publish it toa registry that only canary targets follow before the one the fleetfollows. Targets reach different registries, or a pull-through cache,diff --git a/Documentation/operate/run/deliver-packages.rst b/Documentation/operate/run/deliver-packages.rstindex 2942f0f5..e46a4adf 100644--- a/Documentation/operate/run/deliver-packages.rst+++ b/Documentation/operate/run/deliver-packages.rst@@ -21,7 +21,8 @@ Delivery is a pull in four steps, which Figure 1 shows together withthe upatch tree:#. On the operator host, ``shirudo bpm push`` publishes the package and- a signed entry in the repository index ``shirudo/bpm-index:stable``.+ a signed entry in the repository index+ ``<namespace>/bpm-index:stable``.#. On the target, ``shirudo bpm repo update`` fetches that index fromeach repository, verifies its signature against the repository'scertificate and caches it under@@ -130,10 +131,17 @@ Add a repositoryendpoint = oci://<registry-host>key_file = /etc/shirudo/bpm/keys/<name>.der- ``<name>`` is the repository name that ``bpm install`` uses.+ ``index_repository`` is optional. If omitted, shirudo uses+ ``<name>/bpm-index``; use the package namespace as ``<name>`` to match+ ``bpm push``. Set the field explicitly when the source name differs.++ ``<name>`` is the repository name that ``bpm install`` uses. When the+ default index location is used, it must also be the package namespace.``<registry-host>`` is the registry's host name with an optional port- and no path. ``key_file`` names a certificate inside the config- image. The target refuses any other absolute path.+ and no path. ``index_repository`` names the index repository. Its+ namespace must match the namespace of the package repository passed to+ ``bpm push``. ``key_file`` names a certificate inside the config image.+ The target refuses any other absolute path.#. Copy the certificate whose key signs the repository index, the oneyou pass to ``bpm push``, to ``etc/shirudo/bpm/keys/<name>.der`` indiff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex 009620fa..a63cc671 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -21,8 +21,8 @@ Usage.. code-block:: none- shirudo bpm config register add <source> oci://<registry> (--cert <certificate> | --cert=<certificate>)- shirudo bpm config register list [--json]+ shirudo bpm config register add <source> oci://<registry> [--index-repository <repository>] (--key-file <certificate> | --key-file=<certificate>)+ shirudo bpm config register listshirudo bpm config register (remove | rm) <source>shirudo bpm config update --base-package <package> --version <version>[--file <beefile>]@@ -132,12 +132,10 @@ image, the image's ``etc/shirudo/bpm/registries.d/`` is used instead, andimage mode: change repository definitions and keys in a signed config image, then run `shirudo image update`- ``add`` checks that the endpoint is ``oci://<host>[:<port>]`` without a- path and that ``--cert`` is a certificate, copies the certificate to+ path, checks ``--index-repository`` and that ``--key-file`` is a+ certificate. It copies the certificate to``/etc/shirudo/bpm/keys/<source>.der`` and writes the source file.- Arguments after the endpoint other than ``--cert`` are ignored.-- ``list`` prints the registered source names under the heading- ``Registered sources:``, or ``none``. With ``--json`` it prints one- object with ``schema`` and ``sources``.+- ``list`` prints the registered source names, one per line.- ``remove`` and ``rm`` delete the source file and its certificate.The key file verifies the signed index of the repository@@ -162,9 +160,8 @@ value as the next argument only.repo update------------``repo update`` fetches the signed index ``shirudo/bpm-index:stable`` of-every registered source, or of ``<source>`` alone, verifies it with the-source's key file and caches it in+``repo update`` fetches each source's configured signed index, or the index+for ``<source>`` alone, verifies it with the source's key file and caches it in``/var/lib/shirudo/bpm/repos/<source>/index.json``. A source that failsdoes not stop the others, and the exit status is then 1.@@ -268,7 +265,8 @@ push(no tag) and prints ``oci://<registry>/<repository>:<version>``. With``--keys-dir``, or ``--index-cert`` and ``--index-key`` together, it alsoadds the package to the registry's signed index-``shirudo/bpm-index:stable``, signed with that key. Without them only the+``<namespace>/bpm-index:stable``, signed with that key. The namespace is+derived from the package repository path. Without signing options only thepackage is published. ``push`` does not read ``SHIRUDO_KEYS_DIR``. Itsoptions take the value as the next argument or after ``=``.diff --git a/Documentation/reference/formats/bpm-formats.rst b/Documentation/reference/formats/bpm-formats.rstindex 9221621d..c5c6310a 100644--- a/Documentation/reference/formats/bpm-formats.rst+++ b/Documentation/reference/formats/bpm-formats.rst@@ -222,7 +222,8 @@ The last path component of ``<repository>`` follows the package name rule.Repository index-----------------One index per registry, at ``<registry>/shirudo/bpm-index:stable``.+One index per registry namespace, at+``<registry>/<namespace>/bpm-index:stable``... list-table:::header-rows: 1diff --git a/Documentation/reference/formats/configuration.rst b/Documentation/reference/formats/configuration.rstindex 42b16a09..611d4c4d 100644--- a/Documentation/reference/formats/configuration.rst+++ b/Documentation/reference/formats/configuration.rst@@ -442,6 +442,11 @@ repository name: 1 to 127 characters from ``A-Z``, ``a-z``, ``0-9``,* - ``endpoint``- Required. ``oci://<host>[:<port>]``, no path, ``?``, ``#`` or``@``.+ * - ``index_repository``+ - Optional. The OCI repository containing the signed index. When+ omitted, it defaults to ``<source>/bpm-index``; set ``<source>``+ to the package namespace to use the same namespace as the package+ repository. The repository must end in ``/bpm-index``.* - ``key_file``- Required. DER certificate that verifies the repository's signedindex. With a config image, ``/etc/shirudo/bpm/keys/<file>``diff --git a/Documentation/start/guides/publish-package.rst b/Documentation/start/guides/publish-package.rstindex fd34577e..ad06df0b 100644--- a/Documentation/start/guides/publish-package.rst+++ b/Documentation/start/guides/publish-package.rst@@ -122,7 +122,7 @@ Publish itThe push tags the package with its version. With ``--keys-dir``, italso adds the package to the registry's signed index,- ``shirudo/bpm-index:stable``, which targets read when they install from+ ``<namespace>/bpm-index:stable``, which targets read when they install froma registered repository. ``BPM_OCI_SCHEME=http`` drops TLS, which suitsonly a registry on your own host. Without it, shirudo speaks HTTPS withTLS 1.3 and a hybrid ML-KEM key exchange.diff --git a/Documentation/upatches/publish/publishing.rst b/Documentation/upatches/publish/publishing.rstindex c6786edb..20dfb395 100644--- a/Documentation/upatches/publish/publishing.rst+++ b/Documentation/upatches/publish/publishing.rst@@ -77,9 +77,11 @@ Push it to a repositoryrepository, without a tag. The command uploads the package as an OCIartifact, attaches the package's certificate to it as a referrer, addsthe version with the package's digests to the index at- ``shirudo/bpm-index:stable`` in the same registry, signs the index- with your key, and tags the package with its version. It prints the- pushed reference, ``oci://<registry>/<namespace>/<name>:<version>``.+ ``<namespace>/bpm-index:stable`` in the same registry, signs the index+ with your key, and tags the package with its version. The index+ namespace is the package repository path without its final name. It+ prints the pushed reference,+ ``oci://<registry>/<namespace>/<name>:<version>``.Without ``--keys-dir``, or ``--index-cert`` and ``--index-key``, thecommand pushes the package and moves the version tag, but leaves thediff --git a/builtin-bpm.c b/builtin-bpm.cindex 812ce7be..825799c4 100644--- a/builtin-bpm.c+++ b/builtin-bpm.c@@ -55,6 +55,7 @@struct bpm_source {char name[BPM_MAX_NAME];char endpoint[BPM_MAX_ENDPOINT];+ char index_repository[BPM_MAX_ENDPOINT];char key_file[BPM_MAX_KEY];};@@ -337,6 +338,25 @@ static DIR *registries_opendir(void)return fdopendir(fd);}+static int source_index_repository_valid(const char *endpoint,+ const char *repository)+{+ char raw[PATH_MAX], parsed[BPM_MAX_ENDPOINT];+ const char *name = strrchr(repository, '/');+ struct bpm_oci_client c = {};+ int rc;++ if (!name || name == repository ||+ strcmp(name + 1, BPM_INDEX_REPOSITORY_NAME))+ return -EINVAL;+ if (strsprintf(raw, sizeof(raw), "%s/%s", endpoint, repository) < 0)+ return -ENAMETOOLONG;+ rc = bpm_oci_parse_push_repo(raw, &c, parsed, sizeof(parsed));+ if (rc)+ return rc;+ return strcmp(parsed, repository) ? -EINVAL : 0;+}+static FILE *tree_conf_open(void){int fd;@@ -376,6 +396,8 @@ static int load_source(const char *name, struct bpm_source *s)ret = got < 0 ? got :bpm_parse_source_line(line,s->endpoint, sizeof(s->endpoint),+ s->index_repository,+ sizeof(s->index_repository),s->key_file, sizeof(s->key_file));}libc_free(line);@@ -384,6 +406,14 @@ static int load_source(const char *name, struct bpm_source *s)return ret;if (!s->endpoint[0] || !s->key_file[0])return -EINVAL;+ if (!s->index_repository[0] &&+ strsprintf(s->index_repository, sizeof(s->index_repository),+ "%s/%s", s->name, BPM_INDEX_REPOSITORY_NAME) < 0)+ return -E2BIG;+ ret = source_index_repository_valid(s->endpoint,+ s->index_repository);+ if (ret)+ return ret;/* With a config image the key file is the image's, named from its root. */if (image_config_active()) {if (!strncmp(s->key_file, "/etc/shirudo/bpm/keys/", 22))@@ -418,8 +448,9 @@ static int save_source(const struct bpm_source *s)if (ret)return ret;- if (strsprintf(data, sizeof(data), "endpoint = %s\nkey_file = %s\n",- s->endpoint, s->key_file) < 0)+ if (strsprintf(data, sizeof(data),+ "endpoint = %s\nindex_repository = %s\nkey_file = %s\n",+ s->endpoint, s->index_repository, s->key_file) < 0)return -E2BIG;return seal_write_file(path, data, strlen(data), 0644);}@@ -608,7 +639,7 @@ static int print_package_upatches(const char *source, bool all)const char *archive = source;char package_dir[101];struct bpm_upatches_ctx ctx = { .all = all };- int ret;+ int ret = 0;ret = bpm_info_archive(source, temporary);if (ret)@@ -1057,7 +1088,7 @@ static int repo_update_one(const char *name)int nl = 0;struct bpm_buffer manifest = {}, index = {}, sig = {};char url[PATH_MAX], cache[PATH_MAX], dir[PATH_MAX];- int ret;+ int ret = 0;ret = load_source(name, &s);if (ret)@@ -1065,18 +1096,59 @@ static int repo_update_one(const char *name)ret = bpm_oci_parse_endpoint(s.endpoint, &c);if (ret)return ret;- ret = bpm_oci_url(&c, BPM_INDEX_REPOSITORY, "manifests", BPM_INDEX_TAG, url, sizeof(url));+ ret = bpm_oci_url(&c, s.index_repository, "manifests", BPM_INDEX_TAG,+ url, sizeof(url));if (ret)return ret;- ret = bpm_oci_get_memory(&c, url, BPM_MANIFEST_MEDIA, &manifest);+ ret = bpm_oci_get_memory(&c, url, "application/vnd.oci.image.manifest.v1+json", &manifest);if (ret)goto out;ret = bpm_oci_parse_manifest(manifest.data, manifest.len, layers,ARRAY_SIZE(layers), &nl, NULL, 0);if (ret)goto out;- ret = bpm_oci_get_signed(&c, BPM_INDEX_REPOSITORY, layers, nl,- BPM_INDEX_MEDIA, BPM_INDEX_SIG_MEDIA, &index, &sig);+ if (nl < 2) {+ ret = -EINVAL;+ goto out;+ }+ for (int i = 0; i < nl; i++) {+ if (!strcmp(layers[i].media, BPM_INDEX_MEDIA)) {+ char layer_hex[65];++ ret = bpm_oci_url(&c, s.index_repository, "blobs",+ layers[i].digest, url, sizeof(url));+ if (ret)+ goto out;+ ret = bpm_oci_get_blob(&c, url, BPM_INDEX_MEDIA, &index);+ if (!ret)+ ret = bpm_sha256_hex(index.data, index.len, layer_hex,+ sizeof(layer_hex));+ if (!ret && !bpm_digest_matches(layers[i].digest, layer_hex))+ ret = -EKEYREJECTED;+ break;+ }+ }+ if (ret)+ goto out;+ for (int i = 0; i < nl; i++) {+ if (!strcmp(layers[i].media, BPM_INDEX_SIG_MEDIA)) {+ char sig_hex[65];++ ret = bpm_oci_url(&c, s.index_repository, "blobs",+ layers[i].digest, url, sizeof(url));+ if (ret)+ goto out;+ ret = bpm_oci_get_blob(&c, url, BPM_INDEX_SIG_MEDIA, &sig);+ if (!ret)+ ret = bpm_sha256_hex(sig.data, sig.len, sig_hex,+ sizeof(sig_hex));+ if (!ret && !bpm_digest_matches(layers[i].digest, sig_hex))+ ret = -EKEYREJECTED;+ break;+ }+ }+ if (!ret && !sig.data)+ ret = -EINVAL;if (ret)goto out;if (verify_with_source_key(&s, index.data, index.len, sig.data,@@ -1487,7 +1559,10 @@ static int cmd_bpm_config(int argc, const char **argv)if (!op || is_help(op)) {fprintf(stderr,- "usage: shirudo bpm config register {add|list|remove} ...\n"+ "usage: shirudo bpm config register add <source> "+ "oci://<registry> --index-repository <repository> "+ "--key-file <certificate>\n"+ " shirudo bpm config register {list|remove} ...\n"" shirudo bpm config update --base-package ""<package.bee|oci://host/repo:tag> --version <version> ""[--file <name>.bee] [options]\n");@@ -1576,16 +1651,38 @@ static int cmd_bpm_config(int argc, const char **argv)return 2;}for (int i = 5; i < argc; i++) {- if (!strncmp(argv[i], "--cert=", 7))+ if (!strncmp(argv[i], "--cert=", 7)) {key = argv[i] + 7;- else if (!strcmp(argv[i], "--cert") && i + 1 < argc)+ } else if (!strcmp(argv[i], "--cert") && i + 1 < argc) {key = argv[++i];+ } else if (!strncmp(argv[i], "--index-repository=", 19)) {+ if (strscpy(s.index_repository,+ argv[i] + 19,+ sizeof(s.index_repository)) < 0)+ return 2;+ } else if (!strcmp(argv[i], "--index-repository") &&+ i + 1 < argc) {+ if (strscpy(s.index_repository,+ argv[++i],+ sizeof(s.index_repository)) < 0)+ return 2;+ } else if (!strcmp(argv[i], "--index-repository")) {+ return 2;+ }}+ if (!s.index_repository[0] &&+ strsprintf(s.index_repository, sizeof(s.index_repository),+ "%s/%s", s.name,+ BPM_INDEX_REPOSITORY_NAME) < 0)+ return 2;if (!key || cert_read_file(key, &cert) ||!bpm_name_ok(s.name) ||- bpm_oci_parse_endpoint(s.endpoint, &probe)) {+ bpm_oci_parse_endpoint(s.endpoint, &(struct bpm_oci_client){}) ||+ source_index_repository_valid(s.endpoint,+ s.index_repository)) {cert_free(&cert);- pr_err("bpm: register add requires a valid oci:// endpoint and --cert certificate");+ pr_err("bpm: register add requires a valid endpoint, index "+ "repository and --key-file certificate");return 2;}cert_free(&cert);diff --git a/testing/Makefile b/testing/Makefileindex a37c8a69..234b0b40 100644--- a/testing/Makefile+++ b/testing/Makefile@@ -56,6 +56,8 @@ TEST_VM_BPM_TREE_CONFIG ?=override TEST_VM_BPM_TREE_CONFIG := $(if $(strip $(TEST_VM_BPM_TREE_CONFIG)),$(call root-path,$(TEST_VM_BPM_TREE_CONFIG)),)TEST_VM_PACKAGE_CERT ?=override TEST_VM_PACKAGE_CERT := $(if $(strip $(TEST_VM_PACKAGE_CERT)),$(call root-path,$(TEST_VM_PACKAGE_CERT)),)+TEST_VM_BPM_OCI_REPOSITORY ?=+TEST_VM_BPM_OCI_INDEX_REPOSITORY ?=TEST_VM_BPM_OCI_INDEX_CERT ?=override TEST_VM_BPM_OCI_INDEX_CERT := $(if $(strip $(TEST_VM_BPM_OCI_INDEX_CERT)),$(call root-path,$(TEST_VM_BPM_OCI_INDEX_CERT)),)TEST_VM_CPUS ?= 4@@ -226,6 +228,7 @@ vm: $(VM_KERNEL_DEP) test-vm-bundleTEST_VM_BPM_TREE_CONFIG="$(TEST_VM_BPM_TREE_CONFIG)" \TEST_VM_PACKAGE_CERT="$(TEST_VM_PACKAGE_CERT)" \TEST_VM_BPM_OCI_REPOSITORY="$(TEST_VM_BPM_OCI_REPOSITORY)" \+ TEST_VM_BPM_OCI_INDEX_REPOSITORY="$(TEST_VM_BPM_OCI_INDEX_REPOSITORY)" \TEST_VM_BPM_OCI_INDEX_CERT="$(TEST_VM_BPM_OCI_INDEX_CERT)" \TEST_VM_MODE="$(TEST_VM_MODE)" TEST_VM_INITRD="$(TEST_VM_INITRD)" \TEST_VM_HOST_BUNDLE="$(abspath $(TEST_VM_BUNDLE_DIR))" \@@ -251,6 +254,7 @@ VM_MODE=test TEST_VM_SUITE="$(1)" VM_VERBOSE="$(TEST_VM_VERBOSE)" VM_NAME="$(2)"TEST_VM_MAP_EXPORT_CA="$(TEST_VM_MAP_EXPORT_CA)" \TEST_VM_OCI_CA="$(TEST_VM_OCI_CA)" \TEST_VM_BPM_OCI_REPOSITORY="$(TEST_VM_BPM_OCI_REPOSITORY)" \+ TEST_VM_BPM_OCI_INDEX_REPOSITORY="$(TEST_VM_BPM_OCI_INDEX_REPOSITORY)" \TEST_VM_BPM_OCI_INDEX_CERT="$(TEST_VM_BPM_OCI_INDEX_CERT)" \TEST_VM_MODE="$(TEST_VM_MODE)" \TEST_VM_HOST_BUNDLE="$(abspath $(TEST_VM_BUNDLE_DIR))" \diff --git a/testing/functional/bpm/t7005-bpm-publish.sh b/testing/functional/bpm/t7005-bpm-publish.shindex 3b5bdcee..067421a4 100644--- a/testing/functional/bpm/t7005-bpm-publish.sh+++ b/testing/functional/bpm/t7005-bpm-publish.sh@@ -140,8 +140,8 @@ test_expect_success 'bpm push publishes the package and a signed repository inde--keys-dir "$WORK/keys" >"$WORK/push.out" 2>"$WORK/push.err" &&grep -qx "oci://$REGISTRY/ns/t7005:1.0.0" "$WORK/push.out" &&test -s "$REG/ns/t7005/manifests/1.0.0" &&- test -s "$REG/shirudo/bpm-index/manifests/stable" &&- split_layers "$REG/shirudo/bpm-index/manifests/stable" "$REG/shirudo/bpm-index" "$WORK/index1" &&+ test -s "$REG/ns/bpm-index/manifests/stable" &&+ split_layers "$REG/ns/bpm-index/manifests/stable" "$REG/ns/bpm-index" "$WORK/index1" &&grep -qx "application/vnd.shirudo.bpm.index.v1" "$WORK/index1.artifact_type" &&cms_verify "$WORK/index1.sig" "$WORK/index1.payload"'@@ -173,7 +173,7 @@ PYtest_expect_success 'pushing the same package again keeps a single index entry' '"$S" bpm push "$WORK/t7005-1.0.0.bee" "oci://$REGISTRY/ns/t7005" \--keys-dir "$WORK/keys" >/dev/null 2>&1 &&- split_layers "$REG/shirudo/bpm-index/manifests/stable" "$REG/shirudo/bpm-index" "$WORK/index2" &&+ split_layers "$REG/ns/bpm-index/manifests/stable" "$REG/ns/bpm-index" "$WORK/index2" &&cms_verify "$WORK/index2.sig" "$WORK/index2.payload" &&python3 -c "import json, sys@@ -187,7 +187,7 @@ test_expect_success 'a second version is added to the index in sorted order' '"$S" bpm push "$WORK/t7005-0.9.0.bee" "oci://$REGISTRY/ns/t7005" \--index-cert "$WORK/keys/cert.der" --index-key "$WORK/keys/signing_key.pem" \>/dev/null 2>&1 &&- split_layers "$REG/shirudo/bpm-index/manifests/stable" "$REG/shirudo/bpm-index" "$WORK/index3" &&+ split_layers "$REG/ns/bpm-index/manifests/stable" "$REG/ns/bpm-index" "$WORK/index3" &&cms_verify "$WORK/index3.sig" "$WORK/index3.payload" &&python3 -c "import json, sys@@ -200,7 +200,7 @@ test_expect_success 'push without signing material leaves the index untouched' '( cd "$WORK" && "$S" bpm build pkg --version 2.0.0 --keys-dir "$WORK/keys" ) >/dev/null 2>&1 &&"$S" bpm push "$WORK/t7005-2.0.0.bee" "oci://$REGISTRY/ns/t7005" >/dev/null 2>&1 &&test -s "$REG/ns/t7005/manifests/2.0.0" &&- split_layers "$REG/shirudo/bpm-index/manifests/stable" "$REG/shirudo/bpm-index" "$WORK/index4" &&+ split_layers "$REG/ns/bpm-index/manifests/stable" "$REG/ns/bpm-index" "$WORK/index4" &&cmp -s "$WORK/index3.payload" "$WORK/index4.payload"'@@ -305,7 +305,7 @@ PYtest_expect_success 'the operator commands are reachable directly in shirudo-build' '"$B" bpm --help | grep -q "tree append <package-file> <repository>" &&"$B" bpm tree append --help 2>&1 | grep -q "\-\-op add|delete" &&- "$B" bpm push --help 2>&1 | grep -q "shirudo/bpm-index:stable" &&+ "$B" bpm push --help 2>&1 | grep -q "<namespace>/bpm-index:stable" &&! "$B" bpm not-a-command >/dev/null 2>&1'diff --git a/testing/functional/image/t5004-image-cli.sh b/testing/functional/image/t5004-image-cli.shindex 30251e84..5fec0263 100755--- a/testing/functional/image/t5004-image-cli.sh+++ b/testing/functional/image/t5004-image-cli.sh@@ -21,7 +21,8 @@ printf "host-key\n" >"$WORK/host.der"FP=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdefsource_file() {- printf "endpoint = oci://example.invalid\nkey_file = %s\n" "$2" >"$REG/$1.conf"+ printf "endpoint = oci://example.invalid\nindex_repository = example/bpm-index\nkey_file = %s\n" \+ "$2" >"$REG/$1.conf"}refused() {diff --git a/testing/fuzz/fuzz-config.c b/testing/fuzz/fuzz-config.cindex 6ce17a9f..449db622 100644--- a/testing/fuzz/fuzz-config.c+++ b/testing/fuzz/fuzz-config.c@@ -14,8 +14,9 @@int LLVMFuzzerTestOneInput(const __u8 *data, size_t size){- char endpoint[BPM_MAX_ENDPOINT], reference[BPM_MAX_DIGEST];char line[PATH_MAX], copy[PATH_MAX], key_file[BPM_MAX_KEY];+ char endpoint[BPM_MAX_ENDPOINT], reference[BPM_MAX_DIGEST];+ char index_repository[BPM_MAX_ENDPOINT] = {};struct config cfg;char path[64];FILE *fp;@@ -30,7 +31,9 @@ int LLVMFuzzerTestOneInput(const __u8 *data, size_t size)while (fp && fgets(line, sizeof(line), fp)) {memcpy(copy, line, sizeof(line));bpm_parse_source_line(copy, endpoint, sizeof(endpoint),- key_file, sizeof(key_file));+ index_repository,+ sizeof(index_repository), key_file,+ sizeof(key_file));bpm_parse_tree_line(line, endpoint, sizeof(endpoint),reference, sizeof(reference));}diff --git a/testing/helper/test-bpm-source.c b/testing/helper/test-bpm-source.cindex e0196a3d..90b3d729 100644--- a/testing/helper/test-bpm-source.c+++ b/testing/helper/test-bpm-source.c@@ -10,32 +10,43 @@#include <stdio.h>#include <string.h>+#include <shirudo/string.h>+#include "util/bpm_source_config.h"int main(void){char tree_endpoint_line[] = " endpoint=\"oci://registry.test/tree\"\n";char key_file_line[] = "key_file = /etc/shirudo/bpm/keys/jbrust.pem\n";+ char index_repository_line[] = "index_repository = team/bpm-index\n";char endpoint_line[] = "endpoint = oci://registry.test:5443\n";char unbalanced_line[] = "reference = \"sha256:feed\n";char reference_line[] = "reference = sha256:feed\r\n";char key_file[256] = {};+ char index_repository[512] = {};char reference[80] = {};char endpoint[512] = {};char long_line[512];if (bpm_parse_source_line(endpoint_line, endpoint,- sizeof(endpoint), key_file,+ sizeof(endpoint), index_repository,+ sizeof(index_repository), key_file,+ sizeof(key_file)) ||+ bpm_parse_source_line(index_repository_line, endpoint,+ sizeof(endpoint), index_repository,+ sizeof(index_repository), key_file,sizeof(key_file)) ||- bpm_parse_source_line(key_file_line, endpoint,- sizeof(endpoint), key_file,+ bpm_parse_source_line(key_file_line, endpoint, sizeof(endpoint),+ index_repository, sizeof(index_repository), key_file,sizeof(key_file)))return 1;if (strcmp(endpoint, "oci://registry.test:5443") ||+ strcmp(index_repository, "team/bpm-index") ||strcmp(key_file, "/etc/shirudo/bpm/keys/jbrust.pem"))return 1;- printf("endpoint=%s\nkey_file=%s\n", endpoint, key_file);+ printf("endpoint=%s\nindex_repository=%s\nkey_file=%s\n",+ endpoint, index_repository, key_file);if (bpm_parse_tree_line(tree_endpoint_line, endpoint,sizeof(endpoint), reference,@@ -49,15 +60,19 @@ int main(void)return 1;printf("tree endpoint=%s\ntree reference=%s\n", endpoint, reference);/* A value that does not fit is refused, not cut. */- snprintf(long_line, sizeof(long_line), "reference = sha256:%0*d\n",- (int)sizeof(reference), 0);+ if (strsprintf(long_line, sizeof(long_line),+ "reference = sha256:%0*d\n", (int)sizeof(reference), 0) < 0)+ return 1;if (bpm_parse_tree_line(long_line, endpoint, sizeof(endpoint),reference, sizeof(reference)) != -E2BIG)return 1;- snprintf(long_line, sizeof(long_line), "key_file = /%0*d\n",- (int)sizeof(key_file), 0);+ if (strsprintf(long_line, sizeof(long_line), "key_file = /%0*d\n",+ (int)sizeof(key_file), 0) < 0)+ return 1;if (bpm_parse_source_line(long_line, endpoint, sizeof(endpoint),- key_file, sizeof(key_file)) != -E2BIG)+ index_repository,+ sizeof(index_repository), key_file,+ sizeof(key_file)) != -E2BIG)return 1;return 0;}diff --git a/testing/vm/run.sh b/testing/vm/run.shindex aac2e449..ccd7872c 100755--- a/testing/vm/run.sh+++ b/testing/vm/run.sh@@ -98,6 +98,7 @@ OCI_CA="${TEST_VM_OCI_CA:-}"BPM_TREE_CONFIG="${TEST_VM_BPM_TREE_CONFIG:-}"PACKAGE_CERT="${TEST_VM_PACKAGE_CERT:-}"BPM_OCI_REPOSITORY_RAW="${TEST_VM_BPM_OCI_REPOSITORY:-}"+BPM_OCI_INDEX_REPOSITORY="${TEST_VM_BPM_OCI_INDEX_REPOSITORY:-}"BPM_OCI_INDEX_CERT="${TEST_VM_BPM_OCI_INDEX_CERT:-}"BPM_OCI_SOURCE="default"BPM_OCI_ENDPOINT=""@@ -304,6 +305,10 @@ validate_bpm_oci()die "TEST_VM_BPM_OCI_REPOSITORY is too long"[[ -f "$BPM_OCI_INDEX_CERT" && -r "$BPM_OCI_INDEX_CERT" && -s "$BPM_OCI_INDEX_CERT" ]] ||die "TEST_VM_BPM_OCI_INDEX_CERT is not a readable, non-empty regular file: $BPM_OCI_INDEX_CERT"+ [[ -n "$BPM_OCI_INDEX_REPOSITORY" ]] ||+ die "TEST_VM_BPM_OCI_INDEX_REPOSITORY must be set"+ [[ "$BPM_OCI_INDEX_REPOSITORY" =~ ^[A-Za-z0-9._-]+(/[A-Za-z0-9._-]+)*/bpm-index$ ]] ||+ die "TEST_VM_BPM_OCI_INDEX_REPOSITORY must end in /bpm-index"}# The tool that builds the cloud-init seed: cloud-localds, or an ISO 9660@@ -1338,6 +1343,7 @@ if [[ -n "$BPM_OCI_ENDPOINT" ]]; then"$BPM_OCI_DIR/keys/${BPM_OCI_SOURCE}.pem"{printf '%s\n' "endpoint = $BPM_OCI_ENDPOINT" \+ "index_repository = $BPM_OCI_INDEX_REPOSITORY" \"key_file = /etc/shirudo/bpm/keys/${BPM_OCI_SOURCE}.pem"} >"$BPM_OCI_DIR/registries.d/${BPM_OCI_SOURCE}.conf"chmod 0644 "$BPM_OCI_DIR/registries.d/${BPM_OCI_SOURCE}.conf"diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 5b54cd86..7933d72c 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -100,17 +100,32 @@ static int index_entry_compare(const void *left, const void *right)return ret ? ret : strcmp(a->version, b->version);}+static int index_repository_for_package(const char *repository, char *out, size_t out_sz)+{+ const char *name = strrchr(repository, '/');+ size_t namespace_len;++ if (!name || name == repository)+ return -EINVAL;+ namespace_len = (size_t)(name - repository);+ if (strsprintf(out, out_sz, "%.*s/%s", (int)namespace_len, repository,+ BPM_INDEX_REPOSITORY_NAME) < 0)+ return -ENAMETOOLONG;+ return 0;+}+/* A registry without an index yields an empty one. */-static int fetch_index(struct bpm_oci_client *c, const char *cert,+static int fetch_index(struct bpm_oci_client *c, const char *repository,+ const char *cert,struct bpm_index_entry *entries, int *n){struct bpm_buffer manifest = {}, payload = {}, sig = {};struct bpm_descriptor *layers = NULL;- char url[PATH_MAX];- int nl = 0, ret;+ char url[PATH_MAX], hex[65];+ int nl = 0, ret = 0;*n = 0;- ret = bpm_oci_url(c, BPM_INDEX_REPOSITORY, "manifests", BPM_INDEX_TAG,+ ret = bpm_oci_url(c, repository, "manifests", BPM_INDEX_TAG,url, sizeof(url));if (ret)return ret;@@ -128,11 +143,37 @@ static int fetch_index(struct bpm_oci_client *c, const char *cert,BPM_MAX_ENTRIES, &nl, NULL, 0);if (ret)goto out;- ret = bpm_oci_get_signed(c, BPM_INDEX_REPOSITORY, layers, nl,- BPM_INDEX_MEDIA, BPM_INDEX_SIG_MEDIA, &payload,- &sig);- if (ret)+ for (int i = 0; i < nl; i++) {+ struct bpm_buffer *dst;++ if (!strcmp(layers[i].media, BPM_INDEX_MEDIA))+ dst = &payload;+ else if (!strcmp(layers[i].media, BPM_INDEX_SIG_MEDIA))+ dst = &sig;+ else+ continue;+ if (dst->data) {+ ret = -EINVAL;+ goto out;+ }+ ret = bpm_oci_url(c, repository, "blobs", layers[i].digest,+ url, sizeof(url));+ if (ret)+ goto out;+ ret = bpm_oci_get_blob(c, url, layers[i].media, dst);+ if (ret)+ goto out;+ if ((layers[i].size >= 0 && (__s64)dst->len != layers[i].size) ||+ bpm_sha256_hex(dst->data, dst->len, hex, sizeof(hex)) ||+ !bpm_digest_matches(layers[i].digest, hex)) {+ ret = -EKEYREJECTED;+ goto out;+ }+ }+ if (!payload.data || !sig.data) {+ ret = -EINVAL;goto out;+ }if (bpm_verify_cms(payload.data, payload.len, sig.data, sig.len, cert)) {pr_err("bpm push: the published repository index is not signed by %s",cert);@@ -149,12 +190,12 @@ out:}/*- * Add or refresh the package's entry in the registry's signed index at- * shirudo/bpm-index:stable, the document `bpm repo update` consumes. A- * version already listed with different content is refused: published- * versions are immutable.+ * Add or refresh the package's entry in the signed index beside its+ * repository namespace. A version already listed with different content+ * is refused: published versions are immutable.*/-static int push_index(struct bpm_oci_client *c, const char *repository,+static int push_index(struct bpm_oci_client *c, const char *index_repository,+ const char *repository,const struct bpm_package_info *info,const char *manifest_digest,const struct bpm_descriptor *layer,@@ -169,12 +210,12 @@ static int push_index(struct bpm_oci_client *c, const char *repository,size_t cap = 0, sig_len = 0;time_t now = time(NULL);__u8 *sig = NULL;- int n = 0, ret;+ int n = 0, ret = 0;entries = xcalloc(BPM_MAX_ENTRIES, sizeof(*entries));if (!entries)return -ENOMEM;- ret = fetch_index(c, cert, entries, &n);+ ret = fetch_index(c, index_repository, cert, entries, &n);if (ret)goto out;for (int i = 0; i < n; i++) {@@ -184,7 +225,7 @@ static int push_index(struct bpm_oci_client *c, const char *repository,if (strcmp(entries[i].manifest_digest, manifest_digest) ||strcmp(entries[i].package_digest, layer->digest)) {pr_err("bpm push: the repository index already lists %s %s "- "with different content", info->name, info->version);+ "with different content", info->name, info->version);ret = -EEXIST;goto out;}@@ -243,16 +284,16 @@ static int push_index(struct bpm_oci_client *c, const char *repository,ret = sign_detached(body, e.len, key, cert, &sig, &sig_len);if (ret)goto out;- ret = bpm_oci_push_blob(c, BPM_INDEX_REPOSITORY, empty_config,+ ret = bpm_oci_push_blob(c, index_repository, empty_config,strlen(empty_config), BPM_EMPTY_CONFIG_MEDIA,- &config);+ &config);if (ret)goto out;- ret = bpm_oci_push_blob(c, BPM_INDEX_REPOSITORY, body, e.len,+ ret = bpm_oci_push_blob(c, index_repository, body, e.len,BPM_INDEX_MEDIA, &payload_layer);if (ret)goto out;- ret = bpm_oci_push_blob(c, BPM_INDEX_REPOSITORY, sig, sig_len,+ ret = bpm_oci_push_blob(c, index_repository, sig, sig_len,BPM_INDEX_SIG_MEDIA, &sig_layer);if (ret)goto out;@@ -276,9 +317,9 @@ static int push_index(struct bpm_oci_client *c, const char *repository,ret = e.err;goto out;}- ret = bpm_oci_put_manifest(c, BPM_INDEX_REPOSITORY, BPM_INDEX_TAG,+ ret = bpm_oci_put_manifest(c, index_repository, BPM_INDEX_TAG,(const __u8 *)manifest, e.len, digest,- sizeof(digest));+ sizeof(digest));out:xfree(manifest, BPM_MANIFEST_CAP);sig_free(sig);@@ -298,6 +339,7 @@ static int push_package(const char *package_path, const char *raw_repository,__u8 *archive = NULL, *manifest = NULL;const char *base;char repository[BPM_MAX_ENDPOINT], package_digest[72], key_digest[72];+ char index_repository[BPM_MAX_ENDPOINT] = {};char hex[65];char reference[PATH_MAX];char config_body[] = "{\"mediaType\":\"" BPM_CONFIG_MEDIA "\"}";@@ -307,6 +349,7 @@ static int push_package(const char *package_path, const char *raw_repository,char *key_manifest_body = NULL;struct stat st;size_t archive_len, manifest_len, key_manifest_len;+ size_t index_repo_sz = sizeof(index_repository);int ret;if (!package_path || !*package_path)@@ -326,6 +369,11 @@ static int push_package(const char *package_path, const char *raw_repository,sizeof(repository));if (ret)goto out;+ if (index_key && index_cert) {+ ret = index_repository_for_package(repository, index_repository, index_repo_sz);+ if (ret)+ goto out;+ }base = strrchr(package_path, '/');base = base ? base + 1 : package_path;if (!*base || strlen(base) >= sizeof(package_title)) {@@ -441,8 +489,8 @@ static int push_package(const char *package_path, const char *raw_repository,if (ret)goto out;if (index_key && index_cert) {- ret = push_index(&c, repository, &info, package_digest, &layer,- index_key, index_cert);+ ret = push_index(&c, index_repository, repository, &info,+ package_digest, &layer, index_key, index_cert);if (ret)goto out;}@@ -847,7 +895,7 @@ static void bpm_push_usage(void)" --index-key <signing_key.pem>]\n\n""The repository must start with oci:// and must not include a tag.\n""With signing material the registry's signed index at\n"- "shirudo/bpm-index:stable gains the package entry, signed with the\n"+ "<namespace>/bpm-index:stable gains the package entry, signed with the\n""operator key; without it only the package is published.\n""Examples:\n"" shirudo bpm push ./package.bee oci://quay.io/namespace/package\n"diff --git a/util/bpm_oci.h b/util/bpm_oci.hindex 956142e2..1323ce77 100644--- a/util/bpm_oci.h+++ b/util/bpm_oci.h@@ -39,7 +39,7 @@ struct json_emit;(BPM_OCI_BASIC_ENCODED_SIZE + sizeof("Basic "))#define BPM_OCI_BASIC_HEADER_SIZE \(BPM_OCI_AUTH_VALUE_SIZE + sizeof("Authorization: ") - 1u)-#define BPM_INDEX_REPOSITORY "shirudo/bpm-index"+#define BPM_INDEX_REPOSITORY_NAME "bpm-index"#define BPM_INDEX_TAG "stable"#define BPM_INDEX_MEDIA "application/vnd.shirudo.bpm.index.v1+json"#define BPM_INDEX_SIG_MEDIA "application/vnd.shirudo.bpm.index.signature.v1"diff --git a/util/bpm_source_config.c b/util/bpm_source_config.cindex 66287a3c..5dda4474 100644--- a/util/bpm_source_config.c+++ b/util/bpm_source_config.c@@ -14,11 +14,13 @@int bpm_parse_source_line(char *line,char *endpoint, size_t endpoint_sz,+ char *index_repository, size_t index_repository_sz,char *key_file, size_t key_file_sz){char *eq, *key, *key_end, *val;- if (!line || !endpoint || !endpoint_sz || !key_file || !key_file_sz)+ if (!line || !endpoint || !endpoint_sz || !index_repository ||+ !index_repository_sz || !key_file || !key_file_sz)return -EINVAL;key = line;@@ -39,6 +41,9 @@ int bpm_parse_source_line(char *line,if (!strcmp(key, "endpoint") && strscpy(endpoint, val, endpoint_sz) < 0)return -E2BIG;+ if (!strcmp(key, "index_repository") &&+ strscpy(index_repository, val, index_repository_sz) < 0)+ return -E2BIG;if (!strcmp(key, "key_file") && strscpy(key_file, val, key_file_sz) < 0)return -E2BIG;return 0;diff --git a/util/bpm_source_config.h b/util/bpm_source_config.hindex 1bc0352f..1cd3fbf8 100644--- a/util/bpm_source_config.h+++ b/util/bpm_source_config.h@@ -10,6 +10,7 @@int bpm_parse_source_line(char *line,char *endpoint, size_t endpoint_sz,+ char *index_repository, size_t index_repository_sz,char *key_file, size_t key_file_sz);/* A line of upatch-tree.conf: endpoint and reference, either quoted. */int bpm_parse_tree_line(char *line,