This repository has been archived by the owner on Nov 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Better logging for endpoint verification #6314
Merged
bulldozer-bot
merged 7 commits into
develop
from
skramer/better-logging-for-host-verification
Oct 25, 2022
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
57a299d
Provide better logging for endpoint verification by listing all endpo…
82459c7
Test separate method for obtaining endpoints to verify
992f020
Also test case where we should not expect deduplication
3e585df
Throw socketexception when verifying endpoints if the socket is close…
6253c91
Change log var to be past tense
1c20c4f
Change exception type to SafeSSLPeerUnverifiedException
755df96
Add generated changelog entries
svc-changelog 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.benmanes.caffeine.cache.LoadingCache; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableSet; | ||
import com.palantir.atlasdb.cassandra.CassandraCredentialsConfig; | ||
import com.palantir.atlasdb.cassandra.CassandraKeyValueServiceConfig; | ||
import com.palantir.atlasdb.keyvalue.cassandra.ImmutableCassandraClientConfig.SocketTimeoutMillisBuildStage; | ||
|
@@ -44,7 +45,7 @@ | |
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.stream.Stream; | ||
import java.util.Set; | ||
import javax.net.ssl.SSLSocket; | ||
import javax.net.ssl.SSLSocketFactory; | ||
import org.apache.cassandra.thrift.AuthenticationRequest; | ||
|
@@ -179,8 +180,8 @@ private static Cassandra.Client getRawClient( | |
try { | ||
SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket( | ||
thriftSocket.getSocket(), addr.getHostString(), addr.getPort(), true); | ||
verifyEndpoint(cassandraServer, socket, clientConfig.enableEndpointVerification()); | ||
thriftSocket = tSocketFactory.create(socket); | ||
verifyEndpoint(cassandraServer, socket, clientConfig.enableEndpointVerification()); | ||
success = true; | ||
} catch (IOException e) { | ||
throw new TTransportException(e); | ||
|
@@ -222,23 +223,37 @@ private static void login(Client client, CassandraCredentialsConfig config) thro | |
* This will check both ip address/hostname, and uses the IP address associated with the socket, rather | ||
* that what has been provided. Hostname/ip address are both need to be checked, as historically we've | ||
* connected to Cassandra directly using IP addresses, and therefore need to support such cases. | ||
* | ||
* Will only throw when throwOnFailure is true, even if the socket is closed during verification. | ||
*/ | ||
@VisibleForTesting | ||
static void verifyEndpoint(CassandraServer cassandraServer, SSLSocket socket, boolean throwOnFailure) | ||
throws SafeSSLPeerUnverifiedException { | ||
boolean endpointVerified = Stream.of( | ||
socket.getInetAddress().getHostAddress(), cassandraServer.cassandraHostName()) | ||
.anyMatch(address -> hostnameVerifier.verify(address, socket.getSession())); | ||
|
||
Set<String> endpointsToCheck = getEndpointsToCheck(cassandraServer, socket); | ||
boolean endpointVerified = | ||
endpointsToCheck.stream().anyMatch(address -> hostnameVerifier.verify(address, socket.getSession())); | ||
if (socket.isClosed()) { | ||
if (throwOnFailure) { | ||
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. ah nice catch! |
||
throw new SafeSSLPeerUnverifiedException( | ||
"Unable to verify endpoints as socket is closed.", | ||
SafeArg.of("endpoint", socket.getInetAddress())); | ||
} | ||
return; | ||
} | ||
if (!endpointVerified) { | ||
log.warn("Endpoint verification failed for host.", SafeArg.of("cassandraServer", cassandraServer)); | ||
log.warn("Endpoint verification failed for host.", SafeArg.of("endpointsChecked", endpointsToCheck)); | ||
if (throwOnFailure) { | ||
throw new SafeSSLPeerUnverifiedException( | ||
"Endpoint verification failed for host.", SafeArg.of("cassandraServer", cassandraServer)); | ||
"Endpoint verification failed for host.", SafeArg.of("endpointsChecked", endpointsToCheck)); | ||
} | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
static Set<String> getEndpointsToCheck(CassandraServer cassandraServer, SSLSocket socket) { | ||
return ImmutableSet.of(socket.getInetAddress().getHostAddress(), cassandraServer.cassandraHostName()); | ||
} | ||
|
||
@Override | ||
public boolean validateObject(PooledObject<CassandraClient> client) { | ||
try { | ||
|
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
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.
Noticed this "bug" when revisiting things -- our initial reach-out will not include the thrift options we later set on the socket. It's probably OK for us to ignore this, but from a code "safety" point-of-view we should really just set the options for consistency reasons.
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.
yeah this makes sense, would recommend updating the PR description :)
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.
ah yes, I should do that!