-
Notifications
You must be signed in to change notification settings - Fork 4.1k
CASSANDRA-17559: FQLTool Replay should support full set of login options #5064
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
arvindKandpal-ksolves
wants to merge
4
commits into
apache:trunk
Choose a base branch
from
arvindKandpal-ksolves:CASSANDRA-17559
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
31b3231
CASSANDRA-17559: FQLTool Replay should support full set of login options
arvindKandpal-ksolves 627087c
Address review comments: Move password masking to ParsedTargetHost an…
arvindKandpal-ksolves 8602603
Address review: encapsulate connection options, fix password parsing,…
arvindKandpal-ksolves 2e832eb
Address review: Preserve Replay public API overload
arvindKandpal-ksolves File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
203 changes: 203 additions & 0 deletions
203
tools/fqltool/src/org/apache/cassandra/fqltool/ConnectionOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.cassandra.fqltool; | ||
|
|
||
| import javax.net.ssl.SSLContext; | ||
|
|
||
| import com.datastax.driver.core.AuthProvider; | ||
| import com.datastax.driver.core.RemoteEndpointAwareJdkSSLOptions; | ||
| import com.datastax.driver.core.SSLOptions; | ||
|
|
||
| import org.apache.cassandra.config.EncryptionOptions; | ||
| import org.apache.cassandra.security.SSLFactory; | ||
|
|
||
| /** | ||
| * Holds the SSL and authentication settings used to connect to target hosts during fqltool replay. | ||
| * Note that providing any SSL-related configuration option implicitly enables SSL. | ||
| */ | ||
| public class ConnectionOptions | ||
| { | ||
| private final boolean ssl; | ||
| private final SSLOptions sslOptions; | ||
| private final String authProviderClass; | ||
|
|
||
| private ConnectionOptions(boolean ssl, SSLOptions sslOptions, String authProviderClass) | ||
| { | ||
| this.ssl = ssl; | ||
| this.sslOptions = sslOptions; | ||
| this.authProviderClass = authProviderClass; | ||
| } | ||
|
|
||
| public boolean ssl() | ||
| { | ||
| return ssl; | ||
| } | ||
|
|
||
| public SSLOptions sslOptions() | ||
| { | ||
| return sslOptions; | ||
| } | ||
|
|
||
| public String authProviderClass() | ||
| { | ||
| return authProviderClass; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the configured AuthProvider: a (String,String) constructor when credentials are present, otherwise a no-arg constructor. | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public AuthProvider instantiateAuthProvider(String user, String password) | ||
| { | ||
| try | ||
| { | ||
| Class<? extends AuthProvider> clazz = (Class<? extends AuthProvider>) Class.forName(authProviderClass); | ||
|
|
||
| if (user != null && password != null) | ||
| return clazz.getConstructor(String.class, String.class).newInstance(user, password); | ||
|
|
||
| return clazz.getDeclaredConstructor().newInstance(); | ||
| } | ||
| catch (NoSuchMethodException e) | ||
| { | ||
| throw new RuntimeException("Auth provider " + authProviderClass + " does not support plain text credentials", e); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| throw new RuntimeException("Could not instantiate auth provider: " + authProviderClass, e); | ||
| } | ||
| } | ||
|
|
||
| public static Builder builder() | ||
| { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public static class Builder | ||
| { | ||
| private boolean ssl; | ||
| private String truststorePath; | ||
| private String truststorePassword; | ||
| private String keystorePath; | ||
| private String keystorePassword; | ||
| private String authProviderClass; | ||
|
|
||
| public Builder withSsl(boolean ssl) | ||
| { | ||
| this.ssl = ssl; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withTruststore(String truststorePath) | ||
| { | ||
| this.truststorePath = truststorePath; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withTruststorePassword(String truststorePassword) | ||
| { | ||
| this.truststorePassword = truststorePassword; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withKeystore(String keystorePath) | ||
| { | ||
| this.keystorePath = keystorePath; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withKeystorePassword(String keystorePassword) | ||
| { | ||
| this.keystorePassword = keystorePassword; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder withAuthProviderClass(String authProviderClass) | ||
| { | ||
| this.authProviderClass = authProviderClass; | ||
| return this; | ||
| } | ||
|
|
||
| public ConnectionOptions build() | ||
| { | ||
| if (truststorePassword != null && truststorePath == null) | ||
| throw new IllegalArgumentException("--ssl-truststore-password requires --ssl-truststore to be set"); | ||
| if (keystorePassword != null && keystorePath == null) | ||
| throw new IllegalArgumentException("--ssl-keystore-password requires --ssl-keystore to be set"); | ||
|
|
||
| // any SSL-related option implicitly enables SSL | ||
| boolean effectiveSsl = ssl || truststorePath != null || keystorePath != null; | ||
|
|
||
| if (authProviderClass != null) | ||
| validateAuthProviderClass(); | ||
|
|
||
| SSLOptions sslOptions = effectiveSsl ? buildSSLOptions() : null; | ||
|
|
||
| return new ConnectionOptions(effectiveSsl, sslOptions, authProviderClass); | ||
| } | ||
|
|
||
| private void validateAuthProviderClass() | ||
| { | ||
| try | ||
| { | ||
| Class<?> clazz = Class.forName(authProviderClass); | ||
| if (!AuthProvider.class.isAssignableFrom(clazz)) | ||
| throw new IllegalArgumentException(authProviderClass + " does not implement " + AuthProvider.class.getName()); | ||
| } | ||
| catch (ClassNotFoundException e) | ||
| { | ||
| throw new RuntimeException("Could not find auth provider class: " + authProviderClass, e); | ||
| } | ||
| } | ||
|
|
||
| private SSLOptions buildSSLOptions() | ||
| { | ||
| try | ||
| { | ||
| EncryptionOptions.ClientEncryptionOptions.Builder encBuilder = new EncryptionOptions.ClientEncryptionOptions.Builder(); | ||
| encBuilder.withEnabled(true); | ||
|
|
||
| if (truststorePath != null) | ||
| encBuilder.withTrustStore(truststorePath); | ||
| if (truststorePassword != null) | ||
| encBuilder.withTrustStorePassword(truststorePassword); | ||
|
|
||
| EncryptionOptions.ClientEncryptionOptions.ClientAuth clientAuth = EncryptionOptions.ClientEncryptionOptions.ClientAuth.NOT_REQUIRED; | ||
| if (keystorePath != null) | ||
| { | ||
| encBuilder.withKeyStore(keystorePath); | ||
| clientAuth = EncryptionOptions.ClientEncryptionOptions.ClientAuth.REQUIRED; | ||
| } | ||
| if (keystorePassword != null) | ||
| encBuilder.withKeyStorePassword(keystorePassword); | ||
|
|
||
| EncryptionOptions.ClientEncryptionOptions clientEncOptions = encBuilder.build(); | ||
| SSLContext sslContext = SSLFactory.createSSLContext(clientEncOptions, clientAuth); | ||
|
|
||
| return RemoteEndpointAwareJdkSSLOptions.builder() | ||
| .withSSLContext(sslContext) | ||
| .build(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| throw new RuntimeException("Could not configure SSL for fqltool replay", e); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This always builds a key manager, while ClientAuth.NOT_REQUIRED only disables trust manager creation. That means --ssl or --ssl-truststore will still attempt to load the default keystore. We should instead use the configured/default trust managers and only create key managers when a keystore is explicitly supplied.