Browse Source
Reviewers: Sriharsha Chintalapani <sriharsha@apache.org>, Ismael Juma <ismael@juma.me.uk>pull/6522/merge
saisandeep
6 years ago
committed by
Harsha
8 changed files with 332 additions and 9 deletions
@ -0,0 +1,113 @@
@@ -0,0 +1,113 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.kafka.common.security.ssl.mock; |
||||
|
||||
import javax.net.ssl.KeyManager; |
||||
import javax.net.ssl.KeyManagerFactorySpi; |
||||
import javax.net.ssl.ManagerFactoryParameters; |
||||
import javax.net.ssl.X509ExtendedKeyManager; |
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.net.Socket; |
||||
import java.security.GeneralSecurityException; |
||||
import java.security.KeyPair; |
||||
import java.security.KeyStore; |
||||
import java.security.Principal; |
||||
import java.security.PrivateKey; |
||||
import java.security.cert.X509Certificate; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
import org.apache.kafka.common.config.types.Password; |
||||
import org.apache.kafka.test.TestSslUtils; |
||||
import org.apache.kafka.test.TestSslUtils.CertificateBuilder; |
||||
|
||||
public class TestKeyManagerFactory extends KeyManagerFactorySpi { |
||||
public static final String ALGORITHM = "TestAlgorithm"; |
||||
|
||||
@Override |
||||
protected void engineInit(KeyStore keyStore, char[] chars) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected void engineInit(ManagerFactoryParameters managerFactoryParameters) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected KeyManager[] engineGetKeyManagers() { |
||||
return new KeyManager[] {new TestKeyManager()}; |
||||
} |
||||
|
||||
public static class TestKeyManager extends X509ExtendedKeyManager { |
||||
|
||||
public static String mockTrustStoreFile; |
||||
public static final String ALIAS = "TestAlias"; |
||||
private static final String CN = "localhost"; |
||||
private static final String SIGNATURE_ALGORITHM = "RSA"; |
||||
private KeyPair keyPair; |
||||
private X509Certificate certificate; |
||||
|
||||
protected TestKeyManager() { |
||||
try { |
||||
this.keyPair = TestSslUtils.generateKeyPair(SIGNATURE_ALGORITHM); |
||||
CertificateBuilder certBuilder = new CertificateBuilder(); |
||||
this.certificate = certBuilder.generate("CN=" + CN + ", O=A server", this.keyPair); |
||||
Map<String, X509Certificate> certificates = new HashMap<>(); |
||||
certificates.put(ALIAS, certificate); |
||||
File trustStoreFile = File.createTempFile("testTrustStore", ".jks"); |
||||
mockTrustStoreFile = trustStoreFile.getPath(); |
||||
TestSslUtils.createTrustStore(mockTrustStoreFile, new Password(TestSslUtils.TRUST_STORE_PASSWORD), certificates); |
||||
} catch (IOException | GeneralSecurityException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public String[] getClientAliases(String s, Principal[] principals) { |
||||
return new String[] {ALIAS}; |
||||
} |
||||
|
||||
@Override |
||||
public String chooseClientAlias(String[] strings, Principal[] principals, Socket socket) { |
||||
return ALIAS; |
||||
} |
||||
|
||||
@Override |
||||
public String[] getServerAliases(String s, Principal[] principals) { |
||||
return new String[] {ALIAS}; |
||||
} |
||||
|
||||
@Override |
||||
public String chooseServerAlias(String s, Principal[] principals, Socket socket) { |
||||
return ALIAS; |
||||
} |
||||
|
||||
@Override |
||||
public X509Certificate[] getCertificateChain(String s) { |
||||
return new X509Certificate[] {this.certificate}; |
||||
} |
||||
|
||||
@Override |
||||
public PrivateKey getPrivateKey(String s) { |
||||
return this.keyPair.getPrivate(); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
@ -0,0 +1,36 @@
@@ -0,0 +1,36 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.kafka.common.security.ssl.mock; |
||||
|
||||
import java.security.Provider; |
||||
|
||||
public class TestProvider extends Provider { |
||||
|
||||
private static final String KEY_MANAGER_FACTORY = String.format("KeyManagerFactory.%s", TestKeyManagerFactory.ALGORITHM); |
||||
private static final String TRUST_MANAGER_FACTORY = String.format("TrustManagerFactory.%s", TestTrustManagerFactory.ALGORITHM); |
||||
|
||||
public TestProvider() { |
||||
this("TestProvider", 0.1, "provider for test cases"); |
||||
} |
||||
|
||||
protected TestProvider(String name, double version, String info) { |
||||
super(name, version, info); |
||||
super.put(KEY_MANAGER_FACTORY, TestKeyManagerFactory.class.getName()); |
||||
super.put(TRUST_MANAGER_FACTORY, TestTrustManagerFactory.class.getName()); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
/* |
||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||
* contributor license agreements. See the NOTICE file distributed with |
||||
* this work for additional information regarding copyright ownership. |
||||
* The ASF licenses this file to You 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 org.apache.kafka.common.security.ssl.mock; |
||||
|
||||
import javax.net.ssl.ManagerFactoryParameters; |
||||
import javax.net.ssl.SSLEngine; |
||||
import javax.net.ssl.TrustManager; |
||||
import javax.net.ssl.TrustManagerFactorySpi; |
||||
import javax.net.ssl.X509ExtendedTrustManager; |
||||
import java.net.Socket; |
||||
import java.security.KeyStore; |
||||
import java.security.cert.CertificateException; |
||||
import java.security.cert.X509Certificate; |
||||
|
||||
public class TestTrustManagerFactory extends TrustManagerFactorySpi { |
||||
public static final String ALGORITHM = "TestAlgorithm"; |
||||
|
||||
@Override |
||||
protected void engineInit(KeyStore keyStore) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected void engineInit(ManagerFactoryParameters managerFactoryParameters) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
protected TrustManager[] engineGetTrustManagers() { |
||||
return new TrustManager[] {new TestTrustManager()}; |
||||
} |
||||
|
||||
public static class TestTrustManager extends X509ExtendedTrustManager { |
||||
|
||||
public static final String ALIAS = "TestAlias"; |
||||
|
||||
@Override |
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public X509Certificate[] getAcceptedIssuers() { |
||||
return new X509Certificate[0]; |
||||
} |
||||
|
||||
@Override |
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException { |
||||
|
||||
} |
||||
} |
||||
|
||||
} |
||||
|
Loading…
Reference in new issue