• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

grpc / grpc-java / #19758

02 Apr 2025 04:40AM UTC coverage: 88.103% (-0.5%) from 88.593%
#19758

push

github

web-flow
okhttp: Per-rpc call option authority verification (#11754)

30021 of 34075 relevant lines covered (88.1%)

1.02 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.2
/../okhttp/src/main/java/io/grpc/okhttp/OkHttpServerBuilder.java
1
/*
2
 * Copyright 2022 The gRPC Authors
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package io.grpc.okhttp;
18

19
import static com.google.common.base.Preconditions.checkArgument;
20
import static io.grpc.internal.CertificateUtils.createTrustManager;
21

22
import com.google.common.base.Preconditions;
23
import com.google.errorprone.annotations.CanIgnoreReturnValue;
24
import com.google.errorprone.annotations.DoNotCall;
25
import io.grpc.ChoiceServerCredentials;
26
import io.grpc.ExperimentalApi;
27
import io.grpc.ForwardingServerBuilder;
28
import io.grpc.InsecureServerCredentials;
29
import io.grpc.Internal;
30
import io.grpc.ServerBuilder;
31
import io.grpc.ServerCredentials;
32
import io.grpc.ServerStreamTracer;
33
import io.grpc.TlsServerCredentials;
34
import io.grpc.internal.FixedObjectPool;
35
import io.grpc.internal.GrpcUtil;
36
import io.grpc.internal.InternalServer;
37
import io.grpc.internal.KeepAliveManager;
38
import io.grpc.internal.ObjectPool;
39
import io.grpc.internal.ServerImplBuilder;
40
import io.grpc.internal.SharedResourcePool;
41
import io.grpc.internal.TransportTracer;
42
import io.grpc.okhttp.internal.Platform;
43
import java.io.IOException;
44
import java.net.InetAddress;
45
import java.net.InetSocketAddress;
46
import java.net.Socket;
47
import java.net.SocketAddress;
48
import java.security.GeneralSecurityException;
49
import java.util.EnumSet;
50
import java.util.List;
51
import java.util.Set;
52
import java.util.concurrent.Executor;
53
import java.util.concurrent.ScheduledExecutorService;
54
import java.util.concurrent.TimeUnit;
55
import java.util.logging.Level;
56
import java.util.logging.Logger;
57
import javax.net.ServerSocketFactory;
58
import javax.net.ssl.KeyManager;
59
import javax.net.ssl.SSLContext;
60
import javax.net.ssl.SSLSocket;
61
import javax.net.ssl.SSLSocketFactory;
62
import javax.net.ssl.TrustManager;
63

64
/**
65
 * Build servers with the OkHttp transport.
66
 *
67
 * @since 1.49.0
68
 */
69
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1785")
70
public final class OkHttpServerBuilder extends ForwardingServerBuilder<OkHttpServerBuilder> {
71
  private static final Logger log = Logger.getLogger(OkHttpServerBuilder.class.getName());
1✔
72
  private static final int DEFAULT_FLOW_CONTROL_WINDOW = 65535;
73

74
  static final long MAX_CONNECTION_IDLE_NANOS_DISABLED = Long.MAX_VALUE;
75
  private static final long MIN_MAX_CONNECTION_IDLE_NANO = TimeUnit.SECONDS.toNanos(1L);
1✔
76
  static final long MAX_CONNECTION_AGE_NANOS_DISABLED = Long.MAX_VALUE;
77
  static final long MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE = Long.MAX_VALUE;
78
  static final int MAX_CONCURRENT_STREAMS = Integer.MAX_VALUE;
79
  private static final long MIN_MAX_CONNECTION_AGE_NANO = TimeUnit.SECONDS.toNanos(1L);
1✔
80

81
  private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000L);
1✔
82
  private static final ObjectPool<Executor> DEFAULT_TRANSPORT_EXECUTOR_POOL =
1✔
83
      OkHttpChannelBuilder.DEFAULT_TRANSPORT_EXECUTOR_POOL;
84

85
  /**
86
   * Always throws, to shadow {@code ServerBuilder.forPort()}.
87
   *
88
   * @deprecated Use {@link #forPort(int, ServerCredentials)} instead
89
   */
90
  @DoNotCall("Always throws. Use forPort(int, ServerCredentials) instead")
91
  @Deprecated
92
  public static OkHttpServerBuilder forPort(int port) {
93
    throw new UnsupportedOperationException("Use forPort(int, ServerCredentials) instead");
×
94
  }
95

96
  /**
97
   * Creates a builder for a server listening on {@code port}.
98
   */
99
  public static OkHttpServerBuilder forPort(int port, ServerCredentials creds) {
100
    return forPort(new InetSocketAddress(port), creds);
1✔
101
  }
102

103
  /**
104
   * Creates a builder for a server listening on {@code address}.
105
   */
106
  public static OkHttpServerBuilder forPort(SocketAddress address, ServerCredentials creds) {
107
    HandshakerSocketFactoryResult result = handshakerSocketFactoryFrom(creds);
1✔
108
    if (result.error != null) {
1✔
109
      throw new IllegalArgumentException(result.error);
×
110
    }
111
    return new OkHttpServerBuilder(address, result.factory);
1✔
112
  }
113

114
  final ServerImplBuilder serverImplBuilder = new ServerImplBuilder(this::buildTransportServers);
1✔
115
  final SocketAddress listenAddress;
116
  final HandshakerSocketFactory handshakerSocketFactory;
117
  TransportTracer.Factory transportTracerFactory = TransportTracer.getDefaultFactory();
1✔
118

119
  ObjectPool<Executor> transportExecutorPool = DEFAULT_TRANSPORT_EXECUTOR_POOL;
1✔
120
  ObjectPool<ScheduledExecutorService> scheduledExecutorServicePool =
1✔
121
      SharedResourcePool.forResource(GrpcUtil.TIMER_SERVICE);
1✔
122

123
  ServerSocketFactory socketFactory = ServerSocketFactory.getDefault();
1✔
124
  long keepAliveTimeNanos = GrpcUtil.DEFAULT_SERVER_KEEPALIVE_TIME_NANOS;
1✔
125
  long keepAliveTimeoutNanos = GrpcUtil.DEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOS;
1✔
126
  int flowControlWindow = DEFAULT_FLOW_CONTROL_WINDOW;
1✔
127
  int maxInboundMetadataSize = GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE;
1✔
128
  int maxInboundMessageSize = GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
1✔
129
  long maxConnectionIdleInNanos = MAX_CONNECTION_IDLE_NANOS_DISABLED;
1✔
130
  boolean permitKeepAliveWithoutCalls;
131
  long permitKeepAliveTimeInNanos = TimeUnit.MINUTES.toNanos(5);
1✔
132
  long maxConnectionAgeInNanos = MAX_CONNECTION_AGE_NANOS_DISABLED;
1✔
133
  long maxConnectionAgeGraceInNanos = MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE;
1✔
134
  int maxConcurrentCallsPerConnection = MAX_CONCURRENT_STREAMS;
1✔
135

136
  OkHttpServerBuilder(
137
      SocketAddress address, HandshakerSocketFactory handshakerSocketFactory) {
1✔
138
    this.listenAddress = Preconditions.checkNotNull(address, "address");
1✔
139
    this.handshakerSocketFactory =
1✔
140
        Preconditions.checkNotNull(handshakerSocketFactory, "handshakerSocketFactory");
1✔
141
  }
1✔
142

143
  @Internal
144
  @Override
145
  protected ServerBuilder<?> delegate() {
146
    return serverImplBuilder;
1✔
147
  }
148

149
  // @VisibleForTesting
150
  OkHttpServerBuilder setTransportTracerFactory(TransportTracer.Factory transportTracerFactory) {
151
    this.transportTracerFactory = transportTracerFactory;
1✔
152
    return this;
1✔
153
  }
154

155
  /**
156
   * Override the default executor necessary for internal transport use.
157
   *
158
   * <p>The channel does not take ownership of the given executor. It is the caller' responsibility
159
   * to shutdown the executor when appropriate.
160
   */
161
  public OkHttpServerBuilder transportExecutor(Executor transportExecutor) {
162
    if (transportExecutor == null) {
1✔
163
      this.transportExecutorPool = DEFAULT_TRANSPORT_EXECUTOR_POOL;
×
164
    } else {
165
      this.transportExecutorPool = new FixedObjectPool<>(transportExecutor);
1✔
166
    }
167
    return this;
1✔
168
  }
169

170
  /**
171
   * Override the default {@link ServerSocketFactory} used to listen. If the socket factory is not
172
   * set or set to null, a default one will be used.
173
   */
174
  public OkHttpServerBuilder socketFactory(ServerSocketFactory socketFactory) {
175
    if (socketFactory == null) {
×
176
      this.socketFactory = ServerSocketFactory.getDefault();
×
177
    } else {
178
      this.socketFactory = socketFactory;
×
179
    }
180
    return this;
×
181
  }
182

183
  /**
184
   * Sets the time without read activity before sending a keepalive ping. An unreasonably small
185
   * value might be increased, and {@code Long.MAX_VALUE} nano seconds or an unreasonably large
186
   * value will disable keepalive. Defaults to two hours.
187
   *
188
   * @throws IllegalArgumentException if time is not positive
189
   */
190
  @Override
191
  public OkHttpServerBuilder keepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
192
    Preconditions.checkArgument(keepAliveTime > 0L, "keepalive time must be positive");
×
193
    keepAliveTimeNanos = timeUnit.toNanos(keepAliveTime);
×
194
    keepAliveTimeNanos = KeepAliveManager.clampKeepAliveTimeInNanos(keepAliveTimeNanos);
×
195
    if (keepAliveTimeNanos >= AS_LARGE_AS_INFINITE) {
×
196
      // Bump keepalive time to infinite. This disables keepalive.
197
      keepAliveTimeNanos = GrpcUtil.KEEPALIVE_TIME_NANOS_DISABLED;
×
198
    }
199
    return this;
×
200
  }
