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);
}
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);
}
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);
}
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());
}
}
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);
}
}
Aggregations