Skip to content

Bind the central server id to Central's publishing origins and count altDeploymentRepository as a declared origin - #14104

Draft
slachiewicz wants to merge 3 commits into
apache:masterfrom
slachiewicz:central-publishing-origins-master
Draft

slachiewicz wants to merge 3 commits into
apache:masterfrom
slachiewicz:central-publishing-origins-master

Conversation

@slachiewicz

@slachiewicz slachiewicz commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

A settings server with the id central could not authenticate snapshot deployments to Maven Central: the default repository binds that id to https://repo.maven.apache.org, so its credentials were withheld from https://central.sonatype.com and the deploy ended with HTTP Status: 401. The id is the default publishingServerId of central-publishing-maven-plugin.

The id central is now also bound to the origins Central is published to, https://central.sonatype.com and https://ossrh-staging-api.central.sonatype.com, as long as the built-in location is its only origin. A mirror, a repository or a server <repositoryOrigins> that gives the id another origin takes over and nothing is added. The default repository's origin stays bound on purpose: a parent POM can declare a repository central at another host, and that binding is what withholds the credentials there.

When credentials are used through this binding, Maven warns once per session that the binding will be removed in a future release and names the replacements: the two origins in the server's <repositoryOrigins>, or a server id of its own such as the publishingServerId of central-publishing-maven-plugin. Server aliases need nothing extra: the default repository central is always among the request's repositories, so an alias central gets the same binding.

A deployment repository given on the command line with altDeploymentRepository, altReleaseDeploymentRepository or altSnapshotDeploymentRepository now declares the origin of its id, as the existing warning already says a command-line repository does. Such a server is used without a warning, works in the strict scope, and its credentials are no longer offered to a repository that reuses the id at another host. Only user properties are read; a POM property of the same name declares nothing.

Verified: mvn deploy:deploy -DaltDeploymentRepository=central::https://central.sonatype.com/repository/maven-snapshots/ with a settings server central on a 4.0.x build, against a local stand-in for central.sonatype.com → 401 without credentials before, deployed with credentials after. On master only the unit tests were run. The warning was run end to end on a 3.10.x build only; here it is covered by unit tests.

Verified: the same command with -DaltDeploymentRepository=sonatype-central::<url> and a settings server sonatype-central on a 4.0.x build → one warning and refused under strict before; no warning and deployed under strict after.

I have not run the core ITs.

@slachiewicz slachiewicz added this to the 4.2.0 milestone Nov 23, 2026
@slachiewicz slachiewicz added bug Something isn't working mvn4 labels Nov 23, 2026
@slachiewicz slachiewicz changed the title Bind the central server id to the origins Maven Central is published to Bind the central server id to Central's publishing origins and count altDeploymentRepository as a declared origin Nov 23, 2026

@gnodet-bot gnodet-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review: Bind central server id to Central's publishing origins

Verdict: LGTM — well-designed credential scoping change with good test coverage.

What This Does

This PR makes two improvements to Maven's credential scoping:

  1. Binds the central server id to Central Portal's publishing origins (central.sonatype.com, ossrh-staging-api.central.sonatype.com), so users who configure a central server for publishing don't need explicit <repositoryOrigins>.
  2. Counts altDeploymentRepository (and variants) as a declared credential origin, so credentials in strict/origin scope work for deployment repositories specified on the command line.

Analysis

Security guard is correct: addCentralPublishingOrigins only adds publishing origins when central's origin set is exactly {https://repo.maven.apache.org} — once a mirror, custom repository, or explicit <repositoryOrigins> adds another origin, the guard blocks. This prevents credential leakage if the user has a customized central configuration.

Ordering is correct: addCentralPublishingOrigins is called after all other origin registration (server loop + deployment repo properties), ensuring the guard sees all declarations.

Deployment repo parsing is robust: addDeploymentRepositoryOrigin handles both id::url and the legacy id::layout::url format via indexOf("::") / lastIndexOf("::"). Edge cases (null, missing ::, empty id) are properly guarded; malformed URLs fall through to the existing try/catch in addOrigin.