201

202
  /**
203
   * Sets a custom max connection idle time, connection being idle for longer than which will be
204
   * gracefully terminated. Idleness duration is defined since the most recent time the number of
205
   * outstanding RPCs became zero or the connection establishment. An unreasonably small value might
206
   * be increased. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
207
   * max connection idle.
208
   */
209
  @Override
210
  public OkHttpServerBuilder maxConnectionIdle(long maxConnectionIdle, TimeUnit timeUnit) {
211
    checkArgument(maxConnectionIdle > 0L, "max connection idle must be positive: %s",
1✔
212
        maxConnectionIdle);
213
    maxConnectionIdleInNanos = timeUnit.toNanos(maxConnectionIdle);
1✔
214
    if (maxConnectionIdleInNanos >= AS_LARGE_AS_INFINITE) {
1✔
215
      maxConnectionIdleInNanos = MAX_CONNECTION_IDLE_NANOS_DISABLED;
×
216
    }
217
    if (maxConnectionIdleInNanos < MIN_MAX_CONNECTION_IDLE_NANO) {
1✔
218
      maxConnectionIdleInNanos = MIN_MAX_CONNECTION_IDLE_NANO;
×
219
    }
220
    return this;
1✔
221
  }
222

223
  /**
224
   * Sets a custom max connection age, connection lasting longer than which will be gracefully
225
   * terminated. An unreasonably small value might be increased.  A random jitter of +/-10% will be
226
   * added to it. {@code Long.MAX_VALUE} nano seconds or an unreasonably large value will disable
227
   * max connection age.
228
   */
229
  @Override
230
  public OkHttpServerBuilder maxConnectionAge(long maxConnectionAge, TimeUnit timeUnit) {
231
    checkArgument(maxConnectionAge > 0L, "max connection age must be positive: %s",
1✔
232
        maxConnectionAge);
233
    maxConnectionAgeInNanos = timeUnit.toNanos(maxConnectionAge);
1✔
234
    if (maxConnectionAgeInNanos >= AS_LARGE_AS_INFINITE) {
1✔
235
      maxConnectionAgeInNanos = MAX_CONNECTION_AGE_NANOS_DISABLED;
×
236
    }
237
    if (maxConnectionAgeInNanos < MIN_MAX_CONNECTION_AGE_NANO) {
1✔
238
      maxConnectionAgeInNanos = MIN_MAX_CONNECTION_AGE_NANO;
×
239
    }
240
    return this;
1✔
241
  }
242

243
  /**
244
   * Sets a custom grace time for the graceful connection termination. Once the max connection age
245
   * is reached, RPCs have the grace time to complete. RPCs that do not complete in time will be
246
   * cancelled, allowing the connection to terminate. {@code Long.MAX_VALUE} nano seconds or an
247
   * unreasonably large value are considered infinite.
248
   *
249
   * @see #maxConnectionAge(long, TimeUnit)
250
   */
