Skip to content
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

GH-38460: [Java][FlightRPC] Add mTLS support for Flight SQL JDBC driver #38461

Merged
merged 16 commits into from
Nov 6, 2023

Conversation

prmoore77
Copy link
Contributor

@prmoore77 prmoore77 commented Oct 25, 2023

Rationale for this change

I wanted to add additional security capabilities to the Arrow Flight SQL JDBC driver so that it catches up to ADBC. ADBC already supports mTLS - and it is a great security feature. I wanted to bring this to the JDBC driver as well.

What changes are included in this PR?

This PR adds support for mTLS (client certificate verification/authentication) to the Arrow Flight SQL JDBC driver.

Are these changes tested?

Yes, I've added tests of the new mTLS functionality - and have ensured that the change is backward compatible by verifying all existing tests pass.

Are there any user-facing changes?

Yes - but the end-user documentation for the Arrow Flight SQL JDBC driver has been updated in the PR itself.

@lidavidm lidavidm changed the title GH-38460: [Arrow Flight JDBC Drvier] - add mTLS support GH-38460: [Java][FlightRPC] Add add mTLS support for Flight SQL JDBC driver Oct 25, 2023
@prmoore77 prmoore77 changed the title GH-38460: [Java][FlightRPC] Add add mTLS support for Flight SQL JDBC driver GH-38460: [Java][FlightRPC] Add mTLS support for Flight SQL JDBC driver Oct 25, 2023
@github-actions
Copy link

⚠️ GitHub issue #38460 has been automatically assigned in GitHub to PR creator.

@danepitkin
Copy link
Member

Thanks @prmoore77 ! @vibhatha @davisusanibar and I can take a look this week.

@github-actions github-actions bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Oct 26, 2023
@jbonofre
Copy link
Member

Thanks for the PR. I'm doing a pass and testing as well.

Copy link
Member

@danepitkin danepitkin left a comment

Choose a reason for hiding this comment

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

The tests are quite thorough, nice work! I'll defer to others who have more experience on Flight SQL for approval.

* @throws Exception on error.
*/
@Test
public void testTLSConnectionPropertyTrueIntegerCorrectCastUrlAndPropertiesUsingPutWithDriverManager()
Copy link
Member

Choose a reason for hiding this comment

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

We could use @ParameterizedTest to reduce some code duplication in the test files.

final ArrowFlightJdbcDataSource dataSource =
ArrowFlightJdbcDataSource.createNewDataSource(properties);
try (final Connection connection = dataSource.getConnection()) {
assert connection.isValid(300);
Copy link
Member

@danepitkin danepitkin Oct 26, 2023

Choose a reason for hiding this comment

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

Out of curiosity, why is there a 300 second timeout for ArrowFlightJdbcDataSource connections, but 0 second timeouts for ArrowFlightJdbcDriver/DriverManager connections?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good question :) - I copied the tests from the ConnectionTlsTest.java code - while modifying the tests for mTLS of course. I'm not sure of the rationale for the timeout, but I'll research it.

