-
Notifications
You must be signed in to change notification settings - Fork 16
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
Test latest httpclient release #2348
Draft
carterkozak
wants to merge
8
commits into
develop
Choose a base branch
from
ckozak/test_latest_release_candidate
base: develop
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
63872da
Test latest httpclient release candidate
carterkozak fc66835
work in progress
carterkozak 9760675
Handle updating the validaiton interval
carterkozak 52bfa0e
remove resolved fixme comment
carterkozak 0e37734
avoid cludging sslcontexts
carterkozak d88946d
reorganize IDLE_CONNECTION_TIMEOUT
carterkozak 8dd28d7
update javadoc based on refactor
carterkozak 8a5d598
remove release candidate repo -- use the true release build
carterkozak 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 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
74 changes: 74 additions & 0 deletions
74
...-hc5-client/src/main/java/com/palantir/dialogue/hc5/DialogueConnectionConfigResolver.java
This file contains 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,74 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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 com.palantir.dialogue.hc5; | ||
|
||
import org.apache.hc.client5.http.HttpRoute; | ||
import org.apache.hc.client5.http.config.ConnectionConfig; | ||
import org.apache.hc.core5.function.Resolver; | ||
import org.apache.hc.core5.util.TimeValue; | ||
import org.apache.hc.core5.util.Timeout; | ||
|
||
final class DialogueConnectionConfigResolver implements Resolver<HttpRoute, ConnectionConfig> { | ||
|
||
// Increased from two seconds to four seconds because we have strong support for retries | ||
// and can optimistically avoid expensive connection checks. Failures caused by NoHttpResponseExceptions | ||
// are possible when the target closes connections prior to this timeout, and can be safely retried. | ||
// Ideally this value would be larger for RPC, however some servers use relatively low defaults: | ||
// apache httpd versions 1.3 and 2.0: 15 seconds: | ||
// https://httpd.apache.org/docs/2.0/mod/core.html#keepalivetimeout | ||
// apache httpd version 2.2 and above: 5 seconds | ||
// https://httpd.apache.org/docs/2.2/mod/core.html#keepalivetimeout | ||
// nodejs http server: 5 seconds | ||
// https://nodejs.org/api/http.html#http_server_keepalivetimeout | ||
// nginx: 75 seconds (good) | ||
// https://nginx.org/en/docs/http/ngx_http_core_module.html#keepalive_timeout | ||
// dropwizard: 30 seconds (see idleTimeout in the linked docs) | ||
// https://www.dropwizard.io/en/latest/manual/configuration.html#Connectors | ||
// wc: 60 seconds (internal) | ||
private static final TimeValue CONNECTION_INACTIVITY_CHECK = TimeValue.ofMilliseconds( | ||
Integer.getInteger("dialogue.experimental.inactivity.check.threshold.millis", 4_000)); | ||
|
||
private final Timeout connectTimeout; | ||
private final Timeout socketTimeout; | ||
|
||
// We create a new connectionConfig when the connectionInactivityCheck interval changes | ||
// to avoid allocating a new ConnectionConfig each time the value is queried. | ||
private volatile ConnectionConfig connectionConfig; | ||
|
||
DialogueConnectionConfigResolver(Timeout connectTimeout, Timeout socketTimeout) { | ||
this.connectTimeout = connectTimeout; | ||
this.socketTimeout = socketTimeout; | ||
setValidateAfterInactivity(CONNECTION_INACTIVITY_CHECK); | ||
} | ||
|
||
void setValidateAfterInactivity(TimeValue connectionInactivityCheck) { | ||
connectionConfig = ConnectionConfig.custom() | ||
.setValidateAfterInactivity(connectionInactivityCheck) | ||
.setConnectTimeout(connectTimeout) | ||
.setSocketTimeout(socketTimeout) | ||
.build(); | ||
} | ||
|
||
TimeValue getValidateAfterInactivity() { | ||
return connectionConfig.getValidateAfterInactivity(); | ||
} | ||
|
||
@Override | ||
public ConnectionConfig resolve(HttpRoute _ignored) { | ||
return connectionConfig; | ||
} | ||
} |
120 changes: 120 additions & 0 deletions
120
...-apache-hc5-client/src/main/java/com/palantir/dialogue/hc5/DialogueTlsSocketStrategy.java
This file contains 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,120 @@ | ||
/* | ||
* (c) Copyright 2024 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed 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 com.palantir.dialogue.hc5; | ||
|
||
import com.palantir.logsafe.Preconditions; | ||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.X509Certificate; | ||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLException; | ||
import javax.net.ssl.SSLParameters; | ||
import javax.net.ssl.SSLPeerUnverifiedException; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import org.apache.hc.client5.http.config.TlsConfig; | ||
import org.apache.hc.client5.http.ssl.HttpClientHostnameVerifier; | ||
import org.apache.hc.client5.http.ssl.TlsSocketStrategy; | ||
import org.apache.hc.core5.http.protocol.HttpContext; | ||
import org.apache.hc.core5.io.Closer; | ||
import org.apache.hc.core5.util.Timeout; | ||
|
||
/** | ||
* {@link DialogueTlsSocketStrategy} is based closely on | ||
* {@code org.apache.hc.client5.http.ssl.AbstractClientTlsStrategy}, except that it only requires a | ||
* {@link SSLSocketFactory} rather than an {@link javax.net.ssl.SSLContext}. | ||
* We only implement the minimal required {@link TlsSocketStrategy} interface rather than | ||
* {@link org.apache.hc.core5.http.nio.ssl.TlsStrategy}, which isn't required by socket-based clients. | ||
*/ | ||
final class DialogueTlsSocketStrategy implements TlsSocketStrategy { | ||
|
||
private final SSLSocketFactory sslSocketFactory; | ||
private final String[] supportedProtocols; | ||
private final String[] supportedCipherSuites; | ||
private final HostnameVerifier hostnameVerifier; | ||
|
||
DialogueTlsSocketStrategy( | ||
SSLSocketFactory sslSocketFactory, | ||
String[] supportedProtocols, | ||
String[] supportedCipherSuites, | ||
HostnameVerifier hostnameVerifier) { | ||
this.sslSocketFactory = Preconditions.checkNotNull(sslSocketFactory, "SSLSocketFactory is required"); | ||
this.supportedProtocols = Preconditions.checkNotNull(supportedProtocols, "supportedProtocols is required"); | ||
this.supportedCipherSuites = | ||
Preconditions.checkNotNull(supportedCipherSuites, "supportedCipherSuites is required"); | ||
this.hostnameVerifier = Preconditions.checkNotNull(hostnameVerifier, "hostnameVerifier is required"); | ||
} | ||
|
||
@Override | ||
public SSLSocket upgrade(Socket socket, String target, int port, Object attachment, HttpContext _context) | ||
throws IOException { | ||
SSLSocket upgradedSocket = (SSLSocket) sslSocketFactory.createSocket(socket, target, port, false); | ||
try { | ||
executeHandshake(upgradedSocket, target, attachment); | ||
return upgradedSocket; | ||
} catch (IOException | RuntimeException ex) { | ||
Closer.closeQuietly(upgradedSocket); | ||
throw ex; | ||
} | ||
} | ||
|
||
private void executeHandshake(SSLSocket upgradedSocket, String target, Object attachment) throws IOException { | ||
SSLParameters sslParameters = upgradedSocket.getSSLParameters(); | ||
sslParameters.setProtocols(supportedProtocols); | ||
sslParameters.setCipherSuites(supportedCipherSuites); | ||
|
||
// If we want to enable the builtin hostname verification support: | ||
// sslParameters.setEndpointIdentificationAlgorithm(URIScheme.HTTPS.id); | ||
|
||
upgradedSocket.setSSLParameters(sslParameters); | ||
|
||
if (attachment instanceof TlsConfig) { | ||
TlsConfig tlsConfig = (TlsConfig) attachment; | ||
Timeout handshakeTimeout = tlsConfig.getHandshakeTimeout(); | ||
if (handshakeTimeout != null) { | ||
upgradedSocket.setSoTimeout(handshakeTimeout.toMillisecondsIntBound()); | ||
} | ||
} | ||
|
||
upgradedSocket.startHandshake(); | ||
verifySession(target, upgradedSocket.getSession(), hostnameVerifier); | ||
} | ||
|
||
private static void verifySession(String hostname, SSLSession sslsession, HostnameVerifier verifier) | ||
throws SSLException { | ||
if (verifier instanceof HttpClientHostnameVerifier) { | ||
X509Certificate x509Certificate = getX509Certificate(sslsession); | ||
((HttpClientHostnameVerifier) verifier).verify(hostname, x509Certificate); | ||
} else if (!verifier.verify(hostname, sslsession)) { | ||
throw new SSLPeerUnverifiedException("Certificate doesn't match any of the subject alternative names"); | ||
} | ||
} | ||
|
||
private static X509Certificate getX509Certificate(SSLSession sslsession) throws SSLPeerUnverifiedException { | ||
Certificate[] certs = sslsession.getPeerCertificates(); | ||
if (certs.length < 1) { | ||
throw new SSLPeerUnverifiedException("Peer certificate chain is empty"); | ||
} | ||
Certificate peerCertificate = certs[0]; | ||
if (peerCertificate instanceof X509Certificate) { | ||
return (X509Certificate) peerCertificate; | ||
} | ||
throw new SSLPeerUnverifiedException("Unexpected certificate type: " + peerCertificate.getType()); | ||
} | ||
} |
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.
I don't love boxing this
long
->Long
.The
onBefore
/onAfter
methods don't allow us to collect timing info when requests fail either, sinceonAfterSocketConnect
is never called in that case