251
  @Override
252
  public OkHttpServerBuilder maxConnectionAgeGrace(long maxConnectionAgeGrace, TimeUnit timeUnit) {
253
    checkArgument(maxConnectionAgeGrace >= 0L, "max connection age grace must be non-negative: %s",
1✔
254
        maxConnectionAgeGrace);
255
    maxConnectionAgeGraceInNanos = timeUnit.toNanos(maxConnectionAgeGrace);
1✔
256
    if (maxConnectionAgeGraceInNanos >= AS_LARGE_AS_INFINITE) {
1✔
257
      maxConnectionAgeGraceInNanos = MAX_CONNECTION_AGE_GRACE_NANOS_INFINITE;
×
258
    }
259
    return this;
1✔
260
  }
261

262
  /**
263
   * Sets a time waiting for read activity after sending a keepalive ping. If the time expires
264
   * without any read activity on the connection, the connection is considered dead. An unreasonably
265
   * small value might be increased. Defaults to 20 seconds.
266
   *
267
   * <p>This value should be at least multiple times the RTT to allow for lost packets.
268
   *
269
   * @throws IllegalArgumentException if timeout is not positive
270
   */
271
  @Override
272
  public OkHttpServerBuilder keepAliveTimeout(long keepAliveTimeout, TimeUnit timeUnit) {
273
    Preconditions.checkArgument(keepAliveTimeout > 0L, "keepalive timeout must be positive");
×
274
    keepAliveTimeoutNanos = timeUnit.toNanos(keepAliveTimeout);
×
275
    keepAliveTimeoutNanos = KeepAliveManager.clampKeepAliveTimeoutInNanos(keepAliveTimeoutNanos);
×
276
    return this;
×
277
  }
278

279
  /**
280
   * Specify the most aggressive keep-alive time clients are permitted to configure. The server will
281
   * try to detect clients exceeding this rate and when detected will forcefully close the
282
   * connection. The default is 5 minutes.
283
   *
284
   * <p>Even though a default is defined that allows some keep-alives, clients must not use
285
   * keep-alive without approval from the service owner. Otherwise, they may experience failures in
286
   * the future if the service becomes more restrictive. When unthrottled, keep-alives can cause a
287
   * significant amount of traffic and CPU usage, so clients and servers should be conservative in
288
   * what they use and accept.
289
   *
290
   * @see #permitKeepAliveWithoutCalls(boolean)
291
   */
292
  @CanIgnoreReturnValue
293
  @Override
294
  public OkHttpServerBuilder permitKeepAliveTime(long keepAliveTime, TimeUnit timeUnit) {
295
    checkArgument(keepAliveTime >= 0, "permit keepalive time must be non-negative: %s",
1✔
296
        keepAliveTime);
297
    permitKeepAliveTimeInNanos = timeUnit.toNanos(keepAliveTime);
1✔
298
    return this;
1✔
299
  }
300

301
  /**
302
   * Sets whether to allow clients to send keep-alive HTTP/2 PINGs even if there are no outstanding
303
   * RPCs on the connection. Defaults to {@code false}.
304
   *
305
   * @see #permitKeepAliveTime(long, TimeUnit)
306
   */
307
  @CanIgnoreReturnValue
308
  @Override
309
  public OkHttpServerBuilder permitKeepAliveWithoutCalls(boolean permit) {
310
    permitKeepAliveWithoutCalls = permit;
1✔
311
    return this;
1✔
312
  }
