-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Credential scoping + relocation validation (3.9.x backport) #13005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a5272d1
Validate relocation coordinates from resolved project metadata
slachiewicz 853328a
Bind server credentials to the declared repository origin
slachiewicz fc899f9
Match settings server ids exactly when selecting credentials
slachiewicz b6942fa
Fix Spotless formatting violations
gnodet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
...ore/src/main/java/org/apache/maven/internal/aether/OriginBoundAuthenticationSelector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.maven.internal.aether; | ||
|
|
||
| import java.net.URI; | ||
| import java.net.URISyntaxException; | ||
| import java.util.HashSet; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import org.codehaus.plexus.logging.Logger; | ||
| import org.eclipse.aether.repository.Authentication; | ||
| import org.eclipse.aether.repository.AuthenticationSelector; | ||
| import org.eclipse.aether.repository.RemoteRepository; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * An {@link AuthenticationSelector} that scopes server credentials to the origin (protocol, host and | ||
| * port) of the repository or mirror the operator declared for the same server id. | ||
| * <p> | ||
| * A repository's id and its origin are independent: this selector serves a server id's credentials | ||
| * only to a repository whose origin matches one the operator declared for that id, in settings or on | ||
| * the command line. Ids with no operator-declared origin keep the previous behaviour unless | ||
| * {@code strict} scope is requested, and a warning naming the target origin is emitted once per | ||
| * id/origin pair. | ||
| * | ||
| * @see DefaultRepositorySystemSessionFactory#MAVEN_REPOSITORY_CREDENTIAL_SCOPE | ||
| */ | ||
| class OriginBoundAuthenticationSelector implements AuthenticationSelector { | ||
| /** | ||
| * Credentials are bound to operator-declared origins; ids without a declared origin keep legacy | ||
| * behavior, with a warning. | ||
| */ | ||
| static final String SCOPE_ORIGIN = "origin"; | ||
|
|
||
| /** | ||
| * Credentials are bound to operator-declared origins; ids without a declared origin get no | ||
| * credentials. | ||
| */ | ||
| static final String SCOPE_STRICT = "strict"; | ||
|
|
||
| /** | ||
| * Legacy behavior: credentials are matched by server id only. | ||
| */ | ||
| static final String SCOPE_ID = "id"; | ||
|
|
||
| private final AuthenticationSelector delegate; | ||
|
|
||
| private final Map<String, Set<String>> declaredOrigins; | ||
|
|
||
| private final boolean strict; | ||
|
|
||
| private final Logger logger; | ||
|
|
||
| private final Set<String> reported = ConcurrentHashMap.newKeySet(); | ||
|
|
||
| private OriginBoundAuthenticationSelector( | ||
| AuthenticationSelector delegate, Map<String, Set<String>> declaredOrigins, boolean strict, Logger logger) { | ||
| this.delegate = requireNonNull(delegate, "delegate"); | ||
| this.declaredOrigins = requireNonNull(declaredOrigins, "declaredOrigins"); | ||
| this.strict = strict; | ||
| this.logger = requireNonNull(logger, "logger"); | ||
| } | ||
|
|
||
| /** | ||
| * Wraps the given selector according to the requested credential scope. | ||
| * | ||
| * @param delegate the selector holding the actual credentials, keyed by server id | ||
| * @param credentialScope one of {@link #SCOPE_ORIGIN}, {@link #SCOPE_STRICT} or {@link #SCOPE_ID} | ||
| * @param declaredOrigins origins of operator-declared repositories and mirrors, keyed by id | ||
| * @param logger logger used to report id/origin mismatches | ||
| * @return the delegate itself for {@link #SCOPE_ID}, an origin-bound wrapper otherwise | ||
| */ | ||
| static AuthenticationSelector wrap( | ||
| AuthenticationSelector delegate, | ||
| String credentialScope, | ||
| Map<String, Set<String>> declaredOrigins, | ||
| Logger logger) { | ||
| if (SCOPE_ID.equals(credentialScope)) { | ||
| return delegate; | ||
| } else if (SCOPE_ORIGIN.equals(credentialScope) || SCOPE_STRICT.equals(credentialScope)) { | ||
| return new OriginBoundAuthenticationSelector( | ||
| delegate, declaredOrigins, SCOPE_STRICT.equals(credentialScope), logger); | ||
| } else { | ||
| throw new IllegalArgumentException("Unknown value '" + credentialScope + "' for " | ||
| + DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE | ||
| + ". Supported values are: " + SCOPE_ORIGIN + ", " + SCOPE_STRICT + ", " + SCOPE_ID); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Records the origin of an operator-declared repository or mirror for the given id. URLs without a | ||
| * parseable server authority (for example {@code file:} URLs) are ignored. | ||
| */ | ||
| static void addOrigin(Map<String, Set<String>> declaredOrigins, String id, String url) { | ||
| String origin = originOf(url); | ||
| if (id != null && origin != null) { | ||
| declaredOrigins.computeIfAbsent(id, k -> new HashSet<>()).add(origin); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Authentication getAuthentication(RemoteRepository repository) { | ||
| Authentication auth = delegate.getAuthentication(repository); | ||
| if (auth == null) { | ||
| return null; | ||
| } | ||
| String id = repository.getId(); | ||
| String origin = originOf(repository.getUrl()); | ||
| Set<String> origins = declaredOrigins.get(id); | ||
| if (origins != null && !origins.isEmpty()) { | ||
| if (origin != null && origins.contains(origin)) { | ||
| return auth; | ||
| } | ||
| warnOnce( | ||
| id, | ||
| origin, | ||
| "Not using credentials of server '" + id + "' for repository " + repository.getUrl() | ||
| + ": the repository or mirror declared for this id resides at " + origins | ||
| + ". Set " | ||
| + DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE + "=" | ||
| + SCOPE_ID + " to restore legacy id-only credential matching."); | ||
| return null; | ||
| } | ||
| if (strict) { | ||
| warnOnce( | ||
| id, | ||
| origin, | ||
| "Not using credentials of server '" + id + "' for repository " + repository.getUrl() | ||
| + ": no repository or mirror with this id is declared in settings or on the command" | ||
| + " line, and " + DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE | ||
| + "=" + SCOPE_STRICT + " is in effect."); | ||
| return null; | ||
| } | ||
| warnOnce( | ||
| id, | ||
| origin, | ||
| "Using credentials of server '" + id + "' for repository " + repository.getUrl() | ||
| + ", although no repository or mirror with this id is declared in settings or on the" | ||
| + " command line. Set " | ||
| + DefaultRepositorySystemSessionFactory.MAVEN_REPOSITORY_CREDENTIAL_SCOPE | ||
| + "=" + SCOPE_STRICT + " to refuse such credential use."); | ||
| return auth; | ||
| } | ||
|
|
||
| private void warnOnce(String id, String origin, String message) { | ||
| if (reported.add(id + "->" + origin)) { | ||
| logger.warn(message); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the normalized origin ({@code protocol://host[:port]}, lower-cased, default http/https | ||
| * ports elided) of the given URL, or {@code null} if the URL has no parseable server authority. | ||
| */ | ||
| static String originOf(String url) { | ||
| if (url == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| URI uri = new URI(url).parseServerAuthority(); | ||
| String scheme = uri.getScheme(); | ||
| String host = uri.getHost(); | ||
| if (scheme == null || host == null) { | ||
| return null; | ||
| } | ||
| scheme = scheme.toLowerCase(Locale.ROOT); | ||
| host = host.toLowerCase(Locale.ROOT); | ||
| int port = uri.getPort(); | ||
| if ((port == 80 && "http".equals(scheme)) || (port == 443 && "https".equals(scheme))) { | ||
| port = -1; | ||
| } | ||
| return port >= 0 ? scheme + "://" + host + ":" + port : scheme + "://" + host; | ||
| } catch (URISyntaxException e) { | ||
| return null; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maven 3.9.10 was already released and does not contain this constant. The sibling PR #12954 correctly uses
@since 3.10.0. This should reference the next 3.9.x milestone (3.9.17).