Skip to content

Commit a3f8abb

Browse files
authored
Switch usages from KeyStoreWrapper to SecureSettings (#92339)
Move away from using the KeyStoreWrapper type directly and switch to SecureSettings, where possible.
1 parent 60870d6 commit a3f8abb

File tree

9 files changed

+33
-32
lines changed

9 files changed

+33
-32
lines changed

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/APMJvmOptions.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import org.elasticsearch.cli.ExitCodes;
1414
import org.elasticsearch.cli.UserException;
1515
import org.elasticsearch.common.Strings;
16-
import org.elasticsearch.common.settings.KeyStoreWrapper;
16+
import org.elasticsearch.common.settings.SecureSettings;
1717
import org.elasticsearch.common.settings.SecureString;
1818
import org.elasticsearch.common.settings.Settings;
1919
import org.elasticsearch.core.Nullable;
@@ -133,11 +133,10 @@ class APMJvmOptions {
133133
* because it will be deleted once Elasticsearch starts.
134134
*
135135
* @param settings the Elasticsearch settings to consider
136-
* @param keystore a wrapper to access the keystore, or null if there is no keystore
136+
* @param secrets a wrapper to access the secrets, or null if there is no secrets
137137
* @param tmpdir Elasticsearch's temporary directory, where the config file will be written
138138
*/
139-
static List<String> apmJvmOptions(Settings settings, @Nullable KeyStoreWrapper keystore, Path tmpdir) throws UserException,
140-
IOException {
139+
static List<String> apmJvmOptions(Settings settings, @Nullable SecureSettings secrets, Path tmpdir) throws UserException, IOException {
141140
final Path agentJar = findAgentJar();
142141

143142
if (agentJar == null) {
@@ -158,8 +157,8 @@ static List<String> apmJvmOptions(Settings settings, @Nullable KeyStoreWrapper k
158157
}
159158
}
160159

161-
if (keystore != null) {
162-
extractSecureSettings(keystore, propertiesMap);
160+
if (secrets != null) {
161+
extractSecureSettings(secrets, propertiesMap);
163162
}
164163
final Map<String, String> dynamicSettings = extractDynamicSettings(propertiesMap);
165164

@@ -180,11 +179,11 @@ static String agentCommandLineOption(Path agentJar, Path tmpPropertiesFile) {
180179
return "-javaagent:" + agentJar + "=c=" + tmpPropertiesFile;
181180
}
182181

183-
private static void extractSecureSettings(KeyStoreWrapper keystore, Map<String, String> propertiesMap) {
184-
final Set<String> settingNames = keystore.getSettingNames();
182+
private static void extractSecureSettings(SecureSettings secrets, Map<String, String> propertiesMap) {
183+
final Set<String> settingNames = secrets.getSettingNames();
185184
for (String key : List.of("api_key", "secret_token")) {
186185
if (settingNames.contains("tracing.apm." + key)) {
187-
try (SecureString token = keystore.getString("tracing.apm." + key)) {
186+
try (SecureString token = secrets.getString("tracing.apm." + key)) {
188187
propertiesMap.put(key, token.toString());
189188
}
190189
}

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/JvmOptionsParser.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import org.elasticsearch.bootstrap.ServerArgs;
1212
import org.elasticsearch.cli.ExitCodes;
1313
import org.elasticsearch.cli.UserException;
14-
import org.elasticsearch.common.settings.KeyStoreWrapper;
14+
import org.elasticsearch.common.settings.SecureSettings;
1515

1616
import java.io.BufferedReader;
1717
import java.io.IOException;
@@ -70,7 +70,7 @@ SortedMap<Integer, String> invalidLines() {
7070
* files in the {@code jvm.options.d} directory, and the options given by the {@code ES_JAVA_OPTS} environment
7171
* variable.
7272
*
73-
* @param keystore the installation's keystore
73+
* @param secrets the installation's secrets
7474
* @param configDir the ES config dir
7575
* @param tmpDir the directory that should be passed to {@code -Djava.io.tmpdir}
7676
* @param envOptions the options passed through the ES_JAVA_OPTS env var
@@ -79,7 +79,7 @@ SortedMap<Integer, String> invalidLines() {
7979
* @throws IOException if there is a problem reading any of the files
8080
* @throws UserException if there is a problem parsing the `jvm.options` file or `jvm.options.d` files
8181
*/
82-
static List<String> determineJvmOptions(ServerArgs args, KeyStoreWrapper keystore, Path configDir, Path tmpDir, String envOptions)
82+
static List<String> determineJvmOptions(ServerArgs args, SecureSettings secrets, Path configDir, Path tmpDir, String envOptions)
8383
throws InterruptedException, IOException, UserException {
8484

8585
final JvmOptionsParser parser = new JvmOptionsParser();
@@ -89,7 +89,7 @@ static List<String> determineJvmOptions(ServerArgs args, KeyStoreWrapper keystor
8989
substitutions.put("ES_PATH_CONF", configDir.toString());
9090

9191
try {
92-
return parser.jvmOptions(args, keystore, configDir, tmpDir, envOptions, substitutions);
92+
return parser.jvmOptions(args, secrets, configDir, tmpDir, envOptions, substitutions);
9393
} catch (final JvmOptionsFileParserException e) {
9494
final String errorMessage = String.format(
9595
Locale.ROOT,
@@ -120,7 +120,7 @@ static List<String> determineJvmOptions(ServerArgs args, KeyStoreWrapper keystor
120120

121121
private List<String> jvmOptions(
122122
ServerArgs args,
123-
KeyStoreWrapper keystore,
123+
SecureSettings secrets,
124124
final Path config,
125125
Path tmpDir,
126126
final String esJavaOpts,
@@ -141,7 +141,7 @@ private List<String> jvmOptions(
141141
final List<String> ergonomicJvmOptions = JvmErgonomics.choose(substitutedJvmOptions);
142142
final List<String> systemJvmOptions = SystemJvmOptions.systemJvmOptions();
143143

144-
final List<String> apmOptions = APMJvmOptions.apmJvmOptions(args.nodeSettings(), keystore, tmpDir);
144+
final List<String> apmOptions = APMJvmOptions.apmJvmOptions(args.nodeSettings(), secrets, tmpDir);
145145

146146
final List<String> finalJvmOptions = new ArrayList<>(
147147
systemJvmOptions.size() + substitutedJvmOptions.size() + ergonomicJvmOptions.size() + apmOptions.size()

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/ServerCli.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.elasticsearch.cli.UserException;
2424
import org.elasticsearch.common.cli.EnvironmentAwareCommand;
2525
import org.elasticsearch.common.settings.KeyStoreWrapper;
26+
import org.elasticsearch.common.settings.SecureSettings;
2627
import org.elasticsearch.common.settings.SecureString;
2728
import org.elasticsearch.env.Environment;
2829
import org.elasticsearch.monitor.jvm.JvmInfo;
@@ -229,7 +230,7 @@ protected Command loadTool(String toolname, String libs) {
229230
}
230231

231232
// protected to allow tests to override
232-
protected ServerProcess startServer(Terminal terminal, ProcessInfo processInfo, ServerArgs args, KeyStoreWrapper keystore)
233+
protected ServerProcess startServer(Terminal terminal, ProcessInfo processInfo, ServerArgs args, SecureSettings keystore)
233234
throws UserException {
234235
return ServerProcess.start(terminal, processInfo, args, keystore);
235236
}

distribution/tools/server-cli/src/main/java/org/elasticsearch/server/cli/ServerProcess.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import org.elasticsearch.cli.Terminal;
1616
import org.elasticsearch.cli.UserException;
1717
import org.elasticsearch.common.io.stream.OutputStreamStreamOutput;
18-
import org.elasticsearch.common.settings.KeyStoreWrapper;
18+
import org.elasticsearch.common.settings.SecureSettings;
1919
import org.elasticsearch.core.IOUtils;
2020
import org.elasticsearch.core.PathUtils;
2121
import org.elasticsearch.core.SuppressForbidden;
@@ -37,7 +37,7 @@
3737
/**
3838
* A helper to control a {@link Process} running the main Elasticsearch server.
3939
*
40-
* <p> The process can be started by calling {@link #start(Terminal, ProcessInfo, ServerArgs, KeyStoreWrapper)}.
40+
* <p> The process can be started by calling {@link #start(Terminal, ProcessInfo, ServerArgs, SecureSettings)}.
4141
* The process is controlled by internally sending arguments and control signals on stdin,
4242
* and receiving control signals on stderr. The start method does not return until the
4343
* server is ready to process requests and has exited the bootstrap thread.
@@ -67,7 +67,7 @@ public class ServerProcess {
6767

6868
// this allows mocking the process building by tests
6969
interface OptionsBuilder {
70-
List<String> getJvmOptions(ServerArgs args, KeyStoreWrapper keyStoreWrapper, Path configDir, Path tmpDir, String envOptions)
70+
List<String> getJvmOptions(ServerArgs args, SecureSettings secrets, Path configDir, Path tmpDir, String envOptions)
7171
throws InterruptedException, IOException, UserException;
7272
}
7373

@@ -82,21 +82,21 @@ interface ProcessStarter {
8282
* @param terminal A terminal to connect the standard inputs and outputs to for the new process.
8383
* @param processInfo Info about the current process, for passing through to the subprocess.
8484
* @param args Arguments to the server process.
85-
* @param keystore A keystore for accessing secrets.
85+
* @param secrets A secrets for accessing secrets.
8686
* @return A running server process that is ready for requests
8787
* @throws UserException If the process failed during bootstrap
8888
*/
89-
public static ServerProcess start(Terminal terminal, ProcessInfo processInfo, ServerArgs args, KeyStoreWrapper keystore)
89+
public static ServerProcess start(Terminal terminal, ProcessInfo processInfo, ServerArgs args, SecureSettings secrets)
9090
throws UserException {
91-
return start(terminal, processInfo, args, keystore, JvmOptionsParser::determineJvmOptions, ProcessBuilder::start);
91+
return start(terminal, processInfo, args, secrets, JvmOptionsParser::determineJvmOptions, ProcessBuilder::start);
9292
}
9393

9494
// package private so tests can mock options building and process starting
9595
static ServerProcess start(
9696
Terminal terminal,
9797
ProcessInfo processInfo,
9898
ServerArgs args,
99-
KeyStoreWrapper keystore,
99+
SecureSettings secrets,
100100
OptionsBuilder optionsBuilder,
101101
ProcessStarter processStarter
102102
) throws UserException {
@@ -105,7 +105,7 @@ static ServerProcess start(
105105

106106
boolean success = false;
107107
try {
108-
jvmProcess = createProcess(args, keystore, processInfo, args.configDir(), optionsBuilder, processStarter);
108+
jvmProcess = createProcess(args, secrets, processInfo, args.configDir(), optionsBuilder, processStarter);
109109
errorPump = new ErrorPumpThread(terminal.getErrorWriter(), jvmProcess.getErrorStream());
110110
errorPump.start();
111111
sendArgs(args, jvmProcess.getOutputStream());
@@ -199,7 +199,7 @@ private void sendShutdownMarker() {
199199

200200
private static Process createProcess(
201201
ServerArgs args,
202-
KeyStoreWrapper keystore,
202+
SecureSettings secrets,
203203
ProcessInfo processInfo,
204204
Path configDir,
205205
OptionsBuilder optionsBuilder,
@@ -211,7 +211,7 @@ private static Process createProcess(
211211
envVars.put("LIBFFI_TMPDIR", tempDir.toString());
212212
}
213213

214-
List<String> jvmOptions = optionsBuilder.getJvmOptions(args, keystore, configDir, tempDir, envVars.remove("ES_JAVA_OPTS"));
214+
List<String> jvmOptions = optionsBuilder.getJvmOptions(args, secrets, configDir, tempDir, envVars.remove("ES_JAVA_OPTS"));
215215
// also pass through distribution type
216216
jvmOptions.add("-Des.distribution.type=" + processInfo.sysprops().get("es.distribution.type"));
217217

distribution/tools/server-cli/src/test/java/org/elasticsearch/server/cli/ServerCliTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.cli.UserException;
2323
import org.elasticsearch.common.cli.EnvironmentAwareCommand;
2424
import org.elasticsearch.common.settings.KeyStoreWrapper;
25+
import org.elasticsearch.common.settings.SecureSettings;
2526
import org.elasticsearch.common.settings.Settings;
2627
import org.elasticsearch.env.Environment;
2728
import org.elasticsearch.monitor.jvm.JvmInfo;
@@ -436,7 +437,7 @@ protected Command loadTool(String toolname, String libs) {
436437
}
437438

438439
@Override
439-
protected ServerProcess startServer(Terminal terminal, ProcessInfo processInfo, ServerArgs args, KeyStoreWrapper keystore) {
440+
protected ServerProcess startServer(Terminal terminal, ProcessInfo processInfo, ServerArgs args, SecureSettings secrets) {
440441
if (argsValidator != null) {
441442
argsValidator.accept(args);
442443
}

server/src/main/java/org/elasticsearch/common/settings/SecureSettings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public interface SecureSettings extends Closeable {
2626
Set<String> getSettingNames();
2727

2828
/** Return a string setting. The {@link SecureString} should be closed once it is used. */
29-
SecureString getString(String setting) throws GeneralSecurityException;
29+
SecureString getString(String setting);
3030

3131
/** Return a file setting. The {@link InputStream} should be closed once it is used. */
3232
InputStream getFile(String setting) throws GeneralSecurityException;

server/src/main/java/org/elasticsearch/common/settings/Settings.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ public Set<String> getSettingNames() {
15031503
}
15041504

15051505
@Override
1506-
public SecureString getString(String setting) throws GeneralSecurityException {
1506+
public SecureString getString(String setting) {
15071507
return delegate.getString(addPrefix.apply(setting));
15081508
}
15091509

server/src/main/java/org/elasticsearch/common/settings/StatelessSecureSettings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public Set<String> getSettingNames() {
6262
}
6363

6464
@Override
65-
public SecureString getString(String setting) throws GeneralSecurityException {
65+
public SecureString getString(String setting) {
6666
return new SecureString(STATELESS_SECURE_SETTINGS.getConcreteSetting(PREFIX + setting).get(settings).toCharArray());
6767
}
6868

@@ -74,7 +74,7 @@ public InputStream getFile(String setting) throws GeneralSecurityException {
7474
}
7575

7676
@Override
77-
public byte[] getSHA256Digest(String setting) throws GeneralSecurityException {
77+
public byte[] getSHA256Digest(String setting) {
7878
return MessageDigests.sha256()
7979
.digest(STATELESS_SECURE_SETTINGS.getConcreteSetting(PREFIX + setting).get(settings).getBytes(StandardCharsets.UTF_8));
8080
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/NotificationServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public boolean isLoaded() {
253253
}
254254

255255
@Override
256-
public SecureString getString(String setting) throws GeneralSecurityException {
256+
public SecureString getString(String setting) {
257257
return new SecureString(secureSettingsMap.get(setting));
258258
}
259259

0 commit comments

Comments
 (0)