313

314
  /**
315
   * Sets the flow control window in bytes. If not called, the default value is 64 KiB.
316
   */
317
  public OkHttpServerBuilder flowControlWindow(int flowControlWindow) {
318
    Preconditions.checkState(flowControlWindow > 0, "flowControlWindow must be positive");
1✔
319
    this.flowControlWindow = flowControlWindow;
1✔
320
    return this;
1✔
321
  }
322

323
  /**
324
   * Provides a custom scheduled executor service.
325
   *
326
   * <p>It's an optional parameter. If the user has not provided a scheduled executor service when
327
   * the channel is built, the builder will use a static thread pool.
328
   *
329
   * @return this
330
   */
331
  public OkHttpServerBuilder scheduledExecutorService(
332
      ScheduledExecutorService scheduledExecutorService) {
333
    this.scheduledExecutorServicePool = new FixedObjectPool<>(
1✔
334
        Preconditions.checkNotNull(scheduledExecutorService, "scheduledExecutorService"));
1✔
335
    return this;
1✔
336
  }
337

338
  /**
339
   * Sets the maximum size of metadata allowed to be received. Defaults to 8 KiB.
340
   *
341
   * <p>The implementation does not currently limit memory usage; this value is checked only after
342
   * the metadata is decoded from the wire. It does prevent large metadata from being passed to the
343
   * application.
344
   *
345
   * @param bytes the maximum size of received metadata
346
   * @return this
347
   * @throws IllegalArgumentException if bytes is non-positive
348
   */
349
  @Override
350
  public OkHttpServerBuilder maxInboundMetadataSize(int bytes) {
351
    Preconditions.checkArgument(bytes > 0, "maxInboundMetadataSize must be > 0");
×
352
    this.maxInboundMetadataSize = bytes;
×
353
    return this;
×
354
  }
355

356
  /**
357
   * The maximum number of concurrent calls permitted for each incoming connection. Defaults to no
358
   * limit.
359
   */
360
  @CanIgnoreReturnValue
361
  public OkHttpServerBuilder maxConcurrentCallsPerConnection(int maxConcurrentCallsPerConnection) {
362
    checkArgument(maxConcurrentCallsPerConnection > 0,
1✔
363
        "max must be positive: %s", maxConcurrentCallsPerConnection);
364
    this.maxConcurrentCallsPerConnection = maxConcurrentCallsPerConnection;
1✔
365
    return this;
1✔
366
  }
367

368
  /**
369
   * Sets the maximum message size allowed to be received on the server. If not called, defaults to
370
   * defaults to 4 MiB. The default provides protection to servers who haven't considered the
371
   * possibility of receiving large messages while trying to be large enough to not be hit in normal
372
   * usage.
373
   *
374
   * @param bytes the maximum number of bytes a single message can be.
375
   * @return this
376
   * @throws IllegalArgumentException if bytes is negative.
377
   */
378
  @Override
379
  public OkHttpServerBuilder maxInboundMessageSize(int bytes) {
380
    Preconditions.checkArgument(bytes >= 0, "negative max bytes");
1✔
381
    maxInboundMessageSize = bytes;
1✔
382
    return this;
1✔
383
  }
384

385
  void setStatsEnabled(boolean value) {
386
    this.serverImplBuilder.setStatsEnabled(value);
1✔
387
  }
1✔
388

389
  InternalServer buildTransportServers(
390
      List<? extends ServerStreamTracer.Factory> streamTracerFactories) {
391
    return new OkHttpServer(this, streamTracerFactories, serverImplBuilder.getChannelz());
1✔
392
  }
393

394
  private static final EnumSet<TlsServerCredentials.Feature> understoodTlsFeatures =
1✔
395
      EnumSet.of(
1✔
396
          TlsServerCredentials.Feature.MTLS, TlsServerCredentials.Feature.CUSTOM_MANAGERS);
397

