Skip to content

Add support for mTLS authentication in Arrow Flight client #25179

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

elbinpallimalilibm
Copy link
Contributor

Description

Add support for mTLS authentication in Arrow Flight client

Motivation and Context

If the Flight server has mTLS authentication enabled, then the Flight client should be able to use client certificate and key.

Impact

Test Plan

Added positive and negative test cases against an mTLS enabled Flight server.

Contributor checklist

  • Please make sure your submission complies with our contributing guide, in particular code style and commit standards.
  • PR description addresses the issue accurately and concisely. If the change is non-trivial, a GitHub Issue is referenced.
  • Documented new properties (with its default value), SQL syntax, functions, or other functionality.
  • If release notes are required, they follow the release notes guidelines.
  • Adequate tests were added if applicable.
  • CI passed.

Release Notes

Please follow release notes guidelines and fill in the release notes below.

== RELEASE NOTES ==

Arrow Flight Connector Template Changes
* Added support for mTLS authentication in Arrow Flight client.

@prestodb-ci prestodb-ci added the from:IBM PR from IBM label May 23, 2025
@prestodb-ci prestodb-ci requested review from a team, bibith4 and pratyakshsharma and removed request for a team May 23, 2025 02:50
@elbinpallimalilibm elbinpallimalilibm marked this pull request as draft May 23, 2025 02:58
@elbinpallimalilibm
Copy link
Contributor Author

elbinpallimalilibm commented May 23, 2025

https://github.com/prestodb/presto/actions/runs/15201290385/job/42755765576?pr=25179 @steveburnett I'm not able to figure out why the presto-docs check is failing. Can you help here?

Edit : Waiting for fix to be merged here.

@steveburnett
Copy link
Contributor

Fix has been merged, and the test is now passing - see #25188 for an example. Rebase your PR to re-run the CI tests and docs / test (:presto-docs) (pull_request) should pass for this PR as well.

@elbinpallimalilibm elbinpallimalilibm force-pushed the arrow_mtls branch 2 times, most recently from 7bdfc96 to bb2cf1d Compare May 24, 2025 00:10
@elbinpallimalilibm elbinpallimalilibm marked this pull request as ready for review May 24, 2025 02:28
@prestodb-ci prestodb-ci requested a review from a team May 24, 2025 02:28
Copy link
Contributor

@pratyakshsharma pratyakshsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the fix @elbinpallimalilibm. I have added few comments, please check.

return flightClientSSLCertificate;
}

@Config("arrow-flight.client-ssl-certificate")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be good to add a comment here saying this is needed for mTLS auth.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments.

