mirror of https://github.com/dapr/java-sdk.git
Add dapr api token as system property
This commit is contained in:
parent
59639eb328
commit
a0aa264ab9
|
@ -489,8 +489,7 @@ public class DaprClientGrpc extends AbstractDaprClient {
|
||||||
headers.put(Key.of("grpc-trace-bin", Metadata.BINARY_BYTE_MARSHALLER), grpcTraceBin);
|
headers.put(Key.of("grpc-trace-bin", Metadata.BINARY_BYTE_MARSHALLER), grpcTraceBin);
|
||||||
}
|
}
|
||||||
|
|
||||||
String daprApiToken = Properties.getStringOrDefault(Constants.DAPR_API_TOKEN,
|
String daprApiToken = Properties.DAPR_API_TOKEN.get();
|
||||||
Constants.DAPR_API_TOKEN, null);
|
|
||||||
if (daprApiToken != null) {
|
if (daprApiToken != null) {
|
||||||
headers.put(Key.of(Constants.DAPR_API_TOKEN_HEADER, Metadata.ASCII_STRING_MARSHALLER), daprApiToken);
|
headers.put(Key.of(Constants.DAPR_API_TOKEN_HEADER, Metadata.ASCII_STRING_MARSHALLER), daprApiToken);
|
||||||
}
|
}
|
||||||
|
|
|
@ -260,7 +260,7 @@ public class DaprHttp implements Closeable {
|
||||||
requestBuilder.method(method, body);
|
requestBuilder.method(method, body);
|
||||||
}
|
}
|
||||||
|
|
||||||
String daprApiToken = Properties.getStringOrDefault(Constants.DAPR_API_TOKEN, Constants.DAPR_API_TOKEN, null);
|
String daprApiToken = Properties.DAPR_API_TOKEN.get();
|
||||||
if (daprApiToken != null) {
|
if (daprApiToken != null) {
|
||||||
requestBuilder.addHeader(Constants.DAPR_API_TOKEN_HEADER, daprApiToken);
|
requestBuilder.addHeader(Constants.DAPR_API_TOKEN_HEADER, daprApiToken);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,13 @@ public class Properties {
|
||||||
*/
|
*/
|
||||||
private static final String DEFAULT_STRING_CHARSET = StandardCharsets.UTF_8.name();
|
private static final String DEFAULT_STRING_CHARSET = StandardCharsets.UTF_8.name();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API token for Dapr after checking system property and environment variable.
|
||||||
|
*/
|
||||||
|
public static final Supplier<String> DAPR_API_TOKEN = () -> getStringOrDefault(
|
||||||
|
"dapr.api.token",
|
||||||
|
Constants.DAPR_API_TOKEN, null);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP port for Dapr after checking system property and environment variable.
|
* HTTP port for Dapr after checking system property and environment variable.
|
||||||
*/
|
*/
|
||||||
|
@ -70,7 +77,7 @@ public class Properties {
|
||||||
*
|
*
|
||||||
* @return Integer from system property (1st) or env variable (2nd) or default (last).
|
* @return Integer from system property (1st) or env variable (2nd) or default (last).
|
||||||
*/
|
*/
|
||||||
public static Integer getIntOrDefault(String propName, String envName, Integer defaultValue) {
|
private static Integer getIntOrDefault(String propName, String envName, Integer defaultValue) {
|
||||||
return getValueOrDefault(propName, envName, defaultValue, s -> Integer.valueOf(s));
|
return getValueOrDefault(propName, envName, defaultValue, s -> Integer.valueOf(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,7 +89,7 @@ public class Properties {
|
||||||
*
|
*
|
||||||
* @return Boolean from system property (1st) or env variable (2nd) or default (last).
|
* @return Boolean from system property (1st) or env variable (2nd) or default (last).
|
||||||
*/
|
*/
|
||||||
public static Boolean getBooleanOrDefault(String propName, String envName, Boolean defaultValue) {
|
private static Boolean getBooleanOrDefault(String propName, String envName, Boolean defaultValue) {
|
||||||
return getValueOrDefault(propName, envName, defaultValue, s -> Boolean.valueOf(s));
|
return getValueOrDefault(propName, envName, defaultValue, s -> Boolean.valueOf(s));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,7 +101,7 @@ public class Properties {
|
||||||
*
|
*
|
||||||
* @return String from system property (1st) or env variable (2nd) or default (last).
|
* @return String from system property (1st) or env variable (2nd) or default (last).
|
||||||
*/
|
*/
|
||||||
public static String getStringOrDefault(String propName, String envName, String defaultValue) {
|
private static String getStringOrDefault(String propName, String envName, String defaultValue) {
|
||||||
return getValueOrDefault(propName, envName, defaultValue, s -> s);
|
return getValueOrDefault(propName, envName, defaultValue, s -> s);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ package io.dapr.client;
|
||||||
|
|
||||||
import io.dapr.exceptions.DaprException;
|
import io.dapr.exceptions.DaprException;
|
||||||
import io.dapr.utils.Constants;
|
import io.dapr.utils.Constants;
|
||||||
|
import io.dapr.utils.Properties;
|
||||||
import io.grpc.Context;
|
import io.grpc.Context;
|
||||||
import okhttp3.MediaType;
|
import okhttp3.MediaType;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
@ -54,7 +55,7 @@ public class DaprHttpTest {
|
||||||
.hasHeader(Constants.DAPR_API_TOKEN_HEADER)
|
.hasHeader(Constants.DAPR_API_TOKEN_HEADER)
|
||||||
.respond(serializer.serialize(EXPECTED_RESULT));
|
.respond(serializer.serialize(EXPECTED_RESULT));
|
||||||
environmentVariables.set(Constants.DAPR_API_TOKEN, "xyz");
|
environmentVariables.set(Constants.DAPR_API_TOKEN, "xyz");
|
||||||
assertEquals("xyz", System.getenv(Constants.DAPR_API_TOKEN));
|
assertEquals("xyz", Properties.DAPR_API_TOKEN.get());
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
Mono<DaprHttp.Response> mono =
|
Mono<DaprHttp.Response> mono =
|
||||||
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
|
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
|
||||||
|
@ -70,7 +71,7 @@ public class DaprHttpTest {
|
||||||
.not()
|
.not()
|
||||||
.hasHeader(Constants.DAPR_API_TOKEN_HEADER)
|
.hasHeader(Constants.DAPR_API_TOKEN_HEADER)
|
||||||
.respond(serializer.serialize(EXPECTED_RESULT));
|
.respond(serializer.serialize(EXPECTED_RESULT));
|
||||||
assertNull(System.getenv(Constants.DAPR_API_TOKEN));
|
assertNull(Properties.DAPR_API_TOKEN.get());
|
||||||
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
DaprHttp daprHttp = new DaprHttp(3500, okHttpClient);
|
||||||
Mono<DaprHttp.Response> mono =
|
Mono<DaprHttp.Response> mono =
|
||||||
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
|
daprHttp.invokeApi("POST", "v1.0/state", null, (byte[]) null, null, Context.current());
|
||||||
|
|
Loading…
Reference in New Issue