[ 
https://issues.apache.org/jira/browse/KNOX-1801?focusedWorklogId=207887&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-207887
 ]

ASF GitHub Bot logged work on KNOX-1801:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 05/Mar/19 17:23
            Start Date: 05/Mar/19 17:23
    Worklog Time Spent: 10m 
      Work Description: risdenk commented on 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]


Issue Time Tracking
-------------------

    Worklog Id:     (was: 207887)
    Time Spent: 0.5h  (was: 20m)

> Master secret is incorrectly assumed when a custom truststore is not 
> specified when clientauth is enabled
> ---------------------------------------------------------------------------------------------------------
>
>                 Key: KNOX-1801
>                 URL: https://issues.apache.org/jira/browse/KNOX-1801
>             Project: Apache Knox
>          Issue Type: Bug
>          Components: Server
>    Affects Versions: 1.3.0
>            Reporter: Robert Levas
>            Assignee: Robert Levas
>            Priority: Major
>             Fix For: 1.3.0
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> Master secret is incorrectly assumed when a custom truststore is not 
> specified when clientauth is enabled. 
> *Steps to reproduce*
>  # Create custom TLS keystore for Knox with a custom keystore password (not 
> the master secret)
>  # Specify the custom TLS keystore details in {{gateway-site.xml}}
>  ** {{gateway.tls.keystore.password.alias}}
>  ** {{gateway.tls.keystore.path}}
>  ** {{gateway.tls.keystore.type}}
>  ** {{gateway.tls.key.alias}}
>  ** {{gateway.tls.key.passphrase.alias}} (optional)
>  # Turn on client-auth
>  ** {{gateway.client.auth.needed}} : {{true}}
>  # Create password alias for the custom keystore using Knox CLI
>  ** {{bin/knoxcli.sh create-alias gateway-identity-keystore-password --value 
> <password>}}
>  # (Re)Start the Gateway
> The Gateway will fail to start with the following error in the gateway.log:
> {noformat}
> 2019-03-04 11:03:15,921 FATAL knox.gateway (GatewayServer.java:main(168)) - 
> Failed to start gateway: java.io.IOException: keystore password was incorrect
> java.io.IOException: keystore password was incorrect
>         at 
> sun.security.pkcs12.PKCS12KeyStore.engineLoad(PKCS12KeyStore.java:2059)
>         at java.security.KeyStore.load(KeyStore.java:1445)
>         at 
> org.apache.knox.gateway.services.security.impl.JettySSLService.loadKeyStore(JettySSLService.java:257)
>         at 
> org.apache.knox.gateway.services.security.impl.JettySSLService.buildSslContextFactory(JettySSLService.java:222)
>         at 
> org.apache.knox.gateway.GatewayServer.createConnector(GatewayServer.java:373)
>         at org.apache.knox.gateway.GatewayServer.start(GatewayServer.java:520)
>         at 
> org.apache.knox.gateway.GatewayServer.startGateway(GatewayServer.java:308)
>         at org.apache.knox.gateway.GatewayServer.main(GatewayServer.java:161)
>         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>         at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
>         at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>         at java.lang.reflect.Method.invoke(Method.java:498)
>         at 
> org.apache.knox.gateway.launcher.Invoker.invokeMainMethod(Invoker.java:68)
>         at org.apache.knox.gateway.launcher.Invoker.invoke(Invoker.java:39)
>         at org.apache.knox.gateway.launcher.Command.run(Command.java:99)
>         at org.apache.knox.gateway.launcher.Launcher.run(Launcher.java:75)
>         at org.apache.knox.gateway.launcher.Launcher.main(Launcher.java:52)
> Caused by: java.security.UnrecoverableKeyException: failed to decrypt safe 
> contents entry: javax.crypto.BadPaddingException: Given final block not 
> properly padded. Such issues can arise if a bad key is used during decryption.
>         ... 17 more
> {noformat}
> *Solution*
> Lookup password for the truststore using the appropriate alias name, falling 
> back to the master secret if an alias is not configured or not set. 



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to