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

CASSANDRA-18428: Adding equals/hashCode override for the ServerEncryptionOptions #2507

Closed
Closed
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
37 changes: 37 additions & 0 deletions src/java/org/apache/cassandra/config/EncryptionOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,43 @@ public boolean isExplicitlyOptional()
return optional != null && optional;
}

/**
* The method is being mainly used to cache SslContexts therefore, we only consider
* fields that would make a difference when the TrustStore or KeyStore files are updated
*/
@Override
public boolean equals(Object o)
{
if (o == this)
return true;
if (o == null || getClass() != o.getClass())
return false;
if (!super.equals(o))
return false;

ServerEncryptionOptions opt = (ServerEncryptionOptions) o;
return internode_encryption == opt.internode_encryption &&
legacy_ssl_storage_port_enabled == opt.legacy_ssl_storage_port_enabled &&
Objects.equals(outbound_keystore, opt.outbound_keystore) &&
Objects.equals(outbound_keystore_password, opt.outbound_keystore_password);
}

/**
* The method is being mainly used to cache SslContexts therefore, we only consider
* fields that would make a difference when the TrustStore or KeyStore files are updated
*/
@Override
public int hashCode()
{
int result = 0;
result += 31 * super.hashCode();
result += 31 * (internode_encryption == null ? 0 : internode_encryption.hashCode());
result += 31 * Boolean.hashCode(legacy_ssl_storage_port_enabled);
result += 31 * (outbound_keystore == null ? 0 : outbound_keystore.hashCode());
result += 31 * (outbound_keystore_password == null ? 0 : outbound_keystore_password.hashCode());
return result;
}

public ServerEncryptionOptions withSslContextFactory(ParameterizedClass sslContextFactoryClass)
{
return new ServerEncryptionOptions(sslContextFactoryClass, keystore, keystore_password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
*/
public class EncryptionOptionsEqualityTest
{
private EncryptionOptions.ServerEncryptionOptions createServerEncryptionOptions()
{
return new EncryptionOptions.ServerEncryptionOptions()
.withStoreType("JKS")
.withKeyStore("test/conf/cassandra.keystore")
.withKeyStorePassword("cassandra")
.withTrustStore("test/conf/cassandra_ssl_test.truststore")
.withTrustStorePassword("cassandra")
.withOutboundKeystore("test/conf/cassandra_outbound.keystore")
.withOutboundKeystorePassword("cassandra")
.withProtocol("TLSv1.1")
.withRequireClientAuth(true)
.withRequireEndpointVerification(false);
}

@Test
public void testKeystoreOptions() {
EncryptionOptions encryptionOptions1 =
Expand Down Expand Up @@ -139,4 +154,50 @@ public void testDifferentCustomSslContextFactoryParameters() {
assertNotEquals(encryptionOptions1, encryptionOptions2);
assertNotEquals(encryptionOptions1.hashCode(), encryptionOptions2.hashCode());
}

@Test
public void testServerEncryptionOptions()
{
EncryptionOptions.ServerEncryptionOptions encryptionOptions1 = createServerEncryptionOptions();
EncryptionOptions.ServerEncryptionOptions encryptionOptions2 = createServerEncryptionOptions();

assertEquals(encryptionOptions1, encryptionOptions2);
assertEquals(encryptionOptions1.hashCode(), encryptionOptions2.hashCode());
}

@Test
public void testServerEncryptionOptionsMismatchForOutboundKeystore()
{
EncryptionOptions.ServerEncryptionOptions encryptionOptions1 = createServerEncryptionOptions();
EncryptionOptions.ServerEncryptionOptions encryptionOptions2 = createServerEncryptionOptions();

encryptionOptions1 = encryptionOptions1
.withOutboundKeystore("test/conf/cassandra_outbound1.keystore")
.withOutboundKeystorePassword("cassandra1");

encryptionOptions2 = encryptionOptions2
.withOutboundKeystore("test/conf/cassandra_outbound2.keystore")
.withOutboundKeystorePassword("cassandra2");

assertNotEquals(encryptionOptions1, encryptionOptions2);
assertNotEquals(encryptionOptions1.hashCode(), encryptionOptions2.hashCode());
}

@Test
public void testServerEncryptionOptionsMismatchForInboundKeystore()
{
EncryptionOptions.ServerEncryptionOptions encryptionOptions1 = createServerEncryptionOptions();
EncryptionOptions.ServerEncryptionOptions encryptionOptions2 = createServerEncryptionOptions();

encryptionOptions1 = encryptionOptions1
.withKeyStore("test/conf/cassandra1.keystore")
.withKeyStorePassword("cassandra1");

encryptionOptions2 = encryptionOptions2
.withKeyStore("test/conf/cassandra2.keystore")
.withKeyStorePassword("cassandra2");

assertNotEquals(encryptionOptions1, encryptionOptions2);
assertNotEquals(encryptionOptions1.hashCode(), encryptionOptions2.hashCode());
}
}