Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment
keyStore.setFile(setting, Files.readAllBytes(file));
}

keyStore.save(env.configFile(), getKeyStorePassword().getChars());
keyStore.save(env.configDir(), getKeyStorePassword().getChars());
}

@SuppressForbidden(reason = "file arg for cli")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment
}
}

keyStore.save(env.configFile(), getKeyStorePassword().getChars());
keyStore.save(env.configDir(), getKeyStorePassword().getChars());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public BaseKeyStoreCommand(String description, boolean keyStoreMustExist) {
@Override
public final void execute(Terminal terminal, OptionSet options, Environment env, ProcessInfo processInfo) throws Exception {
try {
final Path configFile = env.configFile();
final Path configFile = env.configDir();
keyStore = KeyStoreWrapper.load(configFile);
if (keyStore == null) {
if (keyStoreMustExist) {
throw new UserException(
ExitCodes.DATA_ERROR,
"Elasticsearch keystore not found at ["
+ KeyStoreWrapper.keystorePath(env.configFile())
+ KeyStoreWrapper.keystorePath(env.configDir())
+ "]. Use 'create' command to create one."
);
} else if (options.has(forceOption) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ChangeKeyStorePasswordCommand extends BaseKeyStoreCommand {
protected void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception {
try (SecureString newPassword = readPassword(terminal, true)) {
final KeyStoreWrapper keyStore = getKeyStore();
keyStore.save(env.configFile(), newPassword.getChars());
keyStore.save(env.configDir(), newPassword.getChars());
terminal.println("Elasticsearch keystore password changed successfully.");
} catch (SecurityException e) {
throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class CreateKeyStoreCommand extends KeyStoreAwareCommand {
@Override
public void execute(Terminal terminal, OptionSet options, Environment env, ProcessInfo processInfo) throws Exception {
try (SecureString password = options.has(passwordOption) ? readPassword(terminal, true) : new SecureString(new char[0])) {
Path keystoreFile = KeyStoreWrapper.keystorePath(env.configFile());
Path keystoreFile = KeyStoreWrapper.keystorePath(env.configDir());
if (Files.exists(keystoreFile)) {
if (terminal.promptYesNo("An elasticsearch keystore already exists. Overwrite?", false) == false) {
terminal.println("Exiting without creating keystore.");
return;
}
}
KeyStoreWrapper keystore = KeyStoreWrapper.create();
keystore.save(env.configFile(), password.getChars());
terminal.println("Created elasticsearch keystore in " + KeyStoreWrapper.keystorePath(env.configFile()));
keystore.save(env.configDir(), password.getChars());
terminal.println("Created elasticsearch keystore in " + KeyStoreWrapper.keystorePath(env.configDir()));
} catch (SecurityException e) {
throw new UserException(ExitCodes.IO_ERROR, "Error creating the elasticsearch keystore.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class HasPasswordKeyStoreCommand extends KeyStoreAwareCommand {

@Override
public void execute(Terminal terminal, OptionSet options, Environment env, ProcessInfo processInfo) throws Exception {
final Path configFile = env.configFile();
final Path configFile = env.configDir();
final KeyStoreWrapper keyStore = KeyStoreWrapper.load(configFile);

// We handle error printing here so we can respect the "--silent" flag
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment
}
keyStore.remove(setting);
}
keyStore.save(env.configFile(), getKeyStorePassword().getChars());
keyStore.save(env.configDir(), getKeyStorePassword().getChars());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class UpgradeKeyStoreCommand extends BaseKeyStoreCommand {

@Override
protected void executeCommand(final Terminal terminal, final OptionSet options, final Environment env) throws Exception {
KeyStoreWrapper.upgrade(getKeyStore(), env.configFile(), getKeyStorePassword().getChars());
KeyStoreWrapper.upgrade(getKeyStore(), env.configDir(), getKeyStorePassword().getChars());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ private Path createRandomFile() throws IOException {
for (int i = 0; i < length; ++i) {
bytes[i] = randomByte();
}
Path file = env.configFile().resolve(randomAlphaOfLength(16));
Path file = env.configDir().resolve(randomAlphaOfLength(16));
Files.write(file, bytes);
return file;
}

private void addFile(KeyStoreWrapper keystore, String setting, Path file, String password) throws Exception {
keystore.setFile(setting, Files.readAllBytes(file));
keystore.save(env.configFile(), password.toCharArray());
keystore.save(env.configDir(), password.toCharArray());
}

public void testMissingCreateWithEmptyPasswordWhenPrompted() throws Exception {
Expand All @@ -77,7 +77,7 @@ public void testMissingNoCreate() throws Exception {
terminal.addSecretInput(randomFrom("", "keystorepassword"));
terminal.addTextInput("n"); // explicit no
execute("foo");
assertNull(KeyStoreWrapper.load(env.configFile()));
assertNull(KeyStoreWrapper.load(env.configDir()));
}

public void testOverwritePromptDefault() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void testMissingPromptCreateWithoutPasswordWithoutPromptIfForced() throws
public void testMissingNoCreate() throws Exception {
terminal.addTextInput("n"); // explicit no
execute("foo");
assertNull(KeyStoreWrapper.load(env.configFile()));
assertNull(KeyStoreWrapper.load(env.configDir()));
}

public void testOverwritePromptDefault() throws Exception {
Expand Down Expand Up @@ -143,7 +143,7 @@ public void testForceNonExistent() throws Exception {

public void testPromptForValue() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
terminal.addSecretInput("secret value");
execute("foo");
Expand All @@ -152,7 +152,7 @@ public void testPromptForValue() throws Exception {

public void testPromptForMultipleValues() throws Exception {
final String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
terminal.addSecretInput("bar1");
terminal.addSecretInput("bar2");
Expand All @@ -165,7 +165,7 @@ public void testPromptForMultipleValues() throws Exception {

public void testStdinShort() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("secret value 1");
execute("-x", "foo");
Expand All @@ -174,7 +174,7 @@ public void testStdinShort() throws Exception {

public void testStdinLong() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("secret value 2");
execute("--stdin", "foo");
Expand All @@ -183,7 +183,7 @@ public void testStdinLong() throws Exception {

public void testStdinNoInput() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("");
execute("-x", "foo");
Expand All @@ -192,7 +192,7 @@ public void testStdinNoInput() throws Exception {

public void testStdinInputWithLineBreaks() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("Typedthisandhitenter\n");
execute("-x", "foo");
Expand All @@ -201,7 +201,7 @@ public void testStdinInputWithLineBreaks() throws Exception {

public void testStdinInputWithCarriageReturn() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("Typedthisandhitenter\r");
execute("-x", "foo");
Expand All @@ -210,7 +210,7 @@ public void testStdinInputWithCarriageReturn() throws Exception {

public void testStdinWithMultipleValues() throws Exception {
final String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("bar1\nbar2\nbar3");
execute(randomFrom("-x", "--stdin"), "foo1", "foo2", "foo3");
Expand All @@ -221,7 +221,7 @@ public void testStdinWithMultipleValues() throws Exception {

public void testAddUtf8String() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configFile(), password.toCharArray());
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
final int stringSize = randomIntBetween(8, 16);
try (CharArrayWriter secretChars = new CharArrayWriter(stringSize)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void setupEnv() throws IOException {

public void testLoadSecureSettings() throws Exception {
final char[] password = KeyStoreWrapperTests.getPossibleKeystorePassword();
final Path configPath = env.configFile();
final Path configPath = env.configDir();
final SecureString seed;
try (KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.create()) {
seed = KeyStoreWrapper.SEED_SETTING.get(Settings.builder().setSecureSettings(keyStoreWrapper).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void testNotMatchingPasswords() throws Exception {
public void testDefaultNotPromptForPassword() throws Exception {
assumeFalse("Cannot open unprotected keystore on FIPS JVM", inFipsJvm());
execute();
Path configDir = env.configFile();
Path configDir = env.configDir();
assertNotNull(KeyStoreWrapper.load(configDir));
}

Expand All @@ -63,7 +63,7 @@ public void testPosix() throws Exception {
} else {
execute();
}
Path configDir = env.configFile();
Path configDir = env.configDir();
assertNotNull(KeyStoreWrapper.load(configDir));
}

Expand All @@ -79,13 +79,13 @@ public void testNotPosix() throws Exception {
} else {
execute();
}
Path configDir = env.configFile();
Path configDir = env.configDir();
assertNotNull(KeyStoreWrapper.load(configDir));
}

public void testOverwrite() throws Exception {
String password = getPossibleKeystorePassword();
Path keystoreFile = KeyStoreWrapper.keystorePath(env.configFile());
Path keystoreFile = KeyStoreWrapper.keystorePath(env.configDir());
byte[] content = "not a keystore".getBytes(StandardCharsets.UTF_8);
Files.write(keystoreFile, content);

Expand All @@ -110,6 +110,6 @@ public void testOverwrite() throws Exception {
} else {
execute();
}
assertNotNull(KeyStoreWrapper.load(env.configFile()));
assertNotNull(KeyStoreWrapper.load(env.configDir()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ KeyStoreWrapper createKeystore(String password, String... settings) throws Excep
}

void saveKeystore(KeyStoreWrapper keystore, String password) throws Exception {
keystore.save(env.configFile(), password.toCharArray());
keystore.save(env.configDir(), password.toCharArray());
}

KeyStoreWrapper loadKeystore(String password) throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configDir());
keystore.decrypt(password.toCharArray());
return keystore;
}
Expand Down
Loading