From a8cafe1c784ae68b2a7cca313b362d14b13fd631 Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Fri, 29 Jan 2021 10:43:17 +0100 Subject: add basic auth using username and password Signed-off-by: Bartosz Gardziejewski Change-Id: I67bc2c25149c8e2d4943f23cfa8d726cdb95995f Issue-ID: INT-1805 --- .../simulator/client/HttpClientAdapterImpl.java | 29 +++++++++++++++++++--- .../src/main/resources/application.properties | 2 +- 2 files changed, 26 insertions(+), 5 deletions(-) (limited to 'pnfsimulator/src/main') diff --git a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java index 4f249b9..ba668fc 100644 --- a/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java +++ b/pnfsimulator/src/main/java/org/onap/pnfsimulator/simulator/client/HttpClientAdapterImpl.java @@ -20,6 +20,8 @@ package org.onap.pnfsimulator.simulator.client; +import org.apache.commons.codec.binary.Base64; +import org.apache.http.HttpHeaders; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; @@ -35,6 +37,9 @@ import org.slf4j.MarkerFactory; import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; import java.util.UUID; @@ -71,22 +76,27 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { LOGGER.info(INVOKE, "Message sent, ves response code: {}", response.getStatusLine()); vesResponse = responseFactory.create(response); EntityUtils.consumeQuietly(response.getEntity()); //response has to be fully consumed otherwise apache won't release connection - } catch (IOException e) { + } catch (IOException | URISyntaxException e) { LOGGER.warn("Error sending message to ves: {}", e.getMessage(), e.getCause()); vesResponse = new HttpResponseAdapter(421, String.format("Fail to connect with ves: %s", e.getMessage())); } return vesResponse; } - private HttpResponse sendAndRetrieve(String content) throws IOException { + private HttpResponse sendAndRetrieve(String content) throws IOException, URISyntaxException { HttpPost request = createRequest(content); HttpResponse httpResponse = client.execute(request); request.releaseConnection(); return httpResponse; } - private HttpPost createRequest(String content) throws UnsupportedEncodingException { - HttpPost request = new HttpPost(this.targetUrl); + private HttpPost createRequest(String content) throws UnsupportedEncodingException, URISyntaxException { + LOGGER.info("sending request using address: {}", this.targetUrl); + URI targetAddress = new URI(this.targetUrl); + HttpPost request = new HttpPost(targetAddress); + if(urlContainsUserInfo(targetAddress)) { + request.addHeader(HttpHeaders.AUTHORIZATION, getAuthenticationHeaderForUser(targetAddress.getUserInfo())); + } StringEntity stringEntity = new StringEntity(content); request.addHeader(CONTENT_TYPE, APPLICATION_JSON); request.addHeader(X_ONAP_REQUEST_ID, MDC.get(REQUEST_ID)); @@ -95,4 +105,15 @@ public class HttpClientAdapterImpl implements HttpClientAdapter { return request; } + private boolean urlContainsUserInfo(URI targetAddress) { + return targetAddress.getUserInfo() != null && !targetAddress.getUserInfo().isEmpty(); + } + + private String getAuthenticationHeaderForUser(String userInfo) { + final byte[] encodedUserInfo = Base64.encodeBase64( + userInfo.getBytes(StandardCharsets.ISO_8859_1) + ); + return String.format("Basic %s", new String(encodedUserInfo)); + } + } diff --git a/pnfsimulator/src/main/resources/application.properties b/pnfsimulator/src/main/resources/application.properties index 1253e52..fddcec0 100644 --- a/pnfsimulator/src/main/resources/application.properties +++ b/pnfsimulator/src/main/resources/application.properties @@ -10,7 +10,7 @@ management.endpoint.configprops.enabled=true management.endpoints.web.base-path=/ management.endpoints.web.exposure.include=refresh,health -ssl.clientCertificateEnabled=true +ssl.clientCertificateEnabled=${USE_CERTIFICATE_FOR_AUTHORIZATION:true} ssl.strictHostnameVerification=${STRICT_HOSTNAME_VERIFICATION:false} ssl.clientCertificatePath=/app/store/cert.p12 ssl.clientCertificatePasswordPath=/app/store/p12.pass -- cgit 1.2.3-korg