-
Notifications
You must be signed in to change notification settings - Fork 21
Apply custom HTTP headers in remote server calls #1549
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
Open
timw
wants to merge
1
commit into
chains-project:main
Choose a base branch
from
indexity-io:feature/remote-auth-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import java.net.http.HttpRequest; | ||
| import java.net.http.HttpResponse; | ||
| import java.security.MessageDigest; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
|
|
@@ -21,7 +22,10 @@ | |
| import java.util.concurrent.Future; | ||
| import org.apache.maven.artifact.Artifact; | ||
| import org.apache.maven.artifact.repository.ArtifactRepository; | ||
| import org.apache.maven.execution.MavenSession; | ||
| import org.apache.maven.project.ProjectBuildingRequest; | ||
| import org.apache.maven.settings.Server; | ||
| import org.codehaus.plexus.util.xml.Xpp3Dom; | ||
|
|
||
| public class RemoteChecksumCalculator extends AbstractChecksumCalculator { | ||
|
|
||
|
|
@@ -30,11 +34,13 @@ public class RemoteChecksumCalculator extends AbstractChecksumCalculator { | |
| private final HttpClient httpClient; | ||
| private final Map<String, String> checksumCache = new ConcurrentHashMap<>(); | ||
| private final Map<String, RepositoryInformation> resolvedCache = new ConcurrentHashMap<>(); | ||
| private final MavenSession session; | ||
|
|
||
| public RemoteChecksumCalculator( | ||
| String checksumAlgorithm, | ||
| ProjectBuildingRequest artifactBuildingRequest, | ||
| ProjectBuildingRequest pluginBuildingRequest) { | ||
| ProjectBuildingRequest pluginBuildingRequest, | ||
| MavenSession session) { | ||
| super(checksumAlgorithm); | ||
| if (!(checksumAlgorithm.equals("MD5") | ||
| || checksumAlgorithm.equals("SHA-1") | ||
|
|
@@ -46,6 +52,7 @@ public RemoteChecksumCalculator( | |
|
|
||
| this.artifactBuildingRequest = artifactBuildingRequest; | ||
| this.pluginBuildingRequest = pluginBuildingRequest; | ||
| this.session = session; | ||
| this.httpClient = HttpClient.newBuilder() | ||
| .followRedirects(HttpClient.Redirect.ALWAYS) | ||
| .build(); | ||
|
|
@@ -60,6 +67,45 @@ private String getCacheKey(Artifact artifact) { | |
| + ":" + artifact.getType(); | ||
| } | ||
|
|
||
| private HttpRequest.Builder repositoryRequest(ArtifactRepository repository, String url) { | ||
| repository.getId(); | ||
| HttpRequest.Builder request = HttpRequest.newBuilder().uri(URI.create(url)); | ||
| Optional<Server> maybeServerSettings = session.getSettings().getServers().stream() | ||
| .filter(s -> s.getId().equals(repository.getId())) | ||
| .findFirst(); | ||
| if (maybeServerSettings.isPresent()) { | ||
| // TODO: username/password willl require per-repo clients so we can apply PasswordAuthenticators separately. | ||
| applyServerConfig(maybeServerSettings.get(), request); | ||
| return request; | ||
| } | ||
| return request; | ||
| } | ||
|
|
||
| private static void applyServerConfig(Server server, HttpRequest.Builder request) { | ||
| // Apply custom transport config from server.xml | ||
| var serverConfig = server.getConfiguration(); | ||
| if (serverConfig instanceof Xpp3Dom) { | ||
| var config = (Xpp3Dom) serverConfig; | ||
| var headers = config.getChild("httpHeaders"); | ||
| if (headers != null) { | ||
| for (var header : headers.getChildren("property")) { | ||
| Xpp3Dom nameElement = header.getChild("name"); | ||
| Xpp3Dom valueElement = header.getChild("value"); | ||
| if (nameElement != null && valueElement != null) { | ||
| request.header(nameElement.getValue(), valueElement.getValue()); | ||
| } | ||
| } | ||
| } | ||
| var requestTimeout = config.getChild("requestTimeout"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also don't see |
||
| if (requestTimeout != null) { | ||
| try { | ||
| request.timeout(Duration.ofMillis(Long.parseLong(requestTimeout.getValue()))); | ||
| } catch (NumberFormatException ignored) { | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBuildingRequest buildingRequest) { | ||
| String cacheKey = getCacheKey(artifact) + ":" + checksumAlgorithm; | ||
| String cached = checksumCache.get(cacheKey); | ||
|
|
@@ -92,7 +138,7 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui | |
| PluginLogManager.getLog().debug(String.format("Checking: %s", checksumUrl)); | ||
|
|
||
| HttpRequest checksumRequest = | ||
| HttpRequest.newBuilder().uri(URI.create(checksumUrl)).build(); | ||
| repositoryRequest(repository, checksumUrl).build(); | ||
| HttpResponse<String> checksumResponse = | ||
| httpClient.send(checksumRequest, HttpResponse.BodyHandlers.ofString()); | ||
|
|
||
|
|
@@ -103,9 +149,8 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui | |
| } | ||
|
|
||
| if (checksumResponse.statusCode() == 404) { | ||
| HttpRequest artifactRequest = HttpRequest.newBuilder() | ||
| .uri(URI.create(artifactUrl)) | ||
| .build(); | ||
| HttpRequest artifactRequest = | ||
| repositoryRequest(repository, artifactUrl).build(); | ||
| HttpResponse<byte[]> artifactResponse = | ||
| httpClient.send(artifactRequest, HttpResponse.BodyHandlers.ofByteArray()); | ||
|
|
||
|
|
@@ -119,9 +164,8 @@ private Optional<String> calculateChecksumInternal(Artifact artifact, ProjectBui | |
| checksumAlgorithm, artifact)); | ||
|
|
||
| // Fallback to and verify downloaded artifact with SHA-1 | ||
| HttpRequest artifactVerificationRequest = HttpRequest.newBuilder() | ||
| .uri(URI.create(artifactUrl + ".sha1")) | ||
| .build(); | ||
| HttpRequest artifactVerificationRequest = | ||
| repositoryRequest(repository, artifactUrl + ".sha1").build(); | ||
| HttpResponse<String> artifactVerificationResponse = | ||
| httpClient.send(artifactVerificationRequest, HttpResponse.BodyHandlers.ofString()); | ||
|
|
||
|
|
@@ -206,8 +250,7 @@ private Optional<RepositoryInformation> getResolvedFieldInternal( | |
|
|
||
| PluginLogManager.getLog().debug(String.format("Checking: %s", url)); | ||
|
|
||
| HttpRequest request = HttpRequest.newBuilder() | ||
| .uri(URI.create(url)) | ||
| HttpRequest request = repositoryRequest(repository, url) | ||
| .method("HEAD", HttpRequest.BodyPublishers.noBody()) | ||
| .build(); | ||
| HttpResponse<Void> response = httpClient.send(request, HttpResponse.BodyHandlers.discarding()); | ||
|
|
||
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.
httpConfigurationseems correct based on the documentation here. This documentation also seems pretty up-to-date.