risdenk commented on a change in pull request #63: KNOX-1801 - Master secret is incorrectly assumed when a custom truststore is not specified when clientauth is enabled URL: https://github.com/apache/knox/pull/63#discussion_r262597821
########## File path: gateway-server/src/test/java/org/apache/knox/gateway/services/security/impl/JettySSLServiceTest.java ########## @@ -0,0 +1,475 @@ +/* + * 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.knox.gateway.services.security.impl; + +import static org.easymock.EasyMock.eq; +import static org.easymock.EasyMock.expect; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import org.apache.knox.gateway.config.GatewayConfig; +import org.apache.knox.gateway.services.security.AliasService; +import org.apache.knox.gateway.services.security.AliasServiceException; +import org.apache.knox.gateway.services.security.KeystoreService; +import org.easymock.EasyMockSupport; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.junit.Test; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.UnrecoverableKeyException; + +public class JettySSLServiceTest extends EasyMockSupport { + @Test + public void TestBuildSslContextFactoryOnlyIdentityKeystore() throws Exception { + String basedir = System.getProperty("basedir"); + if (basedir == null) { + basedir = new File(".").getCanonicalPath(); + } + + Path identityKeystorePath = Paths.get(basedir, "target", "test-classes", "keystores", "server-keystore.jks"); + String identityKeystoreType = "jks"; + char[] identityKeystorePassword = "horton".toCharArray(); + char[] identityKeyPassphrase = "horton".toCharArray(); + String identityKeyAlias = "server"; + Path truststorePath = Paths.get(basedir, "target", "test-classes", "keystores", "server-truststore.jks"); + String truststoreType = "jks"; + + GatewayConfig config = createGatewayConfig(false, false, identityKeystorePath, identityKeystoreType, identityKeyAlias, truststorePath, truststoreType); + + AliasService aliasService = createMock(AliasService.class); + expect(aliasService.getGatewayIdentityKeystorePassword()).andReturn(identityKeystorePassword).atLeastOnce(); + expect(aliasService.getGatewayIdentityPassphrase()).andReturn(identityKeyPassphrase).atLeastOnce(); + + KeystoreService keystoreService = createMock(KeystoreService.class); + + replayAll(); + + JettySSLService sslService = new JettySSLService(); + sslService.setAliasService(aliasService); + sslService.setKeystoreService(keystoreService); + + Object result = sslService.buildSslContextFactory(config); + assertNotNull(result); + assertTrue(result instanceof SslContextFactory); + + SslContextFactory sslContextFactory = (SslContextFactory) result; + sslContextFactory.start(); + + assertEquals(identityKeystorePath.toUri().toString(), sslContextFactory.getKeyStorePath()); + assertEquals(identityKeystoreType, sslContextFactory.getKeyStoreType()); + assertNotNull(sslContextFactory.getKeyStore()); + + assertNull(sslContextFactory.getTrustStorePath()); + assertNull(sslContextFactory.getTrustStoreType()); + + // If the truststore is not set, by default the identity keystore is used by Jetty. + assertEquals(sslContextFactory.getKeyStore().size(), sslContextFactory.getTrustStore().size()); + assertTrue(sslContextFactory.getTrustStore().containsAlias(identityKeyAlias)); + verifyAll(); + } + + @Test(expected = AliasServiceException.class) + public void TestBuildSslContextFactoryOnlyIdentityKeystoreErrorGettingPassword() throws Exception { + String basedir = System.getProperty("basedir"); + if (basedir == null) { + basedir = new File(".").getCanonicalPath(); + } + + Path identityKeystorePath = Paths.get(basedir, "target", "test-classes", "keystores", "server-keystore.jks"); + String identityKeystoreType = "jks"; + char[] identityKeyPassphrase = "horton".toCharArray(); + String identityKeyAlias = "server"; + Path truststorePath = Paths.get(basedir, "target", "test-classes", "keystores", "server-truststore.jks"); + String truststoreType = "jks"; + + GatewayConfig config = createGatewayConfig(false, false, identityKeystorePath, identityKeystoreType, identityKeyAlias, truststorePath, truststoreType); + + AliasService aliasService = createMock(AliasService.class); + expect(aliasService.getGatewayIdentityKeystorePassword()).andThrow(new AliasServiceException(null)).atLeastOnce(); + expect(aliasService.getGatewayIdentityPassphrase()).andReturn(identityKeyPassphrase).atLeastOnce(); + + KeystoreService keystoreService = createMock(KeystoreService.class); + + replayAll(); + + JettySSLService sslService = new JettySSLService(); + sslService.setAliasService(aliasService); + sslService.setKeystoreService(keystoreService); + + sslService.buildSslContextFactory(config); + + fail("AliasServiceException should have been thrown"); + } + + @Test + public void TestBuildSslContextFactoryOnlyIdentityKeystoreNullKeystorePassword() throws Exception { + String basedir = System.getProperty("basedir"); + if (basedir == null) { + basedir = new File(".").getCanonicalPath(); + } + + Path identityKeystorePath = Paths.get(basedir, "target", "test-classes", "keystores", "server-keystore.jks"); + String identityKeystoreType = "jks"; + char[] identityKeyPassphrase = "horton".toCharArray(); + String identityKeyAlias = "server"; + Path truststorePath = Paths.get(basedir, "target", "test-classes", "keystores", "server-truststore.jks"); + String truststoreType = "jks"; + + GatewayConfig config = createGatewayConfig(false, false, identityKeystorePath, identityKeystoreType, identityKeyAlias, truststorePath, truststoreType); + + AliasService aliasService = createMock(AliasService.class); + expect(aliasService.getGatewayIdentityKeystorePassword()).andReturn(null).atLeastOnce(); + expect(aliasService.getGatewayIdentityPassphrase()).andReturn(identityKeyPassphrase).atLeastOnce(); + + KeystoreService keystoreService = createMock(KeystoreService.class); + + replayAll(); + + JettySSLService sslService = new JettySSLService(); + sslService.setAliasService(aliasService); + sslService.setKeystoreService(keystoreService); + + Object result = sslService.buildSslContextFactory(config); + assertNotNull(result); + assertTrue(result instanceof SslContextFactory); + + SslContextFactory sslContextFactory = (SslContextFactory) result; + sslContextFactory.start(); + + assertEquals(identityKeystorePath.toUri().toString(), sslContextFactory.getKeyStorePath()); + assertEquals(identityKeystoreType, sslContextFactory.getKeyStoreType()); + assertNotNull(sslContextFactory.getKeyStore()); + + assertNull(sslContextFactory.getTrustStorePath()); + assertNull(sslContextFactory.getTrustStoreType()); + + // If the truststore is not set, by default the identity keystore is used by Jetty. + assertEquals(sslContextFactory.getKeyStore().size(), sslContextFactory.getTrustStore().size()); + assertTrue(sslContextFactory.getTrustStore().containsAlias(identityKeyAlias)); + + verifyAll(); + + // Note: The key password is used if the keystore password is not set; and vice versa Review comment: Seems like this comment out of place? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
