From 9434bf784aae4fa85fd2e1b99145b403643327ab Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Mon, 4 Mar 2019 21:44:35 -0500 Subject: Add support for CDS basic-auth MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I22834f989afb748917aa2099b1da78c5f794bbe5 Issue-ID: CCSDK-1055 Signed-off-by: Alexis de Talhouët --- .../client/restproperties/CDSPropertiesImpl.java | 6 +++ .../so/client/cds/BasicAuthClientInterceptor.java | 53 ++++++++++++++++++++++ .../onap/so/client/cds/CDSProcessingClient.java | 1 + .../java/org/onap/so/client/cds/CDSProperties.java | 3 ++ .../onap/so/client/cds/TestCDSPropertiesImpl.java | 9 +++- 5 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/org/onap/so/client/cds/BasicAuthClientInterceptor.java diff --git a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/restproperties/CDSPropertiesImpl.java b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/restproperties/CDSPropertiesImpl.java index b8ab588a75..dfdef74886 100644 --- a/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/restproperties/CDSPropertiesImpl.java +++ b/bpmn/MSOCommonBPMN/src/main/java/org/onap/so/client/restproperties/CDSPropertiesImpl.java @@ -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 index 0000000000..384d479501 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/cds/BasicAuthClientInterceptor.java @@ -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 ClientCall interceptCall( + MethodDescriptor method, + CallOptions callOptions, + Channel channel) { + + Key authHeader = Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER); + + return new ForwardingClientCall.SimpleForwardingClientCall( + channel.newCall(method, callOptions)) { + @Override + public void start(Listener responseListener, Metadata headers) { + headers.put(authHeader, props.getBasicAuth()); + super.start(responseListener, headers); + } + }; + } +} diff --git a/common/src/main/java/org/onap/so/client/cds/CDSProcessingClient.java b/common/src/main/java/org/onap/so/client/cds/CDSProcessingClient.java index 0901cf589b..1e372112f1 100644 --- a/common/src/main/java/org/onap/so/client/cds/CDSProcessingClient.java +++ b/common/src/main/java/org/onap/so/client/cds/CDSProcessingClient.java @@ -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); diff --git a/common/src/main/java/org/onap/so/client/cds/CDSProperties.java b/common/src/main/java/org/onap/so/client/cds/CDSProperties.java index bb2a54ec98..52d1d614ed 100644 --- a/common/src/main/java/org/onap/so/client/cds/CDSProperties.java +++ b/common/src/main/java/org/onap/so/client/cds/CDSProperties.java @@ -21,5 +21,8 @@ import org.onap.so.client.RestProperties; public interface CDSProperties extends RestProperties { String getHost(); + int getPort(); + + String getBasicAuth(); } diff --git a/common/src/test/java/org/onap/so/client/cds/TestCDSPropertiesImpl.java b/common/src/test/java/org/onap/so/client/cds/TestCDSPropertiesImpl.java index efb9b07871..a2937869c8 100644 --- a/common/src/test/java/org/onap/so/client/cds/TestCDSPropertiesImpl.java +++ b/common/src/test/java/org/onap/so/client/cds/TestCDSPropertiesImpl.java @@ -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 -- cgit 1.2.3-korg