Skip to content

Commit 78159da

Browse files
authored
Merge pull request #2 from hypertrace/update_bin_headers
Added parsing of binaryheaders in RequestContext
2 parents 5a184ef + fe0cf20 commit 78159da

5 files changed

Lines changed: 65 additions & 4 deletions

File tree

grpc-client-utils/src/main/java/org/hypertrace/core/grpcutils/client/RequestContextAsCreds.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.slf4j.LoggerFactory;
1010

1111
import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
12+
import static io.grpc.Metadata.BINARY_BYTE_MARSHALLER;
1213

1314
public abstract class RequestContextAsCreds extends CallCredentials {
1415
private static Logger LOGGER = LoggerFactory.getLogger(RequestContextAsCreds.class);
@@ -31,7 +32,12 @@ protected void applyRequestContext(MetadataApplier applier, RequestContext reque
3132
for (Map.Entry<String, String> entry : requestContext.getAll().entrySet()) {
3233
// Exclude null headers
3334
if (entry.getValue() != null) {
34-
metadata.put(Metadata.Key.of(entry.getKey(), ASCII_STRING_MARSHALLER), entry.getValue());
35+
String key = entry.getKey();
36+
if (key.toLowerCase().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
37+
metadata.put(Metadata.Key.of(entry.getKey(), BINARY_BYTE_MARSHALLER), entry.getValue().getBytes());
38+
} else {
39+
metadata.put(Metadata.Key.of(entry.getKey(), ASCII_STRING_MARSHALLER), entry.getValue());
40+
}
3541
}
3642
}
3743
}

grpc-client-utils/src/test/java/org/hypertrace/core/grpcutils/client/ContextKeyBasedCredsTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.Assertions;
1313
import org.junit.jupiter.api.Test;
1414

15+
import static io.grpc.Metadata.BINARY_BYTE_MARSHALLER;
1516
import static org.mockito.Mockito.mock;
1617

1718
public class ContextKeyBasedCredsTest {
@@ -29,6 +30,8 @@ public void testApplyRequestMetadata_shouldApplyAllHeaders() {
2930
requestContext.add(RequestContextConstants.TENANT_ID_HEADER_KEY, TENANT_ID);
3031
requestContext.add(RequestContextConstants.AUTHORIZATION_HEADER, TEST_AUTH_HEADER);
3132
requestContext.add("x-some-tenant-header", "v1");
33+
requestContext.add("grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE");
34+
3235

3336
Context ctx = Context.current().withValue(RequestContext.CURRENT, requestContext);
3437

@@ -38,14 +41,23 @@ public void testApplyRequestMetadata_shouldApplyAllHeaders() {
3841
@Override
3942
public void apply(Metadata headers) {
4043
Map<String, String> headersMap = headers.keys().stream()
41-
.collect(Collectors.toUnmodifiableMap(k -> k, k -> headers.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER))));
44+
.collect(Collectors.toUnmodifiableMap(k -> k, k -> {
45+
String value;
46+
if (k.toLowerCase().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
47+
value = new String(headers.get(Metadata.Key.of(k, Metadata.BINARY_BYTE_MARSHALLER)));
48+
} else {
49+
value = headers.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER));
50+
}
51+
return value;
52+
} ));
4253

4354
// Should filter out the TENANT_ID_HEADER_KEY
4455
Assertions.assertEquals(
4556
Map.of(
4657
RequestContextConstants.TENANT_ID_HEADER_KEY, TENANT_ID,
4758
RequestContextConstants.AUTHORIZATION_HEADER, TEST_AUTH_HEADER,
48-
"x-some-tenant-header", "v1"
59+
"x-some-tenant-header", "v1",
60+
"grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE"
4961
),
5062
headersMap);
5163
}

grpc-client-utils/src/test/java/org/hypertrace/core/grpcutils/client/GrpcClientRequestContextUtilTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,26 @@ public void testExecuteWithHeadersContextJustAuthHeader_shouldPropagateAllHeader
6161
}
6262
}
6363

