Search in sources :

Example 76 with InvalidProtocolBufferException

use of com.google.protobuf.InvalidProtocolBufferException in project grpc-java by grpc.

the class ClientXdsClient method parseHttpFilter.

@VisibleForTesting
// Returns null if the filter is optional but not supported.
@Nullable
static StructOrError<FilterConfig> parseHttpFilter(io.envoyproxy.envoy.extensions.filters.network.http_connection_manager.v3.HttpFilter httpFilter, FilterRegistry filterRegistry, boolean isForClient) {
    String filterName = httpFilter.getName();
    boolean isOptional = httpFilter.getIsOptional();
    if (!httpFilter.hasTypedConfig()) {
        if (isOptional) {
            return null;
        } else {
            return StructOrError.fromError("HttpFilter [" + filterName + "] is not optional and has no typed config");
        }
    }
    Message rawConfig = httpFilter.getTypedConfig();
    String typeUrl = httpFilter.getTypedConfig().getTypeUrl();
    try {
        if (typeUrl.equals(TYPE_URL_TYPED_STRUCT_UDPA)) {
            TypedStruct typedStruct = httpFilter.getTypedConfig().unpack(TypedStruct.class);
            typeUrl = typedStruct.getTypeUrl();
            rawConfig = typedStruct.getValue();
        } else if (typeUrl.equals(TYPE_URL_TYPED_STRUCT)) {
            com.github.xds.type.v3.TypedStruct newTypedStruct = httpFilter.getTypedConfig().unpack(com.github.xds.type.v3.TypedStruct.class);
            typeUrl = newTypedStruct.getTypeUrl();
            rawConfig = newTypedStruct.getValue();
        }
    } catch (InvalidProtocolBufferException e) {
        return StructOrError.fromError("HttpFilter [" + filterName + "] contains invalid proto: " + e);
    }
    Filter filter = filterRegistry.get(typeUrl);
    if ((isForClient && !(filter instanceof ClientInterceptorBuilder)) || (!isForClient && !(filter instanceof ServerInterceptorBuilder))) {
        if (isOptional) {
            return null;
        } else {
            return StructOrError.fromError("HttpFilter [" + filterName + "](" + typeUrl + ") is required but unsupported for " + (isForClient ? "client" : "server"));
        }
    }
    ConfigOrError<? extends FilterConfig> filterConfig = filter.parseFilterConfig(rawConfig);
    if (filterConfig.errorDetail != null) {
        return StructOrError.fromError("Invalid filter config for HttpFilter [" + filterName + "]: " + filterConfig.errorDetail);
    }
    return StructOrError.fromStruct(filterConfig.config);
}
Also used : Message(com.google.protobuf.Message) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) TypedStruct(com.github.udpa.udpa.type.v1.TypedStruct) ServerInterceptorBuilder(io.grpc.xds.Filter.ServerInterceptorBuilder) ClientInterceptorBuilder(io.grpc.xds.Filter.ClientInterceptorBuilder) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Nullable(javax.annotation.Nullable)

Example 77 with InvalidProtocolBufferException

use of com.google.protobuf.InvalidProtocolBufferException in project grpc-java by grpc.

the class ClientXdsClient method handleCdsResponse.

