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 {
19
19
implementation(" org.slf4j:slf4j-api:1.7.36" )
20
20
21
21
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" )
24
28
}
25
29
}
26
30
Original file line number Diff line number Diff line change 1
1
package org .hypertrace .core .grpcutils .context ;
2
2
3
+ import static io .grpc .Metadata .ASCII_STRING_MARSHALLER ;
3
4
import static org .hypertrace .core .grpcutils .context .RequestContextConstants .CACHE_MEANINGFUL_HEADERS ;
4
5
import static org .hypertrace .core .grpcutils .context .RequestContextConstants .TENANT_ID_HEADER_KEY ;
5
6
6
7
import com .google .common .collect .Maps ;
7
8
import io .grpc .Context ;
8
9
import io .grpc .Metadata ;
10
+ import io .grpc .Metadata .Key ;
9
11
import java .nio .charset .StandardCharsets ;
10
12
import java .util .Collections ;
11
13
import java .util .HashMap ;
@@ -183,6 +185,19 @@ public <T> ContextualKey<T> buildInternalContextualKey(T data) {
183
185
return new DefaultContextualKey <>(this , data , List .of (TENANT_ID_HEADER_KEY ));
184
186
}
185
187
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
+
186
201
private Map <String , String > getHeadersOtherThanAuth () {
187
202
return Maps .filterKeys (
188
203
headers , key -> !key .equals (RequestContextConstants .AUTHORIZATION_HEADER ));
Original file line number Diff line number Diff line change 2
2
3
3
import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
4
4
import static org .junit .jupiter .api .Assertions .assertEquals ;
5
+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
5
6
6
7
import com .google .common .collect .ImmutableList ;
7
8
import io .grpc .Metadata ;
@@ -156,4 +157,18 @@ public void testMetadataKeys() {
156
157
Assertions .assertEquals (
157
158
"AAARf5ZpQwlN/8FVe1axOPlaAQIdRU/Y8j0LAgE" , requestContext .get ("grpc-trace-bin" ).get ());
158
159
}
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
+ }
159
174
}
You can’t perform that action at this time.
0 commit comments