mirror of https://github.com/grpc/grpc-java.git
xds: Add GcpAuthenticationFilter to FilterRegistry (#12075)
This commit is contained in:
parent
f8700a13ad
commit
9d439d4a44
|
@ -17,6 +17,7 @@
|
||||||
package io.grpc.xds;
|
package io.grpc.xds;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
|
import io.grpc.internal.GrpcUtil;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
@ -32,12 +33,18 @@ final class FilterRegistry {
|
||||||
|
|
||||||
private FilterRegistry() {}
|
private FilterRegistry() {}
|
||||||
|
|
||||||
|
static boolean isEnabledGcpAuthnFilter =
|
||||||
|
GrpcUtil.getFlag("GRPC_EXPERIMENTAL_XDS_GCP_AUTHENTICATION_FILTER", false);
|
||||||
|
|
||||||
static synchronized FilterRegistry getDefaultRegistry() {
|
static synchronized FilterRegistry getDefaultRegistry() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = newRegistry().register(
|
instance = newRegistry().register(
|
||||||
new FaultFilter.Provider(),
|
new FaultFilter.Provider(),
|
||||||
new RouterFilter.Provider(),
|
new RouterFilter.Provider(),
|
||||||
new RbacFilter.Provider());
|
new RbacFilter.Provider());
|
||||||
|
if (isEnabledGcpAuthnFilter) {
|
||||||
|
instance.register(new GcpAuthenticationFilter.Provider());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package io.grpc.xds;
|
package io.grpc.xds;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkNotNull;
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static io.grpc.xds.FilterRegistry.isEnabledGcpAuthnFilter;
|
||||||
import static io.grpc.xds.XdsNameResolver.CLUSTER_SELECTION_KEY;
|
import static io.grpc.xds.XdsNameResolver.CLUSTER_SELECTION_KEY;
|
||||||
import static io.grpc.xds.XdsNameResolver.XDS_CONFIG_CALL_OPTION_KEY;
|
import static io.grpc.xds.XdsNameResolver.XDS_CONFIG_CALL_OPTION_KEY;
|
||||||
|
|
||||||
|
@ -312,6 +313,10 @@ final class GcpAuthenticationFilter implements Filter {
|
||||||
public AudienceWrapper parse(Any any) throws ResourceInvalidException {
|
public AudienceWrapper parse(Any any) throws ResourceInvalidException {
|
||||||
Audience audience;
|
Audience audience;
|
||||||
try {
|
try {
|
||||||
|
if (!isEnabledGcpAuthnFilter) {
|
||||||
|
throw new InvalidProtocolBufferException("Environment variable for GCP Authentication "
|
||||||
|
+ "Filter is Not Set");
|
||||||
|
}
|
||||||
audience = any.unpack(Audience.class);
|
audience = any.unpack(Audience.class);
|
||||||
} catch (InvalidProtocolBufferException ex) {
|
} catch (InvalidProtocolBufferException ex) {
|
||||||
throw new ResourceInvalidException("Invalid Resource in address proto", ex);
|
throw new ResourceInvalidException("Invalid Resource in address proto", ex);
|
||||||
|
|
|
@ -73,6 +73,7 @@ import java.io.IOException;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.JUnit4;
|
import org.junit.runners.JUnit4;
|
||||||
|
@ -89,6 +90,11 @@ public class GcpAuthenticationFilterTest {
|
||||||
private static final RdsUpdate rdsUpdate = getRdsUpdate();
|
private static final RdsUpdate rdsUpdate = getRdsUpdate();
|
||||||
private static final CdsUpdate cdsUpdate = getCdsUpdate();
|
private static final CdsUpdate cdsUpdate = getCdsUpdate();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
System.setProperty("GRPC_EXPERIMENTAL_XDS_GCP_AUTHENTICATION_FILTER", "true");
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNewFilterInstancesPerFilterName() {
|
public void testNewFilterInstancesPerFilterName() {
|
||||||
assertThat(new GcpAuthenticationFilter("FILTER_INSTANCE_NAME1", 10))
|
assertThat(new GcpAuthenticationFilter("FILTER_INSTANCE_NAME1", 10))
|
||||||
|
|
|
@ -2417,6 +2417,7 @@ public class GrpcXdsClientImplDataTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void processCluster_parsesAudienceMetadata() throws Exception {
|
public void processCluster_parsesAudienceMetadata() throws Exception {
|
||||||
|
FilterRegistry.isEnabledGcpAuthnFilter = true;
|
||||||
MetadataRegistry.getInstance();
|
MetadataRegistry.getInstance();
|
||||||
|
|
||||||
Audience audience = Audience.newBuilder()
|
Audience audience = Audience.newBuilder()
|
||||||
|
@ -2460,10 +2461,14 @@ public class GrpcXdsClientImplDataTest {
|
||||||
"FILTER_METADATA", ImmutableMap.of(
|
"FILTER_METADATA", ImmutableMap.of(
|
||||||
"key1", "value1",
|
"key1", "value1",
|
||||||
"key2", 42.0));
|
"key2", 42.0));
|
||||||
assertThat(update.parsedMetadata().get("FILTER_METADATA"))
|
try {
|
||||||
.isEqualTo(expectedParsedMetadata.get("FILTER_METADATA"));
|
assertThat(update.parsedMetadata().get("FILTER_METADATA"))
|
||||||
assertThat(update.parsedMetadata().get("AUDIENCE_METADATA"))
|
.isEqualTo(expectedParsedMetadata.get("FILTER_METADATA"));
|
||||||
.isInstanceOf(AudienceWrapper.class);
|
assertThat(update.parsedMetadata().get("AUDIENCE_METADATA"))
|
||||||
|
.isInstanceOf(AudienceWrapper.class);
|
||||||
|
} finally {
|
||||||
|
FilterRegistry.isEnabledGcpAuthnFilter = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
Loading…
Reference in New Issue