* @param mTlsCACert The CA certificate to use for verifying clients.
*/
public Builder useMTlsClientVerification(final File mTlsCACert) throws IOException {
this.mTlsCACert = new FileInputStream(mTlsCACert);
Copy link
Member

@jduo jduo Oct 26, 2023

Choose a reason for hiding this comment

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

If the user calls this method repeatedly we will leak File handles. Need to close the current one if it exists. Perhaps it would be better to defer opening the physical file until starting the connection, though that delays notification if there's a problem opening it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hey @jduo - I'm not a very good Java developer - but I'm learning (thanks for your patience). Would it be like this?

    public Builder useMTlsClientVerification(final File mTlsCACert) throws IOException {
      if (this.mTlsCACert != null) {
            this.mTlsCACert.close();
        }
      this.mTlsCACert = new FileInputStream(mTlsCACert); 

Thanks for your help!

Copy link
Member

Choose a reason for hiding this comment

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

Yes this would work. However I'm leaning towards deferring opening mTlsCACert until the connection is made. It's odd for builders to have more logic than setting fields.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@jduo just for my knowledge and to clarify a point, would it be more easier if we use try-with-resources here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hi @jduo - this is the FlightServer - I'd rather not change the overall design of the current class's builder. It already uses this approach for useTls - and I think my change is consistent with that.

Copy link
Member

Choose a reason for hiding this comment

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

@vibhatha , try-with-resources wouldn't help here since we need the stream to have a longer lifetime than this builder function. This is more of a case of tracking ownership.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it, thanks @jduo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jduo - I've added small changes that detect if the InputStream attributes are not null, and if so - closes them. Could you please re-review?

Copy link
Member

Choose a reason for hiding this comment

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

Thanks @prmoore77 minor comment about another possible error scenario in useTls() that applies here.

* @param mTlsCACert The CA certificate to use for verifying clients.
*/
public Builder useMTlsClientVerification(final InputStream mTlsCACert) throws IOException {
this.mTlsCACert = mTlsCACert;
Copy link
Member

@jduo jduo Oct 26, 2023

Choose a reason for hiding this comment

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

Similar comment as above about this potentially leaking a file. Also, now it is ambiguous whether the Flight client should close() mTlsCACert (presumably it should but there should be a comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mirrored the existing useTls method here

Does that code have the issue as well?

Copy link
Member

Choose a reason for hiding this comment

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

Correct. It looks to have this issue too.

Copy link
Member

Choose a reason for hiding this comment

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

@vibhatha @davisusanibar can you file an issue to fix this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@lidavidm I will file one. Will study a bit and file one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

given that this issue will be filed separately - is it acceptable to approve/merge this PR?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Filed #38586

Copy link
Contributor

@davisusanibar davisusanibar 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 PR. I am adding a few minor questions.

- Path to PEM-encoded client mTLS certificate when the Flight
SQL server requires client verification.

* - clientKey
Copy link
Contributor

Choose a reason for hiding this comment

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

Will it be necessary to provide an optional client key password argument if this client private key is password-protected?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello – it could be added, but the Java Flight client doesn’t yet support that to my knowledge – so I just wanted to catch up to that for now. We can add it in a later PR, unless you feel strongly that it should be included here.

@vibhatha
Copy link
Collaborator

@prmoore77 I left a few comments and nice work with the test cases and comments. Thanks for working on this issue.

@github-actions github-actions bot added awaiting changes Awaiting changes and removed awaiting change review Awaiting change review labels Nov 3, 2023
@github-actions github-actions bot added awaiting change review Awaiting change review awaiting changes Awaiting changes and removed awaiting changes Awaiting changes awaiting change review Awaiting change review labels Nov 3, 2023
* @param mTlsCACert The CA certificate to use for verifying clients.
*/
public Builder useMTlsClientVerification(final InputStream mTlsCACert) throws IOException {
this.mTlsCACert = mTlsCACert;
Copy link
Member

Choose a reason for hiding this comment

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

Filed #38586

@github-actions github-actions bot added awaiting merge Awaiting merge and removed awaiting changes Awaiting changes labels Nov 4, 2023
@jduo
Copy link
Member

jduo commented Nov 6, 2023

@prmoore77 heads up, after #38521 a copy constructor was added to ArrowFlightSqlClientHandler.Builder
So any new fields that this PR adds will need to be added to that copy constructor.

#38580 also changes ArrowFligthSqlClientHandler to set defaults correctly and adds tests for verifying defaults, so if that goes in first, new fields will be added there too.

@prmoore77
Copy link
Contributor Author

prmoore77 commented Nov 6, 2023

@prmoore77 heads up, after #38521 a copy constructor was added to ArrowFlightSqlClientHandler.Builder So any new fields that this PR adds will need to be added to that copy constructor.

#38580 also changes ArrowFligthSqlClientHandler to set defaults correctly and adds tests for verifying defaults, so if that goes in first, new fields will be added there too.

Hi @jduo - I've added the new fields to the new copy constructor (from #38521) - per your recommendation.

See: 1975786 - for details.

Thanks!

@jbonofre
Copy link
Member

jbonofre commented Nov 6, 2023

I did a quick test and review. LGTM. Thanks !

@lidavidm lidavidm merged commit 4ff1a29 into apache:main Nov 6, 2023
18 checks passed
@lidavidm lidavidm removed the awaiting merge Awaiting merge label Nov 6, 2023
@prmoore77 prmoore77 deleted the feature/flight-sql-jdbc-driver-mtls branch November 6, 2023 20:02
Copy link

After merging your PR, Conbench analyzed the 5 benchmarking runs that have been run so far on merge-commit 4ff1a29.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 3 possible false positives for unstable benchmarks that are known to sometimes produce them.

JerAguilon pushed a commit to JerAguilon/arrow that referenced this pull request Nov 9, 2023
…C driver (apache#38461)

### Rationale for this change

I wanted to add additional security capabilities to the Arrow Flight SQL JDBC driver so that it catches up to ADBC.  ADBC already supports mTLS - and it is a great security feature.  I wanted to bring this to the JDBC driver as well.

### What changes are included in this PR?

This PR adds support for mTLS (client certificate verification/authentication) to the Arrow Flight SQL JDBC driver.

### Are these changes tested?

Yes, I've added tests of the new mTLS functionality - and have ensured that the change is backward compatible by verifying all existing tests pass.

### Are there any user-facing changes?

Yes - but the end-user documentation for the Arrow Flight SQL JDBC driver has been updated in the PR itself.

* Closes: apache#38460

Lead-authored-by: prmoore77 <[email protected]>
Co-authored-by: David Li <[email protected]>
Signed-off-by: David Li <[email protected]>
loicalleyne pushed a commit to loicalleyne/arrow that referenced this pull request Nov 13, 2023
…C driver (apache#38461)

### Rationale for this change

I wanted to add additional security capabilities to the Arrow Flight SQL JDBC driver so that it catches up to ADBC.  ADBC already supports mTLS - and it is a great security feature.  I wanted to bring this to the JDBC driver as well.

### What changes are included in this PR?

This PR adds support for mTLS (client certificate verification/authentication) to the Arrow Flight SQL JDBC driver.

### Are these changes tested?

Yes, I've added tests of the new mTLS functionality - and have ensured that the change is backward compatible by verifying all existing tests pass.

### Are there any user-facing changes?

Yes - but the end-user documentation for the Arrow Flight SQL JDBC driver has been updated in the PR itself.

* Closes: apache#38460

Lead-authored-by: prmoore77 <[email protected]>
Co-authored-by: David Li <[email protected]>
Signed-off-by: David Li <[email protected]>
dgreiss pushed a commit to dgreiss/arrow that referenced this pull request Feb 19, 2024
…C driver (apache#38461)

### Rationale for this change

I wanted to add additional security capabilities to the Arrow Flight SQL JDBC driver so that it catches up to ADBC.  ADBC already supports mTLS - and it is a great security feature.  I wanted to bring this to the JDBC driver as well.

### What changes are included in this PR?

This PR adds support for mTLS (client certificate verification/authentication) to the Arrow Flight SQL JDBC driver.

### Are these changes tested?

Yes, I've added tests of the new mTLS functionality - and have ensured that the change is backward compatible by verifying all existing tests pass.

### Are there any user-facing changes?

Yes - but the end-user documentation for the Arrow Flight SQL JDBC driver has been updated in the PR itself.

* Closes: apache#38460

Lead-authored-by: prmoore77 <[email protected]>
Co-authored-by: David Li <[email protected]>
Signed-off-by: David Li <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Java][FlightRPC] Add mTLS support to the Arrow Flight SQL JDBC driver
7 participants