Search in sources :

Example 1 with QuorumX509Util

use of org.apache.zookeeper.common.QuorumX509Util in project zookeeper by apache.

the class QuorumSSLTest method setup.

@BeforeEach
public void setup() throws Exception {
    quorumX509Util = new QuorumX509Util();
    ClientBase.setupTestEnv();
    tmpDir = createTmpDir().getAbsolutePath();
    clientPortQp1 = PortAssignment.unique();
    clientPortQp2 = PortAssignment.unique();
    clientPortQp3 = PortAssignment.unique();
    validKeystorePath = tmpDir + "/valid.jks";
    truststorePath = tmpDir + "/truststore.jks";
    quorumConfiguration = generateQuorumConfiguration();
    Security.addProvider(new BouncyCastleProvider());
    certStartTime = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(certStartTime);
    cal.add(Calendar.YEAR, 1);
    certEndTime = cal.getTime();
    rootKeyPair = createKeyPair();
    contentSigner = new JcaContentSignerBuilder("SHA256WithRSAEncryption").build(rootKeyPair.getPrivate());
    rootCertificate = createSelfSignedCertifcate(rootKeyPair);
    // Write the truststore
    KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
    trustStore.load(null, PASSWORD);
    trustStore.setCertificateEntry(rootCertificate.getSubjectDN().toString(), rootCertificate);
    FileOutputStream outputStream = new FileOutputStream(truststorePath);
    trustStore.store(outputStream, PASSWORD);
    outputStream.flush();
    outputStream.close();
    defaultKeyPair = createKeyPair();
    X509Certificate validCertificate = buildEndEntityCert(defaultKeyPair, rootCertificate, rootKeyPair.getPrivate(), HOSTNAME, "127.0.0.1", null, null);
    writeKeystore(validCertificate, defaultKeyPair, validKeystorePath);
    setSSLSystemProperties();
}
Also used : QuorumX509Util(org.apache.zookeeper.common.QuorumX509Util) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) Calendar(java.util.Calendar) FileOutputStream(java.io.FileOutputStream) KeyStore(java.security.KeyStore) Date(java.util.Date) X509Certificate(java.security.cert.X509Certificate) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with QuorumX509Util

use of org.apache.zookeeper.common.QuorumX509Util in project zookeeper by apache.

the class CnxManagerTest method testSSLSocketClosedWhenHandshakeTimeout.

/**
 * Test the SSLSocket is explicitly closed when there is IOException
 * happened during connect.
 */
@Test
public void testSSLSocketClosedWhenHandshakeTimeout() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    QuorumX509Util mockedX509Util = new QuorumX509Util() {

        @Override
        public SSLSocket createSSLSocket() {
            return new SSLSocket() {

                @Override
                public void connect(SocketAddress endpoint, int timeout) {
                }

                @Override
                public void startHandshake() throws IOException {
                    throw new IOException();
                }

                @Override
                public void close() {
                    closeLatch.countDown();
                }

                public String[] getSupportedCipherSuites() {
                    throw new UnsupportedOperationException();
                }

                public String[] getEnabledCipherSuites() {
                    throw new UnsupportedOperationException();
                }

                public String[] getSupportedProtocols() {
                    throw new UnsupportedOperationException();
                }

                public String[] getEnabledProtocols() {
                    throw new UnsupportedOperationException();
                }

                public SSLSession getSession() {
                    throw new UnsupportedOperationException();
                }

                public void setEnabledCipherSuites(String[] suites) {
                }

                public void setEnabledProtocols(String[] protocols) {
                }

                public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
                }

                public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {
                }

                public void setUseClientMode(boolean mode) {
                }

                public boolean getUseClientMode() {
                    return true;
                }

                public void setNeedClientAuth(boolean need) {
                }

                public boolean getNeedClientAuth() {
                    return true;
                }

                public void setWantClientAuth(boolean want) {
                }

                public boolean getWantClientAuth() {
                    return true;
                }

                public void setEnableSessionCreation(boolean flag) {
                }

                public boolean getEnableSessionCreation() {
                    return true;
                }
            };
        }
    };
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[0], peerTmpdir[0], peerClientPort[0], 3, 0, 2000, 2, 2, 2) {

        @Override
        public QuorumX509Util createX509Util() {
            return mockedX509Util;
        }
    };
    peer.setSslQuorum(true);
    QuorumCnxManager cnxManager = peer.createCnxnManager();
    cnxManager.connectOne(1, peers.get(1L).electionAddr);
    assertTrue(closeLatch.await(1, TimeUnit.SECONDS));
}
Also used : HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) QuorumX509Util(org.apache.zookeeper.common.QuorumX509Util) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) FLENewEpochTest(org.apache.zookeeper.test.FLENewEpochTest) Test(org.junit.jupiter.api.Test)

Aggregations

QuorumX509Util (org.apache.zookeeper.common.QuorumX509Util)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketAddress (java.net.SocketAddress)1 KeyStore (java.security.KeyStore)1 X509Certificate (java.security.cert.X509Certificate)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 HandshakeCompletedListener (javax.net.ssl.HandshakeCompletedListener)1 SSLSocket (javax.net.ssl.SSLSocket)1 FLENewEpochTest (org.apache.zookeeper.test.FLENewEpochTest)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1 JcaContentSignerBuilder (org.bouncycastle.operator.jcajce.JcaContentSignerBuilder)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1