Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 43 additions & 14 deletions xds/src/main/java/io/grpc/xds/GcpAuthenticationFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package io.grpc.xds;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.xds.XdsNameResolver.CLUSTER_SELECTION_KEY;
import static io.grpc.xds.XdsNameResolver.XDS_CONFIG_CALL_OPTION_KEY;

import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.auth.oauth2.IdTokenCredentials;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.UnsignedLongs;
import com.google.protobuf.Any;
import com.google.protobuf.InvalidProtocolBufferException;
Expand All @@ -41,7 +43,6 @@
import io.grpc.auth.MoreCallCredentials;
import io.grpc.xds.GcpAuthenticationFilter.AudienceMetadataParser.AudienceWrapper;
import io.grpc.xds.MetadataRegistry.MetadataValueParser;
import io.grpc.xds.XdsClusterResource.CdsUpdate;
import io.grpc.xds.XdsConfig.XdsClusterConfig;
import io.grpc.xds.client.XdsResourceType.ResourceInvalidException;
import java.util.LinkedHashMap;
Expand All @@ -59,10 +60,10 @@
static final String TYPE_URL =
"type.googleapis.com/envoy.extensions.filters.http.gcp_authn.v3.GcpAuthnFilterConfig";

String filterInstanceName;
final String filterInstanceName;

GcpAuthenticationFilter(String name) {
filterInstanceName = name;
filterInstanceName = checkNotNull(name, "name");
}


Expand All @@ -79,7 +80,7 @@

@Override
public GcpAuthenticationFilter newInstance(String name) {
return new GcpAuthenticationFilter(name);

Check warning on line 83 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#L83

Added line #L83 was not covered by tests
}

@Override
Expand Down Expand Up @@ -134,31 +135,59 @@
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {

String clusterName = callOptions.getOption(CLUSTER_SELECTION_KEY);
if (clusterName == null) {
return new FailingClientCall<>(
Status.UNAVAILABLE.withDescription(
String.format(

Check warning on line 141 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#L139-L141

Added lines #L139 - L141 were not covered by tests
"GCP Authn for %s does not contain cluster resource", filterInstanceName)));
}

if (!clusterName.startsWith("cluster:")) {
return next.newCall(method, callOptions);
}
XdsConfig xdsConfig = callOptions.getOption(XDS_CONFIG_CALL_OPTION_KEY);
if (xdsConfig == null) {
return new FailingClientCall<>(
Status.UNAVAILABLE.withDescription(
String.format(

Check warning on line 152 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#L150-L152

Added lines #L150 - L152 were not covered by tests
"GCP Authn for %s with %s does not contain xds configuration",
filterInstanceName, clusterName)));
}

StatusOr<XdsClusterConfig> xdsCluster =
xdsConfig.getClusters().get(clusterName);
xdsConfig.getClusters().get(clusterName.substring(8)); // get rid of prefix "cluster:"
if (xdsCluster == null) {
return new FailingClientCall<>(
Status.UNAVAILABLE.withDescription(
String.format(

Check warning on line 162 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#L160-L162

Added lines #L160 - L162 were not covered by tests
"GCP Authn for %s with %s does not contain xds cluster",
filterInstanceName, clusterName)));
}

if (!xdsCluster.hasValue()) {
return next.newCall(method, callOptions);

Check warning on line 168 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#L168

Added line #L168 was not covered by tests
}
CdsUpdate cdsUpdate = xdsCluster.getValue().getClusterResource();
if (cdsUpdate == null) {
return new FailingClientCall<>(
Status.UNAVAILABLE.withDescription("CDS resource unavailable"));
}
ImmutableMap<String, Object> parsedMetadata = xdsCluster.getValue().getClusterResource()
.parsedMetadata();

if (!cdsUpdate.parsedMetadata().containsKey("FILTER_INSTANCE_NAME")) {
if (parsedMetadata == null || !parsedMetadata.containsKey(filterInstanceName)) {
return next.newCall(method, callOptions);

Check warning on line 174 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#L174

Added line #L174 was not covered by tests
}

AudienceWrapper audience;
try {
audience = (AudienceWrapper) cdsUpdate.parsedMetadata().get(filterInstanceName);
} catch (ClassCastException e) {
if (parsedMetadata.get(filterInstanceName) instanceof AudienceWrapper) {
audience = (AudienceWrapper) parsedMetadata.get(filterInstanceName);
if (audience.audience == null) {
return next.newCall(method, callOptions);

Check warning on line 181 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#L181

Added line #L181 was not covered by tests
}
}
else {
return new FailingClientCall<>(
Status.UNAVAILABLE.withDescription("Invalid CDS Resource"));
Status.UNAVAILABLE.withDescription(
String.format("GCP Authn found wrong type in %s metadata: %s=%s",

Check warning on line 187 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#L185-L187

Added lines #L185 - L187 were not covered by tests
clusterName, filterInstanceName,
parsedMetadata.get(filterInstanceName) == null
? null : parsedMetadata.get(filterInstanceName))));

Check warning on line 190 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#L190

Added line #L190 was not covered by tests
}

try {
Expand Down
6 changes: 3 additions & 3 deletions xds/src/test/java/io/grpc/xds/XdsTestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,19 +322,19 @@ static XdsConfig getDefaultXdsConfigWithCdsUpdate(String serverHostName)
parsedMetadata.put("FILTER_INSTANCE_NAME", new AudienceWrapper("TEST_AUDIENCE"));

CdsUpdate.Builder cdsUpdate = CdsUpdate.forEds(
CLUSTER_NAME, EDS_NAME, serverInfo, null, null, null, false)
CLUSTER_NAME, EDS_NAME, null, null, null, null, false)
.lbPolicyConfig(getWrrLbConfigAsMap());
cdsUpdate.parsedMetadata(parsedMetadata.build());
XdsConfig.XdsClusterConfig clusterConfig = new XdsConfig.XdsClusterConfig(
"cluster:" + CLUSTER_NAME,
CLUSTER_NAME,
cdsUpdate.build(),
new EndpointConfig(StatusOr.fromValue(edsUpdate)));

builder
.setListener(ldsUpdate)
.setRoute(rdsUpdate)
.setVirtualHost(virtualHost)
.addCluster("cluster:" + CLUSTER_NAME, StatusOr.fromValue(clusterConfig));
.addCluster(CLUSTER_NAME, StatusOr.fromValue(clusterConfig));

return builder.build();
}
Expand Down