Test coverage is thorough: 4 unit tests + 3 integration tests covering happy path, guard activation, id isolation, and deployment repo parsing edge cases (null, no-id, empty-id, legacy format).

Minor Observations (Non-blocking)

  1. Empty URL edge case: "id::" (empty string after ::) would reach addOrigin with an empty URL. This is safely caught by the existing URISyntaxException | IllegalArgumentException handler in addOrigin, but could be guarded early with an !url.isEmpty() check in addDeploymentRepositoryOrigin for clarity.

  2. Integration test coverage: Only altDeploymentRepository is tested in DefaultRepositorySystemSessionFactoryTest, not altReleaseDeploymentRepository or altSnapshotDeploymentRepository. Since all three go through the same loop calling the same method, this is adequate — but a parameterized test covering all three would make the array contract explicit.

This review was generated by an AI agent, Hermès on behalf of @gnodet.

@gnodet-bot

Copy link
Copy Markdown
Contributor

One interaction worth considering: the server aliases feature from #2333 and the credential scoping fix here may not compose correctly.

Scenario: a user configures central as an alias on another server:

<server>
  <id>my-publishing-server</id>
  <username>...</username>
  <password>...</password>
  <aliases>
    <alias>central</alias>
  </aliases>
</server>

The SettingsBuilder expands this into a virtual server with id central, so request.getServers() contains the right credentials. However, addCentralPublishingOrigins has this condition:

Set<String> origins = declaredOrigins.get(CENTRAL_ID);
if (origins != null && origins.equals(Set.of(CENTRAL_ORIGIN))) {
    origins.addAll(CENTRAL_PUBLISHING_ORIGINS);
}

Since no repository or mirror carries the id central in this case, declaredOrigins.get("central") returns null, the condition never fires, and the publishing origins are never added. Under strict scope, the deploy would fail.

Suggested fix: relax the condition to also cover the alias case (origins == null), but only when the default repository is present at its standard URL — to avoid activating for a fully custom central server. Something like:

In DefaultRepositorySystemSessionFactory, track whether the default central repo was added at the standard origin, then pass a boolean hasDefaultCentralRepo flag to addCentralPublishingOrigins:

public static void addCentralPublishingOrigins(
        Map<String, Set<String>> declaredOrigins, boolean hasDefaultCentralRepo) {
    if (!hasDefaultCentralRepo) {
        return;
    }
    Set<String> origins = declaredOrigins.get(CENTRAL_ID);
    // Cover both cases:
    // - explicit <server id="central"> with default repo → origins == {repo.maven.apache.org}
    // - <aliases><alias>central</alias> without a declared repo → origins == null
    if (origins == null || origins.equals(Set.of(CENTRAL_ORIGIN))) {
        declaredOrigins.computeIfAbsent(CENTRAL_ID, k -> new HashSet<>())
                       .addAll(CENTRAL_PUBLISHING_ORIGINS);
    }
}

This keeps the existing guard (a mirror or <repositoryOrigins> giving central a non-standard origin takes over and nothing is added) while extending coverage to the alias case.

This comment was generated by an AI agent, Hermès on behalf of @gnodet.

The default repository gives the id central the origin repo.maven.apache.org,
which kept a settings server with that id from authenticating deployments to
central.sonatype.com (HTTP 401 on snapshot publishing).

The publishing origins are added only while the built-in location is the sole
origin of the id. Do not fix this by dropping the default repository's origin
instead: a parent POM can declare a repository central at another host, and
that binding is what withholds the credentials there.
A deployment repository given on the command line as id::url was not known to
the credential scoping: its server was used with a warning in the origin scope
and refused in the strict scope, although the warning itself says a repository
declared on the command line counts. Only the user properties of the request
are read, so a POM property of the same name declares nothing.
The binding of the id central to Central's publishing origins keeps
deployments configured before credential scoping working; the warning
names the explicit replacement so the binding can be removed later.
@slachiewicz
slachiewicz force-pushed the central-publishing-origins-master branch from 35a4862 to fc66a78 Compare November 27, 2026 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working mvn4

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants