mirror of https://github.com/dapr/java-sdk.git
Add supoort for DAPR_API_TOKEN
This commit is contained in:
parent
eede480403
commit
c6055c0b96
|
@ -5,6 +5,7 @@
|
|||
|
||||
package io.dapr.client;
|
||||
|
||||
import io.dapr.DaprClientGrpcInterceptor;
|
||||
import io.dapr.serializer.DaprObjectSerializer;
|
||||
import io.dapr.serializer.DefaultObjectSerializer;
|
||||
import io.dapr.utils.Constants;
|
||||
|
@ -107,7 +108,10 @@ public class DaprClientBuilder {
|
|||
if (port <= 0) {
|
||||
throw new IllegalStateException("Invalid port.");
|
||||
}
|
||||
ManagedChannel channel = ManagedChannelBuilder.forAddress(Constants.DEFAULT_HOSTNAME, port).usePlaintext().build();
|
||||
ManagedChannel channel = ManagedChannelBuilder.forAddress(Constants.DEFAULT_HOSTNAME, port)
|
||||
.usePlaintext()
|
||||
.intercept(new DaprClientGrpcInterceptor())
|
||||
.build();
|
||||
Closeable closeableChannel = () -> {
|
||||
if (channel != null && !channel.isShutdown()) {
|
||||
channel.shutdown();
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) Microsoft Corporation.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
|
||||
package io.dapr;
|
||||
|
||||
import io.dapr.utils.Constants;
|
||||
import io.dapr.utils.Properties;
|
||||
import io.grpc.CallOptions;
|
||||
import io.grpc.Channel;
|
||||
import io.grpc.ClientCall;
|
||||
import io.grpc.ClientInterceptor;
|
||||
import io.grpc.ForwardingClientCall;
|
||||
import io.grpc.Metadata;
|
||||
import io.grpc.Metadata.Key;
|
||||
import io.grpc.MethodDescriptor;
|
||||
|
||||
/**
|
||||
* A dapr grpc client call interceptor.
|
||||
*/
|
||||
public class DaprClientGrpcInterceptor implements ClientInterceptor {
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
@Override
|
||||
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT,
|
||||
RespT> methodDescriptor,
|
||||
CallOptions callOptions, Channel channel) {
|
||||
return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(channel.newCall(methodDescriptor,
|
||||
callOptions)) {
|
||||
@Override
|
||||
public void start(final Listener<RespT> responseListener, final Metadata headers) {
|
||||
String daprApiToken = Properties.getStringOrDefault(Constants.DAPR_API_TOKEN,
|
||||
Constants.DAPR_API_TOKEN, null);
|
||||
if (daprApiToken != null) {
|
||||
headers.put(Key.of(Constants.DAPR_API_TOKEN_HEADER, Metadata.ASCII_STRING_MARSHALLER), daprApiToken);
|
||||
}
|
||||
super.start(responseListener, headers);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import io.dapr.exceptions.DaprError;
|
||||
import io.dapr.exceptions.DaprException;
|
||||
import io.dapr.utils.Constants;
|
||||
import io.dapr.utils.Properties;
|
||||
import io.grpc.Context;
|
||||
import io.opentelemetry.OpenTelemetry;
|
||||
import io.opentelemetry.context.propagation.HttpTextFormat;
|
||||
|
@ -258,6 +259,12 @@ public class DaprHttp implements Closeable {
|
|||
} else {
|
||||
requestBuilder.method(method, body);
|
||||
}
|
||||
|
||||
String daprApiToken = Properties.getStringOrDefault(Constants.DAPR_API_TOKEN, Constants.DAPR_API_TOKEN, null);
|
||||
if (daprApiToken != null) {
|
||||
requestBuilder.addHeader(Constants.DAPR_API_TOKEN_HEADER, daprApiToken);
|
||||
}
|
||||
|
||||
if (headers != null) {
|
||||
Optional.ofNullable(headers.entrySet()).orElse(Collections.emptySet()).stream()
|
||||
.forEach(header -> {
|
||||
|
|
|
@ -60,6 +60,16 @@ public final class Constants {
|
|||
*/
|
||||
public static final String ACTOR_TIMER_RELATIVE_URL_FORMAT = ACTORS_BASE_URL + "/%s/%s/timers/%s";
|
||||
|
||||
/**
|
||||
* Environment variable name for dapr api token.
|
||||
*/
|
||||
public static final String DAPR_API_TOKEN = "DAPR_API_TOKEN";
|
||||
|
||||
/**
|
||||
* Header name for the dapr api token environment variable name.
|
||||
*/
|
||||
public static final String DAPR_API_TOKEN_HEADER = "dapr-api-token";
|
||||
|
||||
/**
|
||||
* Base path to invoke methods.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue