From 07ebdbb579bc2b9ef1f0e26d0c98d2c0fdcd353d Mon Sep 17 00:00:00 2001 From: pwielebs Date: Tue, 5 Jun 2018 12:19:28 +0200 Subject: basic authentication for AAI Producer Client Change-Id: I087db36dc3c60015f91de5fde49358b18564ab9e Issue-ID: DCAEGEN2-451 Signed-off-by: pwielebs --- .../services/prh/service/AAIProducerClient.java | 14 +++++++++++-- .../prh/service/AAIProducerClientTest.java | 24 ++++++++++++++++++++++ prh-app-server/config/prh_endpoints.json | 7 +++---- .../src/test/resources/prh_endpoints.json | 7 +++---- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java index dce326ec..f9c2708e 100644 --- a/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java +++ b/prh-aai-client/src/main/java/org/onap/dcaegen2/services/prh/service/AAIProducerClient.java @@ -35,6 +35,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.util.Base64; import java.util.Map; import java.util.Optional; @@ -49,6 +50,8 @@ public class AAIProducerClient implements AAIExtendedHttpClient { private final Integer aaiHostPortNumber; private final String aaiPath; private final Map aaiHeaders; + private final String aaiUserName; + private final String aaiUserPassword; public AAIProducerClient(AAIClientConfiguration aaiClientConfiguration) { @@ -58,6 +61,8 @@ public class AAIProducerClient implements AAIExtendedHttpClient { aaiHostPortNumber = aaiClientConfiguration.aaiHostPortNumber(); aaiPath = aaiClientConfiguration.aaiBasePath() + aaiClientConfiguration.aaiPnfPath(); aaiHeaders = aaiClientConfiguration.aaiHeaders(); + aaiUserName = aaiClientConfiguration.aaiUserName(); + aaiUserPassword = aaiClientConfiguration.aaiUserPassword(); } @@ -71,7 +76,6 @@ public class AAIProducerClient implements AAIExtendedHttpClient { return Optional.empty(); } }); - } private Optional createRequest(ConsumerDmaapModel consumerDmaapModel) throws URISyntaxException { @@ -98,11 +102,17 @@ public class AAIProducerClient implements AAIExtendedHttpClient { }); } - private HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException { + HttpPatch createHttpPatch(URI extendedURI, String jsonBody) throws UnsupportedEncodingException { HttpPatch httpPatch = new HttpPatch(extendedURI); httpPatch.setEntity( new StringEntity(jsonBody)); aaiHeaders.forEach(httpPatch::addHeader); httpPatch.addHeader("Content-Type", "application/merge-patch+json"); + httpPatch.addHeader("Authorization", "Basic " + encode()); return httpPatch; } + + String encode() throws UnsupportedEncodingException { + return Base64.getEncoder().encodeToString((this.aaiUserName + ":" + this.aaiUserPassword) + .getBytes("UTF-8")); + } } diff --git a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java index 594df662..1ef90180 100644 --- a/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java +++ b/prh-aai-client/src/test/java/org/onap/dcaegen2/services/prh/service/AAIProducerClientTest.java @@ -32,6 +32,7 @@ import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModelForUnitTest; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; +import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -98,6 +99,29 @@ public class AAIProducerClientTest { assertNotNull(testedObject.getHttpResponse(consumerDmaapModel)); } + @Test + void encode_shouldCreateEncodedString_whenUserAndPasswordAreSet() throws UnsupportedEncodingException { + // given + String expected = "UFJIOlBSSA=="; + // when + String result = testedObject.encode(); + // then + assertNotNull(result); + assertEquals(expected, result); + } + + @Test + void createHttpPatch_shouldContainAuthorizationBasicValue() throws UnsupportedEncodingException { + // given + String expected = "Authorization: Basic UFJIOlBSSA=="; + // when + HttpPatch patch = testedObject.createHttpPatch(URI.create("localhost"), "{}"); + // then + assertNotNull(patch); + assertEquals(expected, patch.getLastHeader("Authorization").toString()); + } + + private static void setField() throws NoSuchFieldException, IllegalAccessException { Field field = testedObject.getClass().getDeclaredField("closeableHttpClient"); field.setAccessible(true); diff --git a/prh-app-server/config/prh_endpoints.json b/prh-app-server/config/prh_endpoints.json index 8aed235f..f5f79590 100644 --- a/prh-app-server/config/prh_endpoints.json +++ b/prh-app-server/config/prh_endpoints.json @@ -29,8 +29,8 @@ "aaiHost": "localhost", "aaiHostPortNumber": 8080, "aaiProtocol": "https", - "aaiUserName": "admin", - "aaiUserPassword": "admin", + "aaiUserName": "AAI", + "aaiUserPassword": "AAI", "aaiIgnoreSSLCertificateErrors": true, "aaiBasePath": "/aai/v12", "aaiPnfPath": "/network/pnfs/pnf", @@ -38,8 +38,7 @@ "X-FromAppId": "prh", "X-TransactionId": "9999", "Accept": "application/json", - "Real-Time": "true", - "Authorization": "Basic QUFJOkFBSQ==" + "Real-Time": "true" } } } diff --git a/prh-app-server/src/test/resources/prh_endpoints.json b/prh-app-server/src/test/resources/prh_endpoints.json index 0fe4b276..b45ce485 100644 --- a/prh-app-server/src/test/resources/prh_endpoints.json +++ b/prh-app-server/src/test/resources/prh_endpoints.json @@ -6,16 +6,15 @@ "aaiHostPortNumber": 8080, "aaiIgnoreSSLCertificateErrors": true, "aaiProtocol": "https", - "aaiUserName": "admin", - "aaiUserPassword": "admin", + "aaiUserName": "AAI", + "aaiUserPassword": "AAI", "aaiBasePath": "/aai/v11", "aaiPnfPath": "/network/pnfs/pnf", "aaiHeaders": { "X-FromAppId": "prh", "X-TransactionId": "9999", "Accept": "application/json", - "Real-Time": "true", - "Authorization": "Basic QUFJOkFBSQ==" + "Real-Time": "true" } } }, -- cgit 1.2.3-korg