Repository navigation
Description
What
Make oras restore --preserve-source-registry work for source registries that have a port, and turn every source host into a valid repository path component. This brings the code merged in #2376 in line with the proposal in #2358 ("Keeping the Source Registry in the Target Path").
Today restoreTargets (cmd/oras/root/restore.go) rejects any source registry containing ::
source registry "localhost:5000" of "localhost:5000/app:v1" in the backup has a port and cannot be a repository path component
Why
A backup that holds localhost:5000/app and registry.example.com/app cannot be restored to one registry at all:
- Without
--preserve-source-registry, both map to<target>/appand fail the collision check. - With it, the port is rejected.
--rules(Mode 3) is not implemented yet.
Registries with ports are common in air-gapped and test setups, which are the main users of multi-registry backups.
Encoding
Build the host component as follows:
| Source host | Path component |
|---|---|
docker.io |
docker.io |
Registry.Example.com |
registry.example.com (lowercased: host names are case-insensitive, repository names must be lowercase) |
registry.lan:5000 |
registry.lan_5000 (: replaced by _) |
[::1]:5000 |
rejected before pushing: IPv6 literals cannot be encoded; use a rules file (Mode 3) once available |
Host names cannot contain _, so the encoding cannot make two different hosts collide.
Scope
- In
restoreTargets, encode the source host as above instead of rejecting hosts with a port. - Reject IPv6 literal hosts with a clear error and recommendation.
- Reject a target repository longer than 255 characters (namespace + encoded host + path) before anything is pushed. Check whether
properties.NewReferencealready enforces this; if not, add the check. - Keep the existing guarantee that every target is validated before anything is pushed, so no partial restore is left behind.
- Update the
--preserve-source-registryflag help, which currently says "source registries with a port are not supported", and the example in the long help. - Show the encoded targets in
--dry-runoutput. - Unit tests for
restoreTargets: port, uppercase host, IPv6 literal, over-long name, and alocalhost:5000/app+registry.example.com/appbackup that collides without the flag and restores with it. - E2E: back up from the localhost test registry (which has a port) and restore with
--preserve-source-registry.
Evaluation
Effort: Small to Medium. The code change is about 20 lines in restoreTargets. Most of the work is the help text, unit tests and one e2e case. No public API or oras-go change is needed.
Findings
- The encoding passes validation. The oras-go v3 repository pattern allows a single
_between alphanumerics, soregistry.lan_5000andlocalhost_5000are valid path components. - The 255-character limit is not enforced.
properties.NewReference/ValidateRepositoryonly match the pattern and never check length, sorestoreTargetsmust add the check. The limit applies to the repository part (<namespace>/<encoded-host>/<repo>), not the target registry host. --dry-runneeds no code change. Dry-run already printsdstRef, which is built from therestoreTargetsresult, so it will show the encoded targets once the encoding is in place. Only a test is needed.- Collisions after encoding are already handled.
Registry.example.comandregistry.example.comboth encode to the same component, and so would a non-DNS host such asa_5000anda:5000. The existing collision check catches both as long as it runs after encoding, which keeps the no-partial-restore guarantee. - Uppercase hosts change behaviour slightly. Today
Registry.Example.com/appwith the flag fails the repository check. After this change it restores toregistry.example.com/app.
Suggested approach
- Add an
encodeRegistryComponent(host string) (string, error)helper. It rejects a host that starts with[(IPv6 literal) with a recommendation, and otherwise returnsstrings.ReplaceAll(strings.ToLower(host), ":", "_"). - In
restoreTargets, replace the port rejection withtarget + "/" + encoded + "/" + ref.Repository. Keep the originalsrcRepoin collision messages so users see the real source names. - Add the 255-character check next to the existing
NewReference(dstRepo)validation. - Update the flag help and the long-help example, for example
localhost:5000/app→<target>/localhost_5000/app. - Tests:
- Extend the
Test_restoreTargetstable with the cases in the Scope list above. - Rewrite
Test_restoreTargets_preserveSourceRegistryWithPort, which currently asserts the rejection, to assert the encoding instead. - In e2e, back up from
ZOTHost(localhost:7000) and assert the restore lands at<ns>/localhost_7000/..., including a--dry-runcheck on the printed target.
- Extend the
Related code
restoreTargets: the port rejection is at L331-L338, the target validation at L340 and the collision check at L343-L358- Flag help and long help / example
- Dry-run output
Test_restoreTargetsand the port-rejection test- E2E
--preserve-source-registrycase
Activity
Orion777-cmd commentedon Nov 27, 2026
I'd like to work on this, since it builds on #2376. I'll follow the suggested approach (an
encodeRegistryComponenthelper, the 255-character check, and the tests listed in the scope) and open a PR shortly.