Skip to content

Commit 9247f4a

Browse files
committed
ExtProcInterceptor with inner classes ExtProcClientCall and ExtProcListener.
1 parent fa14780 commit 9247f4a

1 file changed

Lines changed: 328 additions & 13 deletions

File tree

‎xds/src/main/java/io/grpc/xds/ExternalProcessorFilter.java‎

Lines changed: 328 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22

33
import static com.google.common.base.Preconditions.checkNotNull;
44

5+
import com.google.common.annotations.VisibleForTesting;
6+
import com.google.common.io.ByteStreams;
57
import com.google.protobuf.Any;
68
import com.google.protobuf.InvalidProtocolBufferException;
79
import com.google.protobuf.Message;
810
import io.envoyproxy.envoy.config.core.v3.GrpcService;
9-
import io.envoyproxy.envoy.extensions.filters.http.ext_proc.v3.ExtProcProto;
11+
import io.envoyproxy.envoy.config.core.v3.HeaderValueOption;
1012
import io.envoyproxy.envoy.extensions.filters.http.ext_proc.v3.ExternalProcessor;
11-
import io.envoyproxy.envoy.service.ext_proc.v3.ExternalProcessorProto;
12-
import io.grpc.CallCredentials;
13+
import io.envoyproxy.envoy.service.ext_proc.v3.ExternalProcessorGrpc;
14+
import io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest;
1315
import io.grpc.CallOptions;
1416
import io.grpc.Channel;
15-
import io.grpc.ChannelCredentials;
1617
import io.grpc.ClientCall;
1718
import io.grpc.ClientInterceptor;
1819

1920
import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
2021
import io.grpc.ManagedChannel;
22+
import io.grpc.Metadata;
2123
import io.grpc.MethodDescriptor;
24+
import java.io.IOException;
25+
import java.io.InputStream;
2226
import javax.annotation.Nullable;
2327
import java.util.concurrent.ScheduledExecutorService;
2428