@Override
public void handleCdsResponse(ServerInfo serverInfo, String versionInfo, List<Any> resources, String nonce) {
    syncContext.throwIfNotInThisSynchronizationContext();
    Map<String, ParsedResource> parsedResources = new HashMap<>(resources.size());
    Set<String> unpackedResources = new HashSet<>(resources.size());
    Set<String> invalidResources = new HashSet<>();
    List<String> errors = new ArrayList<>();
    Set<String> retainedEdsResources = new HashSet<>();
    for (int i = 0; i < resources.size(); i++) {
        Any resource = resources.get(i);
        // Unpack the Cluster.
        Cluster cluster;
        try {
            cluster = unpackCompatibleType(resource, Cluster.class, ResourceType.CDS.typeUrl(), ResourceType.CDS.typeUrlV2());
        } catch (InvalidProtocolBufferException e) {
            errors.add("CDS response Resource index " + i + " - can't decode Cluster: " + e);
            continue;
        }
        if (!isResourceNameValid(cluster.getName(), resource.getTypeUrl())) {
            errors.add("Unsupported resource name: " + cluster.getName() + " for type: " + ResourceType.CDS);
            continue;
        }
        String clusterName = canonifyResourceName(cluster.getName());
        // unrequested resources.
        if (!cdsResourceSubscribers.containsKey(clusterName)) {
            continue;
        }
        unpackedResources.add(clusterName);
        // Process Cluster into CdsUpdate.
        CdsUpdate cdsUpdate;
        try {
            Set<String> certProviderInstances = null;
            if (getBootstrapInfo() != null && getBootstrapInfo().certProviders() != null) {
                certProviderInstances = getBootstrapInfo().certProviders().keySet();
            }
            cdsUpdate = processCluster(cluster, retainedEdsResources, certProviderInstances, serverInfo);
        } catch (ResourceInvalidException e) {
            errors.add("CDS response Cluster '" + clusterName + "' validation error: " + e.getMessage());
            invalidResources.add(clusterName);
            continue;
        }
        parsedResources.put(clusterName, new ParsedResource(cdsUpdate, resource));
    }
    logger.log(XdsLogLevel.INFO, "Received CDS Response version {0} nonce {1}. Parsed resources: {2}", versionInfo, nonce, unpackedResources);
    handleResourceUpdate(serverInfo, ResourceType.CDS, parsedResources, invalidResources, retainedEdsResources, versionInfo, nonce, errors);
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Cluster(io.envoyproxy.envoy.config.cluster.v3.Cluster) Any(com.google.protobuf.Any) LbEndpoint(io.grpc.xds.Endpoints.LbEndpoint) HashSet(java.util.HashSet)

Example 78 with InvalidProtocolBufferException

use of com.google.protobuf.InvalidProtocolBufferException 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 79 with InvalidProtocolBufferException

use of com.google.protobuf.InvalidProtocolBufferException in project grpc-java by grpc.

the class ProtoLiteUtilsTest method parseInvalid.

@Test
public void parseInvalid() throws Exception {
    InputStream is = new ByteArrayInputStream(new byte[] { -127 });
    try {
        marshaller.parse(is);
        fail("Expected exception");
    } catch (StatusRuntimeException ex) {
        assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode());
        assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StatusRuntimeException(io.grpc.StatusRuntimeException) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) Test(org.junit.Test)

Example 80 with InvalidProtocolBufferException

use of com.google.protobuf.InvalidProtocolBufferException in project grpc-java by grpc.

the class RouteLookupServiceClusterSpecifierPlugin method parsePlugin.

@Override
@SuppressWarnings("unchecked")
public ConfigOrError<RlsPluginConfig> parsePlugin(Message rawProtoMessage) {
    if (!(rawProtoMessage instanceof Any)) {
        return ConfigOrError.fromError("Invalid config type: " + rawProtoMessage.getClass());
    }
    try {
        Any anyMessage = (Any) rawProtoMessage;
        Class<? extends Message> protoClass;
        try {
            protoClass = (Class<? extends Message>) Class.forName("io.grpc.lookup.v1.RouteLookupClusterSpecifier");
        } catch (ClassNotFoundException e) {
            return ConfigOrError.fromError("Dependency for 'io.grpc:grpc-rls' is missing: " + e);
        }
        Message configProto;
        try {
            configProto = anyMessage.unpack(protoClass);
        } catch (InvalidProtocolBufferException e) {
            return ConfigOrError.fromError("Invalid proto: " + e);
        }
        String jsonString = MessagePrinter.print(configProto);
        try {
            Map<String, ?> jsonMap = (Map<String, ?>) JsonParser.parse(jsonString);
            Map<String, ?> config = JsonUtil.getObject(jsonMap, "routeLookupConfig");
            return ConfigOrError.fromConfig(RlsPluginConfig.create(config));
        } catch (IOException e) {
            return ConfigOrError.fromError("Unable to parse RouteLookupClusterSpecifier: " + jsonString);
        }
    } catch (RuntimeException e) {
        return ConfigOrError.fromError("Error parsing RouteLookupConfig: " + e);
    }
}
Also used : Message(com.google.protobuf.Message) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IOException(java.io.IOException) Any(com.google.protobuf.Any) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)260 ServerRequest (com.pokegoapi.main.ServerRequest)46 ByteString (com.google.protobuf.ByteString)42 IOException (java.io.IOException)41 RequestFailedException (com.pokegoapi.exceptions.request.RequestFailedException)39 InvalidProtocolBufferException (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.InvalidProtocolBufferException)22 HashMap (java.util.HashMap)21 ArrayList (java.util.ArrayList)19 List (java.util.List)18 Map (java.util.Map)17 Any (com.google.protobuf.Any)16 RunnerApi (org.apache.beam.model.pipeline.v1.RunnerApi)15 HashSet (java.util.HashSet)11 Key (org.apache.accumulo.core.data.Key)10 Value (org.apache.accumulo.core.data.Value)10 Status (org.apache.accumulo.server.replication.proto.Replication.Status)10 Text (org.apache.hadoop.io.Text)10 JsonToken (com.fasterxml.jackson.core.JsonToken)9 ByteString (org.apache.beam.vendor.grpc.v1p43p2.com.google.protobuf.ByteString)9 ContractExeException (org.tron.core.exception.ContractExeException)9