Optional<InputStream> clientCertificate = Optional.empty();
Optional<InputStream> clientKey = Optional.empty();
if (config.getFlightClientSSLCertificate() != null && config.getFlightClientSSLKey() != null) {
clientCertificate = Optional.of(newInputStream(Paths.get(config.getFlightClientSSLCertificate())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trying to understand it a bit better, what happens if the certificate and key are invalid? Lets use a try-catch block here? And maybe add a test case covering this scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the cert is invalid, executing a query will give the user an error that the cert is invalid. Added a test case that covers this scenario.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a try-catch here as well and modify the error message in the test case accordingly. Thank you for adding test case for this though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The invalid cert exception is thrown only at line 84 FlightClient flightClient = flightClientBuilder.build(); and we might get exception due to other reasons as well from the build method. So adding a try...catch here will not help in modifying the error message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I actually foresee improperly configured client cert/key as a very probable source of error, and hence wanted to cover the scenario with a proper user facing message. Anyways I leave the final decision to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the code to catch errors due to invalid cert or key file. Rethrowing a Presto Exception with a custom message for those scenarios.

@@ -52,7 +52,9 @@ Property Name Description
========================================== ==============================================================
``arrow-flight.server`` Endpoint of the Flight server
``arrow-flight.server.port`` Flight server port
``arrow-flight.server-ssl-certificate`` Pass ssl certificate
``arrow-flight.server-ssl-certificate`` Path to SSL certificate of Flight server
``arrow-flight.client-ssl-certificate`` Path to SSL certificate that Flight client should use
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lets modify these to include "in case of mTLS authentication" to make it more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the docs.

}

@BeforeClass
public void setup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 2 test classes have a lot of duplicate code. Please see if we can use inheritance to avoid this redundant code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the test classes.

steveburnett
steveburnett previously approved these changes May 27, 2025
Copy link
Contributor

@steveburnett steveburnett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! (docs)

Pull branch, local doc build, looks good. Thanks!

Copy link
Contributor

@pratyakshsharma pratyakshsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing earlier comments. I have few more minor comments, once addressed, I will approve.

}

private static DistributedQueryRunner createQueryRunner(
public static DistributedQueryRunner createQueryRunner(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we modifying the access-modifier?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to create query runner with pre-defined catalog properties from the new test classes. That's why the method was changed to public from private.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does protected or other modifier work instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to protected

Optional<InputStream> clientCertificate = Optional.empty();
Optional<InputStream> clientKey = Optional.empty();
if (config.getFlightClientSSLCertificate() != null && config.getFlightClientSSLKey() != null) {
clientCertificate = Optional.of(newInputStream(Paths.get(config.getFlightClientSSLCertificate())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a try-catch here as well and modify the error message in the test case accordingly. Thank you for adding test case for this though.

}

@BeforeClass
public void setup()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this method need to be public? How about making it package private? Similarly for other methods apart from createQueryRunner, lets make them as restricted as possible.

}

@Override
protected Map<String, String> getCatalogProperties()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The access modifiers here will change based on my above comment.

Copy link
Contributor

@pratyakshsharma pratyakshsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple more comments, rest looks good

}

private static DistributedQueryRunner createQueryRunner(
public static DistributedQueryRunner createQueryRunner(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does protected or other modifier work instead?


abstract Map<String, String> getCatalogProperties();

protected int getServerPort()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets change this to package private as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

@elbinpallimalilibm elbinpallimalilibm force-pushed the arrow_mtls branch 2 times, most recently from 3fe0e0e to 4bbfaa5 Compare May 28, 2025 13:20
Copy link
Contributor

@pratyakshsharma pratyakshsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for patiently addressing all comments. Few refactorings needed, rest looks good

}
catch (Exception e) {
throw new ArrowException(ARROW_FLIGHT_CLIENT_ERROR, "Error creating flight client: " + e.getMessage(), e);
Optional<Throwable> cause = Optional.ofNullable(e.getCause());
if (cause.filter(c -> c instanceof InvalidKeyException).isPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (cause.filter(c -> c instanceof InvalidKeyException).isPresent()) {
if (e instanceOf InvalidKeyException) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e will be instance of IllegalArgumentException. Inner exception e.getCause if not null, will be an instance of InvalidKeyException

if (cause.filter(c -> c instanceof InvalidKeyException).isPresent()) {
throw new ArrowException(ARROW_FLIGHT_INVALID_KEY_ERROR, "Error creating flight client, invalid key file: " + e.getMessage(), e);
}
else if (cause.filter(c -> c instanceof CertificateException).isPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

clientCertificate.get().close();
}
catch (IOException e) {
logger.error("Error closing input stream", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error messages here are all identical, can be modified in different blocks.

clientCertificate.get().close();
}
catch (IOException e) {
logger.error("Error closing input stream", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
logger.error("Error closing input stream", e);
logger.error("Error closing input stream for clientCertificate", e);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

clientKey.get().close();
}
catch (IOException e) {
logger.error("Error closing input stream", e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor

@pratyakshsharma pratyakshsharma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the proactive responses. LGTM!

@pratyakshsharma
Copy link
Contributor

@prestodb/committers this should be good for final pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from:IBM PR from IBM
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants