Add support for CDS basic-auth 74/79674/3
authorAlexis de Talhouët <adetalhouet89@gmail.com>
Tue, 5 Mar 2019 02:44:35 +0000 (21:44 -0500)
committerMarcus Williams <marcus.williams@intel.com>
Wed, 6 Mar 2019 15:58:15 +0000 (15:58 +0000)
Change-Id: I22834f989afb748917aa2099b1da78c5f794bbe5
Issue-ID: CCSDK-1055
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/restproperties/CDSPropertiesImpl.java
common/src/main/java/org/onap/so/client/cds/BasicAuthClientInterceptor.java [new file with mode: 0644]
common/src/main/java/org/onap/so/client/cds/CDSProcessingClient.java
common/src/main/java/org/onap/so/client/cds/CDSProperties.java
common/src/test/java/org/onap/so/client/cds/TestCDSPropertiesImpl.java

index b8ab588..dfdef74 100644 (file)
@@ -24,6 +24,7 @@ public class CDSPropertiesImpl implements CDSProperties {
 
     private static final String ENDPOINT = "cds.endpoint";
     private static final String PORT = "cds.port";
+    private static final String AUTH = "cds.auth";
 
     public CDSPropertiesImpl() {
         // Needed for service loader
@@ -39,6 +40,11 @@ public class CDSPropertiesImpl implements CDSProperties {
         return Integer.parseInt(Objects.requireNonNull(UrnPropertiesReader.getVariable(PORT)));
     }
 
+    @Override
+    public String getBasicAuth() {
+        return Objects.requireNonNull(UrnPropertiesReader.getVariable(AUTH));
+    }
+
     @Override
     public URL getEndpoint() {
         return null;
diff --git a/common/src/main/java/org/onap/so/client/cds/BasicAuthClientInterceptor.java b/common/src/main/java/org/onap/so/client/cds/BasicAuthClientInterceptor.java
new file mode 100644 (file)
index 0000000..384d479
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2019 Bell Canada.
+ *
+ * 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.
+ */
+package org.onap.so.client.cds;
+
+import io.grpc.CallOptions;
+import io.grpc.Channel;
+import io.grpc.ClientCall;
+import io.grpc.ClientCall.Listener;
+import io.grpc.ClientInterceptor;
+import io.grpc.ForwardingClientCall;
+import io.grpc.Metadata;
+import io.grpc.Metadata.Key;
+import io.grpc.MethodDescriptor;
+
+public class BasicAuthClientInterceptor implements ClientInterceptor {
+
+    private CDSProperties props;
+
+    public BasicAuthClientInterceptor(CDSProperties props) {
+        this.props = props;
+    }
+
+    @Override
+    public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
+        MethodDescriptor<ReqT, RespT> method,
+        CallOptions callOptions,
+        Channel channel) {
+
+        Key<String> authHeader = Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER);
+
+        return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(
+            channel.newCall(method, callOptions)) {
+            @Override
+            public void start(Listener<RespT> responseListener, Metadata headers) {
+                headers.put(authHeader, props.getBasicAuth());
+                super.start(responseListener, headers);
+            }
+        };
+    }
+}
index 0901cf5..1e37211 100644 (file)
@@ -73,6 +73,7 @@ public class CDSProcessingClient implements AutoCloseable {
             .forAddress(props.getHost(), props.getPort())
             .nameResolverFactory(new DnsNameResolverProvider())
             .loadBalancerFactory(new PickFirstLoadBalancerProvider())
+            .intercept(new BasicAuthClientInterceptor(props))
             .usePlaintext()
             .build();
         this.handler = new CDSProcessingHandler(listener);
index bb2a54e..52d1d61 100644 (file)
@@ -21,5 +21,8 @@ import org.onap.so.client.RestProperties;
 public interface CDSProperties extends RestProperties {
 
     String getHost();
+
     int getPort();
+
+    String getBasicAuth();
 }
index efb9b07..a293786 100644 (file)
@@ -26,12 +26,17 @@ public class TestCDSPropertiesImpl implements CDSProperties {
 
     @Override
     public String getHost() {
-        return "endpoint";
+        return "localhost";
     }
 
     @Override
     public int getPort() {
-        return 9999;
+        return 9111;
+    }
+
+    @Override
+    public String getBasicAuth() {
+        return "Basic Y2NzZGthcHBzOmNjc2RrYXBwcw==";
     }
 
     @Override