Skip to content

Commit ef53441

Browse files
committed
Use InsecureChannelCredentials.create instead of Optional.
1 parent 9c44754 commit ef53441

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

s2a/src/main/java/io/grpc/s2a/S2AChannelCredentials.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
import io.grpc.Channel;
2626
import io.grpc.ChannelCredentials;
2727
import io.grpc.ExperimentalApi;
28+
import io.grpc.InsecureChannelCredentials;
2829
import io.grpc.internal.ObjectPool;
2930
import io.grpc.internal.SharedResourcePool;
3031
import io.grpc.netty.InternalNettyChannelCredentials;
3132
import io.grpc.netty.InternalProtocolNegotiator;
3233
import io.grpc.s2a.channel.S2AHandshakerServiceChannel;
3334
import io.grpc.s2a.handshaker.S2AIdentity;
3435
import io.grpc.s2a.handshaker.S2AProtocolNegotiatorFactory;
35-
import java.util.Optional;
3636
import javax.annotation.concurrent.NotThreadSafe;
3737
import org.checkerframework.checker.nullness.qual.Nullable;
3838

@@ -58,13 +58,13 @@ public static Builder newBuilder(String s2aAddress) {
5858
public static final class Builder {
5959
private final String s2aAddress;
6060
private ObjectPool<Channel> s2aChannelPool;
61-
private Optional<ChannelCredentials> s2aChannelCredentials;
61+
private ChannelCredentials s2aChannelCredentials;
6262
private @Nullable S2AIdentity localIdentity = null;
6363

6464
Builder(String s2aAddress) {
6565
this.s2aAddress = s2aAddress;
6666
this.s2aChannelPool = null;
67-
this.s2aChannelCredentials = Optional.empty();
67+
this.s2aChannelCredentials = InsecureChannelCredentials.create();
6868
}
6969

7070
/**
@@ -109,7 +109,7 @@ public Builder setLocalUid(String localUid) {
109109
/** Sets the credentials to be used when connecting to the S2A. */
110110
@CanIgnoreReturnValue
111111
public Builder setS2AChannelCredentials(ChannelCredentials s2aChannelCredentials) {
112-
this.s2aChannelCredentials = Optional.of(s2aChannelCredentials);
112+
this.s2aChannelCredentials = s2aChannelCredentials;
113113
return this;
114114
}
115115

s2a/src/main/java/io/grpc/s2a/channel/S2AHandshakerServiceChannel.java

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import io.netty.channel.socket.nio.NioSocketChannel;
3535
import io.netty.util.concurrent.DefaultThreadFactory;
3636
import java.time.Duration;
37-
import java.util.Optional;
3837
import java.util.concurrent.ConcurrentMap;
3938
import javax.annotation.concurrent.ThreadSafe;
4039

@@ -74,8 +73,9 @@ public final class S2AHandshakerServiceChannel {
7473
* running at {@code s2aAddress}.
7574
*/
7675
public static Resource<Channel> getChannelResource(
77-
String s2aAddress, Optional<ChannelCredentials> s2aChannelCredentials) {
76+
String s2aAddress, ChannelCredentials s2aChannelCredentials) {
7877
checkNotNull(s2aAddress);
78+
checkNotNull(s2aChannelCredentials);
7979
return SHARED_RESOURCE_CHANNELS.computeIfAbsent(
8080
s2aAddress, channelResource -> new ChannelResource(s2aAddress, s2aChannelCredentials));
8181
}
@@ -87,9 +87,9 @@ public static Resource<Channel> getChannelResource(
8787
*/
8888
private static class ChannelResource implements Resource<Channel> {
8989
private final String targetAddress;
90-
private final Optional<ChannelCredentials> channelCredentials;
90+
private final ChannelCredentials channelCredentials;
9191

92-
public ChannelResource(String targetAddress, Optional<ChannelCredentials> channelCredentials) {
92+
public ChannelResource(String targetAddress, ChannelCredentials channelCredentials) {
9393
this.targetAddress = targetAddress;
9494
this.channelCredentials = channelCredentials;
9595
}
@@ -103,25 +103,12 @@ public ChannelResource(String targetAddress, Optional<ChannelCredentials> channe
103103
public Channel create() {
104104
EventLoopGroup eventLoopGroup =
105105
new NioEventLoopGroup(1, new DefaultThreadFactory("S2A channel pool", true));
106-
ManagedChannel channel = null;
107-
if (channelCredentials.isPresent()) {
108-
// Create a secure channel.
109-
channel =
110-
NettyChannelBuilder.forTarget(targetAddress, channelCredentials.get())
111-
.channelType(NioSocketChannel.class)
112-
.directExecutor()
113-
.eventLoopGroup(eventLoopGroup)
114-
.build();
115-
} else {
116-
// Create a plaintext channel.
117-
channel =
118-
NettyChannelBuilder.forTarget(targetAddress)
119-
.channelType(NioSocketChannel.class)
120-
.directExecutor()
121-
.eventLoopGroup(eventLoopGroup)
122-
.usePlaintext()
123-
.build();
124-
}
106+
ManagedChannel channel =
107+
NettyChannelBuilder.forTarget(targetAddress, channelCredentials)
108+
.channelType(NioSocketChannel.class)
109+
.directExecutor()
110+
.eventLoopGroup(eventLoopGroup)
111+
.build();
125112
return EventLoopHoldingChannel.create(channel, eventLoopGroup);
126113
}
127114

s2a/src/test/java/io/grpc/s2a/channel/S2AHandshakerServiceChannelTest.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.grpc.Channel;
2929
import io.grpc.ChannelCredentials;
3030
import io.grpc.ClientCall;
31+
import io.grpc.InsecureChannelCredentials;
3132
import io.grpc.ManagedChannel;
3233
import io.grpc.MethodDescriptor;
3334
import io.grpc.Server;
@@ -48,7 +49,6 @@
4849
import io.netty.channel.EventLoopGroup;
4950
import java.io.File;
5051
import java.time.Duration;
51-
import java.util.Optional;
5252
import java.util.concurrent.TimeUnit;
5353
import org.junit.Before;
5454
import org.junit.ClassRule;
@@ -82,7 +82,7 @@ public void getChannelResource_success() {
8282
Resource<Channel> resource =
8383
S2AHandshakerServiceChannel.getChannelResource(
8484
"localhost:" + plaintextServer.getPort(),
85-
/* s2aChannelCredentials= */ Optional.empty());
85+
InsecureChannelCredentials.create());
8686
assertThat(resource.toString()).isEqualTo("grpc-s2a-channel");
8787
}
8888

@@ -104,11 +104,11 @@ public void getChannelResource_twoEqualChannels() {
104104
Resource<Channel> resource =
105105
S2AHandshakerServiceChannel.getChannelResource(
106106
"localhost:" + plaintextServer.getPort(),
107-
/* s2aChannelCredentials= */ Optional.empty());
107+
InsecureChannelCredentials.create());
108108
Resource<Channel> resourceTwo =
109109
S2AHandshakerServiceChannel.getChannelResource(
110110
"localhost:" + plaintextServer.getPort(),
111-
/* s2aChannelCredentials= */ Optional.empty());
111+
InsecureChannelCredentials.create());
112112
assertThat(resource).isEqualTo(resourceTwo);
113113
}
114114

@@ -133,10 +133,10 @@ public void getChannelResource_twoDistinctChannels() {
133133
Resource<Channel> resource =
134134
S2AHandshakerServiceChannel.getChannelResource(
135135
"localhost:" + plaintextServer.getPort(),
136-
/* s2aChannelCredentials= */ Optional.empty());
136+
InsecureChannelCredentials.create());
137137
Resource<Channel> resourceTwo =
138138
S2AHandshakerServiceChannel.getChannelResource(
139-
"localhost:" + Utils.pickUnusedPort(), /* s2aChannelCredentials= */ Optional.empty());
139+
"localhost:" + Utils.pickUnusedPort(), InsecureChannelCredentials.create());
140140
assertThat(resourceTwo).isNotEqualTo(resource);
141141
}
142142

@@ -161,7 +161,7 @@ public void close_success() {
161161
Resource<Channel> resource =
162162
S2AHandshakerServiceChannel.getChannelResource(
163163
"localhost:" + plaintextServer.getPort(),
164-
/* s2aChannelCredentials= */ Optional.empty());
164+
InsecureChannelCredentials.create());
165165
Channel channel = resource.create();
166166
resource.close(channel);
167167
StatusRuntimeException expected =
@@ -199,7 +199,7 @@ public void newCall_performSimpleRpcSuccess() {
199199
Resource<Channel> resource =
200200
S2AHandshakerServiceChannel.getChannelResource(
201201
"localhost:" + plaintextServer.getPort(),
202-
/* s2aChannelCredentials= */ Optional.empty());
202+
InsecureChannelCredentials.create());
203203
Channel channel = resource.create();
204204
assertThat(channel).isInstanceOf(EventLoopHoldingChannel.class);
205205
assertThat(
@@ -268,7 +268,7 @@ public void create_succeedsAfterCloseIsCalledOnce() throws Exception {
268268
Resource<Channel> resource =
269269
S2AHandshakerServiceChannel.getChannelResource(
270270
"localhost:" + plaintextServer.getPort(),
271-
/* s2aChannelCredentials= */ Optional.empty());
271+
InsecureChannelCredentials.create());
272272
Channel channelOne = resource.create();
273273
resource.close(channelOne);
274274

@@ -320,15 +320,14 @@ private static Server createPlaintextServer() {
320320
ServerBuilder.forPort(Utils.pickUnusedPort()).addService(service).build());
321321
}
322322

323-
private static Optional<ChannelCredentials> getTlsChannelCredentials() throws Exception {
323+
private static ChannelCredentials getTlsChannelCredentials() throws Exception {
324324
File clientCert = new File("src/test/resources/client_cert.pem");
325325
File clientKey = new File("src/test/resources/client_key.pem");
326326
File rootCert = new File("src/test/resources/root_cert.pem");
327-
return Optional.of(
328-
TlsChannelCredentials.newBuilder()
327+
return TlsChannelCredentials.newBuilder()
329328
.keyManager(clientCert, clientKey)
330329
.trustManager(rootCert)
331-
.build());
330+
.build();
332331
}
333332

334333
private static class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {

s2a/src/test/java/io/grpc/s2a/handshaker/S2AProtocolNegotiatorFactoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public void createProtocolNegotiator_nullArgument() throws Exception {
115115
S2AGrpcChannelPool.create(
116116
SharedResourcePool.forResource(
117117
S2AHandshakerServiceChannel.getChannelResource(
118-
"localhost:8080", /* s2aChannelCredentials= */ Optional.empty())));
118+
"localhost:8080", InsecureChannelCredentials.create())));
119119

120120
NullPointerTester tester =
121121
new NullPointerTester()

s2a/src/test/java/io/grpc/s2a/handshaker/S2AStubTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import static org.junit.Assert.assertThrows;
2222

2323
import com.google.common.truth.Expect;
24+
import io.grpc.InsecureChannelCredentials;
2425
import io.grpc.internal.SharedResourcePool;
2526
import io.grpc.s2a.channel.S2AChannelPool;
2627
import io.grpc.s2a.channel.S2AGrpcChannelPool;
2728
import io.grpc.s2a.channel.S2AHandshakerServiceChannel;
2829
import io.grpc.stub.StreamObserver;
2930
import java.io.IOException;
30-
import java.util.Optional;
3131
import org.junit.Before;
3232
import org.junit.Rule;
3333
import org.junit.Test;
@@ -55,7 +55,7 @@ public void send_receiveOkStatus() throws Exception {
5555
S2AGrpcChannelPool.create(
5656
SharedResourcePool.forResource(
5757
S2AHandshakerServiceChannel.getChannelResource(
58-
S2A_ADDRESS, /* s2aChannelCredentials= */ Optional.empty())));
58+
S2A_ADDRESS, InsecureChannelCredentials.create())));
5959
S2AServiceGrpc.S2AServiceStub serviceStub = S2AServiceGrpc.newStub(channelPool.getChannel());
6060
S2AStub newStub = S2AStub.newInstance(serviceStub);
6161

0 commit comments

Comments
 (0)