-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Handle pwd protected keystores in all CLI tools #45289
Merged
jkakavas
merged 9 commits into
elastic:feature-pwd-protected-keystore
from
jkakavas:setup-passwords-keystore-password
Aug 16, 2019
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36dc4b8
Handle pwd protected keystores in all CLI tools
jkakavas 39fa224
Merge remote-tracking branch 'origin/feature-pwd-protected-keystore' …
jkakavas 4e2270f
add missing test and fix long lines
jkakavas e4cb7cc
onlyh test wrong password when keystore is pwd protected
jkakavas 1b0262f
Address feedback to limit code duplication
jkakavas 6471272
Address feedback
jkakavas 3b07ef9
Address feedback
jkakavas f4da1fe
address feedback
jkakavas b1798f9
Merge remote-tracking branch 'origin/feature-pwd-protected-keystore' …
jkakavas 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
This file contains 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 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
81 changes: 81 additions & 0 deletions
81
server/src/main/java/org/elasticsearch/cli/KeyStoreAwareCommand.java
This file contains 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,81 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch 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.elasticsearch.cli; | ||
|
||
import joptsimple.OptionSet; | ||
import org.elasticsearch.common.settings.KeyStoreWrapper; | ||
import org.elasticsearch.common.settings.SecureString; | ||
import org.elasticsearch.env.Environment; | ||
|
||
import javax.crypto.AEADBadTagException; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* An {@link org.elasticsearch.cli.EnvironmentAwareCommand} that needs to access the elasticsearch keystore, possibly | ||
* decrypting it if it is password protected. | ||
*/ | ||
public abstract class KeyStoreAwareCommand extends EnvironmentAwareCommand { | ||
public KeyStoreAwareCommand(String description) { | ||
super(description); | ||
} | ||
|
||
/** | ||
* Reads the keystore password from the {@link Terminal}, prompting for verification where applicable and returns it as a | ||
* {@link SecureString}. | ||
* | ||
* @param terminal the terminal to use for user inputs | ||
* @param withVerification whether the user should be prompted for password verification | ||
* @return a SecureString with the password the user entered | ||
* @throws UserException If the user is prompted for verification and enters a different password | ||
*/ | ||
protected static SecureString readPassword(Terminal terminal, boolean withVerification) throws UserException { | ||
final char[] passwordArray; | ||
if (withVerification) { | ||
passwordArray = terminal.readSecret("Enter new password for the elasticsearch keystore (empty for no password): "); | ||
char[] passwordVerification = terminal.readSecret("Enter same password again: "); | ||
if (Arrays.equals(passwordArray, passwordVerification) == false) { | ||
throw new UserException(ExitCodes.DATA_ERROR, "Passwords are not equal, exiting."); | ||
} | ||
Arrays.fill(passwordVerification, '\u0000'); | ||
} else { | ||
passwordArray = terminal.readSecret("Enter password for the elasticsearch keystore : "); | ||
} | ||
return new SecureString(passwordArray); | ||
} | ||
|
||
/** | ||
* Decrypt the {@code keyStore}, prompting the user to enter the password in the {@link Terminal} if it is password protected | ||
*/ | ||
protected static void decryptKeyStore(KeyStoreWrapper keyStore, Terminal terminal) | ||
throws UserException, GeneralSecurityException, IOException { | ||
try (SecureString keystorePassword = keyStore.hasPassword() ? | ||
readPassword(terminal, false) : new SecureString(new char[0])) { | ||
keyStore.decrypt(keystorePassword.getChars()); | ||
} catch (SecurityException e) { | ||
if (e.getCause() instanceof AEADBadTagException) { | ||
throw new UserException(ExitCodes.DATA_ERROR, "Wrong password for elasticsearch.keystore"); | ||
} | ||
} | ||
} | ||
|
||
protected abstract void execute(Terminal terminal, OptionSet options, Environment env) throws Exception; | ||
} |
This file contains 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 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 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.
Why not just call mainWithoutErrorHandling directly in the one case where we need this? Making that method public seems better than overloading this method for one test case.
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.
I don't have a strong preference and don't mind following your suggestion but could you explain your reasoning/thinking regarding:
for future reference?
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.
Sorry I misunderstood where this method existed. It looks like we don't actually need a
command
member of the test case class. I would remove that, and make the other execute method call this method withnewCommand()
. Then I'm ok with having another method that takes command.