Skip to content

Commit 3a24fe9

Browse files
authored
Move keystore-cli to its own tools project (#40787) (#54294)
This commit moves the keystore cli into its own project, so that the test dependencies can be isolated from the rest of server.
1 parent 0fa1060 commit 3a24fe9

27 files changed

+122
-71
lines changed

distribution/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,9 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
267267
from { project(':distribution:tools:plugin-cli').jar }
268268
from { project(':distribution:tools:plugin-cli').configurations.runtime }
269269
}
270+
into('tools/keystore-cli') {
271+
from { project(':distribution:tools:keystore-cli').jar }
272+
}
270273
if (oss == false) {
271274
into('tools/security-cli') {
272275
from { project(':x-pack:plugin:security:cli').jar }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/bash
22

33
ES_MAIN_CLASS=org.elasticsearch.common.settings.KeyStoreCli \
4+
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli \
45
"`dirname "$0"`"/elasticsearch-cli \
56
"$@"

distribution/src/bin/elasticsearch-keystore.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ setlocal enabledelayedexpansion
44
setlocal enableextensions
55

66
set ES_MAIN_CLASS=org.elasticsearch.common.settings.KeyStoreCli
7+
set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli
78
call "%~dp0elasticsearch-cli.bat" ^
89
%%* ^
910
|| goto exit
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
20+
apply plugin: 'elasticsearch.build'
21+
22+
dependencies {
23+
compileOnly project(":server")
24+
compileOnly project(":libs:elasticsearch-cli")
25+
testCompile project(":test:framework")
26+
testCompile 'com.google.jimfs:jimfs:1.1'
27+
testCompile 'com.google.guava:guava:18.0'
28+
}

server/src/main/java/org/elasticsearch/common/settings/AddFileKeyStoreCommand.java renamed to distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/AddFileKeyStoreCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ class AddFileKeyStoreCommand extends BaseKeyStoreCommand {
4242

4343
AddFileKeyStoreCommand() {
4444
super("Add a file setting to the keystore", false);
45-
this.forceOption = parser.acceptsAll(Arrays.asList("f", "force"),
46-
"Overwrite existing setting without prompting, creating keystore if necessary");
45+
this.forceOption = parser.acceptsAll(
46+
Arrays.asList("f", "force"),
47+
"Overwrite existing setting without prompting, creating keystore if necessary"
48+
);
4749
// jopt simple has issue with multiple non options, so we just get one set of them here
4850
// and convert to File when necessary
4951
// see https://github.com/jopt-simple/jopt-simple/issues/103

server/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java renamed to distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ class AddStringKeyStoreCommand extends BaseKeyStoreCommand {
4848
AddStringKeyStoreCommand() {
4949
super("Add a string settings to the keystore", false);
5050
this.stdinOption = parser.acceptsAll(Arrays.asList("x", "stdin"), "Read setting values from stdin");
51-
this.forceOption = parser.acceptsAll(Arrays.asList("f", "force"),
52-
"Overwrite existing setting without prompting, creating keystore if necessary");
51+
this.forceOption = parser.acceptsAll(
52+
Arrays.asList("f", "force"),
53+
"Overwrite existing setting without prompting, creating keystore if necessary"
54+
);
5355
this.arguments = parser.nonOptions("setting names");
5456
}
5557

server/src/main/java/org/elasticsearch/common/settings/CreateKeyStoreCommand.java renamed to distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/CreateKeyStoreCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class CreateKeyStoreCommand extends KeyStoreAwareCommand {
4545

4646
@Override
4747
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
48-
try (SecureString password = options.has(passwordOption) ?
49-
readPassword(terminal, true) : new SecureString(new char[0])) {
48+
try (SecureString password = options.has(passwordOption) ? readPassword(terminal, true) : new SecureString(new char[0])) {
5049
Path keystoreFile = KeyStoreWrapper.keystorePath(env.configFile());
5150
if (Files.exists(keystoreFile)) {
5251
if (terminal.promptYesNo("An elasticsearch keystore already exists. Overwrite?", false) == false) {

server/src/main/java/org/elasticsearch/common/settings/ListKeyStoreCommand.java renamed to distribution/tools/keystore-cli/src/main/java/org/elasticsearch/common/settings/ListKeyStoreCommand.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.common.settings;
2121

22-
2322
import java.util.ArrayList;
2423
import java.util.Collections;
2524
import java.util.List;

server/src/test/java/org/elasticsearch/bootstrap/BootstrapTests.java renamed to distribution/tools/keystore-cli/src/test/java/org/elasticsearch/bootstrap/BootstrapTests.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,22 @@ public void testReadCharsFromStdin() throws Exception {
9090
public void testPassphraseTooLong() throws Exception {
9191
byte[] source = "hellohello!\n".getBytes(StandardCharsets.UTF_8);
9292
try (InputStream stream = new ByteArrayInputStream(source)) {
93-
expectThrows(RuntimeException.class, "Password exceeded maximum length of 10",
94-
() -> Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH));
93+
expectThrows(
94+
RuntimeException.class,
95+
"Password exceeded maximum length of 10",
96+
() -> Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH)
97+
);
9598
}
9699
}
97100

98101
public void testNoPassPhraseProvided() throws Exception {
99102
byte[] source = "\r\n".getBytes(StandardCharsets.UTF_8);
100103
try (InputStream stream = new ByteArrayInputStream(source)) {
101-
expectThrows(RuntimeException.class, "Keystore passphrase required but none provided.",
102-
() -> Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH));
104+
expectThrows(
105+
RuntimeException.class,
106+
"Keystore passphrase required but none provided.",
107+
() -> Bootstrap.readPassphrase(stream, MAX_PASSPHRASE_LENGTH)
108+
);
103109
}
104110
}
105111

server/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java renamed to distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ public void testSpecialCharacterInName() throws Exception {
256256
final String key = randomAlphaOfLength(4) + '@' + randomAlphaOfLength(4);
257257
final UserException e = expectThrows(UserException.class, () -> execute(key));
258258
final String exceptionString = "Setting name [" + key + "] does not match the allowed setting name pattern [[A-Za-z0-9_\\-.]+]";
259-
assertThat(
260-
e,
261-
hasToString(containsString(exceptionString)));
259+
assertThat(e, hasToString(containsString(exceptionString)));
262260
}
263261

264262
public void testAddToUnprotectedKeystore() throws Exception {

server/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java renamed to distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/KeyStoreCommandTestCase.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ void assertSecureFile(KeyStoreWrapper keystore, String setting, Path file) throw
114114
}
115115
int eof = input.read();
116116
if (eof != -1) {
117-
fail("Found extra bytes in file stream from keystore, expected " + expectedBytes.length +
118-
" bytes but found 0x" + Integer.toHexString(eof));
117+
fail(
118+
"Found extra bytes in file stream from keystore, expected "
119+
+ expectedBytes.length
120+
+ " bytes but found 0x"
121+
+ Integer.toHexString(eof)
122+
);
119123
}
120124
}
121125

server/src/test/java/org/elasticsearch/common/settings/KeyStoreWrapperTests.java renamed to distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/KeyStoreWrapperTests.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ public void testCannotReadStringFromClosedKeystore() throws Exception {
137137
keystore.close();
138138

139139
assertThat(keystore.getSettingNames(), Matchers.hasItem(KeyStoreWrapper.SEED_SETTING.getKey()));
140-
final IllegalStateException exception = expectThrows(IllegalStateException.class,
141-
() -> keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey()));
140+
final IllegalStateException exception = expectThrows(
141+
IllegalStateException.class,
142+
() -> keystore.getString(KeyStoreWrapper.SEED_SETTING.getKey())
143+
);
142144
assertThat(exception.getMessage(), containsString("closed"));
143145
}
144146

@@ -306,9 +308,13 @@ private void possiblyAlterSecretString(DataOutputStream output, int truncLength)
306308
output.write(secret_value);
307309
}
308310

309-
private void possiblyAlterEncryptedBytes(IndexOutput indexOutput, byte[] salt, byte[] iv, byte[] encryptedBytes, int
310-
truncEncryptedDataLength)
311-
throws Exception {
311+
private void possiblyAlterEncryptedBytes(
312+
IndexOutput indexOutput,
313+
byte[] salt,
314+
byte[] iv,
315+
byte[] encryptedBytes,
316+
int truncEncryptedDataLength
317+
) throws Exception {
312318
indexOutput.writeInt(4 + salt.length + 4 + iv.length + 4 + encryptedBytes.length);
313319
indexOutput.writeInt(salt.length);
314320
indexOutput.writeBytes(salt, salt.length);
@@ -451,8 +457,10 @@ public void testStringAndFileDistinction() throws Exception {
451457
public void testLegacyV3() throws GeneralSecurityException, IOException {
452458
final Path configDir = createTempDir();
453459
final Path keystore = configDir.resolve("elasticsearch.keystore");
454-
try (InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
455-
OutputStream os = Files.newOutputStream(keystore)) {
460+
try (
461+
InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
462+
OutputStream os = Files.newOutputStream(keystore)
463+
) {
456464
final byte[] buffer = new byte[4096];
457465
int readBytes;
458466
while ((readBytes = is.read(buffer)) > 0) {

server/src/test/java/org/elasticsearch/common/settings/UpgradeKeyStoreCommandTests.java renamed to distribution/tools/keystore-cli/src/test/java/org/elasticsearch/common/settings/UpgradeKeyStoreCommandTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ protected Environment createEnv(final Map<String, String> settings) {
5050

5151
public void testKeystoreUpgrade() throws Exception {
5252
final Path keystore = KeyStoreWrapper.keystorePath(env.configFile());
53-
try (InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
54-
OutputStream os = Files.newOutputStream(keystore)) {
53+
try (
54+
InputStream is = KeyStoreWrapperTests.class.getResourceAsStream("/format-v3-elasticsearch.keystore");
55+
OutputStream os = Files.newOutputStream(keystore)
56+
) {
5557
final byte[] buffer = new byte[4096];
5658
int read;
5759
while ((read = is.read(buffer, 0, buffer.length)) >= 0) {

server/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ dependencies {
132132
exclude group: 'org.elasticsearch', module: 'server'
133133
}
134134

135-
testCompile 'com.google.jimfs:jimfs:1.1'
136135
testCompile 'com.google.guava:guava:18.0'
137136
}
138137

server/src/test/java/org/elasticsearch/cluster/metadata/MetaDataIndexStateServiceTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.elasticsearch.cluster.metadata;
2121

22-
import com.google.common.collect.ImmutableList;
2322
import org.elasticsearch.Version;
2423
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse;
2524
import org.elasticsearch.action.admin.indices.close.CloseIndexResponse.IndexResult;
@@ -455,7 +454,8 @@ private static ClusterState addRestoredIndex(final String index, final int numSh
455454

456455
final Snapshot snapshot = new Snapshot(randomAlphaOfLength(10), new SnapshotId(randomAlphaOfLength(5), randomAlphaOfLength(5)));
457456
final RestoreInProgress.Entry entry =
458-
new RestoreInProgress.Entry("_uuid", snapshot, RestoreInProgress.State.INIT, ImmutableList.of(index), shardsBuilder.build());
457+
new RestoreInProgress.Entry("_uuid", snapshot, RestoreInProgress.State.INIT,
458+
Collections.singletonList(index), shardsBuilder.build());
459459
return ClusterState.builder(newState)
460460
.putCustom(RestoreInProgress.TYPE, new RestoreInProgress.Builder().add(entry).build())
461461
.build();

server/src/test/java/org/elasticsearch/index/mapper/ObjectMapperMergeTests.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919
package org.elasticsearch.index.mapper;
2020

21-
import com.google.common.collect.ImmutableMap;
2221
import org.elasticsearch.Version;
2322
import org.elasticsearch.common.settings.Settings;
2423
import org.elasticsearch.index.mapper.FieldMapper.CopyTo;
@@ -27,6 +26,8 @@
2726
import org.elasticsearch.test.ESTestCase;
2827
import org.junit.AfterClass;
2928

29+
import java.util.Collections;
30+
import java.util.HashMap;
3031
import java.util.Map;
3132

3233
import static java.util.Collections.emptyMap;
@@ -38,11 +39,7 @@ public class ObjectMapperMergeTests extends ESTestCase {
3839
private static FieldMapper barFieldMapper = createTextFieldMapper("bar");
3940
private static FieldMapper bazFieldMapper = createTextFieldMapper("baz");
4041

41-
private static RootObjectMapper rootObjectMapper = createRootObjectMapper(
42-
"type1", true, ImmutableMap.of(
43-
"disabled", createObjectMapper("disabled", false, emptyMap()),
44-
"foo", createObjectMapper("foo", true, ImmutableMap.of(
45-
"bar", barFieldMapper))));
42+
private static RootObjectMapper rootObjectMapper = createMapping(false, true, true, false);
4643

4744
@AfterClass
4845
public static void cleanupReferences() {
@@ -51,14 +48,24 @@ public static void cleanupReferences() {
5148
rootObjectMapper = null;
5249
}
5350

51+
private static RootObjectMapper createMapping(boolean disabledFieldEnabled, boolean fooFieldEnabled,
52+
boolean includeBarField, boolean includeBazField) {
53+
Map<String, Mapper> mappers = new HashMap<>();
54+
mappers.put("disabled", createObjectMapper("disabled", disabledFieldEnabled, emptyMap()));
55+
Map<String, Mapper> fooMappers = new HashMap<>();
56+
if (includeBarField) {
57+
fooMappers.put("bar", barFieldMapper);
58+
}
59+
if (includeBazField) {
60+
fooMappers.put("baz", bazFieldMapper);
61+
}
62+
mappers.put("foo", createObjectMapper("foo", fooFieldEnabled, Collections.unmodifiableMap(fooMappers)));
63+
return createRootObjectMapper("type1", true, Collections.unmodifiableMap(mappers));
64+
}
65+
5466
public void testMerge() {
5567
// GIVEN an enriched mapping with "baz" new field
56-
ObjectMapper mergeWith = createRootObjectMapper(
57-
"type1", true, ImmutableMap.of(
58-
"disabled", createObjectMapper("disabled", false, emptyMap()),
59-
"foo", createObjectMapper("foo", true, ImmutableMap.of(
60-
"bar", barFieldMapper,
61-
"baz", bazFieldMapper))));
68+
ObjectMapper mergeWith = createMapping(false, true, true, true);
6269

6370
// WHEN merging mappings
6471
final ObjectMapper merged = rootObjectMapper.merge(mergeWith);
@@ -71,10 +78,7 @@ public void testMerge() {
7178

7279
public void testMergeWhenDisablingField() {
7380
// GIVEN a mapping with "foo" field disabled
74-
ObjectMapper mergeWith = createRootObjectMapper(
75-
"type1", true, ImmutableMap.of(
76-
"disabled", createObjectMapper("disabled", false, emptyMap()),
77-
"foo", createObjectMapper("foo", false, emptyMap())));
81+
ObjectMapper mergeWith = createMapping(false, false, false, false);
7882

7983
// WHEN merging mappings
8084
// THEN a MapperException is thrown with an excepted message
@@ -84,11 +88,7 @@ public void testMergeWhenDisablingField() {
8488

8589
public void testMergeWhenEnablingField() {
8690
// GIVEN a mapping with "disabled" field enabled
87-
ObjectMapper mergeWith = createRootObjectMapper(
88-
"type1", true, ImmutableMap.of(
89-
"disabled", createObjectMapper("disabled", true, emptyMap()),
90-
"foo", createObjectMapper("foo", true, ImmutableMap.of(
91-
"bar", barFieldMapper))));
91+
ObjectMapper mergeWith = createMapping(true, true, true, false);
9292

9393
// WHEN merging mappings
9494
// THEN a MapperException is thrown with an excepted message

0 commit comments

Comments
 (0)