Search in sources :

Example 1 with FilterConfig

use of io.grpc.xds.Filter.FilterConfig in project grpc-java by grpc.

the class ClientXdsClient method parseOverrideFilterConfigs.

@VisibleForTesting
static StructOrError<Map<String, FilterConfig>> parseOverrideFilterConfigs(Map<String, Any> rawFilterConfigMap, FilterRegistry filterRegistry) {
    Map<String, FilterConfig> overrideConfigs = new HashMap<>();
    for (String name : rawFilterConfigMap.keySet()) {
        Any anyConfig = rawFilterConfigMap.get(name);
        String typeUrl = anyConfig.getTypeUrl();
        boolean isOptional = false;
        if (typeUrl.equals(TYPE_URL_FILTER_CONFIG)) {
            io.envoyproxy.envoy.config.route.v3.FilterConfig filterConfig;
            try {
                filterConfig = anyConfig.unpack(io.envoyproxy.envoy.config.route.v3.FilterConfig.class);
            } catch (InvalidProtocolBufferException e) {
                return StructOrError.fromError("FilterConfig [" + name + "] contains invalid proto: " + e);
            }
            isOptional = filterConfig.getIsOptional();
            anyConfig = filterConfig.getConfig();
            typeUrl = anyConfig.getTypeUrl();
        }
        Message rawConfig = anyConfig;
        try {
            if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
                TypedStruct typedStruct = anyConfig.unpack(TypedStruct.class);
                typeUrl = typedStruct.getTypeUrl();
                rawConfig = typedStruct.getValue();
            } else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
                com.github.xds.type.v3.TypedStruct newTypedStruct = anyConfig.unpack(com.github.xds.type.v3.TypedStruct.class);
                typeUrl = newTypedStruct.getTypeUrl();
                rawConfig = newTypedStruct.getValue();
            }
        } catch (InvalidProtocolBufferException e) {
            return StructOrError.fromError("FilterConfig [" + name + "] contains invalid proto: " + e);
        }
        Filter filter = filterRegistry.get(typeUrl);
        if (filter == null) {
            if (isOptional) {
                continue;
            }
            return StructOrError.fromError("HttpFilter [" + name + "](" + typeUrl + ") is required but unsupported");
        }
        ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfigOverride(rawConfig);
        if (filterConfig.errorDetail != null) {
            return StructOrError.fromError("Invalid filter config for HttpFilter [" + name + "]: " + filterConfig.errorDetail);
        }
        overrideConfigs.put(name, filterConfig.config);
    }
    return StructOrError.fromStruct(overrideConfigs);
}
Also used : Message(com.google.protobuf.Message) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Any(com.google.protobuf.Any) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) FilterConfig(io.grpc.xds.Filter.FilterConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with FilterConfig

use of io.grpc.xds.Filter.FilterConfig in project grpc-java by grpc.

the class ClientXdsClient method parseHttpConnectionManager.

@VisibleForTesting
static io.grpc.xds.HttpConnectionManager parseHttpConnectionManager(HttpConnectionManager proto, Set<String> rdsResources, FilterRegistry filterRegistry, boolean parseHttpFilter, boolean isForClient) throws ResourceInvalidException {
    if (enableRbac && proto.getXffNumTrustedHops() != 0) {
        throw new ResourceInvalidException("HttpConnectionManager with xff_num_trusted_hops unsupported");
    }
    if (enableRbac && !proto.getOriginalIpDetectionExtensionsList().isEmpty()) {
        throw new ResourceInvalidException("HttpConnectionManager with " + "original_ip_detection_extensions unsupported");
    }
    // Obtain max_stream_duration from Http Protocol Options.
    long maxStreamDuration = 0;
    if (proto.hasCommonHttpProtocolOptions()) {
        HttpProtocolOptions options = proto.getCommonHttpProtocolOptions();
        if (options.hasMaxStreamDuration()) {
            maxStreamDuration = Durations.toNanos(options.getMaxStreamDuration());
        }
    }
    // Parse http filters.
    List<NamedFilterConfig> filterConfigs = null;
    if (parseHttpFilter) {
        if (proto.getHttpFiltersList().isEmpty()) {
            throw new ResourceInvalidException("Missing HttpFilter in HttpConnectionManager.");
        }
        filterConfigs = new ArrayList<>();
        Set<String> names = new HashSet<>();
        for (int i = 0; i < proto.getHttpFiltersCount(); i++) {
            io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter httpFilter = proto.getHttpFiltersList().get(i);
            String filterName = httpFilter.getName();
            if (!names.add(filterName)) {
                throw new ResourceInvalidException("HttpConnectionManager contains duplicate HttpFilter: " + filterName);
            }
            StructOrError<FilterConfig> filterConfig = parseHttpFilter(httpFilter, filterRegistry, isForClient);
            if ((i == proto.getHttpFiltersCount() - 1) && (filterConfig == null || !isTerminalFilter(filterConfig.struct))) {
                throw new ResourceInvalidException("The last HttpFilter must be a terminal filter: " + filterName);
            }
            if (filterConfig == null) {
                continue;
            }
            if (filterConfig.getErrorDetail() != null) {
                throw new ResourceInvalidException("HttpConnectionManager contains invalid HttpFilter: " + filterConfig.getErrorDetail());
            }
            if ((i < proto.getHttpFiltersCount() - 1) && isTerminalFilter(filterConfig.getStruct())) {
                throw new ResourceInvalidException("A terminal HttpFilter must be the last filter: " + filterName);
            }
            filterConfigs.add(new NamedFilterConfig(filterName, filterConfig.struct));
        }
    }
    // Parse inlined RouteConfiguration or RDS.
    if (proto.hasRouteConfig()) {
        List<VirtualHost> virtualHosts = extractVirtualHosts(proto.getRouteConfig(), filterRegistry, parseHttpFilter);
        return io.grpc.xds.HttpConnectionManager.forVirtualHosts(maxStreamDuration, virtualHosts, filterConfigs);
    }
    if (proto.hasRds()) {
        Rds rds = proto.getRds();
        if (!rds.hasConfigSource()) {
            throw new ResourceInvalidException("HttpConnectionManager contains invalid RDS: missing config_source");
        }
        if (!rds.getConfigSource().hasAds() && !rds.getConfigSource().hasSelf()) {
            throw new ResourceInvalidException("HttpConnectionManager contains invalid RDS: must specify ADS or self ConfigSource");
        }
        // Collect the RDS resource referenced by this HttpConnectionManager.
        rdsResources.add(rds.getRouteConfigName());
        return io.grpc.xds.HttpConnectionManager.forRdsName(maxStreamDuration, rds.getRouteConfigName(), filterConfigs);
    }
    throw new ResourceInvalidException("HttpConnectionManager neither has inlined route_config nor RDS");
}
Also used : Rds(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.Rds) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) HttpProtocolOptions(io.envoyproxy.envoy.config.core.v3.HttpProtocolOptions) NamedFilterConfig(io.grpc.xds.Filter.NamedFilterConfig) FilterConfig(io.grpc.xds.Filter.FilterConfig) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with FilterConfig

use of io.grpc.xds.Filter.FilterConfig in project grpc-java by grpc.

the class ClientXdsClientDataTest method parseHttpFilter_typedStructMigration.

@Test
public void parseHttpFilter_typedStructMigration() {
    filterRegistry.register(new TestFilter());
    Struct rawStruct = Struct.newBuilder().putFields("name", Value.newBuilder().setStringValue("default").build()).build();
    HttpFilter httpFilter = HttpFilter.newBuilder().setIsOptional(true).setTypedConfig(Any.pack(com.github.udpa.udpa.type.v1.TypedStruct.newBuilder().setTypeUrl("test-url").setValue(rawStruct).build())).build();
    FilterConfig config = ClientXdsClient.parseHttpFilter(httpFilter, filterRegistry, true).getStruct();
    assertThat(((SimpleFilterConfig) config).getConfig()).isEqualTo(rawStruct);
    HttpFilter httpFilterNewTypeStruct = HttpFilter.newBuilder().setIsOptional(true).setTypedConfig(Any.pack(TypedStruct.newBuilder().setTypeUrl("test-url").setValue(rawStruct).build())).build();
    config = ClientXdsClient.parseHttpFilter(httpFilterNewTypeStruct, filterRegistry, true).getStruct();
    assertThat(((SimpleFilterConfig) config).getConfig()).isEqualTo(rawStruct);
}
Also used : HttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter) FilterConfig(io.grpc.xds.Filter.FilterConfig) Struct(com.google.protobuf.Struct) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) Test(org.junit.Test)

Example 4 with FilterConfig

use of io.grpc.xds.Filter.FilterConfig in project grpc-java by grpc.

the class ClientXdsClientDataTest method parseHttpFilter_faultConfigUnsupportedForServer.

@Test
public void parseHttpFilter_faultConfigUnsupportedForServer() {
    filterRegistry.register(FaultFilter.INSTANCE);
    HttpFilter httpFilter = HttpFilter.newBuilder().setIsOptional(false).setName("envoy.fault").setTypedConfig(Any.pack(HTTPFault.newBuilder().setDelay(FaultDelay.newBuilder().setFixedDelay(Durations.fromNanos(1234L))).setAbort(FaultAbort.newBuilder().setHttpStatus(300).setPercentage(FractionalPercent.newBuilder().setNumerator(10).setDenominator(DenominatorType.HUNDRED))).build())).build();
    StructOrError<FilterConfig> config = ClientXdsClient.parseHttpFilter(httpFilter, filterRegistry, false);
    assertThat(config.getErrorDetail()).isEqualTo("HttpFilter [envoy.fault](" + FaultFilter.TYPE_URL + ") is required but " + "unsupported for server");
}
Also used : HttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter) FilterConfig(io.grpc.xds.Filter.FilterConfig) Test(org.junit.Test)

Example 5 with FilterConfig

use of io.grpc.xds.Filter.FilterConfig in project grpc-java by grpc.

the class ClientXdsClientDataTest method parseHttpFilter_rbacConfigUnsupportedForClient.

@Test
public void parseHttpFilter_rbacConfigUnsupportedForClient() {
    filterRegistry.register(RbacFilter.INSTANCE);
    HttpFilter httpFilter = HttpFilter.newBuilder().setIsOptional(false).setName("envoy.auth").setTypedConfig(Any.pack(io.envoyproxy.envoy.extensions.filters.http.rbac.v3.RBAC.newBuilder().setRules(RBAC.newBuilder().setAction(Action.ALLOW).putPolicies("allow-all", Policy.newBuilder().addPrincipals(Principal.newBuilder().setAny(true)).addPermissions(Permission.newBuilder().setAny(true)).build()).build()).build())).build();
    StructOrError<FilterConfig> config = ClientXdsClient.parseHttpFilter(httpFilter, filterRegistry, true);
    assertThat(config.getErrorDetail()).isEqualTo("HttpFilter [envoy.auth](" + RbacFilter.TYPE_URL + ") is required but " + "unsupported for client");
}
Also used : HttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter) FilterConfig(io.grpc.xds.Filter.FilterConfig) Test(org.junit.Test)

Aggregations

FilterConfig (io.grpc.xds.Filter.FilterConfig)18 Test (org.junit.Test)14 NamedFilterConfig (io.grpc.xds.Filter.NamedFilterConfig)8 HttpFilter (io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter)7 Any (com.google.protobuf.Any)4 Route (io.grpc.xds.VirtualHost.Route)4 TypedStruct (com.github.udpa.udpa.type.v1.TypedStruct)3 HashPolicy (io.grpc.xds.VirtualHost.Route.RouteAction.HashPolicy)3 RouteMatch (io.grpc.xds.VirtualHost.Route.RouteMatch)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Struct (com.google.protobuf.Struct)2 Metadata (io.grpc.Metadata)2 ResolutionResult (io.grpc.NameResolver.ResolutionResult)2 Server (io.grpc.Server)2 ServerCall (io.grpc.ServerCall)2 ServerCallHandler (io.grpc.ServerCallHandler)2 ServerInterceptor (io.grpc.ServerInterceptor)2 StatusException (io.grpc.StatusException)2 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)2 ServerInterceptorBuilder (io.grpc.xds.Filter.ServerInterceptorBuilder)2