Skip to content

fix(snapshot): reach the published endpoint on the kubeconfig port, refuse a foreign output directory - #523

Closed
duckhawk wants to merge 4 commits into
mainfrom
fix/snapshot-public-url-port
Closed

duckhawk wants to merge 4 commits into
mainfrom
fix/snapshot-public-url-port

Conversation

@duckhawk

@duckhawk duckhawk commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Description

Two fixes for d8 snapshot download and the other commands that move volume data through the published endpoint.

Port of the published endpoint (commits reach the published data endpoint on the kubeconfig port, explain a rewritten published endpoint port)

  • New shared helper dataio.PublicEndpoint / dataio.ResolvePublicURL. When status.publicURL has no port and names the same host as the kubeconfig server, which uses a non-default port, the client reaches the published endpoint on the kubeconfig port. Otherwise the URL is used as is.
  • New flag --publish-port sets the port explicitly. It overrides both the kubeconfig port and a port carried by status.publicURL. --publish-port 443 keeps a port-less status.publicURL on the default HTTPS port, which is the opt-out named in the command help.
  • When the address differs from status.publicURL, the resolved address is logged. A 404 from the exporter on the snapshot download path carries a hint to pass --publish-port. A 404 is classified as dataplane.ErrExportNotFound; the error message itself does not change.
  • A --publish-port that has no effect, because the in-cluster endpoint is used, produces a warning.
  • Applied to d8 snapshot download, d8 snapshot upload, d8 data export download, d8 data export list and d8 data import upload (including the Windows build). The deprecated d8 data list/download aliases do not register the flag and keep the automatic behaviour.
  • The address is resolved before the origin-bound HTTP clients are built, so their origin check matches the port that is actually used. buildSubClients now takes the resolved base URL; the tests reach it through a thin wrapper.
  • SafeClient keeps the port in Copy(), the same way as --data-insecure-skip-tls-verify.

Output directory check (commits refuse an output directory a download did not write, tighten the output directory check)

  • d8 snapshot download refuses an output directory that holds an entry a download never writes into an archive root. Allowed:

    • the directories manifests/, snapshots/, data/, data.tar.d/ and data.bin.d/;
    • the files snapshot.yaml, identity.json, data.tar, data.bin*, the publication state files and the lock file;
    • the .tmp form, but only for the files written atomically.

    The entry type is checked as well: a plain file named manifests is refused.

  • lost+found, .DS_Store, Thumbs.db and desktop.ini are accepted, so a freshly formatted mount, or a directory opened in a file manager, can still be used or resumed. The resume-time *.tmp sweep skips lost+found.

  • The check runs before the lock file is created, so nothing is written into a foreign directory, and again through the locked destination once the lock is held.

  • A missing or empty directory, or one an earlier download wrote, is accepted as before, so resume keeps working.

Why do we need it, and what problem does it solve?

storage-foundation builds status.publicURL as https://api.<publicDomain>/<path>/ without a port. Some installations put an external balancer in front of the cluster ingress and publish api.<publicDomain> only on port 6443: api.<publicDomain>:6443 goes through the balancer to ingress-nginx on 443. kubectl works because the kubeconfig carries the port. With --publish=true, d8 goes to port 443, the balancer answers 404 Not Found, and the download fails with prepare filesystem inventory: ... unexpected status 404. The only workaround was to run d8 from a cluster node, where --publish=false is picked. The cluster itself does not know the external port, so the kubeconfig is the only place d8 can learn it from. A platform-wide setting for the external API address is planned separately; this change makes the CLI usable until then.

The same heuristic misfires when api.<publicDomain>:6443 is kube-apiserver itself and ingress-nginx serves that name on 443. The log line, the 404 hint and the documented --publish-port 443 make that case visible and fixable without reading the code.

The output directory issue showed up on the same installation. d8 snapshot download -o ./ and -o /tmp/ failed with scan output directory: ... open archive path /home/<user>/<dir>/logs/...: permission denied. The resume scan treats the output directory as the root of the archive, walks it recursively and removes every *.tmp file it finds. On a home directory or /tmp it walks unrelated trees: it failed only because it hit an unreadable directory first, and otherwise it could have deleted unrelated *.tmp files.

What is the expected result?

  • kubeconfig port. With a kubeconfig server https://api.example.com:6443 and status.publicURL https://api.example.com/ns/snap/vs/:
    • the data requests go to https://api.example.com:6443/ns/snap/vs/...;
    • the log shows status_public_url and the resolved url.
  • Explicit port. With --publish-port 7443 the requests go to port 7443. With --publish-port 443 they go to status.publicURL unchanged.
  • Unchanged setups. With a kubeconfig server on another host, on port 443 or without a port, the requests go to status.publicURL unchanged, so existing setups behave as before.
  • 404 after a rewrite. If the rewritten address answers 404, the snapshot download error ends with the request went to <url> while status.publicURL is <url>; pass --publish-port ....
  • Foreign output directory. d8 snapshot download -o ~ fails right away with output directory holds an entry a snapshot download did not write: /home/<user> contains "..."; choose a new or empty --output directory. No lock file is left there, and nothing is walked or removed.
  • Resume. A rerun into the directory of an interrupted download still resumes, also with .DS_Store added by a file manager.

