Skip to content

Commit 4c50e71

Browse files
committed
Changes.
1 parent 7941abc commit 4c50e71

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

examples/example-tls/src/main/java/io/grpc/examples/helloworldtls/HelloWorldClientTls.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package io.grpc.examples.helloworldtls;
1818

19+
import static io.grpc.examples.helloworld.GreeterGrpc.getSayHelloMethod;
20+
1921
import io.grpc.Channel;
2022
import io.grpc.Grpc;
2123
import io.grpc.ManagedChannel;
@@ -48,11 +50,13 @@ public HelloWorldClientTls(Channel channel) {
4850
* Say hello to server.
4951
*/
5052
public void greet(String name) {
53+
System.setProperty("GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK", "true");
5154
logger.info("Will try to greet " + name + " ...");
5255
HelloRequest request = HelloRequest.newBuilder().setName(name).build();
5356
HelloReply response;
5457
try {
55-
response = blockingStub.sayHello(request);
58+
response = io.grpc.stub.ClientCalls.blockingUnaryCall(
59+
blockingStub.getChannel(), getSayHelloMethod(), blockingStub.getCallOptions().withAuthority("foo.goog.test.in"), request);
5660
} catch (StatusRuntimeException e) {
5761
logger.log(Level.WARNING, "RPC failed: {0}", e.getStatus());
5862
return;

netty/src/main/java/io/grpc/netty/GrpcHttp2OutboundHeaders.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ static GrpcHttp2OutboundHeaders clientRequestHeaders(byte[][] serializedMetadata
4646
return new GrpcHttp2OutboundHeaders(preHeaders, serializedMetadata);
4747
}
4848

49+
String getAuthority() {
50+
for (int i = 0; i < preHeaders.length / 2; i++) {
51+
if (preHeaders[i] == Http2Headers.PseudoHeaderName.AUTHORITY.value()) {
52+
return preHeaders[i + 1].toString();
53+
}
54+
}
55+
return null;
56+
}
57+
4958
static GrpcHttp2OutboundHeaders serverResponseHeaders(byte[][] serializedMetadata) {
5059
AsciiString[] preHeaders = new AsciiString[] {
5160
Http2Headers.PseudoHeaderName.STATUS.value(), Utils.STATUS_OK,

netty/src/main/java/io/grpc/netty/NettyClientHandler.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@
8383
import io.perfmark.Tag;
8484
import io.perfmark.TaskCloseable;
8585
import java.nio.channels.ClosedChannelException;
86+
import java.util.LinkedHashMap;
87+
import java.util.Map;
8688
import java.util.concurrent.Executor;
8789
import java.util.logging.Level;
8890
import java.util.logging.Logger;
@@ -94,6 +96,8 @@
9496
*/
9597
class NettyClientHandler extends AbstractNettyHandler {
9698
private static final Logger logger = Logger.getLogger(NettyClientHandler.class.getName());
99+
static boolean enablePerRpcAuthorityCheck =
100+
GrpcUtil.getFlag("GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK", false);
97101

98102
/**
99103
* A message that simply passes through the channel without any real processing. It is useful to
@@ -128,6 +132,13 @@ protected void handleNotInUse() {
128132
lifecycleManager.notifyInUse(false);
129133
}
130134
};
135+
private final Map<String, Status> peerVerificationResults =
136+
new LinkedHashMap<String, Status>() {
137+
@Override
138+
protected boolean removeEldestEntry(Map.Entry<String, Status> eldest) {
139+
return size() > 100;
140+
}
141+
};
131142

132143
private WriteQueue clientWriteQueue;
133144
private Http2Ping ping;
@@ -591,6 +602,28 @@ private void createStream(CreateStreamCommand command, ChannelPromise promise)
591602
return;
592603
}
593604

605+
String authority = ((GrpcHttp2OutboundHeaders) command.headers()).getAuthority();
606+
if (authority != null) {
607+
Status authorityVerificationStatus = peerVerificationResults.get(authority);
608+
if (authorityVerificationStatus == null) {
609+
authorityVerificationStatus = attributes.get(GrpcAttributes.ATTR_AUTHORITY_VERIFIER)
610+
.verifyAuthority(((GrpcHttp2OutboundHeaders) command.headers()).getAuthority());
611+
peerVerificationResults.put(authority, authorityVerificationStatus);
612+
}
613+
if (!authorityVerificationStatus.isOk()) {
614+
logger.log(Level.WARNING, String.format("%s.%s",
615+
authorityVerificationStatus.getDescription(), enablePerRpcAuthorityCheck
616+
? "" : "This will be an error in the future."),
617+
authorityVerificationStatus.getCause());
618+
if (enablePerRpcAuthorityCheck) {
619+
command.stream().setNonExistent();
620+
command.stream().transportReportStatus(
621+
authorityVerificationStatus, RpcProgress.DROPPED, true, new Metadata());
622+
promise.setFailure(authorityVerificationStatus.getCause());
623+
return;
624+
}
625+
}
626+
}
594627
// Get the stream ID for the new stream.
595628
int streamId;
596629
try {

netty/src/main/java/io/grpc/netty/NettyClientTransport.java

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,9 @@
6060
import io.netty.util.concurrent.GenericFutureListener;
6161
import java.net.SocketAddress;
6262
import java.nio.channels.ClosedChannelException;
63-
import java.util.Collections;
64-
import java.util.LinkedHashMap;
6563
import java.util.Map;
6664
import java.util.concurrent.Executor;
6765
import java.util.concurrent.TimeUnit;
68-
import java.util.logging.Level;
6966
import java.util.logging.Logger;
7067
import javax.annotation.Nullable;
7168

@@ -110,17 +107,7 @@ class NettyClientTransport implements ConnectionClientTransport {
110107
private final boolean useGetForSafeMethods;
111108
private final Ticker ticker;
112109
private final Logger logger = Logger.getLogger(NettyClientTransport.class.getName());
113-
private final Map<String, Status> peerVerificationResults = Collections.synchronizedMap(
114-
new LinkedHashMap<String, Status>() {
115-
@Override
116-
protected boolean removeEldestEntry(Map.Entry<String, Status> eldest) {
117-
return size() > 100;
118-
}
119-
});
120110

121-
@VisibleForTesting
122-
static boolean enablePerRpcAuthorityCheck =
123-
GrpcUtil.getFlag("GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK", false);
124111

125112
NettyClientTransport(
126113
SocketAddress address,
@@ -210,25 +197,6 @@ public ClientStream newStream(
210197
if (channel == null) {
211198
return new FailingClientStream(statusExplainingWhyTheChannelIsNull, tracers);
212199
}
213-
if (callOptions.getAuthority() != null) {
214-
Status verificationStatus = peerVerificationResults.get(callOptions.getAuthority());
215-
if (verificationStatus == null) {
216-
verificationStatus = negotiator.verifyAuthority(callOptions.getAuthority());
217-
peerVerificationResults.put(callOptions.getAuthority(), verificationStatus);
218-
if (!verificationStatus.isOk()) {
219-
logger.log(Level.WARNING, String.format("Peer hostname verification during rpc failed "
220-
+ "for authority '%s' for method '%s' with the error \"%s\". This will "
221-
+ "be an error in the future.", callOptions.getAuthority(),
222-
method.getFullMethodName(), verificationStatus.getDescription()),
223-
verificationStatus.getCause());
224-
}
225-
}
226-
if (!verificationStatus.isOk()) {
227-
if (enablePerRpcAuthorityCheck) {
228-
return new FailingClientStream(verificationStatus, tracers);
229-
}
230-
}
231-
}
232200
StatsTraceContext statsTraceCtx =
233201
StatsTraceContext.newClientContext(tracers, getAttributes(), headers);
234202
return new NettyClientStream(

netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ public void tlsNegotiationServerExecutorShouldSucceed() throws Exception {
867867
public void authorityOverrideInCallOptions_noX509ExtendedTrustManager_newStreamCreationFails()
868868
throws IOException, InterruptedException, GeneralSecurityException, ExecutionException,
869869
TimeoutException {
870-
NettyClientTransport.enablePerRpcAuthorityCheck = true;
870+
NettyClientHandler.enablePerRpcAuthorityCheck = true;
871871
try {
872872
startServer();
873873
InputStream caCert = TlsTesting.loadCert("ca.pem");
@@ -895,15 +895,15 @@ public void authorityOverrideInCallOptions_noX509ExtendedTrustManager_newStreamC
895895
assertThat(status.getCode()).isEqualTo(Code.FAILED_PRECONDITION);
896896
}
897897
} finally {
898-
NettyClientTransport.enablePerRpcAuthorityCheck = false;
898+
NettyClientHandler.enablePerRpcAuthorityCheck = false;
899899
}
900900
}
901901

902902
@Test
903903
public void authorityOverrideInCallOptions_doesntMatchServerPeerHost_newStreamCreationFails()
904904
throws IOException, InterruptedException, GeneralSecurityException, ExecutionException,
905905
TimeoutException {
906-
NettyClientTransport.enablePerRpcAuthorityCheck = true;
906+
NettyClientHandler.enablePerRpcAuthorityCheck = true;
907907
try {
908908
startServer();
909909
NettyClientTransport transport = newTransport(newNegotiator());
@@ -930,15 +930,15 @@ public void authorityOverrideInCallOptions_doesntMatchServerPeerHost_newStreamCr
930930
"No subject alternative DNS name matching foo.test.google.in found.");
931931
}
932932
} finally {
933-
NettyClientTransport.enablePerRpcAuthorityCheck = false;
933+
NettyClientHandler.enablePerRpcAuthorityCheck = false;
934934
}
935935
}
936936

937937
@Test
938938
public void authorityOverrideInCallOptions_matchesServerPeerHost_newStreamCreationSucceeds()
939939
throws IOException, InterruptedException, GeneralSecurityException, ExecutionException,
940940
TimeoutException {
941-
NettyClientTransport.enablePerRpcAuthorityCheck = true;
941+
NettyClientHandler.enablePerRpcAuthorityCheck = true;
942942
try {
943943
startServer();
944944
NettyClientTransport transport = newTransport(newNegotiator());
@@ -951,15 +951,15 @@ public void authorityOverrideInCallOptions_matchesServerPeerHost_newStreamCreati
951951

952952
new Rpc(transport, new Metadata(), "foo.test.google.fr").waitForResponse();
953953
} finally {
954-
NettyClientTransport.enablePerRpcAuthorityCheck = false;;
954+
NettyClientHandler.enablePerRpcAuthorityCheck = false;;
955955
}
956956
}
957957

958958
@Test
959959
public void authorityOverrideInCallOptions_lruCache()
960960
throws IOException, InterruptedException, GeneralSecurityException, ExecutionException,
961961
TimeoutException {
962-
NettyClientTransport.enablePerRpcAuthorityCheck = true;
962+
NettyClientHandler.enablePerRpcAuthorityCheck = true;
963963
try {
964964
startServer();
965965
ProtocolNegotiator mockNegotiator =
@@ -997,7 +997,7 @@ public void authorityOverrideInCallOptions_lruCache()
997997
assertThat(authorityValues.get(100)).isEqualTo("foo-100.test.google.fr");
998998
assertThat(authorityValues.get(101)).isEqualTo("foo-0.test.google.fr");
999999
} finally {
1000-
NettyClientTransport.enablePerRpcAuthorityCheck = false;;
1000+
NettyClientHandler.enablePerRpcAuthorityCheck = false;;
10011001
}
10021002
}
10031003

0 commit comments

Comments
 (0)