64+
@Test
65+
public void testExecuteWithHeadersContextAuthAndTracing_shouldPropagateAllHeaders() {
66+
Context ctx = Context.current();
67+
Context previous = ctx.attach();
68+
try {
69+
Map<String, String> requestHeaders = Map.of("authorization", "v1", "a2", "v2",
70+
"grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE");
71+
72+
GrpcClientRequestContextUtil.executeWithHeadersContext(requestHeaders, () -> {
73+
RequestContext requestContext = RequestContext.CURRENT.get();
74+
Assertions.assertEquals(Map.of("authorization", "v1", "a2", "v2",
75+
"grpc-trace-bin", "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE"), requestContext.getRequestHeaders());
76+
return new Object();
77+
});
78+
} finally {
79+
ctx.detach(previous);
80+
}
81+
}
82+
83+
6484
@Test
6585
public void testExecuteInTenantIdContext() {
6686
Assertions.assertNull(RequestContext.CURRENT.get());

grpc-server-utils/src/main/java/org/hypertrace/core/grpcutils/server/RequestContextServerInterceptor.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import org.hypertrace.core.grpcutils.context.RequestContext;
1010
import org.hypertrace.core.grpcutils.context.RequestContextConstants;
1111

12+
import java.nio.charset.StandardCharsets;
13+
1214
/**
1315
* Interceptor which intercepts the request headers to extract request context and sets it in the context so that the
1416
* server logic can use the context and they can be passed onto other downstream services.
@@ -41,7 +43,14 @@ RequestContext createRequestContextFromMetadata(Metadata metadata) {
4143
RequestContextConstants.HEADER_PREFIXES_TO_BE_PROPAGATED.stream()
4244
.anyMatch(prefix -> k.toLowerCase().startsWith(prefix.toLowerCase())))
4345
.forEach(k -> {
44-
String value = metadata.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER));
46+
String value;
47+
//check if key ends with binary suffix
48+
if (k.toLowerCase().endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
49+
byte[] bytes = metadata.get(Metadata.Key.of(k, Metadata.BINARY_BYTE_MARSHALLER));
50+
value = new String(bytes, StandardCharsets.UTF_8);
51+
} else {
52+
value = metadata.get(Metadata.Key.of(k, Metadata.ASCII_STRING_MARSHALLER));
53+
}
4554
// The value could be null or empty for some keys so validate that.
4655
if (value != null && !value.isEmpty()) {
4756
requestContext.add(k, value);

grpc-server-utils/src/test/java/org/hypertrace/core/grpcutils/server/RequestContextServerInterceptorTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,19 @@ public void testMetadataKeys() {
4343
Assertions.assertEquals(2, requestContext.getAll().size());
4444
Assertions.assertEquals("Bearer Some-bearer-auth-2", requestContext.get("authorization").get());
4545
Assertions.assertEquals("test-tenant-id-2", requestContext.get(RequestContextConstants.TENANT_ID_METADATA_KEY.name()).get());
46+
47+
48+
metadata = new Metadata();
49+
metadata.put(Metadata.Key.of("Authorization", Metadata.ASCII_STRING_MARSHALLER), "Bearer Some-bearer-auth-3");
50+
metadata.put(Metadata.Key.of("X-tenant-Id", Metadata.ASCII_STRING_MARSHALLER), "test-tenant-id-3");
51+
metadata.put(Metadata.Key.of("X-Some-Other-Header", Metadata.ASCII_STRING_MARSHALLER), "Some-other-header-val-3");
52+
metadata.put(Metadata.Key.of("grpc-trace-bin", Metadata.BINARY_BYTE_MARSHALLER), "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE".getBytes());
53+
54+
requestContext = interceptor.createRequestContextFromMetadata(metadata);
55+
56+
Assertions.assertEquals(3, requestContext.getAll().size());
57+
Assertions.assertEquals("Bearer Some-bearer-auth-3", requestContext.get("authorization").get());
58+
Assertions.assertEquals("test-tenant-id-3", requestContext.get(RequestContextConstants.TENANT_ID_METADATA_KEY.name()).get());
59+
Assertions.assertEquals("AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE", requestContext.get("grpc-trace-bin").get());
4660
}
4761
}

0 commit comments

Comments
 (0)