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
Ability to configure SSL truststore and keystore for SR Clients #957
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
57354af
Ability to configure SSL truststore and keystore for SR Clients
mageshn 2b4bc11
Rebase & use configs defs from AK
mageshn d2da35e
Fix formatting
mageshn fd84db3
Add integration test for SSL
mageshn da94f60
throw exception in test case
mageshn 0ec98b0
Merge branch 'master' into CC-3074
rayokota File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
144 changes: 144 additions & 0 deletions
144
core/src/test/java/io/confluent/kafka/schemaregistry/rest/RestApiSslTest.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,144 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.kafka.schemaregistry.rest; | ||
|
||
import io.confluent.kafka.schemaregistry.ClusterTestHarness; | ||
import io.confluent.kafka.schemaregistry.avro.AvroCompatibilityLevel; | ||
import io.confluent.kafka.schemaregistry.avro.AvroUtils; | ||
import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient; | ||
import io.confluent.kafka.schemaregistry.client.SchemaRegistryClientConfig; | ||
import org.apache.avro.Schema; | ||
import org.apache.kafka.common.config.types.Password; | ||
import org.junit.Test; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.SSLSession; | ||
import javax.security.auth.login.Configuration; | ||
import java.io.File; | ||
import java.security.KeyPair; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class RestApiSslTest extends ClusterTestHarness { | ||
|
||
Properties props = new Properties(); | ||
|
||
public RestApiSslTest() { | ||
super(1, true, AvroCompatibilityLevel.BACKWARD.name); | ||
} | ||
|
||
|
||
@Test | ||
public void testRegisterWithClientSecurity() throws Exception { | ||
|
||
setupHostNameVerifier(); | ||
|
||
String subject = "testSubject"; | ||
Schema schema = AvroUtils.parseSchema( | ||
"{\"type\":\"record\"," | ||
+ "\"name\":\"myrecord\"," | ||
+ "\"fields\":" | ||
+ "[{\"type\":\"string\",\"name\":\"f1\"}]}").schemaObj; | ||
|
||
int expectedIdSchema1 = 1; | ||
|
||
Map clientsslConfigs = new HashMap(); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_PROTOCOL_CONFIG, | ||
"TLS"); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_KEYSTORE_LOCATION_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_KEYSTORE_LOCATION_CONFIG)); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_KEYSTORE_PASSWORD_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_KEYSTORE_PASSWORD_CONFIG)); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_KEY_PASSWORD_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_KEYSTORE_PASSWORD_CONFIG)); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_KEYSTORE_TYPE_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_KEYSTORE_TYPE_CONFIG)); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_TRUSTSTORE_LOCATION_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_TRUSTSTORE_LOCATION_CONFIG)); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_TRUSTSTORE_PASSWORD_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_TRUSTSTORE_PASSWORD_CONFIG)); | ||
clientsslConfigs.put( | ||
SchemaRegistryClientConfig.CLIENT_NAMESPACE + SchemaRegistryConfig.SSL_TRUSTSTORE_TYPE_CONFIG, | ||
props.get(SchemaRegistryConfig.SSL_TRUSTSTORE_TYPE_CONFIG)); | ||
CachedSchemaRegistryClient schemaRegistryClient = new CachedSchemaRegistryClient(restApp.restClient, 10, clientsslConfigs); | ||
|
||
assertEquals( | ||
"Registering should succeed", | ||
expectedIdSchema1, | ||
schemaRegistryClient.register(subject, schema) | ||
); | ||
|
||
} | ||
|
||
|
||
@Override | ||
protected Properties getSchemaRegistryProperties() { | ||
Configuration.setConfiguration(null); | ||
props.put( | ||
SchemaRegistryConfig.SCHEMAREGISTRY_INTER_INSTANCE_PROTOCOL_CONFIG, | ||
"https" | ||
); | ||
props.put(SchemaRegistryConfig.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, ""); | ||
try { | ||
File trustStoreFile = File.createTempFile("truststore", ".jks"); | ||
trustStoreFile.deleteOnExit(); | ||
List<X509Certificate> clientCerts = new ArrayList<>(); | ||
|
||
List<KeyPair> keyPairs = new ArrayList<>(); | ||
props.putAll( | ||
SecureTestUtils.clientSslConfigsWithKeyStore(1, trustStoreFile, new Password | ||
("TrustPassword"), clientCerts, | ||
keyPairs | ||
)); | ||
props.put(SchemaRegistryConfig.SSL_CLIENT_AUTHENTICATION_CONFIG, SchemaRegistryConfig.SSL_CLIENT_AUTHENTICATION_REQUIRED); | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return props; | ||
} | ||
|
||
@Override | ||
protected String getSchemaRegistryProtocol() { | ||
return "https"; | ||
} | ||
|
||
private void setupHostNameVerifier() { | ||
// Create all-trusting host name verifier | ||
HostnameVerifier allHostsValid = new HostnameVerifier() { | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
}; | ||
// Install the all-trusting host verifier | ||
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
core/src/test/java/io/confluent/kafka/schemaregistry/rest/SecureTestUtils.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,118 @@ | ||
/* | ||
* Copyright 2019 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.kafka.schemaregistry.rest; | ||
|
||
import org.apache.kafka.common.config.types.Password; | ||
import org.apache.kafka.test.TestSslUtils; | ||
|
||
import javax.net.ssl.TrustManagerFactory; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyPair; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.X509Certificate; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Properties; | ||
|
||
public class SecureTestUtils { | ||
|
||
public static Properties clientSslConfigsWithKeyStore( | ||
int numberOfCerts, | ||
File trustStoreFile, | ||
Password trustPassword, | ||
List<X509Certificate> clientCerts, | ||
List<KeyPair> keyPairs | ||
) throws GeneralSecurityException, IOException { | ||
|
||
Map<String, X509Certificate> certificateMap = new HashMap<>(); | ||
|
||
File clientKSFile = File.createTempFile("CKeystore", ".jks"); | ||
String keyStorePassword = new Password("Client-KS-Password").value(); | ||
|
||
for (int i = 0; i < numberOfCerts; i++) { | ||
KeyPair kp = TestSslUtils.generateKeyPair("RSA"); | ||
X509Certificate cert = TestSslUtils.generateCertificate( | ||
"CN=localhost, O=Client" + i, kp, 30, "SHA1withRSA"); | ||
|
||
clientCerts.add(cert); | ||
keyPairs.add(kp); | ||
certificateMap.put("client-" + i, cert); | ||
} | ||
|
||
createKeyStore(clientKSFile, keyStorePassword, clientCerts, keyPairs); | ||
|
||
TestSslUtils.createTrustStore(trustStoreFile.toString(), trustPassword, certificateMap); | ||
|
||
Properties sslConfigs = | ||
getClientSslConfigs(trustStoreFile, trustPassword.value(), clientKSFile, keyStorePassword); | ||
|
||
return sslConfigs; | ||
} | ||
|
||
public static void createKeyStore( | ||
File keyStoreFile, | ||
String keyStorePassword, | ||
List<X509Certificate> clientCerts, | ||
List<KeyPair> keyPairs | ||
) throws CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException { | ||
KeyStore keyStore = KeyStore.getInstance("JKS"); | ||
keyStore.load(null, null); | ||
|
||
for (int i = 0; i < clientCerts.size(); i++) { | ||
keyStore.setKeyEntry( | ||
"client-" + i, | ||
keyPairs.get(i).getPrivate(), | ||
keyStorePassword.toCharArray(), | ||
new Certificate[]{clientCerts.get(i)} | ||
); | ||
} | ||
|
||
FileOutputStream out = new FileOutputStream(keyStoreFile); | ||
keyStore.store(out, keyStorePassword.toCharArray()); | ||
out.close(); | ||
|
||
} | ||
|
||
private static Properties getClientSslConfigs( | ||
File trustStoreFile, String trustPassword, | ||
File clientKSFile, String keyStorePassword | ||
) { | ||
//makes sure client also runs TLSv1.2 | ||
System.setProperty("https.protocols", "TLSv1.2"); | ||
Properties sslConfigs = new Properties(); | ||
sslConfigs.put("ssl.protocol", "TLSv1.2"); | ||
sslConfigs.put("ssl.keystore.location", clientKSFile.getPath()); | ||
sslConfigs.put("ssl.keystore.type", "JKS"); | ||
sslConfigs.put("ssl.keymanager.algorithm", TrustManagerFactory.getDefaultAlgorithm()); | ||
sslConfigs.put("ssl.keystore.password", keyStorePassword); | ||
sslConfigs.put("ssl.key.password", keyStorePassword); | ||
sslConfigs.put("ssl.truststore.location", trustStoreFile.getPath()); | ||
sslConfigs.put("ssl.truststore.password", trustPassword); | ||
sslConfigs.put("ssl.truststore.type", "JKS"); | ||
sslConfigs.put("ssl.trustmanager.algorithm", TrustManagerFactory.getDefaultAlgorithm()); | ||
sslConfigs.put("ssl.enabled.protocols", "TLSv1.2"); | ||
sslConfigs.put("security.protocol", "SSL"); | ||
return sslConfigs; | ||
} | ||
} |
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.
Do you want to rethrow the exception?