Copilot commented on code in PR #1677: URL: https://github.com/apache/maven-dependency-plugin/pull/1677#discussion_r3741217886
########## src/main/java/org/apache/maven/plugins/dependency/utils/RepositorySessionInjector.java: ########## @@ -0,0 +1,193 @@ +/* + * 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.plugins.dependency.utils; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.apache.maven.RepositoryUtils; +import org.apache.maven.artifact.repository.ArtifactRepository; +import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy; +import org.apache.maven.artifact.repository.Authentication; +import org.apache.maven.artifact.repository.MavenArtifactRepository; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout; +import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout2; +import org.apache.maven.repository.Proxy; +import org.eclipse.aether.RepositorySystemSession; +import org.eclipse.aether.repository.AuthenticationContext; +import org.eclipse.aether.repository.AuthenticationSelector; +import org.eclipse.aether.repository.MirrorSelector; +import org.eclipse.aether.repository.ProxySelector; +import org.eclipse.aether.repository.RemoteRepository; + +/** + * Applies the mirror, proxy and authentication configuration of a {@link RepositorySystemSession} to repositories + * that were built by hand rather than obtained from the project, so that they can be used for resolution. + * <p> + * This is the work {@code org.apache.maven.bridge.MavenRepositorySystem} does in its session-based + * {@code injectMirror}/{@code injectProxy}/{@code injectAuthentication} methods, reimplemented here because + * {@code org.apache.maven.bridge} is not one of the packages maven-core exports to plugin class realms — a plugin + * that references it compiles and unit-tests cleanly and then fails at runtime with + * {@code NoClassDefFoundError: org/apache/maven/bridge/MavenRepositorySystem}. Everything used below is in a package + * maven-core does export: {@code org.apache.maven} (for {@link RepositoryUtils}), {@code org.apache.maven.artifact}, + * {@code org.apache.maven.repository} and {@code org.eclipse.aether.repository}. + * <p> + * The session's selectors are populated by Maven from the mirrors, proxies and servers of + * <code>settings.xml</code>, with the servers and proxies already decrypted, and they are the same selectors + * Resolver consults when it performs the transfer. + */ +public class RepositorySessionInjector { + + private final Map<String, ArtifactRepositoryLayout> repositoryLayouts; + + public RepositorySessionInjector(Map<String, ArtifactRepositoryLayout> repositoryLayouts) { + this.repositoryLayouts = repositoryLayouts; + } + + /** + * Applies the session's mirror, then proxy, then authentication configuration to each repository, in that order: + * mirroring rewrites the id and URL a repository is known by, and the proxy and the credentials are selected for + * the mirror rather than for the repository it replaced. + * + * @param session the repository session, may be {@code null} + * @param repositories the repositories to modify in place, may be {@code null} + */ + public void inject(RepositorySystemSession session, List<ArtifactRepository> repositories) { + if (session == null || repositories == null) { + return; + } + + for (ArtifactRepository repository : repositories) { + injectMirror(session, repository); + repository.setProxy(getProxy(session, repository)); + repository.setAuthentication(getAuthentication(session, repository)); + } + } + + private void injectMirror(RepositorySystemSession session, ArtifactRepository repository) { + MirrorSelector selector = session.getMirrorSelector(); + if (selector == null) { + return; + } + + RemoteRepository mirror = selector.getMirror(RepositoryUtils.toRepo(repository)); + if (mirror == null) { + return; + } + + repository.setMirroredRepositories(Collections.singletonList(createArtifactRepository( + repository.getId(), + repository.getUrl(), + repository.getLayout(), + repository.getSnapshots(), + repository.getReleases()))); + + repository.setId(mirror.getId()); + repository.setUrl(mirror.getUrl()); + + String layoutId = mirror.getContentType(); + if (layoutId != null && !layoutId.isEmpty()) { + repository.setLayout(getLayout(layoutId)); + } + + repository.setBlocked(mirror.isBlocked()); + } + + /** + * Same as {@code MavenRepositorySystem.createArtifactRepository}, including its substitution of default policies + * for null ones — {@link MavenArtifactRepository} stores null and fails later at whichever call site validates. + */ + private static ArtifactRepository createArtifactRepository( + String id, + String url, + ArtifactRepositoryLayout layout, + ArtifactRepositoryPolicy snapshots, + ArtifactRepositoryPolicy releases) { + ArtifactRepositoryPolicy snapshotPolicy = snapshots != null ? snapshots : new ArtifactRepositoryPolicy(); + ArtifactRepositoryPolicy releasePolicy = releases != null ? releases : new ArtifactRepositoryPolicy(); + + if (layout instanceof ArtifactRepositoryLayout2) { + return ((ArtifactRepositoryLayout2) layout) + .newMavenArtifactRepository(id, url, snapshotPolicy, releasePolicy); + } + return new MavenArtifactRepository(id, url, layout, snapshotPolicy, releasePolicy); + } + + /** + * Resolves a mirror's layout id. Maven core wraps an unknown id in a layout that delegates to the default one; + * falling back to the default layout outright computes the same paths. + */ + private ArtifactRepositoryLayout getLayout(String layoutId) { + ArtifactRepositoryLayout layout = repositoryLayouts.get(layoutId); + return layout != null ? layout : repositoryLayouts.get("default"); + } + + private Proxy getProxy(RepositorySystemSession session, ArtifactRepository repository) { + ProxySelector selector = session.getProxySelector(); + if (selector == null) { + return null; + } + + RemoteRepository repo = RepositoryUtils.toRepo(repository); + org.eclipse.aether.repository.Proxy proxy = selector.getProxy(repo); + if (proxy == null) { + return null; + } + + Proxy result = new Proxy(); + result.setHost(proxy.getHost()); + result.setProtocol(proxy.getType()); + result.setPort(proxy.getPort()); + + if (proxy.getAuthentication() != null) { + repo = new RemoteRepository.Builder(repo).setProxy(proxy).build(); + try (AuthenticationContext authCtx = AuthenticationContext.forProxy(session, repo)) { + result.setUserName(authCtx.get(AuthenticationContext.USERNAME)); + result.setPassword(authCtx.get(AuthenticationContext.PASSWORD)); + result.setNtlmDomain(authCtx.get(AuthenticationContext.NTLM_DOMAIN)); + result.setNtlmHost(authCtx.get(AuthenticationContext.NTLM_WORKSTATION)); + } + } + + return result; + } + + private Authentication getAuthentication(RepositorySystemSession session, ArtifactRepository repository) { + AuthenticationSelector selector = session.getAuthenticationSelector(); + if (selector == null) { + return null; + } + + RemoteRepository repo = RepositoryUtils.toRepo(repository); + org.eclipse.aether.repository.Authentication auth = selector.getAuthentication(repo); + if (auth == null) { + return null; + } + + repo = new RemoteRepository.Builder(repo).setAuthentication(auth).build(); + try (AuthenticationContext authCtx = AuthenticationContext.forRepository(session, repo)) { + Authentication result = new Authentication( + authCtx.get(AuthenticationContext.USERNAME), authCtx.get(AuthenticationContext.PASSWORD)); + result.setPrivateKey(authCtx.get(AuthenticationContext.PRIVATE_KEY_PATH)); + result.setPassphrase(authCtx.get(AuthenticationContext.PRIVATE_KEY_PASSPHRASE)); + return result; Review Comment: `getAuthentication(...)` always returns a non-null `Authentication` once the selector returns a non-null Aether auth, even if the `AuthenticationContext` resolves all credential fields to null. That can lead to setting an “empty” authentication object on the repository and also contradicts the intended behavior described in the PR (null auth when no credentials are present). Consider returning `null` when no username/password/private key/passphrase is available. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
