Skip to content

Commit

Permalink
Rename file to dir in Environment (#2730)
Browse files Browse the repository at this point in the history
* Rename `file` to `dir` in Environment

Signed-off-by: ruanwenjun <[email protected]>

* Fix compile error

Signed-off-by: ruanwenjun <[email protected]>

* fix compile error

Signed-off-by: ruanwenjun <[email protected]>
  • Loading branch information
ruanwenjun authored Apr 5, 2022
1 parent 406ee36 commit 0b1f4a2
Show file tree
Hide file tree
Showing 57 changed files with 277 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,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 @@ -121,7 +121,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 @@ -59,16 +59,16 @@ class CreateKeyStoreCommand extends KeyStoreAwareCommand {
@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) 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 opensearch 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 opensearch keystore in " + KeyStoreWrapper.keystorePath(env.configFile()));
keystore.save(env.configDir(), password.getChars());
terminal.println("Created opensearch keystore in " + KeyStoreWrapper.keystorePath(env.configDir()));
} catch (SecurityException e) {
throw new UserException(ExitCodes.IO_ERROR, "Error creating the opensearch keystore.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 @@ -71,7 +71,7 @@ public void setupEnv() throws IOException {
}

public void testLoadSecureSettings() throws Exception {
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 @@ -66,14 +66,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 @@ -95,7 +95,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 @@ -101,7 +101,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 @@ -161,7 +161,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 @@ -170,7 +170,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 @@ -183,7 +183,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 @@ -192,7 +192,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 @@ -201,7 +201,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 @@ -210,7 +210,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 @@ -219,7 +219,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 @@ -228,7 +228,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 @@ -239,7 +239,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 @@ -67,7 +67,7 @@ public void testNotMatchingPasswords() throws Exception {

public void testDefaultNotPromptForPassword() throws Exception {
execute();
Path configDir = env.configFile();
Path configDir = env.configDir();
assertNotNull(KeyStoreWrapper.load(configDir));
}

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

Expand All @@ -86,13 +86,13 @@ public void testNotPosix() throws Exception {
terminal.addSecretInput(password);
env = setupEnv(false, fileSystems);
execute();
Path configDir = env.configFile();
Path configDir = env.configDir();
assertNotNull(KeyStoreWrapper.load(configDir));
}

public void testOverwrite() throws Exception {
String password = randomFrom("", "keystorepassword");
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 @@ -108,6 +108,6 @@ public void testOverwrite() throws Exception {
terminal.addSecretInput(password);
terminal.addSecretInput(password);
execute();
assertNotNull(KeyStoreWrapper.load(env.configFile()));
assertNotNull(KeyStoreWrapper.load(env.configDir()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ KeyStoreWrapper createKeystore(String password, String... settings) throws Excep
for (int i = 0; i < settings.length; i += 2) {
keystore.setString(settings[i], settings[i + 1].toCharArray());
}
keystore.save(env.configFile(), password.toCharArray());
keystore.save(env.configDir(), password.toCharArray());
return keystore;
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public void testFileSettingExhaustiveBytes() throws Exception {
bytes[i] = (byte) i;
}
keystore.setFile("foo", bytes);
keystore.save(env.configFile(), new char[0]);
keystore = KeyStoreWrapper.load(env.configFile());
keystore.save(env.configDir(), new char[0]);
keystore = KeyStoreWrapper.load(env.configDir());
keystore.decrypt(new char[0]);
try (InputStream stream = keystore.getFile("foo")) {
for (int i = 0; i < 256; ++i) {
Expand All @@ -125,11 +125,11 @@ public void testCreate() throws Exception {

public void testDecryptKeyStoreWithWrongPassword() throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.create();
keystore.save(env.configFile(), new char[0]);
final KeyStoreWrapper loadedkeystore = KeyStoreWrapper.load(env.configFile());
keystore.save(env.configDir(), new char[0]);
final KeyStoreWrapper loadedKeystore = KeyStoreWrapper.load(env.configDir());
final SecurityException exception = expectThrows(
SecurityException.class,
() -> loadedkeystore.decrypt(new char[] { 'i', 'n', 'v', 'a', 'l', 'i', 'd' })
() -> loadedKeystore.decrypt(new char[] { 'i', 'n', 'v', 'a', 'l', 'i', 'd' })
);
if (inFipsJvm()) {
assertThat(
Expand Down Expand Up @@ -183,17 +183,17 @@ public void testValueSHA256Digest() throws Exception {
public void testUpgradeNoop() throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.create();
SecureString seed = keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey());
keystore.save(env.configFile(), new char[0]);
keystore.save(env.configDir(), new char[0]);
// upgrade does not overwrite seed
KeyStoreWrapper.upgrade(keystore, env.configFile(), new char[0]);
KeyStoreWrapper.upgrade(keystore, env.configDir(), new char[0]);
assertEquals(seed.toString(), keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey()).toString());
keystore = KeyStoreWrapper.load(env.configFile());
keystore = KeyStoreWrapper.load(env.configDir());
keystore.decrypt(new char[0]);
assertEquals(seed.toString(), keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey()).toString());
}

public void testFailWhenCannotConsumeSecretStream() throws Exception {
Path configDir = env.configFile();
Path configDir = env.configDir();
NIOFSDirectory directory = new NIOFSDirectory(configDir);
try (IndexOutput indexOutput = directory.createOutput("opensearch.keystore", IOContext.DEFAULT)) {
CodecUtil.writeHeader(indexOutput, "opensearch.keystore", 3);
Expand Down Expand Up @@ -221,7 +221,7 @@ public void testFailWhenCannotConsumeSecretStream() throws Exception {
}

public void testFailWhenCannotConsumeEncryptedBytesStream() throws Exception {
Path configDir = env.configFile();
Path configDir = env.configDir();
NIOFSDirectory directory = new NIOFSDirectory(configDir);
try (IndexOutput indexOutput = directory.createOutput("opensearch.keystore", IOContext.DEFAULT)) {
CodecUtil.writeHeader(indexOutput, "opensearch.keystore", 3);
Expand Down Expand Up @@ -250,7 +250,7 @@ public void testFailWhenCannotConsumeEncryptedBytesStream() throws Exception {
}

public void testFailWhenSecretStreamNotConsumed() throws Exception {
Path configDir = env.configFile();
Path configDir = env.configDir();
NIOFSDirectory directory = new NIOFSDirectory(configDir);
try (IndexOutput indexOutput = directory.createOutput("opensearch.keystore", IOContext.DEFAULT)) {
CodecUtil.writeHeader(indexOutput, "opensearch.keystore", 3);
Expand All @@ -277,7 +277,7 @@ public void testFailWhenSecretStreamNotConsumed() throws Exception {
}

public void testFailWhenEncryptedBytesStreamIsNotConsumed() throws Exception {
Path configDir = env.configFile();
Path configDir = env.configDir();
NIOFSDirectory directory = new NIOFSDirectory(configDir);
try (IndexOutput indexOutput = directory.createOutput("opensearch.keystore", IOContext.DEFAULT)) {
CodecUtil.writeHeader(indexOutput, "opensearch.keystore", 3);
Expand Down Expand Up @@ -343,11 +343,11 @@ private void possiblyAlterEncryptedBytes(
public void testUpgradeAddsSeed() throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.create();
keystore.remove(KeyStoreWrapper.SEED_SETTING.getKey());
keystore.save(env.configFile(), new char[0]);
KeyStoreWrapper.upgrade(keystore, env.configFile(), new char[0]);
keystore.save(env.configDir(), new char[0]);
KeyStoreWrapper.upgrade(keystore, env.configDir(), new char[0]);
SecureString seed = keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey());
assertNotNull(seed);
keystore = KeyStoreWrapper.load(env.configFile());
keystore = KeyStoreWrapper.load(env.configDir());
keystore.decrypt(new char[0]);
assertEquals(seed.toString(), keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey()).toString());
}
Expand All @@ -364,7 +364,7 @@ public void testIllegalSettingName() throws Exception {

public void testBackcompatV1() throws Exception {
assumeFalse("Can't run in a FIPS JVM as PBE is not available", inFipsJvm());
Path configDir = env.configFile();
Path configDir = env.configDir();
NIOFSDirectory directory = new NIOFSDirectory(configDir);
try (IndexOutput output = EndiannessReverserUtil.createOutput(directory, "opensearch.keystore", IOContext.DEFAULT)) {
CodecUtil.writeHeader(output, "opensearch.keystore", 1);
Expand Down Expand Up @@ -395,7 +395,7 @@ public void testBackcompatV1() throws Exception {

public void testBackcompatV2() throws Exception {
assumeFalse("Can't run in a FIPS JVM as PBE is not available", inFipsJvm());
Path configDir = env.configFile();
Path configDir = env.configDir();
NIOFSDirectory directory = new NIOFSDirectory(configDir);
byte[] fileBytes = new byte[20];
random().nextBytes(fileBytes);
Expand Down Expand Up @@ -457,10 +457,10 @@ public void testStringAndFileDistinction() throws Exception {
final Path temp = createTempDir();
Files.write(temp.resolve("file_setting"), "file_value".getBytes(StandardCharsets.UTF_8));
wrapper.setFile("file_setting", Files.readAllBytes(temp.resolve("file_setting")));
wrapper.save(env.configFile(), new char[0]);
wrapper.save(env.configDir(), new char[0]);
wrapper.close();

final KeyStoreWrapper afterSave = KeyStoreWrapper.load(env.configFile());
final KeyStoreWrapper afterSave = KeyStoreWrapper.load(env.configDir());
assertNotNull(afterSave);
afterSave.decrypt(new char[0]);
assertThat(afterSave.getSettingNames(), equalTo(new HashSet<>(Arrays.asList("keystore.seed", "string_setting", "file_setting"))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ protected Environment createEnv(final Map<String, String> settings) {

@AwaitsFix(bugUrl = "https://github.com/opensearch-project/OpenSearch/issues/468")
public void testKeystoreUpgrade() throws Exception {
final Path keystore = KeyStoreWrapper.keystorePath(env.configFile());
final Path keystore = KeyStoreWrapper.keystorePath(env.configDir());
try (
InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-opensearch.keystore");
OutputStream os = Files.newOutputStream(keystore)
Expand All @@ -74,12 +74,12 @@ public void testKeystoreUpgrade() throws Exception {
os.write(buffer, 0, read);
}
}
try (KeyStoreWrapper beforeUpgrade = KeyStoreWrapper.load(env.configFile())) {
try (KeyStoreWrapper beforeUpgrade = KeyStoreWrapper.load(env.configDir())) {
assertNotNull(beforeUpgrade);
assertThat(beforeUpgrade.getFormatVersion(), equalTo(3));
}
execute();
try (KeyStoreWrapper afterUpgrade = KeyStoreWrapper.load(env.configFile())) {
try (KeyStoreWrapper afterUpgrade = KeyStoreWrapper.load(env.configDir())) {
assertNotNull(afterUpgrade);
assertThat(afterUpgrade.getFormatVersion(), equalTo(KeyStoreWrapper.FORMAT_VERSION));
afterUpgrade.decrypt(new char[0]);
Expand All @@ -89,7 +89,7 @@ public void testKeystoreUpgrade() throws Exception {

public void testKeystoreDoesNotExist() {
final UserException e = expectThrows(UserException.class, this::execute);
assertThat(e, hasToString(containsString("keystore not found at [" + KeyStoreWrapper.keystorePath(env.configFile()) + "]")));
assertThat(e, hasToString(containsString("keystore not found at [" + KeyStoreWrapper.keystorePath(env.configDir()) + "]")));
}

}
Loading

0 comments on commit 0b1f4a2

Please sign in to comment.