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 --- .../so/client/cds/BasicAuthClientInterceptor.java | 53 ++++++++++++++++++++++ .../onap/so/client/cds/CDSProcessingClient.java | 1 + .../java/org/onap/so/client/cds/CDSProperties.java | 3 ++ 3 files changed, 57 insertions(+) create mode 100644 common/src/main/java/org/onap/so/client/cds/BasicAuthClientInterceptor.java (limited to 'common/src/main/java/org') 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(); } -- cgit 1.2.3-korg