@@ -71,7 +75,7 @@ public ConfigOrError<? extends FilterConfig> parseFilterConfigOverride(Message r
7175
@Override
7276
public ClientInterceptor buildClientInterceptor(FilterConfig filterConfig,
7377
@Nullable FilterConfig overrideConfig, ScheduledExecutorService scheduler) {
74-
return new ExternalProcessorInterceptor(filterConfig, overrideConfig, scheduler);
78+
return new ExternalProcessorInterceptor((ExternalProcessorFilterConfig) filterConfig, overrideConfig, scheduler);
7579
}
7680

7781
static final class ExternalProcessorFilterConfig implements FilterConfig {
@@ -89,13 +93,12 @@ public String typeUrl() {
8993
}
9094

9195
static final class ExternalProcessorInterceptor implements ClientInterceptor {
92-
private final FilterConfig filterConfig;
96+
private final ExternalProcessorFilterConfig filterConfig;
9397
private final FilterConfig overrideConfig;
9498
private final ScheduledExecutorService scheduler;
9599

96-
ExternalProcessorInterceptor(FilterConfig filterConfig,
100+
ExternalProcessorInterceptor(ExternalProcessorFilterConfig filterConfig,
97101
@Nullable FilterConfig overrideConfig, ScheduledExecutorService scheduler) {
98-
99102
this.filterConfig = filterConfig;
100103
this.overrideConfig = overrideConfig;
101104
this.scheduler = scheduler;
@@ -107,12 +110,324 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
107110
CallOptions callOptions,
108111
Channel next) {
109112

110-
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
111-
@Override
112-
public void sendMessage(ReqT message) {
113-
super.sendMessage(message);
113+
ExternalProcessorGrpc.ExternalProcessorStub stub = getExternalProcessorStub(filterConfig.externalProcessor.getGrpcService());
114+
115+
// Wrap the outgoing call to intercept client events
116+
return new ExtProcClientCall<>(next.newCall(method, callOptions), stub, method);
117+
}
118+
119+
// --- SHARED UTILITY METHODS ---
120+
private static io.envoyproxy.envoy.config.core.v3.HeaderMap toHeaderMap(Metadata metadata) {
121+
io.envoyproxy.envoy.config.core.v3.HeaderMap.Builder builder =
122+
io.envoyproxy.envoy.config.core.v3.HeaderMap.newBuilder();
123+
124+
for (String key : metadata.keys()) {
125+
// Skip binary headers for this basic mapping
126+
if (key.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
127+
Metadata.Key<byte[]> binKey = Metadata.Key.of(key, Metadata.BINARY_BYTE_MARSHALLER);
128+
for (byte[] binValue : metadata.getAll(binKey)) {
129+
String encoded = com.google.common.io.BaseEncoding.base64().encode(binValue);
130+
io.envoyproxy.envoy.config.core.v3.HeaderValue headerValue =
131+
io.envoyproxy.envoy.config.core.v3.HeaderValue.newBuilder()
132+
.setKey(key.toLowerCase()) // Envoy expects lowercase keys, following the same convention here
133+
.setValue(encoded)
134+
.build();
135+
builder.addHeaders(headerValue);
136+
}
137+
} else {
138+
Metadata.Key<String> asciiKey = Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER);
139+
Iterable<String> values = metadata.getAll(asciiKey);
140+
141+
if (values != null) {
142+
for (String value : values) {
143+
io.envoyproxy.envoy.config.core.v3.HeaderValue headerValue =
144+
io.envoyproxy.envoy.config.core.v3.HeaderValue.newBuilder()
145+
.setKey(key.toLowerCase()) // Envoy expects lowercase keys, following the same convention here
146+
.setValue(value)
147+
.build();
148+
builder.addHeaders(headerValue);
149+
}
150+
}
151+
}
152+
}
153+
return builder.build();
154+
}
155+
156+
private static void applyHeaderMutations(Metadata headers, io.envoyproxy.envoy.service.ext_proc.v3.HeaderMutation mutation) {
157+
// 1. Process Set/Add/Append operations
158+
for (io.envoyproxy.envoy.config.core.v3.HeaderValueOption opt : mutation.getSetHeadersList()) {
159+
String keyStr = opt.getHeader().getKey().toLowerCase();
160+
String valueStr = opt.getHeader().getValue();
161+
boolean isBinary = keyStr.endsWith(Metadata.BINARY_HEADER_SUFFIX);
162+
163+
if (isBinary) {
164+
Metadata.Key<byte[]> key = Metadata.Key.of(keyStr, Metadata.BINARY_BYTE_MARSHALLER);
165+
if (!opt.getAppend().getValue()) {
166+
headers.discardAll(key);
167+
}
168+
// Decode Base64 string from ExtProc back to raw bytes for gRPC
169+
byte[] decodedValue = com.google.common.io.BaseEncoding.base64().decode(valueStr);
170+
headers.put(key, decodedValue);
171+
} else {
172+
Metadata.Key<String> key = Metadata.Key.of(keyStr, Metadata.ASCII_STRING_MARSHALLER);
173+
if (!opt.getAppend().getValue()) {
174+
headers.discardAll(key);
175+
}
176+
headers.put(key, valueStr);
177+
}
178+
}
179+
180+
// 2. Process Remove operations
181+
for (String keyToRemove : mutation.getRemoveHeadersList()) {
182+
String lowKey = keyToRemove.toLowerCase();
183+
if (lowKey.endsWith(Metadata.BINARY_HEADER_SUFFIX)) {
184+
headers.discardAll(Metadata.Key.of(lowKey, Metadata.BINARY_BYTE_MARSHALLER));
185+
} else {
186+
headers.discardAll(Metadata.Key.of(lowKey, Metadata.ASCII_STRING_MARSHALLER));
187+
}
188+
}
189+
}
190+
191+
/**
192+
* Handles the bidirectional stream with the External Processor.
193+
* Buffers the actual RPC start until the Ext Proc header response is received.
194+
*/
195+
private static class ExtProcClientCall<ReqT, RespT> extends SimpleForwardingClientCall<ReqT, RespT> {
196+
private final ExternalProcessorGrpc.ExternalProcessorStub stub;
197+
private final MethodDescriptor<ReqT, RespT> method;
198+
private io.grpc.stub.StreamObserver<io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest> requestObserver;
199+
200+
private boolean headersSent = false;
201+
private Metadata requestHeaders;
202+
private final java.util.Queue<Runnable> pendingActions = new java.util.concurrent.ConcurrentLinkedQueue<>();
203+
204+
protected ExtProcClientCall(ClientCall<ReqT, RespT> delegate,
205+
ExternalProcessorGrpc.ExternalProcessorStub stub,
206+
MethodDescriptor<ReqT, RespT> method) {
207+
super(delegate);
208+
this.stub = stub;
209+
this.method = method;
210+
}
211+
212+
@Override
213+
public void start(Listener<RespT> responseListener, Metadata headers) {
214+
this.requestHeaders = headers;
215+
ExternalProcessorInterceptor.ExtProcListener<RespT> wrappedListener = new ExternalProcessorInterceptor.ExtProcListener<>(responseListener, delegate(), method);
216+
217+
requestObserver = stub.process(new io.grpc.stub.StreamObserver<io.envoyproxy.envoy.service.ext_proc.v3.ProcessingResponse>() {
218+
@Override
219+
public void onNext(io.envoyproxy.envoy.service.ext_proc.v3.ProcessingResponse response) {
220+
if (response.hasImmediateResponse()) {
221+
handleImmediateResponse(response.getImmediateResponse(), responseListener);
222+
return;
223+
}
224+
225+
// --- Handlers for 6 Event types ---
226+
227+
// 1. Client Headers
228+
if (response.hasRequestHeaders()) {
229+
if (response.getRequestHeaders().hasResponse()) {
230+
applyHeaderMutations(requestHeaders, response.getRequestHeaders().getResponse().getHeaderMutation());
231+
}
232+
headersSent = true;
233+
delegate().start(wrappedListener, requestHeaders);
234+
drainQueue();
235+
}
236+
// 2. Client Message (Request Body)
237+
else if (response.hasRequestBody()) {
238+
handleRequestBodyResponse(response.getRequestBody());
239+
drainQueue();
240+
}
241+
// 3. We don't send request trailers in gRPC for half close.
242+
// 4. Server Headers
243+
else if (response.hasResponseHeaders()) {
244+
if (response.getResponseHeaders().hasResponse()) {
245+
applyHeaderMutations(wrappedListener.savedHeaders, response.getResponseHeaders().getResponse().getHeaderMutation());
246+
}
247+
wrappedListener.proceedWithHeaders();
248+
}
249+
// 5. Server Message (Response Body)
250+
else if (response.hasResponseBody()) {
251+
handleResponseBodyResponse(response.getResponseBody(), wrappedListener);
252+
}
253+
// 6. Response Trailers Handshake Result
254+
if (response.hasResponseTrailers()) {
255+
// Use header_mutation directly from the TrailersResponse message
256+
if (response.getResponseTrailers().hasHeaderMutation()) {
257+
applyHeaderMutations(
258+
wrappedListener.savedTrailers,
259+
response.getResponseTrailers().getHeaderMutation()
260+
);
261+
}
262+
// Finally notify the local app of the completion
263+
wrappedListener.proceedWithClose();
264+
}
265+
}
266+
267+
@Override public void onError(Throwable t) { delegate().cancel("ExtProc failed", t); }
268+
@Override public void onCompleted() {}
269+
});
270+
271+
wrappedListener.setStream(requestObserver);
272+
273+
requestObserver.onNext(io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest.newBuilder()
274+
.setRequestHeaders(io.envoyproxy.envoy.service.ext_proc.v3.HttpHeaders.newBuilder()
275+
.setHeaders(toHeaderMap(headers))
276+
.build())
277+
.build());
278+
}
279+
280+
@Override
281+
public void sendMessage(ReqT message) {
282+
if (!headersSent) {
283+
// If headers haven't been cleared by ext_proc yet, buffer the whole action
284+
pendingActions.add(() -> sendMessage(message));
285+
return;
114286
}
115-
};
287+
288+
try (InputStream is = method.streamRequest(message)) {
289+
// Correctly convert InputStream to byte array using Guava
290+
byte[] bodyBytes = ByteStreams.toByteArray(is);
291+
292+
requestObserver.onNext(io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest.newBuilder()
293+
.setRequestBody(io.envoyproxy.envoy.service.ext_proc.v3.HttpBody.newBuilder()
294+
.setBody(com.google.protobuf.ByteString.copyFrom(bodyBytes))
295+
.build())
296+
.build());
297+
298+
// 3. Queue the ACTUAL delegate call.
299+
// We use super.sendMessage to bypass this interceptor's logic and move to the next call in the chain.
300+
pendingActions.add(() -> super.sendMessage(message));
301+
} catch (IOException e) {
302+
delegate().cancel("Failed to serialize message for External Processor", e);
303+
}
304+
}
305+
306+
private void handleRequestBodyResponse(io.envoyproxy.envoy.service.ext_proc.v3.BodyResponse bodyResponse) {
307+
// If ExtProc modified the body, you would deserialize it here.
308+
// For simplicity, we assume the original message is sent if no BodyMutation exists.
309+
if (bodyResponse.hasResponse() && bodyResponse.getResponse().hasBodyMutation()) {
310+
// Logic to deserialize modified bytes back to ReqT would go here
311+
}
312+
}
313+
314+
private void handleResponseBodyResponse(io.envoyproxy.envoy.service.ext_proc.v3.BodyResponse bodyResponse, ExternalProcessorInterceptor.ExtProcListener<RespT> listener) {
315+
// Pass the (potentially modified) message to the real listener
316+
listener.proceedWithNextMessage();
317+
}
318+
319+
private void drainQueue() {
320+
Runnable action;
321+
while ((action = pendingActions.poll()) != null) action.run();
322+
}
323+
324+
private void handleImmediateResponse(io.envoyproxy.envoy.service.ext_proc.v3.ImmediateResponse immediate, Listener<RespT> listener) {
325+
io.grpc.Status status = io.grpc.Status.fromCodeValue(immediate.getGrpcStatus().getStatus());
326+
delegate().cancel("Rejected by ExtProc", null);
327+
listener.onClose(status, new Metadata());
328+
requestObserver.onCompleted();
329+
}
330+
331+
@Override
332+
public void halfClose() {
333+
// Event: Client Half-Close
334+
requestObserver.onNext(io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest.newBuilder()
335+
.setRequestTrailers(io.envoyproxy.envoy.service.ext_proc.v3.HttpTrailers.newBuilder().build())
336+
.build());
337+
super.halfClose();
338+
}
339+
}
340+
341+
private static class ExtProcListener<RespT> extends io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener<RespT> {
342+
private final MethodDescriptor<?, RespT> method;
343+
private final ClientCall<?, RespT> callDelegate; // The actual RPC call
344+
private io.grpc.stub.StreamObserver<io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest> stream;
345+
Metadata savedHeaders;
346+
Metadata savedTrailers;
347+
io.grpc.Status savedStatus;
348+
private final java.util.Queue<RespT> messageQueue = new java.util.concurrent.ConcurrentLinkedQueue<>();
349+
350+
protected ExtProcListener(ClientCall.Listener<RespT> delegate, ClientCall<?, RespT> callDelegate, MethodDescriptor<?, RespT> method) {
351+
super(delegate);
352+
this.method = method;
353+
this.callDelegate = callDelegate;
354+
}
355+
356+
void setStream(io.grpc.stub.StreamObserver<io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest> stream) { this.stream = stream; }
357+
358+
@Override
359+
public void onHeaders(Metadata headers) {
360+
this.savedHeaders = headers;
361+
stream.onNext(ProcessingRequest.newBuilder()
362+
.setResponseHeaders(io.envoyproxy.envoy.service.ext_proc.v3.HttpHeaders.newBuilder()
363+
.setHeaders(toHeaderMap(headers))
364+
.build())
365+
.build());
366+
}
367+
368+
void proceedWithHeaders() { super.onHeaders(savedHeaders); }
369+
370+
@Override
371+
public void onMessage(RespT message) {
372+
try (java.io.InputStream is = method.streamResponse(message)) {
373+
// Use Guava to convert the server's response message to bytes
374+
byte[] bodyBytes = ByteStreams.toByteArray(is);
375+
376+
messageQueue.add(message);
377+
378+
// Event 5: Server Message (Response Body) sent to Ext Proc
379+
stream.onNext(io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest.newBuilder()
380+
.setResponseBody(io.envoyproxy.envoy.service.ext_proc.v3.HttpBody.newBuilder()
381+
.setBody(com.google.protobuf.ByteString.copyFrom(bodyBytes))
382+
.build())
383+
.build());
384+
385+
} catch (java.io.IOException e) {
386+
// 1. Notify the external processor stream of the failure
387+
stream.onError(io.grpc.Status.INTERNAL
388+
.withDescription("Failed to serialize server response for ExtProc")
389+
.withCause(e)
390+
.asRuntimeException());
391+
392+
// 2. Kill the RPC toward the remote service
393+
// This tells the transport to stop receiving/sending data immediately.
394+
callDelegate.cancel("Serialization error in interceptor", e);
395+
396+
// 3. Notify the local application
397+
// This triggers the client's StreamObserver.onError()
398+
super.onClose(io.grpc.Status.INTERNAL.withDescription("Failed to process server response"), new Metadata());
399+
}
400+
}
401+
402+
@Override
403+
public void onClose(io.grpc.Status status, Metadata trailers) {
404+
this.savedStatus = status;
405+
this.savedTrailers = trailers;
406+
407+
// Event 6: Server Trailers with ACTUAL data
408+
stream.onNext(io.envoyproxy.envoy.service.ext_proc.v3.ProcessingRequest.newBuilder()
409+
.setResponseTrailers(io.envoyproxy.envoy.service.ext_proc.v3.HttpTrailers.newBuilder()
410+
.setTrailers(toHeaderMap(savedTrailers)) // Map the captured trailers here
411+
.build())
412+
.build());
413+
}
414+
415+
/**
416+
* Called when ExtProc gives the final "OK" for the trailers phase.
417+
*/
418+
void proceedWithClose() {
419+
super.onClose(savedStatus, savedTrailers);
420+
}
421+
422+
void proceedWithNextMessage() {
423+
RespT msg = messageQueue.poll();
424+
if (msg != null) super.onMessage(msg);
425+
}
426+
}
427+
428+
@VisibleForTesting
429+
ExternalProcessorGrpc.ExternalProcessorStub getExternalProcessorStub(GrpcService service) {
430+
return null; // Implementation needed
116431
}
117432
}
118433
}

0 commit comments

Comments
 (0)