Tests:

  • PublicEndpoint.Resolve table tests cover:
    • the same host, case-insensitive matching, IPv6;
    • an explicit port, including 443 as the opt-out;
    • another host, the default port, a URL with its own port;
    • a kubeconfig server with a path, http URLs.
  • Flag tests cover --publish-port parsing, the log line of ResolvePublicURL and the unused-port warning.
  • OpenExport runs against an httptest TLS server, with status.publicURL pointing at port 443 of the server host:
    • the export is reached through the kubeconfig port and through WithPublishPort;
    • a 404 from the rewritten address carries the hint.
  • dataplane: a 404 is ErrExportNotFound with an unchanged message, and the hint is appended only when set.
  • uploadEndpoint for the snapshot importer, including that the in-cluster URL is never rewritten.
  • CheckOutputRoot is tested on the archive layout, on metadata names and on foreign entries (.tmp forms of directory names, wrong entry types). The full-tree pipeline test checks that every downloaded node directory passes it.
  • The *.tmp sweep keeps files under lost+found, both for a node directory and for a rooted output directory.
  • checkOutputDir is tested on a missing, empty, earlier-download, foreign and unreadable directory. A command-level test checks that a refused directory gets no lock file.

Checklist

  • The code is covered by unit tests.
  • e2e tests passed.
  • Documentation updated according to the changes. (command help and examples)
  • Changes were tested in the Kubernetes cluster manually.

storage-foundation builds status.publicURL as https://api.<publicDomain>/<path>/
without a port. When the cluster ingress sits behind an external balancer that
publishes api.<publicDomain> on another port (6443, for example), kubectl works
through the kubeconfig, while d8 snapshot download/upload and d8 data
export/import with --publish go to port 443 and get a 404 from the balancer.

When status.publicURL has no port and names the same host as the kubeconfig
server, d8 now uses the kubeconfig server port. --publish-port sets the port
explicitly. The address is resolved before the origin-bound HTTP clients are
built, so they are pinned to the port actually used.

Signed-off-by: v.oleynikov <vasily.oleynikov@flant.com>
Resuming a download walks the output root and deletes every *.tmp file under
it. With -o pointing at an existing directory such as the home directory or
/tmp, d8 snapshot download descended into unrelated subdirectories, failed on
the first unreadable one with "permission denied", and could otherwise have
removed unrelated *.tmp files.

The output directory is now refused when it holds an entry a download never
writes into an archive root. It is checked before the lock file is created and
again once the lock is held. A missing or empty directory, or one an earlier
download wrote, is accepted as before.

Signed-off-by: v.oleynikov <vasily.oleynikov@flant.com>
@duckhawk
duckhawk requested a review from ldmonster as a code owner December 3, 2026 16:07
When the published endpoint is reached on a port taken from the kubeconfig
or --publish-port, the resolved address is now logged, and a 404 from it
carries a hint to pass --publish-port (443 for the default HTTPS port). This
covers a setup where api.<publicDomain>:6443 is kube-apiserver itself rather
than a balancer in front of the ingress. The help of the commands names
--publish-port 443 as the opt-out.

A --publish-port that has no effect because the in-cluster endpoint is used
now produces a warning. An explicit port equal to the default HTTPS port no
longer adds ":443" to a status.publicURL without a port.

Resolving the address is shared by the snapshot and data commands, and a 404
from the exporter is classified as dataplane.ErrExportNotFound without
changing the error message.

Signed-off-by: v.oleynikov <vasily.oleynikov@flant.com>
The output root check now accepts the .tmp form only for the files a download
writes atomically (snapshot.yaml, identity.json, data.tar, data.bin* and the
publication state files), and checks that directories and files are what the
archive layout expects, so a foreign manifests.tmp or a plain file named
manifests is refused instead of being swept or walked.

lost+found, .DS_Store, Thumbs.db and desktop.ini are accepted, so a freshly
formatted mount or a directory opened in a file manager can still be used or
resumed. The resume-time *.tmp sweep skips lost+found.

Signed-off-by: v.oleynikov <vasily.oleynikov@flant.com>
@duckhawk

duckhawk commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Moved to the canonical repository (MR !537). Closing.

@duckhawk duckhawk closed this Dec 3, 2026
@duckhawk
duckhawk deleted the fix/snapshot-public-url-port branch December 3, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant