Skip to content

Commit

Permalink
Moving AudienceMetadataParser to gcp auth filter and better error han…
Browse files Browse the repository at this point in the history
…dling
  • Loading branch information
shivaspeaks committed Dec 13, 2024
1 parent a66f3c0 commit 02e346a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 37 deletions.
39 changes: 7 additions & 32 deletions xds/src/main/java/io/grpc/xds/ClusterMetadataRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import io.envoyproxy.envoy.extensions.filters.http.gcp_authn.v3.Audience;
import io.grpc.xds.GcpAuthenticationFilter.AudienceMetadataParser;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -34,11 +34,7 @@ final class ClusterMetadataRegistry {
private final Map<String, ClusterMetadataValueParser> supportedParsers = new HashMap<>();

private ClusterMetadataRegistry() {
registerParsers(
new Object[][]{
{"extensions.filters.http.gcp_authn.v3.Audience", new AudienceMetadataParser()},
// Add more parsers here as needed
});
registerParser(new AudienceMetadataParser());
}

static ClusterMetadataRegistry getInstance() {
Expand All @@ -49,16 +45,14 @@ ClusterMetadataValueParser findParser(String typeUrl) {
return supportedParsers.get(typeUrl);

Check warning on line 45 in xds/src/main/java/io/grpc/xds/ClusterMetadataRegistry.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/ClusterMetadataRegistry.java#L45

Added line #L45 was not covered by tests
}

private void registerParsers(Object[][] parserEntries) {
for (Object[] entry : parserEntries) {
String typeUrl = (String) entry[0];
ClusterMetadataValueParser parser = (ClusterMetadataValueParser) entry[1];
supportedParsers.put(typeUrl, parser);
}
private void registerParser(ClusterMetadataValueParser parser) {
supportedParsers.put(parser.getTypeUrl(), parser);
}

@FunctionalInterface
interface ClusterMetadataValueParser {

String getTypeUrl();

/**
* Parses the given {@link Any} object into a specific metadata value.
*
Expand All @@ -68,23 +62,4 @@ interface ClusterMetadataValueParser {
*/
Object parse(Any any) throws InvalidProtocolBufferException;
}

/**
* Parser for Audience metadata type.
*/
class AudienceMetadataParser implements ClusterMetadataValueParser {
@Override
public String parse(Any any) throws InvalidProtocolBufferException {
if (any.is(Audience.class)) {
Audience audience = any.unpack(Audience.class);
String url = audience.getUrl();
if (url.isEmpty()) {
throw new InvalidProtocolBufferException("Audience URL is empty.");
}
return url;
} else {
throw new InvalidProtocolBufferException("Unexpected message type: " + any.getTypeUrl());
}
}
}
}
30 changes: 30 additions & 0 deletions xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Message;
import io.envoyproxy.envoy.extensions.filters.http.gcp_authn.v3.Audience;
import io.envoyproxy.envoy.extensions.filters.http.gcp_authn.v3.GcpAuthnFilterConfig;
import io.envoyproxy.envoy.extensions.filters.http.gcp_authn.v3.TokenCacheConfig;
import io.grpc.CallCredentials;
Expand All @@ -35,6 +36,7 @@
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import io.grpc.auth.MoreCallCredentials;
import io.grpc.xds.ClusterMetadataRegistry.ClusterMetadataValueParser;
import io.grpc.xds.Filter.ClientInterceptorBuilder;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -219,4 +221,32 @@ V getOrInsert(K key, Function<K, V> create) {
return cache.computeIfAbsent(key, create);
}
}

/**
* Parser for Audience metadata type.
*/
static class AudienceMetadataParser implements ClusterMetadataValueParser {

@Override
public String getTypeUrl() {
return "extensions.filters.http.gcp_authn.v3.Audience";
}

@Override
public String parse(Any any) throws InvalidProtocolBufferException {
if (any.is(Audience.class)) {
Audience audience = any.unpack(Audience.class);
String url = audience.getUrl();

Check warning on line 239 in xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java#L238-L239

Added lines #L238 - L239 were not covered by tests
if (url.isEmpty()) {
throw new InvalidProtocolBufferException(

Check warning on line 241 in xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java#L241

Added line #L241 was not covered by tests
"Audience URL is empty. Metadata value must contain a valid URL.");
}
return url;

Check warning on line 244 in xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java#L244

Added line #L244 was not covered by tests
} else {
throw new InvalidProtocolBufferException(
String.format("Unexpected message type: %s. Expected: %s",
any.getTypeUrl(), Audience.getDescriptor().getFullName()));

Check warning on line 248 in xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java#L246-L248

Added lines #L246 - L248 were not covered by tests
}
}
}
}
8 changes: 3 additions & 5 deletions xds/src/main/java/io/grpc/xds/XdsClusterResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ static CdsUpdate processCluster(Cluster cluster,
parseClusterMetadata(cluster.getMetadata());
updateBuilder.parsedMetadata(parsedFilterMetadata);
} catch (InvalidProtocolBufferException e) {
throw new ResourceInvalidException("xDS filter metadata invalid.");
throw new ResourceInvalidException(
"Failed to parse xDS filter metadata for cluster '" + cluster.getName() + "': "
+ e.getMessage(), e);

Check warning on line 184 in xds/src/main/java/io/grpc/xds/XdsClusterResource.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsClusterResource.java#L181-L184

Added lines #L181 - L184 were not covered by tests
}

return updateBuilder.build();
Expand All @@ -197,10 +199,6 @@ private static ImmutableMap<String, Object> parseClusterMetadata(Metadata metada
ClusterMetadataValueParser parser = registry.findParser(value.getTypeUrl());

Check warning on line 199 in xds/src/main/java/io/grpc/xds/XdsClusterResource.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsClusterResource.java#L197-L199

Added lines #L197 - L199 were not covered by tests
if (parser != null) {
Object parsedValue = parser.parse(value);
if (parsedValue == null) {
// parsing failed
throw new InvalidProtocolBufferException("Could not parse!");
}
parsedMetadata.put(key, parsedValue);

Check warning on line 202 in xds/src/main/java/io/grpc/xds/XdsClusterResource.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsClusterResource.java#L201-L202

Added lines #L201 - L202 were not covered by tests
}
}

Check warning on line 204 in xds/src/main/java/io/grpc/xds/XdsClusterResource.java

View check run for this annotation

Codecov / codecov/patch

xds/src/main/java/io/grpc/xds/XdsClusterResource.java#L204

Added line #L204 was not covered by tests
Expand Down

0 comments on commit 02e346a

Please sign in to comment.