diff options
author | Kajur, Harish (vk250x) <vk250x@att.com> | 2020-02-21 14:34:10 -0500 |
---|---|---|
committer | Harish Venkata Kajur <vk250x@att.com> | 2020-02-25 23:59:33 -0500 |
commit | 98749c47bbb5f5ddcc1c4f0690b79c7288f6bdd6 (patch) | |
tree | a472ce2edabd497b643917f44785b775fa16e15e /aai-rest/src | |
parent | e654645a50a0d028d8e67ea997f84efe8d28a6a0 (diff) |
Enhancements for the aai-common library
Issue-ID: AAI-2806
Change-Id: I2dbb46b897b35136ac1bb802978d3f974af1b307
Signed-off-by: Kajur, Harish (vk250x) <vk250x@att.com>
Diffstat (limited to 'aai-rest/src')
8 files changed, 169 insertions, 92 deletions
diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java index b9fe87f1..3470de9d 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/AAIRestClient.java @@ -20,8 +20,8 @@ package org.onap.aai.restclient; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Collections; import java.util.Map; @@ -35,7 +35,7 @@ import org.springframework.util.MultiValueMap; @Component(value = ClientType.AAI) public class AAIRestClient extends TwoWaySSLRestClient { - private static EELFLogger logger = EELFManager.getInstance().getLogger(AAIRestClient.class); + private static Logger logger = LoggerFactory.getLogger(AAIRestClient.class); @Value("${aai.base.url}") private String baseUrl; diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java index 31dd0c92..68ff3e5e 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/NoAuthRestClient.java @@ -20,30 +20,40 @@ package org.onap.aai.restclient; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -import javax.annotation.PostConstruct; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; +import org.onap.aai.aailog.filter.RestClientLoggingInterceptor; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; +import javax.annotation.PostConstruct; + public abstract class NoAuthRestClient extends RestClient { - private static EELFLogger logger = EELFManager.getInstance().getLogger(NoAuthRestClient.class); + private static Logger logger = LoggerFactory.getLogger(NoAuthRestClient.class); protected RestTemplate restTemplate; @PostConstruct public void init() throws Exception { - HttpClient client = HttpClients.createDefault(); restTemplate = - new RestTemplateBuilder().requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client)).build(); - + new RestTemplateBuilder().requestFactory(this.getHttpRequestFactory()).build(); restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); + RestClientLoggingInterceptor loggingInterceptor = new RestClientLoggingInterceptor(); + restTemplate.getInterceptors().add(loggingInterceptor); + + } + + protected HttpComponentsClientHttpRequestFactory getHttpRequestFactory() throws Exception { + return new HttpComponentsClientHttpRequestFactory(this.getClient()); + } + + protected HttpClient getClient() throws Exception { + HttpClient client = HttpClients.createDefault(); + return client; } @Override diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java index aa672575..b2534f57 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/OneWaySSLRestClient.java @@ -20,49 +20,55 @@ package org.onap.aai.restclient; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -import java.io.File; -import java.io.FileInputStream; -import java.io.InputStream; -import java.security.KeyStore; - -import javax.annotation.PostConstruct; -import javax.net.ssl.SSLContext; - +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.apache.http.client.HttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; +import org.onap.aai.aailog.filter.RestClientLoggingInterceptor; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.util.ResourceUtils; import org.springframework.web.client.RestTemplate; +import javax.annotation.PostConstruct; +import javax.net.ssl.SSLContext; + public abstract class OneWaySSLRestClient extends RestClient { - private static EELFLogger logger = EELFManager.getInstance().getLogger(OneWaySSLRestClient.class); + private static Logger logger = LoggerFactory.getLogger(OneWaySSLRestClient.class); private RestTemplate restTemplate; @PostConstruct public void init() throws Exception { + restTemplate = + new RestTemplateBuilder().requestFactory(this.getHttpRequestFactory()).build(); + + restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); + RestClientLoggingInterceptor loggingInterceptor = new RestClientLoggingInterceptor(); + restTemplate.getInterceptors().add(loggingInterceptor); + + } + + protected HttpComponentsClientHttpRequestFactory getHttpRequestFactory() throws Exception { + return new HttpComponentsClientHttpRequestFactory(this.getClient()); + } + + protected HttpClient getClient() throws Exception { char[] trustStorePassword = getTruststorePassword(); String trustStore = getTruststorePath(); - SSLContext sslContext = SSLContextBuilder.create() + SSLContext sslContext = + SSLContextBuilder.create() .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword).build(); HttpClient client = - HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier((s, sslSession) -> true).build(); - - restTemplate = - new RestTemplateBuilder().requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client)).build(); - - restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); + HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier((s, sslSession) -> true).build(); + return client; } protected abstract String getTruststorePath(); diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/PropertyPasswordConfiguration.java b/aai-rest/src/main/java/org/onap/aai/restclient/PropertyPasswordConfiguration.java index 3160469a..29d9506f 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/PropertyPasswordConfiguration.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/PropertyPasswordConfiguration.java @@ -17,14 +17,11 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.onap.aai.restclient; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; @@ -32,23 +29,103 @@ import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + public class PropertyPasswordConfiguration implements ApplicationContextInitializer<ConfigurableApplicationContext> { private static final Pattern decodePasswordPattern = Pattern.compile("password\\((.*?)\\)"); - private PasswordDecoder passwordDecoder = new JettyPasswordDecoder(); + private static final Logger logger = LoggerFactory.getLogger(PropertyPasswordConfiguration.class.getName()); @Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); + String certPath = environment.getProperty("server.certs.location"); + File passwordFile = null; + File passphrasesFile = null; + InputStream passwordStream = null; + InputStream passphrasesStream = null; + Map<String, Object> sslProps = new LinkedHashMap<>(); + + // Override the passwords from application.properties if we find AAF certman files + if (certPath != null) { + try { + passwordFile = new File(certPath + ".password"); + passwordStream = new FileInputStream(passwordFile); + + if (passwordStream != null) { + String keystorePassword = null; + + keystorePassword = IOUtils.toString(passwordStream); + if (keystorePassword != null) { + keystorePassword = keystorePassword.trim(); + } + sslProps.put("server.ssl.key-store-password", keystorePassword); + sslProps.put("schema.service.ssl.key-store-password", keystorePassword); + sslProps.put("validation.service.ssl.key-store-password", keystorePassword); + } else { + logger.info("Not using AAF Certman password file"); + } + } catch (IOException e) { + logger.warn("Not using AAF Certman password file, e=" + e.getMessage()); + } finally { + if (passwordStream != null) { + try { + passwordStream.close(); + } catch (Exception e) { + } + } + } + try { + passphrasesFile = new File(certPath + ".passphrases"); + passphrasesStream = new FileInputStream(passphrasesFile); + + if (passphrasesStream != null) { + String truststorePassword = null; + Properties passphrasesProps = new Properties(); + passphrasesProps.load(passphrasesStream); + truststorePassword = passphrasesProps.getProperty("cadi_truststore_password"); + if (truststorePassword != null) { + truststorePassword = truststorePassword.trim(); + } + sslProps.put("server.ssl.trust-store-password", truststorePassword); + sslProps.put("schema.service.ssl.trust-store-password", truststorePassword); + sslProps.put("validation.service.ssl.trust-store-password", truststorePassword); + } else { + logger.info("Not using AAF Certman passphrases file"); + } + } catch (IOException e) { + logger.warn("Not using AAF Certman passphrases file, e=" + e.getMessage()); + } finally { + if (passphrasesStream != null) { + try { + passphrasesStream.close(); + } catch (Exception e) { + } + } + } + } for (PropertySource<?> propertySource : environment.getPropertySources()) { Map<String, Object> propertyOverrides = new LinkedHashMap<>(); decodePasswords(propertySource, propertyOverrides); if (!propertyOverrides.isEmpty()) { - PropertySource<?> decodedProperties = - new MapPropertySource("decoded " + propertySource.getName(), propertyOverrides); + PropertySource<?> decodedProperties = new MapPropertySource("decoded "+ propertySource.getName(), propertyOverrides); environment.getPropertySources().addBefore(propertySource.getName(), decodedProperties); } + + } + if (!sslProps.isEmpty()) { + logger.info("Using AAF Certman files"); + PropertySource<?> additionalProperties = new MapPropertySource("additionalProperties", sslProps); + environment.getPropertySources().addFirst(additionalProperties); } } @@ -66,8 +143,7 @@ public class PropertyPasswordConfiguration implements ApplicationContextInitiali } private String decodePasswordsInString(String input) { - if (input == null) - return null; + if (input == null) return null; StringBuffer output = new StringBuffer(); Matcher matcher = decodePasswordPattern.matcher(input); while (matcher.find()) { diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java index f5fc074b..67fb01e1 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/RestClient.java @@ -22,8 +22,8 @@ package org.onap.aai.restclient; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.net.URI; import java.net.URISyntaxException; @@ -40,13 +40,13 @@ import org.springframework.web.client.RestTemplate; public abstract class RestClient { - private static EELFLogger log = EELFManager.getInstance().getLogger(RestClient.class); + private static Logger log = LoggerFactory.getLogger(RestClient.class); @Value("${spring.application.name}") protected String appName; /** * Execute the given http method against the uri with passed headers - * + * * @param uri properly encoded, can include query params also properly encoded * @param method http method of the request * @param headers headers for the request @@ -58,7 +58,7 @@ public abstract class RestClient { throws RestClientException { HttpEntity<String> httpEntity; - log.debug("Headers: {}", headers); + log.debug("Request Headers: {}", headers); if (body == null) { httpEntity = new HttpEntity<>(getHeaders(headers)); } else { @@ -79,16 +79,15 @@ public abstract class RestClient { log.error("URL syntax error with url {}{}", getBaseUrl(), uri); throw new RestClientException(e.getMessage()); } - log.debug("METHOD={},URL={},HEADERS={}", method, url, httpEntity); - + log.debug("METHOD={}, URL={}, BODY={}", method, url, httpEntity.getBody()); ResponseEntity responseEntity = getRestTemplate().exchange(url, method, httpEntity, String.class); - log.debug("RESPONSE={}", responseEntity); + log.trace("RESPONSE={}", responseEntity); return responseEntity; } /** * Execute the given http method against the uri with passed headers - * + * * @param uri properly encoded, can include query params also properly encoded * @param method http method of the request * @param headers headers for the request @@ -103,7 +102,7 @@ public abstract class RestClient { /** * Execute the given http method against the uri with passed headers - * + * * @param uri properly encoded, can include query params also properly encoded * @param method http method of the request * @param headers headers for the request @@ -117,7 +116,7 @@ public abstract class RestClient { /** * Execute the given http method against the uri with passed headers - * + * * @param uri properly encoded, can include query params also properly encoded * @param method http method of the request * @param headers headers for the request diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java b/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java index 9c4876d4..9945275a 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/RestClientResponseErrorHandler.java @@ -20,8 +20,8 @@ package org.onap.aai.restclient; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; @@ -31,7 +31,7 @@ import org.springframework.web.client.ResponseErrorHandler; public class RestClientResponseErrorHandler implements ResponseErrorHandler { - private static EELFLogger logger = EELFManager.getInstance().getLogger(RestClientResponseErrorHandler.class); + private static Logger logger = LoggerFactory.getLogger(RestClientResponseErrorHandler.class); @Override public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException { diff --git a/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java b/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java index 58f2106c..58ee79f1 100644 --- a/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java +++ b/aai-rest/src/main/java/org/onap/aai/restclient/TwoWaySSLRestClient.java @@ -20,8 +20,8 @@ package org.onap.aai.restclient; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.File; import java.io.FileInputStream; @@ -38,15 +38,30 @@ import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.util.ResourceUtils; import org.springframework.web.client.RestTemplate; +import org.onap.aai.aailog.filter.RestClientLoggingInterceptor; public abstract class TwoWaySSLRestClient extends RestClient { - private static EELFLogger logger = EELFManager.getInstance().getLogger(TwoWaySSLRestClient.class); + private static Logger logger = LoggerFactory.getLogger(TwoWaySSLRestClient.class); private RestTemplate restTemplate; @PostConstruct public void init() throws Exception { + restTemplate = + new RestTemplateBuilder().requestFactory(this.getHttpRequestFactory()).build(); + + restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); + RestClientLoggingInterceptor loggingInterceptor = new RestClientLoggingInterceptor(); + restTemplate.getInterceptors().add(loggingInterceptor); + + } + + protected HttpComponentsClientHttpRequestFactory getHttpRequestFactory() throws Exception { + return new HttpComponentsClientHttpRequestFactory(this.getClient()); + } + + protected HttpClient getClient() throws Exception { char[] keyStorePassword = getKeystorePassword(); char[] trustStorePassword = getTruststorePassword(); @@ -55,17 +70,13 @@ public abstract class TwoWaySSLRestClient extends RestClient { String trustStore = getTruststorePath(); SSLContext sslContext = - SSLContextBuilder.create().loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword) - .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword).build(); + SSLContextBuilder.create().loadKeyMaterial(loadPfx(keyStore, keyStorePassword), keyStorePassword) + .loadTrustMaterial(ResourceUtils.getFile(trustStore), trustStorePassword).build(); HttpClient client = - HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier((s, sslSession) -> true).build(); - - restTemplate = - new RestTemplateBuilder().requestFactory(() -> new HttpComponentsClientHttpRequestFactory(client)).build(); - - restTemplate.setErrorHandler(new RestClientResponseErrorHandler()); + HttpClients.custom().setSSLContext(sslContext).setSSLHostnameVerifier((s, sslSession) -> true).build(); + return client; } private KeyStore loadPfx(String file, char[] password) throws Exception { diff --git a/aai-rest/src/test/java/org/onap/aai/restclient/RestClientTest.java b/aai-rest/src/test/java/org/onap/aai/restclient/RestClientTest.java deleted file mode 100644 index 36b8fb3e..00000000 --- a/aai-rest/src/test/java/org/onap/aai/restclient/RestClientTest.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * ============LICENSE_START======================================================= - * org.onap.aai - * ================================================================================ - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.aai.restclient; - -public class RestClientTest { - -} |