File tree Expand file tree Collapse file tree 3 files changed +36
-2
lines changed
main/java/org/hypertrace/core/grpcutils/context
test/java/org/hypertrace/core/grpcutils/context Expand file tree Collapse file tree 3 files changed +36
-2
lines changed Original file line number Diff line number Diff line change @@ -19,8 +19,12 @@ dependencies {
1919 implementation(" org.slf4j:slf4j-api:1.7.36" )
2020
2121 constraints {
22- implementation(" com.fasterxml.jackson.core:jackson-databind:2.13.4" ) {
23- because(" https://nvd.nist.gov/vuln/detail/CVE-2022-42004" )
22+ implementation(" com.fasterxml.jackson.core:jackson-databind:2.13.4.2" ) {
23+ because(" https://nvd.nist.gov/vuln/detail/CVE-2022-42003" )
24+ }
25+ implementation(" com.google.protobuf:protobuf-java:3.21.7" ) {
26+ // Not used directly, but typically used together for since we always use proto and grpc together
27+ because(" CVE-2022-3171" )
2428 }
2529 }
2630
Original file line number Diff line number Diff line change 11package org .hypertrace .core .grpcutils .context ;
22
3+ import static io .grpc .Metadata .ASCII_STRING_MARSHALLER ;
34import static org .hypertrace .core .grpcutils .context .RequestContextConstants .CACHE_MEANINGFUL_HEADERS ;
45import static org .hypertrace .core .grpcutils .context .RequestContextConstants .TENANT_ID_HEADER_KEY ;
56
67import com .google .common .collect .Maps ;
78import io .grpc .Context ;
89import io .grpc .Metadata ;
10+ import io .grpc .Metadata .Key ;
911import java .nio .charset .StandardCharsets ;
1012import java .util .Collections ;
1113import java .util .HashMap ;
@@ -183,6 +185,19 @@ public <T> ContextualKey<T> buildInternalContextualKey(T data) {
183185 return new DefaultContextualKey <>(this , data , List .of (TENANT_ID_HEADER_KEY ));
184186 }
185187
188+ /** Converts the request context into metadata to be used as trailers */
189+ public Metadata buildTrailers () {
190+ Metadata trailers = new Metadata ();
191+ // For now, the only context item to use as a trailer is the request id
192+ this .getRequestId ()
193+ .ifPresent (
194+ requestId ->
195+ trailers .put (
196+ Key .of (RequestContextConstants .REQUEST_ID_HEADER_KEY , ASCII_STRING_MARSHALLER ),
197+ requestId ));
198+ return trailers ;
199+ }
200+
186201 private Map <String , String > getHeadersOtherThanAuth () {
187202 return Maps .filterKeys (
188203 headers , key -> !key .equals (RequestContextConstants .AUTHORIZATION_HEADER ));
Original file line number Diff line number Diff line change 22
33import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
44import static org .junit .jupiter .api .Assertions .assertEquals ;
5+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
56
67import com .google .common .collect .ImmutableList ;
78import io .grpc .Metadata ;
@@ -156,4 +157,18 @@ public void testMetadataKeys() {
156157 Assertions .assertEquals (
157158 "AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE" , requestContext .get ("grpc-trace-bin" ).get ());
158159 }
160+
161+ @ Test
162+ public void buildsTrailers () {
163+ RequestContext requestContext = RequestContext .forTenantId ("test" );
164+
165+ // Try building trailers and then request context from them.
166+ RequestContext requestContextFromBuiltTrailers =
167+ RequestContext .fromMetadata (requestContext .buildTrailers ());
168+
169+ // Should not be equal because tenant id is not a trailer so should be lost
170+ assertNotEquals (requestContext , requestContextFromBuiltTrailers );
171+ // Request IDs should however be equal
172+ assertEquals (requestContext .getRequestId (), requestContextFromBuiltTrailers .getRequestId ());
173+ }
159174}
You can’t perform that action at this time.
0 commit comments