Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netty: Per-rpc call option authority verification against peer cert subject names #11724

Open
wants to merge 77 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
fa36f83
In-progress changes.
kannanjgithub Nov 18, 2024
f31b8bc
In-progress changes that used ExtendedSSLSession.
kannanjgithub Nov 26, 2024
0152478
Authority verify in Netty transport.
kannanjgithub Nov 27, 2024
c31995e
In-progress changes for Authority verify in okhttp transport.
kannanjgithub Nov 29, 2024
5e2e22e
Netty authority verify against peer host in server cert.
kannanjgithub Nov 30, 2024
b0f86cf
unit test.
kannanjgithub Dec 1, 2024
dd95f8f
in-progress changes.
kannanjgithub Dec 2, 2024
5ba5431
nit changes and drop unintended changes.
kannanjgithub Dec 2, 2024
e42492b
nit changes and drop unintended changes.
kannanjgithub Dec 2, 2024
5285353
Cache the peer verification result.
kannanjgithub Dec 4, 2024
ea969ef
Move extraction of X509ExtendedTrustManager to utils.
kannanjgithub Dec 4, 2024
4d71cce
commit
kannanjgithub Dec 4, 2024
94172de
Fixes.
kannanjgithub Dec 4, 2024
0ddd42c
Fixes.
kannanjgithub Dec 4, 2024
909b863
Fixes.
kannanjgithub Dec 4, 2024
5109115
Fixes.
kannanjgithub Dec 4, 2024
d5f3968
nit
kannanjgithub Dec 4, 2024
24500f4
Changes.
kannanjgithub Dec 4, 2024
1e072d4
In-progress review comments.
kannanjgithub Dec 9, 2024
32104ce
Address review comments.
kannanjgithub Dec 9, 2024
95aae4a
revert unintended
kannanjgithub Dec 9, 2024
c0327b5
revert unintended
kannanjgithub Dec 9, 2024
b24a605
Address review comments.
kannanjgithub Dec 13, 2024
862b95b
Fix warning.
kannanjgithub Dec 13, 2024
8902dbd
Address review comments.
kannanjgithub Dec 14, 2024
01b0eb2
Address review comments.
kannanjgithub Dec 14, 2024
60bfa06
In progress changes.
kannanjgithub Dec 14, 2024
8dd8749
Remove duplicate definitions of createTrustManager.
kannanjgithub Dec 14, 2024
23a0822
in-progress changes.
kannanjgithub Dec 14, 2024
0a9124c
in-progress changes.
kannanjgithub Dec 14, 2024
f9305e7
in-progress changes.
kannanjgithub Dec 14, 2024
4af94ec
in-progress changes.
kannanjgithub Dec 14, 2024
ece5487
Merge branch 'grpc:master' into authorityverifyokhttp
kannanjgithub Dec 15, 2024
aa59965
Unit tests and using HostnameVerifier in per-rpc.
kannanjgithub Dec 15, 2024
20fa07d
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub Dec 15, 2024
3d744d9
Review comments.
kannanjgithub Dec 16, 2024
746717e
Revert unintended changes.
kannanjgithub Dec 16, 2024
0d310b3
Revert unintended changes.
kannanjgithub Dec 16, 2024
4ef0fdb
Move NoopSslSession to io.grpc.internal under grpc-core project.
kannanjgithub Dec 18, 2024
3e2ef72
Address review comments.
kannanjgithub Dec 18, 2024
30b1e14
Address review comments.
kannanjgithub Dec 18, 2024
fdc6e94
Added flag with default false for the per-rpc authority check.
kannanjgithub Dec 19, 2024
f996474
Added flag with default false for the per-rpc authority check and rem…
kannanjgithub Dec 19, 2024
f5b3614
Update README etc to reference 1.69.0
kannanjgithub Dec 9, 2024
86b9529
:Revert "Update README etc to reference 1.69.0"
kannanjgithub Dec 20, 2024
60efd84
Fix style.
kannanjgithub Jan 7, 2025
60b1ee0
Fix style.
kannanjgithub Jan 7, 2025
d2af807
Merge branch 'master' into authoritychecktls
kannanjgithub Jan 7, 2025
2594842
Fix merge conflicts.
kannanjgithub Jan 7, 2025
916d0d5
Review comments and use reflection for X509ExtendedTrustManager.
kannanjgithub Jan 10, 2025
040035b
Include failed method name in the tls verification failed log message.
kannanjgithub Jan 13, 2025
89b24f2
Merge branch 'master' into authorityverifyokhttp
kannanjgithub Jan 13, 2025
ecbf7b7
Use reflection to access X509ExtendedTrustManager.
kannanjgithub Jan 13, 2025
26a94c9
Merge remote-tracking branch 'origin/master' into authorityverifyokhttp
kannanjgithub Jan 13, 2025
ce800f7
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub Jan 13, 2025
44f2412
Address review comments.
kannanjgithub Jan 15, 2025
6449728
Address review comments.
kannanjgithub Jan 16, 2025
4273452
Address review comments.
kannanjgithub Jan 17, 2025
a9a019b
Remove the code handling for impossible exception.
kannanjgithub Jan 17, 2025
1f56282
Update comment for NoSuchMethodError.
kannanjgithub Jan 17, 2025
649f53c
Some changes based on similar comments in the authority check for Net…
kannanjgithub Jan 17, 2025
73bb42f
Animal sniffer suppress
kannanjgithub Jan 17, 2025
b8b70bf
Merge branch 'master' into authoritychecktls
kannanjgithub Jan 21, 2025
2af1cca
Ignore animal sniffer errors via annotation in tests.
kannanjgithub Jan 22, 2025
15c8161
In-progress changes to move authority verification to the ChannelHand…
kannanjgithub Jan 24, 2025
7941abc
Save sslEngine and use it later after the handshake is complete, to s…
kannanjgithub Jan 24, 2025
7fcd98d
temp testing changes
kannanjgithub Jan 29, 2025
4c50e71
Changes.
kannanjgithub Jan 30, 2025
da19b28
Merge branch 'authoritychecktls-fork' into authoritychecktls
kannanjgithub Jan 30, 2025
4e24446
Merge branch 'grpc:master' into authoritychecktls
kannanjgithub Jan 30, 2025
8576a4e
Merge remote-tracking branch 'origin/authorityverifyokhttp' into auth…
kannanjgithub Jan 30, 2025
0627bf4
Merge remote-tracking branch 'origin/authoritychecktls' into authorit…
kannanjgithub Jan 30, 2025
02a1041
Revert "Merge remote-tracking branch 'origin/authorityverifyokhttp' i…
kannanjgithub Jan 30, 2025
1d6f46f
Fix style
kannanjgithub Jan 30, 2025
42058ea
Revert unintended changes.
kannanjgithub Jan 31, 2025
cd93d4e
Revert unintended changes.
kannanjgithub Jan 31, 2025
1ff1f0b
Revert unintended changes.
kannanjgithub Jan 31, 2025
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
66 changes: 66 additions & 0 deletions core/src/main/java/io/grpc/internal/CertificateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Collection;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.security.auth.x500.X500Principal;

/**
* Contains certificate/key PEM file utility method(s) for internal usage.
*/
public class CertificateUtils {

Check warning on line 35 in core/src/main/java/io/grpc/internal/CertificateUtils.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/CertificateUtils.java#L35

Added line #L35 was not covered by tests
/**
* Creates X509TrustManagers using the provided CA certs.
*/
public static TrustManager[] createTrustManager(InputStream rootCerts)
throws GeneralSecurityException {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
ks.load(null, null);
} catch (IOException ex) {

Check warning on line 44 in core/src/main/java/io/grpc/internal/CertificateUtils.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/CertificateUtils.java#L44

Added line #L44 was not covered by tests
// Shouldn't really happen, as we're not loading any data.
throw new GeneralSecurityException(ex);

Check warning on line 46 in core/src/main/java/io/grpc/internal/CertificateUtils.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/CertificateUtils.java#L46

Added line #L46 was not covered by tests
}
X509Certificate[] certs = CertificateUtils.getX509Certificates(rootCerts);
for (X509Certificate cert : certs) {
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
}

TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks);
return trustManagerFactory.getTrustManagers();
}

private static X509Certificate[] getX509Certificates(InputStream inputStream)
throws CertificateException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certs = factory.generateCertificates(inputStream);
return certs.toArray(new X509Certificate[0]);
}
}
132 changes: 132 additions & 0 deletions core/src/main/java/io/grpc/internal/NoopSslSession.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2024 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.grpc.internal;

import java.security.Principal;
import java.security.cert.Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSessionContext;

/** A no-op ssl session, to facilitate overriding only the required methods in specific
* implementations.
*/
public class NoopSslSession implements SSLSession {
@Override
public byte[] getId() {
return new byte[0];

Check warning on line 31 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L31

Added line #L31 was not covered by tests
}

@Override
public SSLSessionContext getSessionContext() {
return null;

Check warning on line 36 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L36

Added line #L36 was not covered by tests
}

@Override
@SuppressWarnings("deprecation")
public javax.security.cert.X509Certificate[] getPeerCertificateChain() {
throw new UnsupportedOperationException("This method is deprecated and marked for removal. "

Check warning on line 42 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L42

Added line #L42 was not covered by tests
+ "Use the getPeerCertificates() method instead.");
}

@Override
public long getCreationTime() {
return 0;

Check warning on line 48 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L48

Added line #L48 was not covered by tests
}

@Override
public long getLastAccessedTime() {
return 0;

Check warning on line 53 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L53

Added line #L53 was not covered by tests
}

@Override
public void invalidate() {
}

Check warning on line 58 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L58

Added line #L58 was not covered by tests

@Override
public boolean isValid() {
return false;

Check warning on line 62 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L62

Added line #L62 was not covered by tests
}

@Override
public void putValue(String s, Object o) {
}

Check warning on line 67 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L67

Added line #L67 was not covered by tests

@Override
public Object getValue(String s) {
return null;

Check warning on line 71 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L71

Added line #L71 was not covered by tests
}

@Override
public void removeValue(String s) {
}

Check warning on line 76 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L76

Added line #L76 was not covered by tests

@Override
public String[] getValueNames() {
return new String[0];

Check warning on line 80 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L80

Added line #L80 was not covered by tests
}

@Override
public Certificate[] getPeerCertificates() throws SSLPeerUnverifiedException {
return new Certificate[0];

Check warning on line 85 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L85

Added line #L85 was not covered by tests
}

@Override
public Certificate[] getLocalCertificates() {
return new Certificate[0];

Check warning on line 90 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L90

Added line #L90 was not covered by tests
}

@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
return null;

Check warning on line 95 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L95

Added line #L95 was not covered by tests
}

@Override
public Principal getLocalPrincipal() {
return null;

Check warning on line 100 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L100

Added line #L100 was not covered by tests
}

@Override
public String getCipherSuite() {
return null;

Check warning on line 105 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L105

Added line #L105 was not covered by tests
}

@Override
public String getProtocol() {
return null;

Check warning on line 110 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L110

Added line #L110 was not covered by tests
}

@Override
public String getPeerHost() {
return null;

Check warning on line 115 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L115

Added line #L115 was not covered by tests
}

@Override
public int getPeerPort() {
return 0;

Check warning on line 120 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L120

Added line #L120 was not covered by tests
}

@Override
public int getPacketBufferSize() {
return 0;

Check warning on line 125 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L125

Added line #L125 was not covered by tests
}

@Override
public int getApplicationBufferSize() {
return 0;

Check warning on line 130 in core/src/main/java/io/grpc/internal/NoopSslSession.java

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/io/grpc/internal/NoopSslSession.java#L130

Added line #L130 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
ObjectPool<? extends Executor> executorPool,
Optional<Runnable> handshakeCompleteRunnable) {
final io.grpc.netty.ProtocolNegotiator negotiator = ProtocolNegotiators.tls(sslContext,
executorPool, handshakeCompleteRunnable);
executorPool, handshakeCompleteRunnable, null);
final class TlsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {

@Override
Expand Down Expand Up @@ -170,7 +170,7 @@
ChannelHandler next, SslContext sslContext, String authority,
ChannelLogger negotiationLogger) {
return new ClientTlsHandler(next, sslContext, authority, null, negotiationLogger,
Optional.absent());
Optional.absent(), null);

Check warning on line 173 in netty/src/main/java/io/grpc/netty/InternalProtocolNegotiators.java

View check run for this annotation

Codecov / codecov/patch

netty/src/main/java/io/grpc/netty/InternalProtocolNegotiators.java#L173

Added line #L173 was not covered by tests
}

public static class ProtocolNegotiationHandler
Expand Down
2 changes: 1 addition & 1 deletion netty/src/main/java/io/grpc/netty/NettyChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ static ProtocolNegotiator createProtocolNegotiatorByType(
case PLAINTEXT_UPGRADE:
return ProtocolNegotiators.plaintextUpgrade();
case TLS:
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.absent());
return ProtocolNegotiators.tls(sslContext, executorPool, Optional.absent(), null);
default:
throw new IllegalArgumentException("Unsupported negotiationType: " + negotiationType);
}
Expand Down
35 changes: 35 additions & 0 deletions netty/src/main/java/io/grpc/netty/NettyClientTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,13 @@
import io.netty.util.concurrent.GenericFutureListener;
import java.net.SocketAddress;
import java.nio.channels.ClosedChannelException;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -105,6 +109,18 @@ class NettyClientTransport implements ConnectionClientTransport {
private final ChannelLogger channelLogger;
private final boolean useGetForSafeMethods;
private final Ticker ticker;
private final Logger logger = Logger.getLogger(NettyClientTransport.class.getName());
private final Map<String, Status> peerVerificationResults = Collections.synchronizedMap(
new LinkedHashMap<String, Status>() {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Status> eldest) {
return size() > 100;
}
});

@VisibleForTesting
static boolean enablePerRpcAuthorityCheck =
GrpcUtil.getFlag("GRPC_ENABLE_PER_RPC_AUTHORITY_CHECK", false);

NettyClientTransport(
SocketAddress address,
Expand Down Expand Up @@ -194,6 +210,25 @@ public ClientStream newStream(
if (channel == null) {
return new FailingClientStream(statusExplainingWhyTheChannelIsNull, tracers);
}
if (callOptions.getAuthority() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now that we're getting this per-RPC authority separately from the data flow that uses the authority. That makes me nervous, especially for security. As the code changes, it's likely this will break. In fact, it is already broken because the LB-based override only influences setAuthority().

For using the authority, the per-RPC authority is copied to the stream in ClientCallImpl. NettyClientStream then copies it into the Netty Http2Headers (while running an application's thread).

Having the logic in NettyClientTransport has been a bit odd, but fine, and I saw why it was done to avoid plumbing of the protocol negotiator to other places. Normally we'd add RPC logic in NettyClientHandler/NettyClientStream. NettyClientTransport doesn't do much other than create/manage the Netty channel (connection). I suggest we move this logic to NettyClientHandler and forgo any cache synchronization because it will always run on the transport thread.

Copy link
Contributor Author

@kannanjgithub kannanjgithub Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you say "it is already broken because the LB-based override only influences setAuthority()" I thought you were implying what it doesn't influence is that at the authority using time a different authority could still be set into CallOption and ClientCallImpl.start would set it into the stream.
But in the normal sequence I observe that DelayedClientTransport::reprocess occurs later then ClientCallImpl.start, so it will overwrite any earlier authority set into the stream with the authority for which the peer verification actually happens against the peer cert. So what is the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also when you say we do the authority verification now in NettyClientHandler, where exactly there? At first I thought we would have a NettyClientHandler::verifyAuthority(String authority) invoked from NettyClientStream before enqueuing the CreateStreamCommand. But you said it needs to be done on the transport thread whereas the writing the command to the queue is the application thread.

writeHeadersInternal:171, NettyClientStream$Sink (io.grpc.netty.shaded.io.grpc.netty)
writeHeaders:123, NettyClientStream$Sink (io.grpc.netty.shaded.io.grpc.netty)
start:159, AbstractClientStream (io.grpc.internal)
start:92, ForwardingClientStream (io.grpc.internal)
start:722, InternalSubchannel$CallTracingTransport$1 (io.grpc.internal)
internalStart:256, DelayedStream (io.grpc.internal)
setStream:144, DelayedStream (io.grpc.internal)
createRealStream:367, DelayedClientTransport$PendingStream (io.grpc.internal)
access$500:346, DelayedClientTransport$PendingStream (io.grpc.internal)
reprocess:304, DelayedClientTransport (io.grpc.internal)
updateSubchannelPicker:803, ManagedChannelImpl (io.grpc.internal)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is the authority returned by the LB policy would be used (stream.setAuthority() will put it in the headers) without being verified (because this code is looking at callOptions).

Where CreateStreamCommand is consumed:

private void createStream(CreateStreamCommand command, ChannelPromise promise)

Copy link
Contributor Author

@kannanjgithub kannanjgithub Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is the authority returned by the LB policy would be used (stream.setAuthority() will put it in the headers) without being verified (because this code is looking at callOptions).

Even with the suggested change to move the authority verification code to NettyClientHandler , that code can't still influence the using code for authority in ClientCallImpl , right? And this will require plumbing some new code to take into account any authority from picker result to be considered in ClientCallImpl.setAuthority.

With both approaches we are failing real stream creation and the headers set into the pending stream such as from ClientCallImpl.setAuthority are only copied into the real stream after its creation, which would have failed if the authority didn't match.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NettyClientHandler would see any changes to setAuthority(). Since it is sending the request on-the-wire, it will always have the "correct" authority to verify. The point is not to influence what authority is being used, but verify that the chose authority is appropriate. So, yes, the ClientCallImpl code will call setAuthority(), and the LB result might cause setAuthority(), but NettyClientHandler can fail the RPC before it is sent.

With both approaches we are failing real stream creation and the headers set into the pending stream such as from ClientCallImpl.setAuthority are only copied into the real stream after its creation, which would have failed if the authority didn't match.

PendingStream shouldn't have much to do with it. setAuthority() is after stream creation but before stream start(). It is expected for setAuthority() to be called before start(). Stream start() is the point that CreateStreamCommand is sent to NettyClientHandler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved logic to NettyClientHandler. Yet to write/fix unit tests (planning to do via NettyClilentTransportTest itself like before).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed tests.

Status verificationStatus = peerVerificationResults.get(callOptions.getAuthority());
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
if (verificationStatus == null) {
verificationStatus = negotiator.verifyAuthority(callOptions.getAuthority());
ejona86 marked this conversation as resolved.
Show resolved Hide resolved
peerVerificationResults.put(callOptions.getAuthority(), verificationStatus);
if (!verificationStatus.isOk()) {
logger.log(Level.WARNING, String.format("Peer hostname verification during rpc failed "
+ "for authority '%s' for method '%s' with the error \"%s\". This will "
+ "be an error in the future.", callOptions.getAuthority(),
method.getFullMethodName(), verificationStatus.getDescription()),
verificationStatus.getCause());
}
}
if (!verificationStatus.isOk()) {
if (enablePerRpcAuthorityCheck) {
return new FailingClientStream(verificationStatus, tracers);
}
}
}
StatsTraceContext statsTraceCtx =
StatsTraceContext.newClientContext(tracers, getAttributes(), headers);
return new NettyClientStream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
Preconditions.checkArgument(sslContext.isClient(),
"Server SSL context can not be used for client channel");
GrpcSslContexts.ensureAlpnAndH2Enabled(sslContext.applicationProtocolNegotiator());
return NettyChannelCredentials.create(ProtocolNegotiators.tlsClientFactory(sslContext));
return NettyChannelCredentials.create(ProtocolNegotiators.tlsClientFactory(sslContext, null));

Check warning on line 37 in netty/src/main/java/io/grpc/netty/NettySslContextChannelCredentials.java

View check run for this annotation

Codecov / codecov/patch

netty/src/main/java/io/grpc/netty/NettySslContextChannelCredentials.java#L37

Added line #L37 was not covered by tests
}
}
Loading
Loading