Skip to content

Commit 525c768

Browse files
committed
Persist created keystore on startup unless keystore is present (#26253)
We already added the functionality to create a new keystore on startup in #26126 but apparently missed to persist the keystore. This change adds peristence and adds a test for the boostrap loading.
1 parent c99f5ce commit 525c768

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

core/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.elasticsearch.common.PidFile;
3636
import org.elasticsearch.common.SuppressForbidden;
3737
import org.elasticsearch.common.inject.CreationException;
38-
import org.elasticsearch.common.logging.DeprecationLogger;
3938
import org.elasticsearch.common.logging.ESLoggerFactory;
4039
import org.elasticsearch.common.logging.LogConfigurator;
4140
import org.elasticsearch.common.logging.Loggers;
@@ -220,7 +219,7 @@ protected void validateNodeBeforeAcceptingRequests(
220219
};
221220
}
222221

223-
private static SecureSettings loadSecureSettings(Environment initialEnv) throws BootstrapException {
222+
static SecureSettings loadSecureSettings(Environment initialEnv) throws BootstrapException {
224223
final KeyStoreWrapper keystore;
225224
try {
226225
keystore = KeyStoreWrapper.load(initialEnv.configFile());
@@ -231,7 +230,9 @@ private static SecureSettings loadSecureSettings(Environment initialEnv) throws
231230
try {
232231
if (keystore == null) {
233232
// create it, we always want one! we use an empty passphrase, but a user can change this later if they want.
234-
KeyStoreWrapper.create(new char[0]);
233+
KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.create(new char[0]);
234+
keyStoreWrapper.save(initialEnv.configFile());
235+
return keyStoreWrapper;
235236
} else {
236237
keystore.decrypt(new char[0] /* TODO: read password from stdin */);
237238
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.elasticsearch.bootstrap;
20+
21+
import org.apache.lucene.util.IOUtils;
22+
import org.elasticsearch.common.settings.KeyStoreCommandTestCase;
23+
import org.elasticsearch.common.settings.KeyStoreWrapper;
24+
import org.elasticsearch.common.settings.SecureSettings;
25+
import org.elasticsearch.common.settings.SecureString;
26+
import org.elasticsearch.common.settings.Settings;
27+
import org.elasticsearch.env.Environment;
28+
import org.elasticsearch.test.ESTestCase;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
32+
import java.io.IOException;
33+
import java.nio.file.FileSystem;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.util.ArrayList;
37+
import java.util.List;
38+
39+
public class BootstrapTests extends ESTestCase {
40+
Environment env;
41+
List<FileSystem> fileSystems = new ArrayList<>();
42+
43+
@After
44+
public void closeMockFileSystems() throws IOException {
45+
IOUtils.close(fileSystems);
46+
}
47+
48+
@Before
49+
public void setupEnv() throws IOException {
50+
env = KeyStoreCommandTestCase.setupEnv(true, fileSystems);
51+
}
52+
53+
public void testLoadSecureSettingsCreatesKeystore() throws BootstrapException {
54+
final Path configPath = env.configFile();
55+
assertFalse(Files.exists(configPath.resolve("elasticsearch.keystore")));
56+
Bootstrap.loadSecureSettings(env);
57+
assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore")));
58+
}
59+
60+
public void testLoadSecureSettings() throws Exception {
61+
final Path configPath = env.configFile();
62+
final SecureString seed;
63+
try (KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.create(new char[0])) {
64+
seed = KeyStoreWrapper.SEED_SETTING.get(Settings.builder().setSecureSettings(keyStoreWrapper).build());
65+
assertNotNull(seed);
66+
assertTrue(seed.length() > 0);
67+
keyStoreWrapper.save(configPath);
68+
}
69+
assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore")));
70+
try (SecureSettings secureSettings = Bootstrap.loadSecureSettings(env)) {
71+
SecureString seedAfterLoad = KeyStoreWrapper.SEED_SETTING.get(Settings.builder().setSecureSettings(secureSettings).build());
72+
assertEquals(seedAfterLoad.toString(), seed.toString());
73+
assertTrue(Files.exists(configPath.resolve("elasticsearch.keystore")));
74+
}
75+
}
76+
}

core/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public void setupEnv() throws IOException {
5858
env = setupEnv(true, fileSystems); // default to posix, but tests may call setupEnv(false) to overwrite
5959
}
6060

61-
static Environment setupEnv(boolean posix, List<FileSystem> fileSystems) throws IOException {
61+
public static Environment setupEnv(boolean posix, List<FileSystem> fileSystems) throws IOException {
6262
final Configuration configuration;
6363
if (posix) {
6464
configuration = Configuration.unix().toBuilder().setAttributeViews("basic", "owner", "posix", "unix").build();

0 commit comments

Comments
 (0)