398
  static HandshakerSocketFactoryResult handshakerSocketFactoryFrom(ServerCredentials creds) {
399
    if (creds instanceof TlsServerCredentials) {
1✔
400
      TlsServerCredentials tlsCreds = (TlsServerCredentials) creds;
1✔
401
      Set<TlsServerCredentials.Feature> incomprehensible =
1✔
402
          tlsCreds.incomprehensible(understoodTlsFeatures);
1✔
403
      if (!incomprehensible.isEmpty()) {
1✔
404
        return HandshakerSocketFactoryResult.error(
×
405
            "TLS features not understood: " + incomprehensible);
406
      }
407
      KeyManager[] km = null;
1✔
408
      if (tlsCreds.getKeyManagers() != null) {
1✔
409
        km = tlsCreds.getKeyManagers().toArray(new KeyManager[0]);
×
410
      } else if (tlsCreds.getPrivateKey() != null) {
1✔
411
        if (tlsCreds.getPrivateKeyPassword() != null) {
1✔
412
          return HandshakerSocketFactoryResult.error("byte[]-based private key with password "
×
413
              + "unsupported. Use unencrypted file or KeyManager");
414
        }
415
        try {
416
          km = OkHttpChannelBuilder.createKeyManager(
1✔
417
              tlsCreds.getCertificateChain(), tlsCreds.getPrivateKey());
1✔
418
        } catch (GeneralSecurityException gse) {
×
419
          log.log(Level.FINE, "Exception loading private key from credential", gse);
×
420
          return HandshakerSocketFactoryResult.error(
×
421
              "Unable to load private key: " + gse.getMessage());
×
422
        }
1✔
423
      } // else don't have a client cert
424
      TrustManager[] tm = null;
1✔
425
      if (tlsCreds.getTrustManagers() != null) {
1✔
426
        tm = tlsCreds.getTrustManagers().toArray(new TrustManager[0]);
×
427
      } else if (tlsCreds.getRootCertificates() != null) {
1✔
428
        try {
429
          tm = createTrustManager(tlsCreds.getRootCertificates());
1✔
430
        } catch (GeneralSecurityException gse) {
×
431
          log.log(Level.FINE, "Exception loading root certificates from credential", gse);
×
432
          return HandshakerSocketFactoryResult.error(
×
433
              "Unable to load root certificates: " + gse.getMessage());
×
434
        }
1✔
435
      } // else use system default
436
      SSLContext sslContext;
437
      try {
438
        sslContext = SSLContext.getInstance("TLS", Platform.get().getProvider());
1✔
439
        sslContext.init(km, tm, null);
1✔
440
      } catch (GeneralSecurityException gse) {
×
441
        throw new RuntimeException("TLS Provider failure", gse);
×
442
      }
1✔
443
      SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
1✔
444
      switch (tlsCreds.getClientAuth()) {
1✔
445
        case OPTIONAL:
446
          sslSocketFactory = new ClientCertRequestingSocketFactory(sslSocketFactory, false);
1✔
447
          break;
1✔
448

449
        case REQUIRE:
450
          sslSocketFactory = new ClientCertRequestingSocketFactory(sslSocketFactory, true);
1✔
451
          break;
1✔
452

453
        case NONE:
454
          // NOOP; this is the SSLContext default
455
          break;
1✔
456

457
        default:
458
          return HandshakerSocketFactoryResult.error(
×
459
              "Unknown TlsServerCredentials.ClientAuth value: " + tlsCreds.getClientAuth());
×
460
      }
461
      return HandshakerSocketFactoryResult.factory(new TlsServerHandshakerSocketFactory(
1✔
462
          new SslSocketFactoryServerCredentials.ServerCredentials(sslSocketFactory)));
463

464
    } else if (creds instanceof InsecureServerCredentials) {
1✔
465
      return HandshakerSocketFactoryResult.factory(new PlaintextHandshakerSocketFactory());
1✔
466

467
    } else if (creds instanceof SslSocketFactoryServerCredentials.ServerCredentials) {
1✔
468
      SslSocketFactoryServerCredentials.ServerCredentials factoryCreds =
×
469
          (SslSocketFactoryServerCredentials.ServerCredentials) creds;
470
      return HandshakerSocketFactoryResult.factory(
×
471
          new TlsServerHandshakerSocketFactory(factoryCreds));
472

473
    } else if (creds instanceof ChoiceServerCredentials) {
1✔
474
      ChoiceServerCredentials choiceCreds = (ChoiceServerCredentials) creds;
×
475
      StringBuilder error = new StringBuilder();
×
476
      for (ServerCredentials innerCreds : choiceCreds.getCredentialsList()) {
×
477
        HandshakerSocketFactoryResult result = handshakerSocketFactoryFrom(innerCreds);
×
478
        if (result.error == null) {
×
479
          return result;
×
480
        }
481
        error.append(", ");
×
482
        error.append(result.error);
×
483
      }
×
484
      return HandshakerSocketFactoryResult.error(error.substring(2));
×
485

486
    } else {
487
      return HandshakerSocketFactoryResult.error(
1✔
488
          "Unsupported credential type: " + creds.getClass().getName());
1✔
489
    }
490
  }
