|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with this |
| 4 | + * work for additional information regarding copyright ownership. The ASF |
| 5 | + * licenses this file to you under the Apache License, Version 2.0 (the |
| 6 | + * "License"); you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * <p> |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * <p> |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | + * License for the specific language governing permissions and limitations under |
| 15 | + * the License. |
| 16 | + */ |
| 17 | +package org.apache.knox.gateway.topology.discovery.cm; |
| 18 | + |
| 19 | +import org.apache.knox.gateway.config.GatewayConfig; |
| 20 | +import org.apache.knox.gateway.services.security.AliasService; |
| 21 | +import org.apache.knox.gateway.services.security.AliasServiceException; |
| 22 | +import org.apache.knox.gateway.topology.discovery.ServiceDiscoveryConfig; |
| 23 | +import org.easymock.EasyMock; |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Assert; |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.security.KeyStore; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.Properties; |
| 32 | + |
| 33 | +public class ApiClientFactoryTest { |
| 34 | + |
| 35 | + Properties originalProps; |
| 36 | + |
| 37 | + @Before |
| 38 | + public void setUp() { |
| 39 | + originalProps = System.getProperties(); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void tearDown() { |
| 44 | + System.setProperties(originalProps); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testSystemPropertySetWhenAliasExists() throws AliasServiceException { |
| 49 | + testGetApiClient(true); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testSystemPropertyNotSetWhenAliasNotExists() throws AliasServiceException { |
| 54 | + testGetApiClient(false); |
| 55 | + } |
| 56 | + |
| 57 | + private void testGetApiClient(final boolean shouldSetSystemProperty) throws AliasServiceException { |
| 58 | + final String trustStorePassword = "changeit"; |
| 59 | + final RecordingProperties testProps = new RecordingProperties(originalProps); |
| 60 | + System.setProperties(testProps); |
| 61 | + |
| 62 | + final GatewayConfig gatewayConfig = EasyMock.createMock(GatewayConfig.class); |
| 63 | + EasyMock.expect(gatewayConfig.getClouderaManagerServiceDiscoveryApiVersion()).andReturn("57").anyTimes(); |
| 64 | + EasyMock.expect(gatewayConfig.getClouderaManagerServiceDiscoveryConnectTimeoutMillis()).andReturn(1L).anyTimes(); |
| 65 | + EasyMock.expect(gatewayConfig.getClouderaManagerServiceDiscoveryReadTimeoutMillis()).andReturn(1L).anyTimes(); |
| 66 | + EasyMock.expect(gatewayConfig.getClouderaManagerServiceDiscoveryWriteTimeoutMillis()).andReturn(1L).anyTimes(); |
| 67 | + EasyMock.expect(gatewayConfig.getIncludedSSLCiphers()).andReturn(Collections.emptyList()).anyTimes(); |
| 68 | + EasyMock.expect(gatewayConfig.getIncludedSSLProtocols()).andReturn(Collections.emptySet()).anyTimes(); |
| 69 | + final ServiceDiscoveryConfig serviceDiscoveryConfig = EasyMock.createMock(ServiceDiscoveryConfig.class); |
| 70 | + EasyMock.expect(serviceDiscoveryConfig.getAddress()).andReturn("myCmHost").anyTimes(); |
| 71 | + EasyMock.expect(serviceDiscoveryConfig.getUser()).andReturn("myCmUser").anyTimes(); |
| 72 | + EasyMock.expect(serviceDiscoveryConfig.getPasswordAlias()).andReturn("myCmPasswordAlias").anyTimes(); |
| 73 | + final AliasService aliasService = EasyMock.createMock(AliasService.class); |
| 74 | + if (shouldSetSystemProperty) { |
| 75 | + EasyMock.expect(aliasService.getPasswordFromAliasForGateway(ApiClientFactory.TRUSTSTORE_PASSWORD_ALIAS)).andReturn(trustStorePassword.toCharArray()).anyTimes(); |
| 76 | + } else { |
| 77 | + EasyMock.expect(aliasService.getPasswordFromAliasForGateway(ApiClientFactory.TRUSTSTORE_PASSWORD_ALIAS)).andReturn(null).anyTimes(); |
| 78 | + } |
| 79 | + EasyMock.expect(aliasService.getPasswordFromAliasForGateway("myCmPasswordAlias")).andReturn("myCmPassword".toCharArray()).anyTimes(); |
| 80 | + final KeyStore trustStore = EasyMock.createMock(KeyStore.class); |
| 81 | + |
| 82 | + EasyMock.replay(aliasService, gatewayConfig, serviceDiscoveryConfig, trustStore); |
| 83 | + ApiClientFactory.getApiClient(gatewayConfig, serviceDiscoveryConfig, aliasService, trustStore); |
| 84 | + |
| 85 | + if (shouldSetSystemProperty) { |
| 86 | + Assert.assertEquals(ApiClientFactory.TRUSTSTORE_PASSWORD_SYSTEM_PROPERTY, testProps.lastSetKey); |
| 87 | + Assert.assertEquals(trustStorePassword, testProps.lastSetValue); |
| 88 | + Assert.assertEquals(ApiClientFactory.TRUSTSTORE_PASSWORD_SYSTEM_PROPERTY, testProps.lastRemovedKey); |
| 89 | + } else { |
| 90 | + Assert.assertNull(testProps.lastSetKey); |
| 91 | + Assert.assertNull(testProps.lastSetValue); |
| 92 | + Assert.assertEquals(ApiClientFactory.TRUSTSTORE_PASSWORD_SYSTEM_PROPERTY, testProps.lastRemovedKey); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + static class RecordingProperties extends Properties { |
| 97 | + private final Properties delegate; |
| 98 | + |
| 99 | + String lastSetKey; |
| 100 | + String lastSetValue; |
| 101 | + String lastRemovedKey; |
| 102 | + |
| 103 | + RecordingProperties(Properties delegate) { |
| 104 | + this.delegate = delegate; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public synchronized Object setProperty(String key, String value) { |
| 109 | + lastSetKey = key; |
| 110 | + lastSetValue = value; |
| 111 | + return delegate.setProperty(key, value); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public synchronized Object remove(Object key) { |
| 116 | + lastRemovedKey = (String) key; |
| 117 | + return delegate.remove(key); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String getProperty(String key) { |
| 122 | + return delegate.getProperty(key); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments