Skip to content

refactor: add support for trailer building and update deps #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions grpc-context-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ dependencies {
implementation("org.slf4j:slf4j-api:1.7.36")

constraints {
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.4") {
because("https://nvd.nist.gov/vuln/detail/CVE-2022-42004")
implementation("com.fasterxml.jackson.core:jackson-databind:2.13.4.2") {
because("https://nvd.nist.gov/vuln/detail/CVE-2022-42003")
}
implementation("com.google.protobuf:protobuf-java:3.21.7") {
// Not used directly, but typically used together for since we always use proto and grpc together
because("CVE-2022-3171")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.hypertrace.core.grpcutils.context;

import static io.grpc.Metadata.ASCII_STRING_MARSHALLER;
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.CACHE_MEANINGFUL_HEADERS;
import static org.hypertrace.core.grpcutils.context.RequestContextConstants.TENANT_ID_HEADER_KEY;

import com.google.common.collect.Maps;
import io.grpc.Context;
import io.grpc.Metadata;
import io.grpc.Metadata.Key;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -183,6 +185,19 @@ public <T> ContextualKey<T> buildInternalContextualKey(T data) {
return new DefaultContextualKey<>(this, data, List.of(TENANT_ID_HEADER_KEY));
}

/** Converts the request context into metadata to be used as trailers */
public Metadata buildTrailers() {
Metadata trailers = new Metadata();
// For now, the only context item to use as a trailer is the request id
this.getRequestId()
.ifPresent(
requestId ->
trailers.put(
Key.of(RequestContextConstants.REQUEST_ID_HEADER_KEY, ASCII_STRING_MARSHALLER),
requestId));
return trailers;
}

private Map<String, String> getHeadersOtherThanAuth() {
return Maps.filterKeys(
headers, key -> !key.equals(RequestContextConstants.AUTHORIZATION_HEADER));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import com.google.common.collect.ImmutableList;
import io.grpc.Metadata;
Expand Down Expand Up @@ -156,4 +157,18 @@ public void testMetadataKeys() {
Assertions.assertEquals(
"AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE", requestContext.get("grpc-trace-bin").get());
}

@Test
public void buildsTrailers() {
RequestContext requestContext = RequestContext.forTenantId("test");

// Try building trailers and then request context from them.
RequestContext requestContextFromBuiltTrailers =
RequestContext.fromMetadata(requestContext.buildTrailers());

// Should not be equal because tenant id is not a trailer so should be lost
assertNotEquals(requestContext, requestContextFromBuiltTrailers);
// Request IDs should however be equal
assertEquals(requestContext.getRequestId(), requestContextFromBuiltTrailers.getRequestId());
}
}