491

492
  static final class HandshakerSocketFactoryResult {
493
    public final HandshakerSocketFactory factory;
494
    public final String error;
495

496
    private HandshakerSocketFactoryResult(HandshakerSocketFactory factory, String error) {
1✔
497
      this.factory = factory;
1✔
498
      this.error = error;
1✔
499
    }
1✔
500

501
    public static HandshakerSocketFactoryResult error(String error) {
502
      return new HandshakerSocketFactoryResult(
1✔
503
          null, Preconditions.checkNotNull(error, "error"));
1✔
504
    }
505

506
    public static HandshakerSocketFactoryResult factory(HandshakerSocketFactory factory) {
507
      return new HandshakerSocketFactoryResult(
1✔
508
          Preconditions.checkNotNull(factory, "factory"), null);
1✔
509
    }
510
  }
511

512
  static final class ClientCertRequestingSocketFactory extends SSLSocketFactory {
513
    private final SSLSocketFactory socketFactory;
514
    private final boolean required;
515

516
    public ClientCertRequestingSocketFactory(SSLSocketFactory socketFactory, boolean required) {
1✔
517
      this.socketFactory = Preconditions.checkNotNull(socketFactory, "socketFactory");
1✔
518
      this.required = required;
1✔
519
    }
1✔
520

521
    private Socket apply(Socket s) throws IOException {
522
      if (!(s instanceof SSLSocket)) {
1✔
523
        throw new IOException(
×
524
            "SocketFactory " + socketFactory + " did not produce an SSLSocket: " + s.getClass());
×
525
      }
526
      SSLSocket sslSocket = (SSLSocket) s;
1✔
527
      if (required) {
1✔
528
        sslSocket.setNeedClientAuth(true);
1✔
529
      } else {
530
        sslSocket.setWantClientAuth(true);
1✔
531
      }
532
      return sslSocket;
1✔
533
    }
534

535
    @Override public Socket createSocket(Socket s, String host, int port, boolean autoClose)
536
        throws IOException {
537
      return apply(socketFactory.createSocket(s, host, port, autoClose));
1✔
538
    }
539

540
    @Override public Socket createSocket(String host, int port) throws IOException {
541
      return apply(socketFactory.createSocket(host, port));
×
542
    }
543

544
    @Override public Socket createSocket(
545
        String host, int port, InetAddress localHost, int localPort) throws IOException {
546
      return apply(socketFactory.createSocket(host, port, localHost, localPort));
×
547
    }
548

549
    @Override public Socket createSocket(InetAddress host, int port) throws IOException {
550
      return apply(socketFactory.createSocket(host, port));
×
551
    }
552

553
    @Override public Socket createSocket(
554
        InetAddress host, int port, InetAddress localAddress, int localPort) throws IOException {
555
      return apply(socketFactory.createSocket(host, port, localAddress, localPort));
×
556
    }
557

558
    @Override public String[] getDefaultCipherSuites() {
559
      return socketFactory.getDefaultCipherSuites();
×
560
    }
561

562
    @Override public String[] getSupportedCipherSuites() {
563
      return socketFactory.getSupportedCipherSuites();
×
564
    }
565
  }
566
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc