AAIRestClient support for Basic Auth 21/43621/2
authorRob Daugherty <rd472p@att.com>
Wed, 18 Apr 2018 20:57:11 +0000 (16:57 -0400)
committerRob Daugherty <rd472p@att.com>
Wed, 18 Apr 2018 21:07:26 +0000 (17:07 -0400)
Adding support for Basic Auth in the AAI Rest Clients because
ONAP uses this (whereas ECOMP uses 2-way SSL).

In general, each AAI client will allow the user to configure
properties called "aai.auth" and "mso.msoKey".  If these are
set, then the client will add the Authorization header to every
request.

Change-Id: I7c81ec05d2ec4a7dca131f2e9e19d341ac89b09f
Issue-ID: SO-576
Signed-off-by: Rob Daugherty <rd472p@att.com>
asdc-controller/src/main/java/org/openecomp/mso/asdc/tenantIsolation/AaiClientPropertiesImpl.java
bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/adapter/vnf/AdapterRestClient.java
bpmn/MSOCommonBPMN/src/main/java/org/openecomp/mso/client/restproperties/AAIPropertiesImpl.java
common/src/main/java/org/openecomp/mso/client/aai/AAIProperties.java
common/src/main/java/org/openecomp/mso/client/aai/AAIRestClient.java
common/src/main/java/org/openecomp/mso/client/defaultproperties/DefaultAAIPropertiesImpl.java
common/src/main/java/org/openecomp/mso/client/policy/RestClient.java
common/src/test/java/org/openecomp/mso/client/aai/AAIResourcesClientTest.java
common/src/test/resources/aai.properties
mso-api-handlers/mso-api-handler-infra/src/main/java/org/openecomp/mso/apihandlerinfra/tenantisolation/AaiClientPropertiesImpl.java

index 537de3e..be5af5d 100644 (file)
@@ -49,4 +49,14 @@ public class AaiClientPropertiesImpl implements AAIProperties {
        public AAIVersion getDefaultVersion() {
                return AAIVersion.LATEST;
        }
+
+       @Override
+       public String getAuth() {
+               return props.getProperty("aai.auth", null);
+       }
+
+       @Override
+       public String getKey() {
+               return props.getProperty("mso.msoKey", null);
+       }
 }
index 3e315a5..16fd351 100644 (file)
@@ -21,7 +21,6 @@
 package org.openecomp.mso.client.adapter.vnf;
 
 import java.net.URI;
-import java.security.GeneralSecurityException;
 import java.util.Map;
 import java.util.Optional;
 import java.util.UUID;
@@ -29,8 +28,6 @@ import java.util.UUID;
 import javax.ws.rs.client.ClientResponseFilter;
 import javax.ws.rs.ext.ContextResolver;
 
-import org.apache.commons.codec.binary.Base64;
-import org.openecomp.mso.bpmn.common.util.CryptoUtils;
 import org.openecomp.mso.client.ResponseExceptionMapperImpl;
 import org.openecomp.mso.client.policy.JettisonStyleMapperProvider;
 import org.openecomp.mso.client.policy.RestClient;
@@ -52,8 +49,7 @@ public class AdapterRestClient extends RestClient {
 
        @Override
        protected void initializeHeaderMap(Map<String, String> headerMap) {
-               headerMap.put("Authorization",
-                               this.getBasicAuth(props.getAuth(), props.getKey()));
+               addBasicAuthHeader(props.getAuth(), props.getKey());
        }
 
        @Override
@@ -70,20 +66,4 @@ public class AdapterRestClient extends RestClient {
        protected ContextResolver<ObjectMapper> getMapper() {
                return new JettisonStyleMapperProvider();
        }
-       
-       private String getBasicAuth(String encryptedAuth, String msoKey) {
-               if ((encryptedAuth == null || encryptedAuth.isEmpty()) || (msoKey == null || msoKey.isEmpty())) {
-                       return null;
-               }
-               try {
-                       String auth = CryptoUtils.decrypt(encryptedAuth, msoKey);
-                       byte[] encoded = Base64.encodeBase64(auth.getBytes());
-                       String encodedString = new String(encoded);
-                       encodedString = "Basic " + encodedString;
-                       return encodedString;
-               } catch (GeneralSecurityException e) {
-                       this.logger.warn(e.getMessage(), e);
-                       return null;
-               }
-       }
 }
index 27352dc..a1ef35a 100644 (file)
@@ -52,4 +52,13 @@ public class AAIPropertiesImpl implements AAIProperties {
                return AAIVersion.LATEST;
        }
 
+       @Override
+       public String getAuth() {
+               return props.get("aai.auth");
+       }
+
+       @Override
+       public String getKey() {
+               return props.get("mso.msoKey");
+       }
 }
index 358bbbb..c208d6d 100644 (file)
@@ -25,4 +25,6 @@ import org.openecomp.mso.client.RestProperties;
 public interface AAIProperties extends RestProperties {
 
        public AAIVersion getDefaultVersion();
+       public String getAuth();
+       public String getKey();
 }
index 9348beb..e36033f 100644 (file)
@@ -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
index 354d47a..3f5bfa9 100644 (file)
@@ -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();
+       }
 }
index 4e6ffd1..77afe82 100644 (file)
@@ -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();
index daf8130..c7cc549 100644 (file)
@@ -77,6 +77,21 @@ public class AAIResourcesClientTest {
                client.delete(path);
        }
        
+       @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");
index 9d9f1bd..897659b 100644 (file)
@@ -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
index 03af038..92e74e8 100644 (file)
@@ -49,4 +49,14 @@ public class AaiClientPropertiesImpl implements AAIProperties {
        public AAIVersion getDefaultVersion() {
                return AAIVersion.LATEST;
        }
+
+       @Override
+       public String getAuth() {
+               return props.getProperty("aai.auth", null);
+       }
+
+       @Override
+       public String getKey() {
+               return props.getProperty("mso.msoKey", null);
+       }
 }