Skip to content

feat(restore): encode source registry hosts with ports for --preserve-source-registry #2398

Done

Description

@TerryHowe

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>/app and 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.NewReference already 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-registry flag 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-run output.
  • Unit tests for restoreTargets: port, uppercase host, IPv6 literal, over-long name, and a localhost:5000/app + registry.example.com/app backup 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.

Refs #2358, #2364, #2376

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, so registry.lan_5000 and localhost_5000 are valid path components.
  • The 255-character limit is not enforced. properties.NewReference / ValidateRepository only match the pattern and never check length, so restoreTargets must add the check. The limit applies to the repository part (<namespace>/<encoded-host>/<repo>), not the target registry host.
  • --dry-run needs no code change. Dry-run already prints dstRef, which is built from the restoreTargets result, 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.com and registry.example.com both encode to the same component, and so would a non-DNS host such as a_5000 and a: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/app with the flag fails the repository check. After this change it restores to registry.example.com/app.

Suggested approach

  1. Add an encodeRegistryComponent(host string) (string, error) helper. It rejects a host that starts with [ (IPv6 literal) with a recommendation, and otherwise returns strings.ReplaceAll(strings.ToLower(host), ":", "_").
  2. In restoreTargets, replace the port rejection with target + "/" + encoded + "/" + ref.Repository. Keep the original srcRepo in collision messages so users see the real source names.
  3. Add the 255-character check next to the existing NewReference(dstRepo) validation.
  4. Update the flag help and the long-help example, for example localhost:5000/app → <target>/localhost_5000/app.
  5. Tests:
    • Extend the Test_restoreTargets table 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-run check on the printed target.

Related code

Activity

  1. added
    enhancement
    experimental
    on Nov 27, 2026
  2. Orion777-cmd commented on Nov 27, 2026

    @Orion777-cmd
    Contributor

    I'd like to work on this, since it builds on #2376. I'll follow the suggested approach (an encodeRegistryComponent helper, the 255-character check, and the tests listed in the scope) and open a PR shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementexperimental

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions