Skip to content

Commit 215cdcd

Browse files
committed
Check for router filter in validation step
1 parent f03b4dd commit 215cdcd

File tree

3 files changed

+19
-39
lines changed

3 files changed

+19
-39
lines changed

packages/grpc-js-xds/src/resolver-xds.ts

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ import { createHttpFilter, HttpFilterConfig, parseOverrideFilterConfig, parseTop
4646
import { EXPERIMENTAL_FAULT_INJECTION } from './environment';
4747
import Filter = experimental.Filter;
4848
import FilterFactory = experimental.FilterFactory;
49-
import BaseFilter = experimental.BaseFilter;
50-
import CallStream = experimental.CallStream;
5149

5250
const TRACER_NAME = 'xds_resolver';
5351

@@ -209,25 +207,6 @@ function protoDurationToDuration(duration: Duration__Output): Duration {
209207
}
210208
}
211209

212-
class NoRouterFilter extends BaseFilter implements Filter {
213-
constructor(private call: CallStream) {
214-
super();
215-
}
216-
217-
sendMetadata(metadata: Promise<Metadata>): Promise<Metadata> {
218-
this.call.cancelWithStatus(status.UNAVAILABLE, 'no xDS HTTP router filter configured');
219-
return Promise.reject<Metadata>(new Error('no xDS HTTP router filter configured'));
220-
}
221-
}
222-
223-
class NoRouterFilterFactory implements FilterFactory<NoRouterFilter> {
224-
createFilter(callStream: CallStream): NoRouterFilter {
225-
return new NoRouterFilter(callStream);
226-
}
227-
}
228-
229-
const ROUTER_FILTER_URL = 'type.googleapis.com/envoy.extensions.filters.http.router.v3.Router';
230-
231210
class XdsResolver implements Resolver {
232211
private hasReportedSuccess = false;
233212

@@ -247,7 +226,6 @@ class XdsResolver implements Resolver {
247226
private latestDefaultTimeout: Duration | undefined = undefined;
248227

249228
private ldsHttpFilterConfigs: {name: string, config: HttpFilterConfig}[] = [];
250-
private hasRouterFilter = false;
251229

252230
constructor(
253231
private target: GrpcUri,
@@ -265,12 +243,7 @@ class XdsResolver implements Resolver {
265243
}
266244
if (EXPERIMENTAL_FAULT_INJECTION) {
267245
this.ldsHttpFilterConfigs = [];
268-
this.hasRouterFilter = false;
269246
for (const filter of httpConnectionManager.http_filters) {
270-
if (filter.typed_config?.type_url === ROUTER_FILTER_URL) {
271-
this.hasRouterFilter = true;
272-
break;
273-
}
274247
// typed_config must be set here, or validation would have failed
275248
const filterConfig = parseTopLevelFilterConfig(filter.typed_config!);
276249
if (filterConfig) {
@@ -421,9 +394,6 @@ class XdsResolver implements Resolver {
421394
}
422395
}
423396
}
424-
if (!this.hasRouterFilter) {
425-
extraFilterFactories.push(new NoRouterFilterFactory());
426-
}
427397
}
428398
routeAction = new SingleClusterRouteAction(cluster, timeout, extraFilterFactories);
429399
break;
@@ -464,9 +434,6 @@ class XdsResolver implements Resolver {
464434
}
465435
}
466436
}
467-
if (!this.hasRouterFilter) {
468-
extraFilterFactories.push(new NoRouterFilterFactory());
469-
}
470437
}
471438
weightedClusters.push({name: clusterWeight.name, weight: clusterWeight.weight?.value ?? 0, dynamicFilterFactories: extraFilterFactories});
472439
}

packages/grpc-js-xds/src/xds-stream-state/lds-state.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ function trace(text: string): void {
3131
experimental.trace(logVerbosity.DEBUG, TRACER_NAME, text);
3232
}
3333

34+
const ROUTER_FILTER_URL = 'type.googleapis.com/envoy.extensions.filters.http.router.v3.Router';
35+
3436
export class LdsState implements XdsStreamState<Listener__Output> {
3537
versionInfo = '';
3638
nonce = '';
@@ -103,10 +105,26 @@ export class LdsState implements XdsStreamState<Listener__Output> {
103105
}
104106
const httpConnectionManager = decodeSingleResource(HTTP_CONNECTION_MANGER_TYPE_URL_V3, message.api_listener!.api_listener.value);
105107
if (EXPERIMENTAL_FAULT_INJECTION) {
106-
for (const httpFilter of httpConnectionManager.http_filters) {
108+
const filterNames = new Set<string>();
109+
for (const [index, httpFilter] of httpConnectionManager.http_filters.entries()) {
110+
if (filterNames.has(httpFilter.name)) {
111+
return false;
112+
}
113+
filterNames.add(httpFilter.name);
107114
if (!validateTopLevelFilter(httpFilter)) {
108115
return false;
109116
}
117+
/* Validate that the last filter, and only the last filter, is the
118+
* router filter. */
119+
if (index < httpConnectionManager.http_filters.length - 1) {
120+
if (httpFilter.name === ROUTER_FILTER_URL) {
121+
return false;
122+
}
123+
} else {
124+
if (httpFilter.name !== ROUTER_FILTER_URL) {
125+
return false;
126+
}
127+
}
110128
}
111129
}
112130
switch (httpConnectionManager.route_specifier) {

packages/grpc-js-xds/src/xds-stream-state/rds-state.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,7 @@ export class RdsState implements XdsStreamState<RouteConfiguration__Output> {
141141
return false;
142142
}
143143
if (EXPERIMENTAL_FAULT_INJECTION) {
144-
const filterNames = new Set<string>();
145144
for (const [name, filterConfig] of Object.entries(route.typed_per_filter_config ?? {})) {
146-
if (filterNames.has(name)) {
147-
return false;
148-
}
149-
filterNames.add(name);
150145
if (!validateOverrideFilter(filterConfig)) {
151146
return false;
152147
}

0 commit comments

Comments
 (0)