Repository navigation
feat: send registry requests to a mirror with URL replacement rules - #577
Conversation
A client can be given ordered prefix rules that send requests for a URL prefix to another URL. The user's config.toml carries them in network.url_replacements, and WithURLReplacements sets them explicitly. A replaced request is anonymous: no credentials for the original site, no telemetry identifiers, no query, and no fallback to the original URL. Registry identities and package URLs stay canonical, so lock files do not change. A project's dbc.toml cannot set network settings.
Registry clients and the package download path use the rules of the user's config.toml, also when DBC_BASE_URL is set. Invalid rules make registry operations fail instead of only warning, because continuing would contact the upstream the user meant to avoid. A dbc.toml with a network table is rejected.
A mirror could answer with a redirect to the registry it stands in for, and the HTTP client followed it, contacting the upstream despite the rule.
A sync that replays a lock entry can download an archive without building a registry client, so it would have contacted the upstream despite the invalid rules.
zeroshade
left a comment
There was a problem hiding this comment.
Thanks, this is carefully done. Matching on origin plus a segment-boundary path prefix with unreserved-escape normalization, first match without chaining, anonymous replaced requests with no fallback, and rejecting [network] in dbc.toml all look right, and the tests cover the edges well. I merged it with current main (including #583's lint config): build, vet, go test ./... and golangci-lint v2.15.0 are clean.
Two things I'd like addressed before approving:
- Redirects into a replaced origin from an unmatched request. The redirect guard only wraps replaced requests. If a request that matches no rule gets a redirect to a URL a rule covers, the client's normal
checkRedirectfollows it straight to the upstream. I checked with a quick test:WithBaseURL(other)whereother302s toupstream/pkg.tar.gz, with a rule forupstream, succeeds with one upstream hit and no mirror hits. Since the point of failing closed is not to contact the upstream the user replaced, could the router also refuse redirects to replaced URLs for unreplaced requests, with a test? - 401/403 from a mirror.
anonymousGetwrapsErrUnauthorized, so the CLI adds "Did you rundbc auth login?", but replaced requests never send credentials, so logging in can't help. A separate error or message, e.g. that the mirror requires authentication, which isn't supported yet, would avoid sending users the wrong way.
Also: 2ee202f makes every user-config load failure (bad TOML, invalid [[registries]] entry) a hard error for registry commands, where it used to be a warning that fell back to the default registries. That makes sense with mirrors, but it's a behaviour change for all users, so please mention it in the PR description so it shows up in the release notes.
Requests that match no URL replacement rule could be redirected to a URL that a rule replaces and reach the upstream the user replaced. Guard the redirects of unreplaced requests too, keeping the client's own redirect policy. A 401 or 403 from a URL replacement target no longer wraps ErrUnauthorized, so the CLI does not suggest `dbc auth login`, which cannot help because replaced requests carry no credentials. It wraps the new ErrURLReplacementUnauthorized instead.
A custom CheckRedirect can rewrite the redirect URL. The guard already checked the rewritten URL against the replacement rules; check it for an HTTPS to HTTP downgrade as well.
|
Both points are addressed in 1539401:
I also merged current |
zeroshade
left a comment
There was a problem hiding this comment.
Thanks, both addressed. guardRedirects now covers unreplaced requests on both the client and package-level paths, so a site the user didn't replace can no longer bounce a request to the upstream they did. I re-ran my repro and it's refused with no upstream hit. Keeping the caller's CheckRedirect semantics (the 10-hop default, ErrUseLastResponse passed through unwrapped) and re-checking after the callback rewrites the URL, including the HTTPS→HTTP check, is careful, and the tests cover each case. ErrURLReplacementUnauthorized with its own formatErr hint fixes the misleading dbc auth login suggestion, and the behavior-change note in the description is clear. Build, tests and golangci-lint v2.15.0 are clean on 4ef85cc. LGTM.
…uiltin_registries` (#598) Follow-up to #360. `replace_defaults` is named for replacing the built-in registries, which is no longer needed: with #595 a third-party registry can name its signing key and works like the built-in ones, and with #577 the built-in registries can be reached through a mirror. What the setting does is remove the built-in registries (the public and the private Columnar registry) while keeping the `[[registries]]` entries, so this renames it to `exclude_builtin_registries`. The user config now wins when it excludes them. ```toml exclude_builtin_registries = true [[registries]] url = "https://drivers.example.com" name = "example" ``` - **Semantics:** the built-in registries are excluded when either the user `config.toml` or the project's `dbc.toml` sets `exclude_builtin_registries = true`, and a project's `false` no longer adds them back. The CLI warns when a `dbc.toml` sets `false` that the user config overrides, and keeps that `false` when it writes the file back. The setting only stops dbc from adding the built-in registries to the list; it is not an access control, so a `[[registries]]` entry with a built-in registry's URL is still used. Omitting the key and `false` now mean the same thing in both files. - **Migration:** `replace_defaults` has not been released, so there is no alias. The old key is ignored like any other unknown key, so a `replace_defaults = true` written for a development build has to be renamed or the built-in registries are used again. - **Go API:** `GlobalConfig.ReplaceDefaults` becomes `ExcludeBuiltinRegistries`. `LoadProjectRegistries` returns a `bool` instead of a `*bool`, and `WithProjectRegistries` takes a `bool`, since there is no longer an "inherit" state. - **Docs:** adds a "Configuring registries" section to the driver registry concepts page and a "Registries" section to the driver list reference with the same content as #393, updated for the new key and semantics, so this supersedes #393. For drivers that several registries publish, they describe the behavior since #584 (the highest-priority registry that has a matching version).
Motivation
In #394, @tokoko pointed out that absolute artifact URLs are a problem behind a corporate firewall, where the ideal setup is a plain Artifactory-style proxy in front of the real host. My reply suggested keeping canonical upstream URLs in the lock file and rewriting them locally when fetching, as mise's URL replacements do. This PR implements that for registries.
It matters now because lock file v2 records the registry URL. Today a mirror has to be configured as a different registry, so a
dbc.lockcreated on the internet cannot be replayed behind the mirror, and the other way round. With this PR the user's own config says where requests go, and project and lock files keep the original URLs.mid/uid, and a URL with a query is refused. There is no fallback to the original URL, and any redirect to a replaced URL is refused, whether it comes from the mirror or from a site no rule replaces. A 401 or 403 from the mirror is reported as such rather than suggestingdbc auth login.DBC_BASE_URL,WithBaseURLandWithRegistriesstill choose registries; replacements apply on top.config.tomlcannot be read, is not valid TOML, or has an invalid[[registries]]entry or URL replacement rule. The warning is still printed. This applies even without URL replacements. A missing config file works as before.dbc.tomlwith anetworktable is rejected, so a repository cannot redirect a user's requests.GlobalConfig.Network,WithURLReplacements,ErrURLReplacementUnauthorized, andSetDefaultURLReplacementsfor the deprecated package-level functions.Not included: mirrors that need authentication, regex rules, and an environment variable. The Node and other bindings pick the rules up when they load the default registry config; with an explicit registry list they do not. In the Wasm build redirects are followed by
fetch, so the redirect refusal does not apply there.