Skip to content

Commit

Permalink
Fix split package in keystore-cli (#78047)
Browse files Browse the repository at this point in the history
The keystore-cli contains cli command classes for reading and writing
the elasticsearch-keystore. These classes need access to the save
methods of the KeyStoreWrapper, but those are package private. This
change openes those methods to make them available. Note that setString
was already public, only setFile and a couple minor methods were
necessary to open up. Additionally, some methods from bootstrap for
reading the keystore needed to be accessible to tests. This change moves
those to a public utility class, as they were already static.
  • Loading branch information
rjernst authored Sep 21, 2021
1 parent 0c8bbb9 commit 8f2a9b9
Show file tree
Hide file tree
Showing 27 changed files with 117 additions and 88 deletions.
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-keystore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

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

set ES_MAIN_CLASS=org.elasticsearch.common.settings.KeyStoreCli
set ES_MAIN_CLASS=org.elasticsearch.cli.keystore.KeyStoreCli
set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
Expand Down
14 changes: 0 additions & 14 deletions distribution/tools/keystore-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,3 @@ dependencies {
testImplementation "com.google.jimfs:jimfs:${versions.jimfs}"
testRuntimeOnly "com.google.guava:guava:${versions.jimfs_guava}"
}

tasks.named('splitPackagesAudit').configure {
// common.settings is owned by server, this should be keystore specifici
ignoreClasses 'org.elasticsearch.common.settings.AddFileKeyStoreCommand',
'org.elasticsearch.common.settings.AddStringKeyStoreCommand',
'org.elasticsearch.common.settings.BaseKeyStoreCommand',
'org.elasticsearch.common.settings.ChangeKeyStorePasswordCommand',
'org.elasticsearch.common.settings.CreateKeyStoreCommand',
'org.elasticsearch.common.settings.HasPasswordKeyStoreCommand',
'org.elasticsearch.common.settings.KeyStoreCli',
'org.elasticsearch.common.settings.ListKeyStoreCommand',
'org.elasticsearch.common.settings.RemoveSettingKeyStoreCommand',
'org.elasticsearch.common.settings.UpgradeKeyStoreCommand'
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.core.PathUtils;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.env.Environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.core.CheckedFunction;
import org.elasticsearch.env.Environment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
Expand All @@ -15,6 +15,8 @@
import org.elasticsearch.cli.KeyStoreAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.env.Environment;

import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;

import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.env.Environment;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;
import joptsimple.OptionSpec;
Expand All @@ -15,6 +15,8 @@
import org.elasticsearch.cli.KeyStoreAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.env.Environment;

import java.nio.file.Files;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;

import org.elasticsearch.cli.KeyStoreAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.env.Environment;

import java.nio.file.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import org.elasticsearch.cli.LoggingAwareMultiCommand;
import org.elasticsearch.cli.Terminal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;

import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.env.Environment;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.env.Environment;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

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

import joptsimple.OptionSet;

import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.env.Environment;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

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

import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.env.Environment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

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

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

import java.io.ByteArrayInputStream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.bootstrap;
package org.elasticsearch.cli.keystore;

import org.elasticsearch.common.settings.KeyStoreCommandTestCase;
import org.elasticsearch.bootstrap.BootstrapUtil;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.KeyStoreWrapperTests;
import org.elasticsearch.common.settings.SecureSettings;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
Expand Down Expand Up @@ -61,7 +60,7 @@ public void testLoadSecureSettings() throws Exception {
? new ByteArrayInputStream(new String(password).getBytes(StandardCharsets.UTF_8))
: System.in;
assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore")));
try (SecureSettings secureSettings = Bootstrap.loadSecureSettings(env, in)) {
try (SecureSettings secureSettings = BootstrapUtil.loadSecureSettings(env, in)) {
SecureString seedAfterLoad = KeyStoreWrapper.SEED_SETTING.get(Settings.builder().setSecureSettings(secureSettings).build());
assertEquals(seedAfterLoad.toString(), seed.toString());
assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore")));
Expand All @@ -87,7 +86,7 @@ public void testPassphraseTooLong() throws Exception {
expectThrows(
RuntimeException.class,
"Password exceeded maximum length of 10",
() -> Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH)
() -> BootstrapUtil.readPassphrase(stream, MAX_PASSPHRASE_LENGTH)
);
}
}
Expand All @@ -98,14 +97,14 @@ public void testNoPassPhraseProvided() throws Exception {
expectThrows(
RuntimeException.class,
"Keystore passphrase required but none provided.",
() -> Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH)
() -> BootstrapUtil.readPassphrase(stream, MAX_PASSPHRASE_LENGTH)
);
}
}

private void assertPassphraseRead(String source, String expected) {
try (InputStream stream = new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8))) {
SecureString result = Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH);
SecureString result = BootstrapUtil.readPassphrase(stream, MAX_PASSPHRASE_LENGTH);
assertThat(result, equalTo(expected));
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
* Side Public License, v 1.
*/

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

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

import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.UserException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
* Side Public License, v 1.
*/

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

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;

import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.cli.CommandTestCase;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.PathUtilsForTesting;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.env.Environment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import org.apache.lucene.backward_codecs.store.EndiannessReverserUtil;
import org.apache.lucene.codecs.CodecUtil;
Expand All @@ -15,6 +15,8 @@
import org.apache.lucene.store.IOContext;
import org.apache.lucene.store.IndexOutput;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.core.internal.io.IOUtils;
import org.elasticsearch.env.Environment;
import org.elasticsearch.test.ESTestCase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

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

import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
* Side Public License, v 1.
*/

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

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

import java.io.InputStream;
Expand Down
Loading

0 comments on commit 8f2a9b9

Please sign in to comment.