From 2c41d11de03c07739ae395683c9d834ab282821c Mon Sep 17 00:00:00 2001 From: Sylvain Desbureaux Date: Fri, 3 Apr 2020 11:51:58 +0200 Subject: Allow SDC client to connect to SDC in HTTP I've added a toggle which allow to connect to SDC using HTTP and not only HTTPS. By default, we're still on HTTPS. Issue-ID: SDC-2721 Signed-off-by: Sylvain Desbureaux Change-Id: I48046c19aed7d81f9e03a30b3b081b9d8dd1495e --- .../org/onap/sdc/api/consumer/IConfiguration.java | 12 ++++ .../java/org/onap/sdc/http/HttpAsdcClient.java | 31 +++++++-- .../main/java/org/onap/sdc/impl/Configuration.java | 12 ++++ .../org/onap/sdc/impl/DistributionClientImpl.java | 5 ++ .../org/onap/sdc/http/SdcConnectorClientTest.java | 1 + .../org/onap/sdc/impl/DistributionClientTest.java | 81 +++++++++++++++------- .../java/org/onap/sdc/utils/TestConfiguration.java | 17 ++++- 7 files changed, 123 insertions(+), 36 deletions(-) diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/api/consumer/IConfiguration.java b/sdc-distribution-client/src/main/java/org/onap/sdc/api/consumer/IConfiguration.java index 88db133..c7248eb 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/api/consumer/IConfiguration.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/api/consumer/IConfiguration.java @@ -44,6 +44,18 @@ public interface IConfiguration { */ String getUser(); + /** + * Return True if ssl is needed, false otherwise. + * This param can be null, then default (HTTPS) behavior will be + * applied. If set to false, distribution client will use HTTP when + * connecting to SDC. + * + * @return + */ + default Boolean isUseHttpsWithSDC() { + return true; + } + /** * User Password for SDC distribution consumer authentication. * diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java b/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java index 7871816..44f1295 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/http/HttpAsdcClient.java @@ -70,18 +70,28 @@ public class HttpAsdcClient implements IHttpAsdcClient { private static final String TLS = "TLSv1.2"; private static final String AUTHORIZATION_HEADER = "Authorization"; private static final String HTTPS = "https://"; + private static final String HTTP = "http://"; public static final int AUTHORIZATION_SCOPE_PORT = 443; + public static final int AUTHORIZATION_SCOPE_PLAIN_PORT = 80; private static Logger log = LoggerFactory.getLogger(HttpAsdcClient.class.getName()); private CloseableHttpClient httpClient = null; private String serverFqdn = null; private String authHeaderValue = ""; + private Boolean use_ssl = true; - public HttpAsdcClient(IConfiguration configuraion) { - this.serverFqdn = configuraion.getAsdcAddress(); + public HttpAsdcClient(IConfiguration configuration) { + this.serverFqdn = configuration.getAsdcAddress(); - String username = configuraion.getUser(); - String password = configuraion.getPassword(); - initSSL(username, password, configuraion.getKeyStorePath(), configuraion.getKeyStorePassword(), configuraion.activateServerTLSAuth()); + String username = configuration.getUser(); + String password = configuration.getPassword(); + this.use_ssl = configuration.isUseHttpsWithSDC(); + if (this.use_ssl) { + initSSL(username, password, configuration.getKeyStorePath(), configuration.getKeyStorePassword(), configuration.activateServerTLSAuth()); + } else { + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials(new AuthScope("localhost", AUTHORIZATION_SCOPE_PLAIN_PORT), new UsernamePasswordCredentials(username, password)); + httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credsProvider).build(); + } String userNameAndPassword = username + ":" + password; this.authHeaderValue = "Basic " + Base64.getEncoder().encodeToString(userNameAndPassword.getBytes()); @@ -217,7 +227,7 @@ public class HttpAsdcClient implements IHttpAsdcClient { Pair ret; CloseableHttpResponse httpResponse = null; HttpAsdcResponse response = null; - HttpPost httpPost = new HttpPost(HTTPS + serverFqdn + requestUrl); + HttpPost httpPost = new HttpPost(getScheme() + serverFqdn + requestUrl); List
headers = addHeadersToHttpRequest(headersMap); for (Header header : headers) { httpPost.addHeader(header); @@ -268,7 +278,7 @@ public class HttpAsdcClient implements IHttpAsdcClient { public Pair getRequest(String requestUrl, Map headersMap, boolean closeTheRequest) { Pair ret; CloseableHttpResponse httpResponse = null; - String url = HTTPS + serverFqdn + requestUrl; + String url = getScheme() + serverFqdn + requestUrl; log.debug("url to send {}", url); HttpGet httpGet = new HttpGet(url); List
headers = addHeadersToHttpRequest(headersMap); @@ -354,4 +364,11 @@ public class HttpAsdcClient implements IHttpAsdcClient { return requestHeaders; } + private String getScheme() { + if (this.use_ssl) { + return HTTPS; + } + return HTTP; + } + } diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/Configuration.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/Configuration.java index 75e4acc..67071e9 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/Configuration.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/Configuration.java @@ -42,6 +42,7 @@ public class Configuration implements IConfiguration { private boolean activateServerTLSAuth; private boolean filterInEmptyResources; private Boolean useHttpsWithDmaap; + private Boolean useHttpsWithSDC; private boolean consumeProduceStatusTopic; public Configuration(IConfiguration other) { @@ -55,6 +56,7 @@ public class Configuration implements IConfiguration { this.pollingTimeout = other.getPollingTimeout(); this.relevantArtifactTypes = other.getRelevantArtifactTypes(); this.user = other.getUser(); + this.useHttpsWithSDC = other.isUseHttpsWithSDC(); this.keyStorePath = other.getKeyStorePath(); this.keyStorePassword = other.getKeyStorePassword(); this.activateServerTLSAuth = other.activateServerTLSAuth(); @@ -73,6 +75,11 @@ public class Configuration implements IConfiguration { return msgBusAddressList; } + @Override + public Boolean isUseHttpsWithSDC() { + return useHttpsWithSDC; + } + @Override public String getUser() { return user; @@ -190,6 +197,10 @@ public class Configuration implements IConfiguration { return this.useHttpsWithDmaap; } + public void setUseHttpsWithSDC(boolean useHttpsWithSDC) { + this.useHttpsWithSDC = useHttpsWithSDC; + } + public void setUseHttpsWithDmaap(boolean useHttpsWithDmaap) { this.useHttpsWithDmaap = useHttpsWithDmaap; } @@ -206,6 +217,7 @@ public class Configuration implements IConfiguration { + "asdcAddress=" + asdcAddress + ", user=" + user + ", password=" + password + + ", useHttpsWithSDC=" + useHttpsWithSDC + ", pollingInterval=" + pollingInterval + ", pollingTimeout=" + pollingTimeout + ", relevantArtifactTypes=" + relevantArtifactTypes diff --git a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java index eda9cf6..5640540 100644 --- a/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java +++ b/sdc-distribution-client/src/main/java/org/onap/sdc/impl/DistributionClientImpl.java @@ -545,6 +545,11 @@ public class DistributionClientImpl implements IDistributionClient { generateConsumerGroup(); } + //Default use HTTPS with SDC + if (conf.isUseHttpsWithSDC() == null) { + configuration.setUseHttpsWithSDC(true); + } + //Default use HTTPS with DMAAP if (conf.isUseHttpsWithDmaap() == null) { configuration.setUseHttpsWithDmaap(true); diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java index 20228e0..d3a6ffb 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/http/SdcConnectorClientTest.java @@ -128,6 +128,7 @@ public class SdcConnectorClientTest { IConfiguration conf = Mockito.mock(IConfiguration.class); when(conf.getUser()).thenReturn("user"); when(conf.getPassword()).thenReturn("password"); + when(conf.isUseHttpsWithSDC()).thenReturn(true); when(conf.activateServerTLSAuth()).thenReturn(false); SdcConnectorClient client = new SdcConnectorClient(); diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java index e2bad5a..bb3c7bf 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/impl/DistributionClientTest.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -219,14 +219,43 @@ public class DistributionClientTest { Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); client.cambriaIdentityManager = cambriaMock; + TestConfiguration badAsdcConfig = new TestConfiguration(); + if (badAsdcConfig.isUseHttpsWithSDC() == null) { + System.out.println("null for HTTPS then TRUE"); + } else { + System.out.println("isUseHttpsWithSDC set to " + badAsdcConfig.isUseHttpsWithSDC()); + } + badAsdcConfig.setAsdcAddress("badhost:8080"); + + IDistributionClientResult init = client.init(badAsdcConfig, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, init.getDistributionActionResult()); + + badAsdcConfig = new TestConfiguration(); + badAsdcConfig.setAsdcAddress("localhost:8181"); + + init = client.init(badAsdcConfig, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, init.getDistributionActionResult()); + + } + + @Test + public void initFailedConnectAsdcInHttpTest() throws HttpException, CambriaApiException, IOException { + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + TestConfiguration badAsdcConfig = new TestConfiguration(); badAsdcConfig.setAsdcAddress("badhost:8080"); + badAsdcConfig.setUseHttpsWithSDC(false); IDistributionClientResult init = client.init(badAsdcConfig, new TestNotificationCallback()); assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, init.getDistributionActionResult()); badAsdcConfig = new TestConfiguration(); badAsdcConfig.setAsdcAddress("localhost:8181"); + badAsdcConfig.setUseHttpsWithSDC(false); init = client.init(badAsdcConfig, new TestNotificationCallback()); assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, init.getDistributionActionResult()); @@ -444,7 +473,7 @@ public class DistributionClientTest { assertEquals(DistributionActionResultEnum.SUCCESS, init.getDistributionActionResult()); } - + @Test public void testDecodeVfModuleArtifact() throws IOException{ String vfModuleContent = getVFModuleExample(); @@ -454,30 +483,30 @@ public class DistributionClientTest { assertTrue(iVfModuleMetadata.getArtifacts().size() == 11); assertEquals(iVfModuleMetadata.getVfModuleModelName(), "Vccfdb..base_vDB_11032016..module-0"); } - + private String getVFModuleExample() { - return "[\r\n" + - " {\r\n" + - " \"vfModuleModelName\": \"Vccfdb..base_vDB_11032016..module-0\",\r\n" + - " \"vfModuleModelInvariantUUID\": \"89bcc10e-84f9-475a-b7e3-bdac6cd2b31a\",\r\n" + - " \"vfModuleModelVersion\": \"1\",\r\n" + - " \"vfModuleModelUUID\": \"f7e1c7aa-cc7b-4dfc-b761-237e8063bd96\",\r\n" + - " \"GuguBubu\": true,\r\n" + - " \"isBase\": true,\r\n" + - " \"artifacts\": [\r\n" + - " \"68733000-7656-487c-aecb-040af96df5a5\",\r\n" + - " \"d3519bb4-be98-4c04-8815-4557379fdff3\",\r\n" + - " \"b445d84b-de23-4f0c-a0aa-8d794d85bebe\",\r\n" + - " \"52a6656a-63f4-4ae8-80f4-40febcaa15d6\",\r\n" + - " \"fdcf20b5-1bac-4da7-9e77-b0b565115027\",\r\n" + - " \"d3fcfd98-941c-4627-8b94-386dd3eab1ab\",\r\n" + - " \"bdd6c2b6-793b-49d7-8590-51e7d6998f69\",\r\n" + - " \"554a62b0-3a56-4c29-bc5e-23badf6da67f\",\r\n" + - " \"4b922d87-f2c9-44da-b933-57a91294fb42\",\r\n" + - " \"ad5cceda-0fa4-415e-b319-96f080e4b5c7\",\r\n" + - " \"8f4312f4-7be5-4d64-a3f5-564be7a0f01e\"\r\n" + - " ]\r\n" + - " }\r\n" + + return "[\r\n" + + " {\r\n" + + " \"vfModuleModelName\": \"Vccfdb..base_vDB_11032016..module-0\",\r\n" + + " \"vfModuleModelInvariantUUID\": \"89bcc10e-84f9-475a-b7e3-bdac6cd2b31a\",\r\n" + + " \"vfModuleModelVersion\": \"1\",\r\n" + + " \"vfModuleModelUUID\": \"f7e1c7aa-cc7b-4dfc-b761-237e8063bd96\",\r\n" + + " \"GuguBubu\": true,\r\n" + + " \"isBase\": true,\r\n" + + " \"artifacts\": [\r\n" + + " \"68733000-7656-487c-aecb-040af96df5a5\",\r\n" + + " \"d3519bb4-be98-4c04-8815-4557379fdff3\",\r\n" + + " \"b445d84b-de23-4f0c-a0aa-8d794d85bebe\",\r\n" + + " \"52a6656a-63f4-4ae8-80f4-40febcaa15d6\",\r\n" + + " \"fdcf20b5-1bac-4da7-9e77-b0b565115027\",\r\n" + + " \"d3fcfd98-941c-4627-8b94-386dd3eab1ab\",\r\n" + + " \"bdd6c2b6-793b-49d7-8590-51e7d6998f69\",\r\n" + + " \"554a62b0-3a56-4c29-bc5e-23badf6da67f\",\r\n" + + " \"4b922d87-f2c9-44da-b933-57a91294fb42\",\r\n" + + " \"ad5cceda-0fa4-415e-b319-96f080e4b5c7\",\r\n" + + " \"8f4312f4-7be5-4d64-a3f5-564be7a0f01e\"\r\n" + + " ]\r\n" + + " }\r\n" + "]"; } diff --git a/sdc-distribution-client/src/test/java/org/onap/sdc/utils/TestConfiguration.java b/sdc-distribution-client/src/test/java/org/onap/sdc/utils/TestConfiguration.java index 0aa314e..de74831 100644 --- a/sdc-distribution-client/src/test/java/org/onap/sdc/utils/TestConfiguration.java +++ b/sdc-distribution-client/src/test/java/org/onap/sdc/utils/TestConfiguration.java @@ -7,9 +7,9 @@ * Licensed 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. @@ -41,6 +41,7 @@ public class TestConfiguration implements IConfiguration { private boolean activateServerTLSAuth; private boolean isFilterInEmptyResources; private boolean useHttpsWithDmaap; + private boolean useHttpsWithSDC; private List msgBusAddress; public TestConfiguration(IConfiguration other) { @@ -74,6 +75,7 @@ public class TestConfiguration implements IConfiguration { this.keyStorePassword = "Aa123456"; this.activateServerTLSAuth = false; this.isFilterInEmptyResources = false; + this.useHttpsWithSDC = true; msgBusAddress = new ArrayList(); msgBusAddress.add("www.cnn.com"); msgBusAddress.add("www.cnn.com"); @@ -281,7 +283,7 @@ public class TestConfiguration implements IConfiguration { return "TestConfiguration [asdcAddress=" + asdcAddress + ", user=" + user + ", password=" + password + ", pollingInterval=" + pollingInterval + ", pollingTimeout=" + pollingTimeout + ", relevantArtifactTypes=" + relevantArtifactTypes + ", consumerGroup=" + consumerGroup + ", environmentName=" + environmentName + ", comsumerID=" + comsumerID + "]"; } - + @Override public boolean isFilterInEmptyResources() { return isFilterInEmptyResources; @@ -296,4 +298,13 @@ public class TestConfiguration implements IConfiguration { public Boolean isUseHttpsWithDmaap() { return this.useHttpsWithDmaap; } + + @Override + public Boolean isUseHttpsWithSDC() { + return this.useHttpsWithSDC; + } + + public void setUseHttpsWithSDC(Boolean useHttpsWithSDC) { + this.useHttpsWithSDC = useHttpsWithSDC; + } } -- cgit 1.2.3-korg