Skip to content

Commit f75d6e5

Browse files
committed
GH-155: Delegate opening mTLS cert files to build()
1 parent 1795832 commit f75d6e5

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

flight/flight-core/src/main/java/org/apache/arrow/flight/FlightServer.java

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,18 @@ public static final class Builder {
190190
private int maxInboundMessageSize = MAX_GRPC_MESSAGE_SIZE;
191191
private int maxHeaderListSize = MAX_GRPC_MESSAGE_SIZE;
192192
private int backpressureThreshold = DEFAULT_BACKPRESSURE_THRESHOLD;
193+
194+
/*
195+
TODO: Remove certChain/key/mTlsCACert as instance vars and make them local vars in build() after they are
196+
no longer being set in the builder methods.
197+
*/
198+
private File certChainFile;
193199
private InputStream certChain;
200+
private File keyFile;
194201
private InputStream key;
202+
private File mTlsCACertFile;
195203
private InputStream mTlsCACert;
204+
196205
private SslContext sslContext;
197206
private final List<KeyFactory<?>> interceptors;
198207
// Keep track of inserted interceptors
@@ -213,6 +222,16 @@ public static final class Builder {
213222

214223
/** Create the server for this builder. */
215224
public FlightServer build() {
225+
// Get TLS info in order if the server is being configured to use mTLS.
226+
try {
227+
prepareTlsSettings();
228+
} catch (IOException e) {
229+
closeMTlsCACert();
230+
closeCertChain();
231+
closeKey();
232+
throw new RuntimeException("Could not create FlightServer with mTLS", e);
233+
}
234+
216235
// Add the auth middleware if applicable.
217236
if (headerAuthenticator != CallHeaderAuthenticator.NO_OP) {
218237
this.middleware(
@@ -442,11 +461,8 @@ private void closeMTlsCACert() {
442461
* @param key The private key to use.
443462
*/
444463
public Builder useTls(final File certChain, final File key) throws IOException {
445-
closeCertChain();
446-
this.certChain = new FileInputStream(certChain);
447-
448-
closeKey();
449-
this.key = new FileInputStream(key);
464+
this.certChainFile = certChain;
465+
this.keyFile = key;
450466

451467
return this;
452468
}
@@ -457,8 +473,8 @@ public Builder useTls(final File certChain, final File key) throws IOException {
457473
* @param mTlsCACert The CA certificate to use for verifying clients.
458474
*/
459475
public Builder useMTlsClientVerification(final File mTlsCACert) throws IOException {
460-
closeMTlsCACert();
461-
this.mTlsCACert = new FileInputStream(mTlsCACert);
476+
this.mTlsCACertFile = mTlsCACert;
477+
462478
return this;
463479
}
464480

@@ -468,6 +484,7 @@ public Builder useMTlsClientVerification(final File mTlsCACert) throws IOExcepti
468484
* @param certChain The certificate chain to use.
469485
* @param key The private key to use.
470486
*/
487+
@Deprecated(forRemoval = true, since = "18.4.0")
471488
public Builder useTls(final InputStream certChain, final InputStream key) throws IOException {
472489
closeCertChain();
473490
this.certChain = certChain;
@@ -483,6 +500,7 @@ public Builder useTls(final InputStream certChain, final InputStream key) throws
483500
*
484501
* @param mTlsCACert The CA certificate to use for verifying clients.
485502
*/
503+
@Deprecated(forRemoval = true, since = "18.4.0")
486504
public Builder useMTlsClientVerification(final InputStream mTlsCACert) throws IOException {
487505
closeMTlsCACert();
488506
this.mTlsCACert = mTlsCACert;
@@ -552,5 +570,20 @@ public Builder producer(FlightProducer producer) {
552570
this.producer = Preconditions.checkNotNull(producer);
553571
return this;
554572
}
573+
574+
private void prepareTlsSettings() throws IOException {
575+
if (this.keyFile != null) {
576+
closeKey();
577+
this.key = new FileInputStream(this.keyFile);
578+
}
579+
if (this.certChainFile != null) {
580+
closeCertChain();
581+
this.certChain = new FileInputStream(this.certChainFile);
582+
}
583+
if (this.mTlsCACertFile != null) {
584+
closeMTlsCACert();
585+
this.mTlsCACert = new FileInputStream(this.mTlsCACertFile);
586+
}
587+
}
555588
}
556589
}

0 commit comments

Comments
 (0)