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

ok-http: Per-rpc call option authority verification #11754

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
c31995e
In-progress changes for Authority verify in okhttp transport.
kannanjgithub Nov 29, 2024
dd95f8f
in-progress changes.
kannanjgithub Dec 2, 2024
4d71cce
commit
kannanjgithub Dec 4, 2024
909b863
Fixes.
kannanjgithub Dec 4, 2024
24500f4
Changes.
kannanjgithub Dec 4, 2024
60bfa06
In progress changes.
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
746717e
Revert unintended changes.
kannanjgithub Dec 16, 2024
0d310b3
Revert unintended changes.
kannanjgithub Dec 16, 2024
3e2ef72
Address review comments.
kannanjgithub Dec 18, 2024
30b1e14
Address review comments.
kannanjgithub Dec 18, 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
60b1ee0
Fix style.
kannanjgithub Jan 7, 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
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
2af1cca
Ignore animal sniffer errors via annotation in tests.
kannanjgithub Jan 22, 2025
fb0e733
Changes to move the authority verification logic to the stream creati…
kannanjgithub Feb 3, 2025
a8f696f
fix style
kannanjgithub Feb 4, 2025
49f058d
Fix test
kannanjgithub Feb 4, 2025
2a04f62
style checks.
kannanjgithub Feb 4, 2025
8880733
Review comments
kannanjgithub Feb 28, 2025
0c5f1af
Merge remote-tracking branch 'origin/master' into authorityverifyokhttp
kannanjgithub Feb 28, 2025
671b6a7
Style fixes
kannanjgithub Feb 28, 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
22 changes: 18 additions & 4 deletions core/src/main/java/io/grpc/internal/CertificateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.grpc.internal;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
Expand All @@ -32,12 +33,25 @@
/**
* Contains certificate/key PEM file utility method(s) for internal usage.
*/
public final class CertificateUtils {
public class CertificateUtils {

Check warning on line 36 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#L36

Added line #L36 was not covered by tests
/**
* Creates X509TrustManagers using the provided CA certs.
*/
public static TrustManager[] createTrustManager(InputStream rootCerts)
public static TrustManager[] createTrustManager(byte[] rootCerts)
throws GeneralSecurityException {
InputStream rootCertsStream = new ByteArrayInputStream(rootCerts);
try {
return CertificateUtils.createTrustManager(rootCertsStream);
} finally {
GrpcUtil.closeQuietly(rootCertsStream);
}
}

/**
* Creates X509TrustManagers using the provided input stream of CA certs.
*/
public static TrustManager[] createTrustManager(InputStream rootCerts)
throws GeneralSecurityException {
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
try {
ks.load(null, null);
Expand All @@ -52,13 +66,13 @@
}

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

private static X509Certificate[] getX509Certificates(InputStream inputStream)
throws CertificateException {
throws CertificateException {
CertificateFactory factory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certs = factory.generateCertificates(inputStream);
return certs.toArray(new X509Certificate[0]);
Expand Down
117 changes: 117 additions & 0 deletions okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.okhttp;

import java.io.IOException;
import javax.net.ssl.HandshakeCompletedListener;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocket;

/** A no-op ssl socket, to facilitate overriding only the required methods in specific
* implementations.
*/
class NoopSslSocket extends SSLSocket {
@Override
public String[] getSupportedCipherSuites() {
return new String[0];

Check warning on line 30 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L30

Added line #L30 was not covered by tests
}

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

Check warning on line 35 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L35

Added line #L35 was not covered by tests
}

@Override
public void setEnabledCipherSuites(String[] suites) {

}

Check warning on line 41 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L41

Added line #L41 was not covered by tests

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

Check warning on line 45 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L45

Added line #L45 was not covered by tests
}

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

Check warning on line 50 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L50

Added line #L50 was not covered by tests
}

@Override
public void setEnabledProtocols(String[] protocols) {

}

Check warning on line 56 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L56

Added line #L56 was not covered by tests

@Override
public SSLSession getSession() {
return null;

Check warning on line 60 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L60

Added line #L60 was not covered by tests
}

@Override
public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {

}

Check warning on line 66 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L66

Added line #L66 was not covered by tests

@Override
public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {

}

Check warning on line 71 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L71

Added line #L71 was not covered by tests

@Override
public void startHandshake() throws IOException {

}

Check warning on line 76 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L76

Added line #L76 was not covered by tests

@Override
public void setUseClientMode(boolean mode) {

}

Check warning on line 81 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L81

Added line #L81 was not covered by tests

@Override
public boolean getUseClientMode() {
return false;

Check warning on line 85 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L85

Added line #L85 was not covered by tests
}

@Override
public void setNeedClientAuth(boolean need) {

}

Check warning on line 91 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L91

Added line #L91 was not covered by tests

@Override
public boolean getNeedClientAuth() {
return false;

Check warning on line 95 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L95

Added line #L95 was not covered by tests
}

@Override
public void setWantClientAuth(boolean want) {

}

Check warning on line 101 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L101

Added line #L101 was not covered by tests

@Override
public boolean getWantClientAuth() {
return false;

Check warning on line 105 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L105

Added line #L105 was not covered by tests
}

@Override
public void setEnableSessionCreation(boolean flag) {

}

Check warning on line 111 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L111

Added line #L111 was not covered by tests

@Override
public boolean getEnableSessionCreation() {
return false;

Check warning on line 115 in okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java

View check run for this annotation

Codecov / codecov/patch

okhttp/src/main/java/io/grpc/okhttp/NoopSslSocket.java#L115

Added line #L115 was not covered by tests
}
}
18 changes: 14 additions & 4 deletions okhttp/src/main/java/io/grpc/okhttp/OkHttpChannelBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.grpc.okhttp;

import static com.google.common.base.Preconditions.checkNotNull;
import static io.grpc.internal.CertificateUtils.createTrustManager;
import static io.grpc.internal.GrpcUtil.DEFAULT_KEEPALIVE_TIMEOUT_NANOS;
import static io.grpc.internal.GrpcUtil.KEEPALIVE_TIME_NANOS_DISABLED;

Expand Down Expand Up @@ -89,6 +90,7 @@ public final class OkHttpChannelBuilder extends ForwardingChannelBuilder2<OkHttp
public static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535;

private final ManagedChannelImplBuilder managedChannelImplBuilder;
private final ChannelCredentials channelCredentials;
private TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();


Expand Down Expand Up @@ -206,6 +208,7 @@ private OkHttpChannelBuilder(String target) {
new OkHttpChannelTransportFactoryBuilder(),
new OkHttpChannelDefaultPortProvider());
this.freezeSecurityConfiguration = false;
this.channelCredentials = null;
}

OkHttpChannelBuilder(
Expand All @@ -218,6 +221,7 @@ private OkHttpChannelBuilder(String target) {
this.sslSocketFactory = factory;
this.negotiationType = factory == null ? NegotiationType.PLAINTEXT : NegotiationType.TLS;
this.freezeSecurityConfiguration = true;
this.channelCredentials = channelCreds;
}

private final class OkHttpChannelTransportFactoryBuilder
Expand Down Expand Up @@ -534,7 +538,8 @@ OkHttpTransportFactory buildTransportFactory() {
keepAliveWithoutCalls,
maxInboundMetadataSize,
transportTracerFactory,
useGetForSafeMethods);
useGetForSafeMethods,
channelCredentials);
}

OkHttpChannelBuilder disableCheckAuthority() {
Expand Down Expand Up @@ -777,6 +782,7 @@ static final class OkHttpTransportFactory implements ClientTransportFactory {
private final boolean keepAliveWithoutCalls;
final int maxInboundMetadataSize;
final boolean useGetForSafeMethods;
private final ChannelCredentials channelCredentials;
private boolean closed;

private OkHttpTransportFactory(
Expand All @@ -794,7 +800,8 @@ private OkHttpTransportFactory(
boolean keepAliveWithoutCalls,
int maxInboundMetadataSize,
TransportTracer.Factory transportTracerFactory,
boolean useGetForSafeMethods) {
boolean useGetForSafeMethods,
ChannelCredentials channelCredentials) {
this.executorPool = executorPool;
this.executor = executorPool.getObject();
this.scheduledExecutorServicePool = scheduledExecutorServicePool;
Expand All @@ -812,6 +819,7 @@ private OkHttpTransportFactory(
this.keepAliveWithoutCalls = keepAliveWithoutCalls;
this.maxInboundMetadataSize = maxInboundMetadataSize;
this.useGetForSafeMethods = useGetForSafeMethods;
this.channelCredentials = channelCredentials;

this.transportTracerFactory =
Preconditions.checkNotNull(transportTracerFactory, "transportTracerFactory");
Expand Down Expand Up @@ -839,7 +847,8 @@ public void run() {
options.getUserAgent(),
options.getEagAttributes(),
options.getHttpConnectProxiedSocketAddress(),
tooManyPingsRunnable);
tooManyPingsRunnable,
channelCredentials);
if (enableKeepAlive) {
transport.enableKeepAlive(
true, keepAliveTimeNanosState.get(), keepAliveTimeoutNanos, keepAliveWithoutCalls);
Expand Down Expand Up @@ -875,7 +884,8 @@ public SwapChannelCredentialsResult swapChannelCredentials(ChannelCredentials ch
keepAliveWithoutCalls,
maxInboundMetadataSize,
transportTracerFactory,
useGetForSafeMethods);
useGetForSafeMethods,
channelCredentials);
return new SwapChannelCredentialsResult(factory, result.callCredentials);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ private void streamReady(Metadata metadata, String path) {
transport.isUsingPlaintext());
// TODO(b/145386688): This access should be guarded by 'this.transport.lock'; instead found:
// 'this.lock'
transport.streamReadyToStart(OkHttpClientStream.this);
transport.streamReadyToStart(OkHttpClientStream.this, authority);
}

Tag tag() {
Expand Down
Loading