You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2022/05/25 19:45:57 UTC
[GitHub] [cassandra] jonmeredith commented on a diff in pull request #1645: CASSANDRA-17661 Adding support to perform certificate based internode authentication
jonmeredith commented on code in PR #1645:
URL: https://github.com/apache/cassandra/pull/1645#discussion_r882025839
##########
src/java/org/apache/cassandra/auth/IInternodeAuthenticator.java:
##########
@@ -35,10 +36,51 @@
*/
boolean authenticate(InetAddress remoteAddress, int remotePort);
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(Certificate[] certificates)
+ {
+ return false;
+ }
+
+
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param remoteAddress ip address of the connecting node.
+ * @param remorePort port of the connecting node.
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(InetAddress remoteAddress, int remorePort, Certificate[] certificates)
+ {
+ if (certificates != null)
+ {
+ return authenticate(certificates);
+ }
Review Comment:
I think this can be replaced with a comment saying something like `Default implementation calls the deprecated authenticate by IP and port method`.
##########
src/java/org/apache/cassandra/auth/IInternodeAuthenticator.java:
##########
@@ -35,10 +36,51 @@
*/
boolean authenticate(InetAddress remoteAddress, int remotePort);
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(Certificate[] certificates)
+ {
+ return false;
+ }
Review Comment:
I don't think you need this new method. It would be up to the implementor to write an `authenticator` method that checks the certificates if that's what they need.
##########
src/java/org/apache/cassandra/auth/IInternodeAuthenticator.java:
##########
@@ -35,10 +36,51 @@
*/
boolean authenticate(InetAddress remoteAddress, int remotePort);
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(Certificate[] certificates)
+ {
+ return false;
+ }
+
+
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param remoteAddress ip address of the connecting node.
+ * @param remorePort port of the connecting node.
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(InetAddress remoteAddress, int remorePort, Certificate[] certificates)
+ {
+ if (certificates != null)
+ {
+ return authenticate(certificates);
+ }
+ return authenticate(remoteAddress, remorePort);
+ }
+
/**
* Validates configuration of IInternodeAuthenticator implementation (if configurable).
*
* @throws ConfigurationException when there is a configuration error.
*/
void validateConfiguration() throws ConfigurationException;
+
+ /**
+ * Setup is called once upon system startup to initialize the IAuthenticator.
+ *
+ * For example, use this method to create any required keyspaces/column families.
+ */
+ default void setup()
Review Comment:
If somebody wanted to centralize all their client `IAuthenticator` and internode auth `IInternodeAuthenticator` by implementing both in the same class the compiler would forbid with an `inherits unrelated defaults for setup()` error. WDYT about renaming this `setupInternode` to make that possible.
##########
src/java/org/apache/cassandra/net/OutboundConnectionInitiator.java:
##########
@@ -211,6 +204,15 @@ public void initChannel(SocketChannel channel) throws Exception
logger.trace("creating outbound netty SslContext: context={}, engine={}", sslContext.getClass().getName(), sslHandler.engine().getClass().getName());
pipeline.addFirst("ssl", sslHandler);
}
+ else
+ {
+ // If a connection is SSL based we don't need to make additional authentication when making a client
+ // connection. Trusted roots in the client's truststore would suffice for authentication.
+ //
+ // Outbound connection is a client connection we don't need to extract identity from server certificate and authenticate.
+ // Add this ServerAuthenticationHandler only when encryption is not enabled just for backward compatibility.
+ pipeline.addLast("server-authentication", new ServerAuthenticationHandler());
Review Comment:
I think you should call the authenticator for outbound too. This changes the behavior of always authenticating on outbound regardless of SSL.
It may not be true for all operators that the trusted root check is sufficient and they may also want to check the outbound IP addresses too, the age of the certificate or log the authentication attempt. Adding the direction to the `authenticate` method would make it easy to implement different behaviors on inbound/outbound.
##########
src/java/org/apache/cassandra/auth/IInternodeAuthenticator.java:
##########
@@ -35,10 +36,51 @@
*/
boolean authenticate(InetAddress remoteAddress, int remotePort);
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(Certificate[] certificates)
+ {
+ return false;
+ }
+
+
+ /**
+ * Decides whether a peer is allowed to connect to this node.
+ * If this method returns false, the socket will be immediately closed.
+ *
+ * @param remoteAddress ip address of the connecting node.
+ * @param remorePort port of the connecting node.
+ * @param certificates client certificates
+ * @return true if the connection should be accepted, false otherwise.
+ */
+ default boolean authenticate(InetAddress remoteAddress, int remorePort, Certificate[] certificates)
Review Comment:
The other piece of information that could be useful is whether this is an inbound or outbound internode connection being authenticated. Organizations may have different standards for the two types of collection and/or create different audit messages depending on the connection.
Maybe an `enum` argument for connections direction `INTERNODE_INBOUND`, `INTERNODE_OUTBOUND` ?
##########
test/distributed/org/apache/cassandra/distributed/test/InternodeEncryptionEnforcementTest.java:
##########
@@ -40,6 +48,103 @@
public final class InternodeEncryptionEnforcementTest extends TestBaseImpl
{
+
+ @Test
+ public void testConnectionsAreRejectedWhenAuthFails() throws IOException, InterruptedException
+ {
+ Cluster.Builder builder = builder()
+ .withNodes(2)
+ .withConfig(c ->
+ {
+ c.with(Feature.NETWORK);
+ c.with(Feature.NATIVE_PROTOCOL);
+
+ HashMap<String, Object> encryption = new HashMap<>();
+ encryption.put("keystore", "test/conf/cassandra_ssl_test.keystore");
+ encryption.put("keystore_password", "cassandra");
+ encryption.put("truststore", "test/conf/cassandra_ssl_test.truststore");
+ encryption.put("truststore_password", "cassandra");
+ encryption.put("internode_encryption", "dc");
+ encryption.put("require_client_auth", "true");
+ c.set("server_encryption_options", encryption);
+ c.set("internode_authenticator", AllowNothingAuthenticator.class.getName());
+ })
+ .withNodeIdTopology(ImmutableMap.of(1, NetworkTopology.dcAndRack("dc1", "r1a"),
+ 2, NetworkTopology.dcAndRack("dc2", "r2a")));
+
+ final ExecutorService executorService = Executors.newSingleThreadExecutor();
+ try (Cluster cluster = builder.start())
+ {
+ executorService.submit(() -> {
+ openConnections(cluster);
+ });
+
+ /*
+ * instance (1) should connect to instance (2) without any issues;
+ * instance (2) should connect to instance (1) without any issues.
+ */
+ IIsolatedExecutor.SerializableCallable<Boolean> callable = () ->
+ {
+ InboundMessageHandlers inbound = getOnlyElement(MessagingService.instance().messageHandlers.values());
+ assertEquals(0, inbound.count());
+ OutboundConnections outbound = getOnlyElement(MessagingService.instance().channelManagers.values());
+ assertTrue(!outbound.small.isConnected() && !outbound.large.isConnected() && !outbound.urgent.isConnected());
+ return true;
+ };
+
+ // Wait for cluster to get started
+ Thread.sleep(3000);
+ assertTrue(cluster.get(1).callsOnInstance(callable).call());
+ assertTrue(cluster.get(2).callsOnInstance(callable).call());
+ }
+ executorService.shutdown();
+ }
+
+ @Test
+ public void testConnectionsAreAcceptedWhenAuthSucceds() throws IOException
+ {
+ Cluster.Builder builder = builder()
+ .withNodes(2)
+ .withConfig(c ->
+ {
+ c.with(Feature.NETWORK);
+ c.with(Feature.NATIVE_PROTOCOL);
+
+ HashMap<String, Object> encryption = new HashMap<>();
+ encryption.put("keystore", "test/conf/cassandra_ssl_test.keystore");
+ encryption.put("keystore_password", "cassandra");
+ encryption.put("truststore", "test/conf/cassandra_ssl_test.truststore");
+ encryption.put("truststore_password", "cassandra");
+ encryption.put("internode_encryption", "dc");
+ encryption.put("require_client_auth", "true");
+ c.set("server_encryption_options", encryption);
+ c.set("internode_authenticator", "org.apache.cassandra.auth.AllowAllInternodeAuthenticator");
+ })
+ .withNodeIdTopology(ImmutableMap.of(1, NetworkTopology.dcAndRack("dc1", "r1a"),
+ 2, NetworkTopology.dcAndRack("dc2", "r2a")));
+ try (Cluster cluster = builder.start())
+ {
+ openConnections(cluster);
+
+ /*
+ * instance (1) should connect to instance (2) without any issues;
+ * instance (2) should connect to instance (1) without any issues.
+ */
+
+ SerializableRunnable runnable = () ->
+ {
+ InboundMessageHandlers inbound = getOnlyElement(MessagingService.instance().messageHandlers.values());
+ assertTrue(inbound.count() > 0);
+
+ OutboundConnections outbound = getOnlyElement(MessagingService.instance().channelManagers.values());
+ assertTrue(outbound.small.isConnected() || outbound.large.isConnected() || outbound.urgent.isConnected());
+ };
+
+ cluster.get(1).runOnInstance(runnable);
+ cluster.get(2).runOnInstance(runnable);
+ }
+ }
+
Review Comment:
Could you add a test that checks the `MessagingService.instance().interruptOutbound(settings.to);` functionality
I think you could add an authenticator that works on the first attempt, but fails all subsequent attempts. You could use the `internode_outbound` table to confirm no connections were made. Might be nice to add that check to the other tests just to make sure the connection hadn't been established then immediately dropped.
##########
test/distributed/org/apache/cassandra/distributed/test/InternodeEncryptionEnforcementTest.java:
##########
@@ -40,6 +48,103 @@
public final class InternodeEncryptionEnforcementTest extends TestBaseImpl
{
+
+ @Test
+ public void testConnectionsAreRejectedWhenAuthFails() throws IOException, InterruptedException
+ {
+ Cluster.Builder builder = builder()
+ .withNodes(2)
+ .withConfig(c ->
+ {
+ c.with(Feature.NETWORK);
+ c.with(Feature.NATIVE_PROTOCOL);
+
+ HashMap<String, Object> encryption = new HashMap<>();
+ encryption.put("keystore", "test/conf/cassandra_ssl_test.keystore");
+ encryption.put("keystore_password", "cassandra");
+ encryption.put("truststore", "test/conf/cassandra_ssl_test.truststore");
+ encryption.put("truststore_password", "cassandra");
+ encryption.put("internode_encryption", "dc");
+ encryption.put("require_client_auth", "true");
+ c.set("server_encryption_options", encryption);
+ c.set("internode_authenticator", AllowNothingAuthenticator.class.getName());
+ })
+ .withNodeIdTopology(ImmutableMap.of(1, NetworkTopology.dcAndRack("dc1", "r1a"),
+ 2, NetworkTopology.dcAndRack("dc2", "r2a")));
+
+ final ExecutorService executorService = Executors.newSingleThreadExecutor();
+ try (Cluster cluster = builder.start())
+ {
+ executorService.submit(() -> {
+ openConnections(cluster);
+ });
+
+ /*
+ * instance (1) should connect to instance (2) without any issues;
+ * instance (2) should connect to instance (1) without any issues.
+ */
Review Comment:
Comment does not match code.
How do you know the failed connection was due to the authenticator failing the attempt rather than another issue? You could add an @Shared class that either holds counters or a log of authentication attempts in the test.
--
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: pr-unsubscribe@cassandra.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org