forked from grpc/grpc-java
/
SdsProtocolNegotiators.java
454 lines (391 loc) · 16.6 KB
/
SdsProtocolNegotiators.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
* Copyright 2019 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.xds.internal.sds;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.Attributes;
import io.grpc.internal.GrpcUtil;
import io.grpc.internal.ObjectPool;
import io.grpc.netty.GrpcHttp2ConnectionHandler;
import io.grpc.netty.InternalNettyChannelBuilder;
import io.grpc.netty.InternalNettyChannelBuilder.ProtocolNegotiatorFactory;
import io.grpc.netty.InternalProtocolNegotiationEvent;
import io.grpc.netty.InternalProtocolNegotiator;
import io.grpc.netty.InternalProtocolNegotiator.ProtocolNegotiator;
import io.grpc.netty.InternalProtocolNegotiators;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.netty.ProtocolNegotiationEvent;
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
import io.grpc.xds.XdsAttributes;
import io.grpc.xds.XdsClientWrapperForServerSds;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.ssl.SslContext;
import io.netty.util.AsciiString;
import java.security.cert.CertStoreException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
* Provides client and server side gRPC {@link ProtocolNegotiator}s that use SDS to provide the SSL
* context.
*/
@VisibleForTesting
public final class SdsProtocolNegotiators {
// Prevent instantiation.
private SdsProtocolNegotiators() {
}
private static final Logger logger = Logger.getLogger(SdsProtocolNegotiators.class.getName());
public static final Attributes.Key<XdsClientWrapperForServerSds> SERVER_XDS_CLIENT
= Attributes.Key.create("serverXdsClient");
private static final AsciiString SCHEME = AsciiString.of("http");
/**
* Returns a {@link ProtocolNegotiatorFactory} to be used on {@link NettyChannelBuilder}.
*
* @param fallbackNegotiator protocol negotiator to use as fallback.
*/
public static ProtocolNegotiatorFactory clientProtocolNegotiatorFactory(
@Nullable ProtocolNegotiator fallbackNegotiator) {
return new ClientSdsProtocolNegotiatorFactory(fallbackNegotiator);
}
/**
* Returns a {@link InternalProtocolNegotiator.ClientFactory} to be used on {@link
* NettyChannelBuilder}.
*
* @param fallbackNegotiator protocol negotiator to use as fallback.
*/
public static InternalProtocolNegotiator.ClientFactory clientProtocolNegotiatorFactory(
@Nullable InternalProtocolNegotiator.ClientFactory fallbackNegotiator) {
return new ClientFactory(fallbackNegotiator);
}
public static InternalProtocolNegotiator.ServerFactory serverProtocolNegotiatorFactory(
@Nullable InternalProtocolNegotiator.ServerFactory fallbackNegotiator) {
return new ServerFactory(fallbackNegotiator);
}
/**
* Creates an SDS based {@link ProtocolNegotiator} for a {@link io.grpc.netty.NettyServerBuilder}.
* If xDS returns no DownstreamTlsContext, it will fall back to plaintext.
*
* @param fallbackProtocolNegotiator protocol negotiator to use as fallback.
*/
public static ServerSdsProtocolNegotiator serverProtocolNegotiator(
@Nullable ProtocolNegotiator fallbackProtocolNegotiator) {
return new ServerSdsProtocolNegotiator(fallbackProtocolNegotiator);
}
private static final class ServerFactory implements InternalProtocolNegotiator.ServerFactory {
private final InternalProtocolNegotiator.ServerFactory fallbackProtocolNegotiator;
private ServerFactory(InternalProtocolNegotiator.ServerFactory fallbackNegotiator) {
this.fallbackProtocolNegotiator = fallbackNegotiator;
}
@Override
public ProtocolNegotiator newNegotiator(ObjectPool<? extends Executor> offloadExecutorPool) {
return new ServerSdsProtocolNegotiator(
fallbackProtocolNegotiator.newNegotiator(offloadExecutorPool));
}
}
private static final class ClientFactory implements InternalProtocolNegotiator.ClientFactory {
private final InternalProtocolNegotiator.ClientFactory fallbackProtocolNegotiator;
private ClientFactory(InternalProtocolNegotiator.ClientFactory fallbackNegotiator) {
this.fallbackProtocolNegotiator = fallbackNegotiator;
}
@Override
public ProtocolNegotiator newNegotiator() {
return new ClientSdsProtocolNegotiator(fallbackProtocolNegotiator.newNegotiator());
}
@Override
public int getDefaultPort() {
return GrpcUtil.DEFAULT_PORT_SSL;
}
}
private static final class ClientSdsProtocolNegotiatorFactory
implements InternalNettyChannelBuilder.ProtocolNegotiatorFactory {
private final ProtocolNegotiator fallbackProtocolNegotiator;
private ClientSdsProtocolNegotiatorFactory(ProtocolNegotiator fallbackNegotiator) {
this.fallbackProtocolNegotiator = fallbackNegotiator;
}
@Override
public InternalProtocolNegotiator.ProtocolNegotiator buildProtocolNegotiator() {
final ClientSdsProtocolNegotiator negotiator =
new ClientSdsProtocolNegotiator(fallbackProtocolNegotiator);
final class LocalSdsNegotiator implements InternalProtocolNegotiator.ProtocolNegotiator {
@Override
public AsciiString scheme() {
return negotiator.scheme();
}
@Override
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
return negotiator.newHandler(grpcHandler);
}
@Override
public void close() {
negotiator.close();
}
}
return new LocalSdsNegotiator();
}
}
@VisibleForTesting
static final class ClientSdsProtocolNegotiator implements ProtocolNegotiator {
@Nullable private final ProtocolNegotiator fallbackProtocolNegotiator;
ClientSdsProtocolNegotiator(@Nullable ProtocolNegotiator fallbackProtocolNegotiator) {
this.fallbackProtocolNegotiator = fallbackProtocolNegotiator;
}
@Override
public AsciiString scheme() {
return SCHEME;
}
@Override
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
// check if SslContextProviderSupplier was passed via attributes
SslContextProviderSupplier localSslContextProviderSupplier =
grpcHandler.getEagAttributes().get(XdsAttributes.ATTR_SSL_CONTEXT_PROVIDER_SUPPLIER);
if (localSslContextProviderSupplier == null) {
checkNotNull(
fallbackProtocolNegotiator, "No TLS config and no fallbackProtocolNegotiator!");
return fallbackProtocolNegotiator.newHandler(grpcHandler);
}
return new ClientSdsHandler(grpcHandler, localSslContextProviderSupplier);
}
@Override
public void close() {}
}
private static class BufferReadsHandler extends ChannelInboundHandlerAdapter {
private final List<Object> reads = new ArrayList<>();
private boolean readComplete;
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
reads.add(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
readComplete = true;
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
for (Object msg : reads) {
super.channelRead(ctx, msg);
}
if (readComplete) {
super.channelReadComplete(ctx);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
logger.log(Level.SEVERE, "exceptionCaught", cause);
ctx.fireExceptionCaught(cause);
}
}
@VisibleForTesting
static final class ClientSdsHandler
extends InternalProtocolNegotiators.ProtocolNegotiationHandler {
private final GrpcHttp2ConnectionHandler grpcHandler;
private final SslContextProviderSupplier sslContextProviderSupplier;
ClientSdsHandler(
GrpcHttp2ConnectionHandler grpcHandler,
SslContextProviderSupplier sslContextProviderSupplier) {
super(
// superclass (InternalProtocolNegotiators.ProtocolNegotiationHandler) expects 'next'
// handler but we don't have a next handler _yet_. So we "disable" superclass's behavior
// here and then manually add 'next' when we call fireProtocolNegotiationEvent()
new ChannelHandlerAdapter() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
ctx.pipeline().remove(this);
}
});
checkNotNull(grpcHandler, "grpcHandler");
this.grpcHandler = grpcHandler;
this.sslContextProviderSupplier = sslContextProviderSupplier;
}
@Override
protected void handlerAdded0(final ChannelHandlerContext ctx) {
final BufferReadsHandler bufferReads = new BufferReadsHandler();
ctx.pipeline().addBefore(ctx.name(), null, bufferReads);
sslContextProviderSupplier.updateSslContext(
new SslContextProvider.Callback(ctx.executor()) {
@Override
public void updateSecret(SslContext sslContext) {
logger.log(
Level.FINEST,
"ClientSdsHandler.updateSecret authority={0}, ctx.name={1}",
new Object[]{grpcHandler.getAuthority(), ctx.name()});
ChannelHandler handler =
InternalProtocolNegotiators.tls(sslContext).newHandler(grpcHandler);
// Delegate rest of handshake to TLS handler
ctx.pipeline().addAfter(ctx.name(), null, handler);
fireProtocolNegotiationEvent(ctx);
ctx.pipeline().remove(bufferReads);
}
@Override
public void onException(Throwable throwable) {
ctx.fireExceptionCaught(throwable);
}
}
);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
logger.log(Level.SEVERE, "exceptionCaught", cause);
ctx.fireExceptionCaught(cause);
}
}
@VisibleForTesting
public static final class ServerSdsProtocolNegotiator implements ProtocolNegotiator {
@Nullable private final ProtocolNegotiator fallbackProtocolNegotiator;
/** Constructor. */
@VisibleForTesting
public ServerSdsProtocolNegotiator(@Nullable ProtocolNegotiator fallbackProtocolNegotiator) {
this.fallbackProtocolNegotiator = fallbackProtocolNegotiator;
}
@Override
public AsciiString scheme() {
return SCHEME;
}
@Override
public ChannelHandler newHandler(GrpcHttp2ConnectionHandler grpcHandler) {
XdsClientWrapperForServerSds xdsClientWrapperForServerSds =
grpcHandler.getEagAttributes().get(SERVER_XDS_CLIENT);
return new HandlerPickerHandler(grpcHandler, xdsClientWrapperForServerSds,
fallbackProtocolNegotiator);
}
@Override
public void close() {}
}
@VisibleForTesting
static final class HandlerPickerHandler
extends ChannelInboundHandlerAdapter {
private final GrpcHttp2ConnectionHandler grpcHandler;
private final XdsClientWrapperForServerSds xdsClientWrapperForServerSds;
@Nullable private final ProtocolNegotiator fallbackProtocolNegotiator;
HandlerPickerHandler(
GrpcHttp2ConnectionHandler grpcHandler,
@Nullable XdsClientWrapperForServerSds xdsClientWrapperForServerSds,
ProtocolNegotiator fallbackProtocolNegotiator) {
this.grpcHandler = checkNotNull(grpcHandler, "grpcHandler");
this.xdsClientWrapperForServerSds = xdsClientWrapperForServerSds;
this.fallbackProtocolNegotiator = fallbackProtocolNegotiator;
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof ProtocolNegotiationEvent) {
DownstreamTlsContext downstreamTlsContext =
xdsClientWrapperForServerSds == null
? null
: xdsClientWrapperForServerSds.getDownstreamTlsContext(ctx.channel());
if (downstreamTlsContext == null) {
if (fallbackProtocolNegotiator == null) {
ctx.fireExceptionCaught(new CertStoreException("No certificate source found!"));
return;
}
logger.log(Level.INFO, "Using fallback for {0}", ctx.channel().localAddress());
ctx.pipeline()
.replace(
this,
null,
fallbackProtocolNegotiator.newHandler(grpcHandler));
ProtocolNegotiationEvent pne = InternalProtocolNegotiationEvent.getDefault();
ctx.fireUserEventTriggered(pne);
return;
} else {
ctx.pipeline()
.replace(
this,
null,
new ServerSdsHandler(
grpcHandler, downstreamTlsContext, fallbackProtocolNegotiator));
ProtocolNegotiationEvent pne = InternalProtocolNegotiationEvent.getDefault();
ctx.fireUserEventTriggered(pne);
return;
}
} else {
super.userEventTriggered(ctx, evt);
}
}
}
@VisibleForTesting
static final class ServerSdsHandler
extends InternalProtocolNegotiators.ProtocolNegotiationHandler {
private final GrpcHttp2ConnectionHandler grpcHandler;
private final DownstreamTlsContext downstreamTlsContext;
@Nullable private final ProtocolNegotiator fallbackProtocolNegotiator;
ServerSdsHandler(
GrpcHttp2ConnectionHandler grpcHandler,
DownstreamTlsContext downstreamTlsContext,
ProtocolNegotiator fallbackProtocolNegotiator) {
super(
// superclass (InternalProtocolNegotiators.ProtocolNegotiationHandler) expects 'next'
// handler but we don't have a next handler _yet_. So we "disable" superclass's behavior
// here and then manually add 'next' when we call fireProtocolNegotiationEvent()
new ChannelHandlerAdapter() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
ctx.pipeline().remove(this);
}
});
checkNotNull(grpcHandler, "grpcHandler");
this.grpcHandler = grpcHandler;
this.downstreamTlsContext = downstreamTlsContext;
this.fallbackProtocolNegotiator = fallbackProtocolNegotiator;
}
@Override
protected void handlerAdded0(final ChannelHandlerContext ctx) {
final BufferReadsHandler bufferReads = new BufferReadsHandler();
ctx.pipeline().addBefore(ctx.name(), null, bufferReads);
SslContextProvider sslContextProviderTemp = null;
try {
sslContextProviderTemp =
TlsContextManagerImpl.getInstance()
.findOrCreateServerSslContextProvider(downstreamTlsContext);
} catch (Exception e) {
if (fallbackProtocolNegotiator == null) {
ctx.fireExceptionCaught(new CertStoreException("No certificate source found!", e));
return;
}
logger.log(Level.INFO, "Using fallback for {0}", ctx.channel().localAddress());
// Delegate rest of handshake to fallback handler
ctx.pipeline().replace(this, null, fallbackProtocolNegotiator.newHandler(grpcHandler));
ctx.pipeline().remove(bufferReads);
return;
}
final SslContextProvider sslContextProvider = sslContextProviderTemp;
sslContextProvider.addCallback(
new SslContextProvider.Callback(ctx.executor()) {
@Override
public void updateSecret(SslContext sslContext) {
ChannelHandler handler =
InternalProtocolNegotiators.serverTls(sslContext).newHandler(grpcHandler);
// Delegate rest of handshake to TLS handler
ctx.pipeline().addAfter(ctx.name(), null, handler);
fireProtocolNegotiationEvent(ctx);
ctx.pipeline().remove(bufferReads);
TlsContextManagerImpl.getInstance()
.releaseServerSslContextProvider(sslContextProvider);
}
@Override
public void onException(Throwable throwable) {
ctx.fireExceptionCaught(throwable);
}
}
);
}
}
}