Skip to content

Commit

Permalink
Refactor for JPMS Support
Browse files Browse the repository at this point in the history
Signed-off-by: Prudhvi Godithi <[email protected]>
  • Loading branch information
prudhvigodithi committed Jan 28, 2025
1 parent b5234a5 commit 9c20d11
Show file tree
Hide file tree
Showing 50 changed files with 163 additions and 116 deletions.
2 changes: 1 addition & 1 deletion distribution/src/bin/opensearch-keystore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e -o pipefail

OPENSEARCH_MAIN_CLASS=org.opensearch.common.settings.KeyStoreCli \
OPENSEARCH_MAIN_CLASS=org.opensearch.cli.keystore.KeyStoreCli \
OPENSEARCH_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli \
"`dirname "$0"`"/opensearch-cli \
"$@"
2 changes: 1 addition & 1 deletion distribution/src/bin/opensearch-keystore.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setlocal enabledelayedexpansion
setlocal enableextensions

set OPENSEARCH_MAIN_CLASS=org.opensearch.common.settings.KeyStoreCli
set OPENSEARCH_MAIN_CLASS=org.opensearch.cli.keystore.KeyStoreCli
set OPENSEARCH_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli
call "%~dp0opensearch-cli.bat" ^
%%* ^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
Expand All @@ -39,6 +39,7 @@
import org.opensearch.cli.UserException;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.io.PathUtils;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.CheckedFunction;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.io.BufferedReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.KeyStoreAwareCommand;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.env.Environment;

Expand All @@ -53,7 +53,7 @@ public abstract class BaseKeyStoreCommand extends KeyStoreAwareCommand {
private KeyStoreWrapper keyStore;
private SecureString keyStorePassword;
private final boolean keyStoreMustExist;
OptionSpec<Void> forceOption;
protected OptionSpec<Void> forceOption;

public BaseKeyStoreCommand(String description, boolean keyStoreMustExist) {
super(description);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.env.Environment;

Expand All @@ -44,7 +45,7 @@
*
* @opensearch.internal
*/
class ChangeKeyStorePasswordCommand extends BaseKeyStoreCommand {
public class ChangeKeyStorePasswordCommand extends BaseKeyStoreCommand {

ChangeKeyStorePasswordCommand() {
super("Changes the password of a keystore", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.KeyStoreAwareCommand;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.env.Environment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import org.opensearch.cli.KeyStoreAwareCommand;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.nio.file.Path;
Expand All @@ -49,7 +49,7 @@ public class HasPasswordKeyStoreCommand extends KeyStoreAwareCommand {

static final int NO_PASSWORD_EXIT_CODE = 1;

HasPasswordKeyStoreCommand() {
protected HasPasswordKeyStoreCommand() {
super(
"Succeeds if the keystore exists and is password-protected, " + "fails with exit code " + NO_PASSWORD_EXIT_CODE + " otherwise."
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@
* GitHub history for details.
*/

package org.opensearch.cli;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.KeystoreConstants;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.cli.EnvironmentAwareCommand;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.env.Environment;
Expand All @@ -42,7 +47,7 @@
import java.util.Arrays;

/**
* An {@link org.opensearch.cli.EnvironmentAwareCommand} that needs to access the opensearch keystore, possibly
* An {@link EnvironmentAwareCommand} that needs to access the opensearch keystore, possibly
* decrypting it if it is password protected.
*
* @opensearch.internal
Expand All @@ -52,9 +57,6 @@ public KeyStoreAwareCommand(String description) {
super(description);
}

/** Arbitrarily chosen maximum passphrase length */
public static final int MAX_PASSPHRASE_LENGTH = 128;

/**
* Reads the keystore password from the {@link Terminal}, prompting for verification where applicable and returns it as a
* {@link SecureString}.
Expand All @@ -69,9 +71,9 @@ protected static SecureString readPassword(Terminal terminal, boolean withVerifi
if (withVerification) {
passwordArray = terminal.readSecret(
"Enter new password for the opensearch keystore (empty for no password): ",
MAX_PASSPHRASE_LENGTH
KeystoreConstants.MAX_PASSPHRASE_LENGTH
);
char[] passwordVerification = terminal.readSecret("Enter same password again: ", MAX_PASSPHRASE_LENGTH);
char[] passwordVerification = terminal.readSecret("Enter same password again: ", KeystoreConstants.MAX_PASSPHRASE_LENGTH);
if (Arrays.equals(passwordArray, passwordVerification) == false) {
throw new UserException(ExitCodes.DATA_ERROR, "Passwords are not equal, exiting.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.LoggingAwareMultiCommand;
import org.opensearch.cli.Terminal;
import org.opensearch.common.cli.LoggingAwareMultiCommand;

/**
* A CLI tool for managing secrets in the OpenSearch keystore.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import org.opensearch.cli.Terminal;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.Terminal;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import joptsimple.OptionSet;
import org.opensearch.cli.Terminal;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

/**
Expand All @@ -43,7 +44,7 @@
*/
public class UpgradeKeyStoreCommand extends BaseKeyStoreCommand {

UpgradeKeyStoreCommand() {
public UpgradeKeyStoreCommand() {
super("Upgrade the keystore format", true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
/**
* Classes implementing a CLI tool for managing secrets in the OpenSearch keystore.
*/
package org.opensearch.common.settings;
package org.opensearch.cli.keystore;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

package org.opensearch.bootstrap;

import org.opensearch.common.settings.KeyStoreCommandTestCase;
import org.opensearch.cli.keystore.KeyStoreCommandTestCase;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.common.settings.SecureSettings;
import org.opensearch.common.settings.Settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.UserException;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.io.ByteArrayInputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.ExitCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.ExitCodes;
import org.opensearch.cli.UserException;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.env.Environment;

import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.UserException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import org.apache.lucene.tests.util.LuceneTestCase;
import org.opensearch.cli.CommandTestCase;
import org.opensearch.cli.KeystoreConstants;
import org.opensearch.cli.MockTerminal;
import org.opensearch.common.io.PathUtilsForTesting;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.env.Environment;
import org.opensearch.env.TestEnvironment;
Expand Down Expand Up @@ -137,4 +141,13 @@ void assertSecureFile(KeyStoreWrapper keystore, String setting, Path file) throw
}

}

public void testPasswordVerificationExceedingLimit() {
String longPassword = randomAlphaOfLength(KeystoreConstants.MAX_PASSPHRASE_LENGTH + 1);
MockTerminal terminal = new MockTerminal();
terminal.addTextInput(longPassword + "\n");

Exception e = expectThrows(Exception.class, () -> KeyStoreAwareCommand.readPassword(terminal, true));
assertTrue(e.getMessage().contains("Input exceeded maximum length"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.apache.lucene.backward_codecs.store.EndiannessReverserUtil;
import org.apache.lucene.codecs.CodecUtil;
Expand All @@ -39,6 +39,7 @@
import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.store.NIOFSDirectory;
import org.opensearch.common.Randomness;
import org.opensearch.common.settings.KeyStoreWrapper;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.core.common.settings.SecureString;
import org.opensearch.env.Environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.ExitCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* GitHub history for details.
*/

package org.opensearch.common.settings;
package org.opensearch.cli.keystore;

import org.opensearch.cli.Command;
import org.opensearch.cli.ExitCodes;
Expand Down
Loading

0 comments on commit 9c20d11

Please sign in to comment.