Skip to content
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

Add auth and proxy support for Search RR #55

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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 eu.maveniverse.maven.toolbox.shared.internal;

import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.ProxySelector;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.HashMap;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.AuthenticationContext;
import org.eclipse.aether.repository.RemoteRepository;

/**
* Java 11 {@link HttpClient} factory that creates pre-configured HTTP client based on Resolver {@link RemoteRepository}.
*/
public final class Java11HttpClientFactory {
public static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(10L);

public static HttpClient buildHttpClient(RepositorySystemSession session, RemoteRepository repository) {
return buildHttpClient(DEFAULT_TIMEOUT, session, repository);
}

private static HttpClient buildHttpClient(
Duration timeout, RepositorySystemSession session, RemoteRepository repository) {

HttpClient.Builder builder =
HttpClient.newBuilder().connectTimeout(timeout).followRedirects(HttpClient.Redirect.NEVER);

HashMap<Authenticator.RequestorType, PasswordAuthentication> authentications = new HashMap<>();
try (AuthenticationContext repoAuthContext = AuthenticationContext.forRepository(session, repository)) {
if (repoAuthContext != null) {
authentications.put(
Authenticator.RequestorType.SERVER,
new PasswordAuthentication(
repoAuthContext.get(AuthenticationContext.USERNAME),
repoAuthContext
.get(AuthenticationContext.USERNAME)
.toCharArray()));
}
}
if (repository.getProxy() != null) {
builder.proxy(ProxySelector.of(new InetSocketAddress(
repository.getProxy().getHost(), repository.getProxy().getPort())));
try (AuthenticationContext proxyAuthContext = AuthenticationContext.forProxy(session, repository)) {
if (proxyAuthContext != null) {
authentications.put(
Authenticator.RequestorType.PROXY,
new PasswordAuthentication(
proxyAuthContext.get(AuthenticationContext.USERNAME),
proxyAuthContext
.get(AuthenticationContext.PASSWORD)
.toCharArray()));
}
}
}
if (!authentications.isEmpty()) {
builder.authenticator(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return authentications.get(getRequestorType());
}
});
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ private LibYearSink(
this.searchBackends = new ArrayList<>();
for (RemoteRepository remoteRepository : context.remoteRepositories()) {
try {
this.searchBackends.add(toolboxSearchApi.getRemoteRepositoryBackend(remoteRepository));
this.searchBackends.add(toolboxSearchApi.getRemoteRepositoryBackend(
context.repositorySystemSession(), remoteRepository));
} catch (IllegalArgumentException e) {
// most likely cannot figure out what extractor to use; ignore
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ public boolean resolveTransitive(
}
}

private boolean doResolveTransitive(
private void doResolveTransitive(
ResolutionScope resolutionScope,
ResolutionRoot resolutionRoot,
boolean sources,
Expand Down Expand Up @@ -603,7 +603,6 @@ private boolean doResolveTransitive(
}
}
}
return true;
}

@Override
Expand Down Expand Up @@ -687,7 +686,8 @@ public boolean exists(
throws IOException {
ArrayList<Artifact> missingOnes = new ArrayList<>();
ArrayList<Artifact> existingOnes = new ArrayList<>();
try (SearchBackend backend = toolboxSearchApi.getRemoteRepositoryBackend(remoteRepository)) {
try (SearchBackend backend =
toolboxSearchApi.getRemoteRepositoryBackend(context.repositorySystemSession(), remoteRepository)) {
Artifact artifact = new DefaultArtifact(gav);
boolean exists = toolboxSearchApi.exists(backend, artifact);
if (!exists) {
Expand Down Expand Up @@ -770,7 +770,8 @@ public boolean identify(RemoteRepository remoteRepository, String target, Output
sha1 = target;
}
output.verbose("Identifying artifact with SHA1={}", sha1);
try (SearchBackend backend = toolboxSearchApi.getSmoBackend(remoteRepository)) {
try (SearchBackend backend =
toolboxSearchApi.getSmoBackend(context.repositorySystemSession(), remoteRepository)) {
SearchRequest searchRequest = new SearchRequest(fieldQuery(MAVEN.SHA1, sha1));
SearchResponse searchResponse = backend.search(searchRequest);

Expand All @@ -786,7 +787,8 @@ public boolean identify(RemoteRepository remoteRepository, String target, Output

@Override
public boolean list(RemoteRepository remoteRepository, String gavoid, Output output) throws IOException {
try (SearchBackend backend = toolboxSearchApi.getRemoteRepositoryBackend(remoteRepository)) {
try (SearchBackend backend =
toolboxSearchApi.getRemoteRepositoryBackend(context.repositorySystemSession(), remoteRepository)) {
String[] elements = gavoid.split(":");
if (elements.length < 1 || elements.length > 3) {
throw new IllegalArgumentException("Invalid gavoid");
Expand Down Expand Up @@ -828,7 +830,8 @@ public boolean list(RemoteRepository remoteRepository, String gavoid, Output out

@Override
public boolean search(RemoteRepository remoteRepository, String expression, Output output) throws IOException {
try (SearchBackend backend = toolboxSearchApi.getSmoBackend(remoteRepository)) {
try (SearchBackend backend =
toolboxSearchApi.getSmoBackend(context.repositorySystemSession(), remoteRepository)) {
Query query;
try {
query = toolboxSearchApi.toSmoQuery(new DefaultArtifact(expression));
Expand All @@ -851,7 +854,8 @@ public boolean search(RemoteRepository remoteRepository, String expression, Outp
@Override
public boolean verify(RemoteRepository remoteRepository, String gav, String sha1, Output output)
throws IOException {
try (SearchBackend backend = toolboxSearchApi.getRemoteRepositoryBackend(remoteRepository)) {
try (SearchBackend backend =
toolboxSearchApi.getRemoteRepositoryBackend(context.repositorySystemSession(), remoteRepository)) {
Artifact artifact = new DefaultArtifact(gav);
boolean verified = toolboxSearchApi.verify(backend, new DefaultArtifact(gav), sha1);
output.normal("Artifact SHA1({})={}: {}", artifact, sha1, verified ? "MATCHED" : "NOT MATCHED");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.maven.search.backend.remoterepository.extractor.MavenCentralResponseExtractor;
import org.apache.maven.search.backend.remoterepository.extractor.Nx2ResponseExtractor;
import org.apache.maven.search.backend.smo.SmoSearchBackendFactory;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.artifact.Artifact;
import org.eclipse.aether.repository.RemoteRepository;
import org.slf4j.Logger;
Expand All @@ -39,7 +40,8 @@ public class ToolboxSearchApiImpl {

public ToolboxSearchApiImpl() {}

public SearchBackend getRemoteRepositoryBackend(RemoteRepository remoteRepository) {
public SearchBackend getRemoteRepositoryBackend(
RepositorySystemSession session, RemoteRepository remoteRepository) {
final ResponseExtractor extractor;
if ("central".equals(remoteRepository.getContentType())) {
extractor = new MavenCentralResponseExtractor();
Expand All @@ -65,19 +67,23 @@ public SearchBackend getRemoteRepositoryBackend(RemoteRepository remoteRepositor
remoteRepository.getId() + "-rr",
remoteRepository.getId(),
remoteRepository.getUrl(),
new Java11HttpClientTransport(),
new Java11HttpClientTransport(
Java11HttpClientFactory.DEFAULT_TIMEOUT,
Java11HttpClientFactory.buildHttpClient(session, remoteRepository)),
extractor);
}

public SearchBackend getSmoBackend(RemoteRepository remoteRepository) {
public SearchBackend getSmoBackend(RepositorySystemSession session, RemoteRepository remoteRepository) {
if (!ContextOverrides.CENTRAL.getId().equals(remoteRepository.getId())) {
throw new IllegalArgumentException("The SMO service is offered for Central only");
}
return SmoSearchBackendFactory.create(
remoteRepository.getId() + "-smo",
remoteRepository.getId(),
"https://search.maven.org/solrsearch/select",
new Java11HttpClientTransport());
new Java11HttpClientTransport(
Java11HttpClientFactory.DEFAULT_TIMEOUT,
Java11HttpClientFactory.buildHttpClient(session, remoteRepository)));
}

public void renderPage(List<Record> page, Predicate<String> versionPredicate, Output output) {
Expand Down