diff options
author | Seshu Kumar M <seshu.kumar.m@huawei.com> | 2018-04-19 12:03:54 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-04-19 12:03:54 +0000 |
commit | 6c9f839aa8e8dd848eb10216713490a17726c86c (patch) | |
tree | 902046a129d26046788005b49f2d8bb044554762 /common/src | |
parent | e75e31d0d63c3ef9f63f1d81edcef0e47ad8123a (diff) | |
parent | 9218d80e65bf3eaa01e7942730bac8c72dcb7862 (diff) |
Merge "AAIRestClient support for Basic Auth"
Diffstat (limited to 'common/src')
6 files changed, 60 insertions, 5 deletions
diff --git a/common/src/main/java/org/openecomp/mso/client/aai/AAIProperties.java b/common/src/main/java/org/openecomp/mso/client/aai/AAIProperties.java index 358bbbbbec..c208d6dd5f 100644 --- a/common/src/main/java/org/openecomp/mso/client/aai/AAIProperties.java +++ b/common/src/main/java/org/openecomp/mso/client/aai/AAIProperties.java @@ -25,4 +25,6 @@ import org.openecomp.mso.client.RestProperties; public interface AAIProperties extends RestProperties { public AAIVersion getDefaultVersion(); + public String getAuth(); + public String getKey(); } diff --git a/common/src/main/java/org/openecomp/mso/client/aai/AAIRestClient.java b/common/src/main/java/org/openecomp/mso/client/aai/AAIRestClient.java index 9348beb02a..e36033faa0 100644 --- a/common/src/main/java/org/openecomp/mso/client/aai/AAIRestClient.java +++ b/common/src/main/java/org/openecomp/mso/client/aai/AAIRestClient.java @@ -28,22 +28,31 @@ import java.util.UUID; import javax.ws.rs.client.ClientResponseFilter; import javax.ws.rs.ext.ContextResolver; -import org.openecomp.mso.client.RestProperties; import org.openecomp.mso.client.policy.RestClient; import org.openecomp.mso.client.policy.RestClientSSL; import com.fasterxml.jackson.databind.ObjectMapper; public class AAIRestClient extends RestClientSSL { + + private final AAIProperties props; - protected AAIRestClient(RestProperties props, UUID requestId, URI uri) { + protected AAIRestClient(AAIProperties props, UUID requestId, URI uri) { super(props, requestId, Optional.of(uri)); + this.props = props; headerMap.put("X-TransactionId", requestId.toString()); } @Override protected void initializeHeaderMap(Map<String, String> headerMap) { headerMap.put("X-FromAppId", "MSO"); + + String auth = props.getAuth(); + String key = props.getKey(); + + if (auth != null && !auth.isEmpty() && key != null && !key.isEmpty()) { + addBasicAuthHeader(auth, key); + } } @Override diff --git a/common/src/main/java/org/openecomp/mso/client/defaultproperties/DefaultAAIPropertiesImpl.java b/common/src/main/java/org/openecomp/mso/client/defaultproperties/DefaultAAIPropertiesImpl.java index 354d47af06..3f5bfa97d0 100644 --- a/common/src/main/java/org/openecomp/mso/client/defaultproperties/DefaultAAIPropertiesImpl.java +++ b/common/src/main/java/org/openecomp/mso/client/defaultproperties/DefaultAAIPropertiesImpl.java @@ -65,4 +65,15 @@ public class DefaultAAIPropertiesImpl implements AAIProperties { return AAIVersion.LATEST; } + @Override + public String getAuth() { + Object value = props.get("aai.auth"); + return value == null ? null : value.toString(); + } + + @Override + public String getKey() { + Object value = props.get("mso.msoKey"); + return value == null ? null : value.toString(); + } } diff --git a/common/src/main/java/org/openecomp/mso/client/policy/RestClient.java b/common/src/main/java/org/openecomp/mso/client/policy/RestClient.java index 4e6ffd1c6a..77afe82758 100644 --- a/common/src/main/java/org/openecomp/mso/client/policy/RestClient.java +++ b/common/src/main/java/org/openecomp/mso/client/policy/RestClient.java @@ -23,6 +23,7 @@ package org.openecomp.mso.client.policy; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; +import java.security.GeneralSecurityException; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; @@ -41,9 +42,11 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.ext.ContextResolver; +import org.apache.commons.codec.binary.Base64; import org.apache.log4j.Logger; import org.openecomp.mso.client.RestProperties; import org.openecomp.mso.logger.MsoLogger; +import org.openecomp.mso.utils.CryptoUtils; import org.springframework.stereotype.Service; import com.fasterxml.jackson.databind.ObjectMapper; @@ -84,8 +87,6 @@ public abstract class RestClient { this(props, requestId, path); this.accept = accept; this.contentType = contentType; - this.requestId = requestId; - } protected RestClient(URL host, UUID requestId, String contentType) { @@ -134,6 +135,21 @@ public abstract class RestClient { protected abstract Optional<ClientResponseFilter> addResponseFilter(); public abstract RestClient addRequestId(UUID requestId); + + /** + * Adds a basic authentication header to the request. + * @param auth the encrypted credentials + * @param key the key for decrypting the credentials + */ + protected void addBasicAuthHeader(String auth, String key) { + try { + byte[] decryptedAuth = CryptoUtils.decrypt(auth, key).getBytes(); + String authHeaderValue = "Basic " + new String(Base64.encodeBase64(decryptedAuth)); + headerMap.put("Authorization", authHeaderValue); + } catch (GeneralSecurityException e) { + logger.warn(e.getMessage(), e); + } + } protected ContextResolver<ObjectMapper> getMapper() { return new CommonObjectMapperProvider(); diff --git a/common/src/test/java/org/openecomp/mso/client/aai/AAIResourcesClientTest.java b/common/src/test/java/org/openecomp/mso/client/aai/AAIResourcesClientTest.java index daf8130cd4..c7cc549130 100644 --- a/common/src/test/java/org/openecomp/mso/client/aai/AAIResourcesClientTest.java +++ b/common/src/test/java/org/openecomp/mso/client/aai/AAIResourcesClientTest.java @@ -78,6 +78,21 @@ public class AAIResourcesClientTest { } @Test + public void verifyBasicAuth() { + AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3"); + wireMockRule.stubFor(get( + urlPathEqualTo("/aai/" + AAIVersion.LATEST + path.build().toString())) + .withHeader("Authorization", equalTo("Basic TVNPOk1TTw==")) + .willReturn( + aResponse() + .withHeader("Content-Type", "application/json") + .withBodyFile("aai/resources/mockObject.json") + .withStatus(200))); + AAIResourcesClient client = new AAIResourcesClient(); + client.get(path); + } + + @Test public void verifyConnect() { AAIResourceUri path = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test2"); AAIResourceUri path2 = AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, "test3"); diff --git a/common/src/test/resources/aai.properties b/common/src/test/resources/aai.properties index 9d9f1bdce9..897659b332 100644 --- a/common/src/test/resources/aai.properties +++ b/common/src/test/resources/aai.properties @@ -1 +1,3 @@ -aai.endpoint=http://localhost:8443
\ No newline at end of file +aai.endpoint=http://localhost:8443 +aai.auth=2630606608347B7124C244AB0FE34F6F +mso.msoKey=07a7159d3bf51a0e53be7a8f89699be7
\ No newline at end of file |