diff --git a/elasticsearch/license/licensor/bin/key-pair-generator b/elasticsearch/license/licensor/bin/key-pair-generator index 08785726269..66eb8cc3c53 100755 --- a/elasticsearch/license/licensor/bin/key-pair-generator +++ b/elasticsearch/license/licensor/bin/key-pair-generator @@ -34,25 +34,5 @@ else JAVA=`which java` fi - -# Parse any long getopt options and put them into properties before calling getopt below -# Be dash compatible to make sure running under ubuntu works -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - -D*=*) - properties="$properties $1" - shift 1; COUNT=$(($COUNT+1)) - ;; - -D*) - properties="$properties $1=$2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m $properties -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool "$@" +exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.KeyPairGeneratorTool "$@" diff --git a/elasticsearch/license/licensor/bin/license-generator b/elasticsearch/license/licensor/bin/license-generator index 15410bb5a0c..c36ee2aa3e1 100755 --- a/elasticsearch/license/licensor/bin/license-generator +++ b/elasticsearch/license/licensor/bin/license-generator @@ -34,23 +34,4 @@ else JAVA=`which java` fi -# Parse any long getopt options and put them into properties before calling getopt below -# Be dash compatible to make sure running under ubuntu works -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - -D*=*) - properties="$properties $1" - shift 1; COUNT=$(($COUNT+1)) - ;; - -D*) - properties="$properties $1=$2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m $properties -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.LicenseGeneratorTool "$@" +exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.LicenseGeneratorTool "$@" diff --git a/elasticsearch/license/licensor/bin/verify-license b/elasticsearch/license/licensor/bin/verify-license index 16a12e6089c..cbb3284ae1d 100755 --- a/elasticsearch/license/licensor/bin/verify-license +++ b/elasticsearch/license/licensor/bin/verify-license @@ -34,24 +34,5 @@ else JAVA=`which java` fi -# Parse any long getopt options and put them into properties before calling getopt below -# Be dash compatible to make sure running under ubuntu works -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - -D*=*) - properties="$properties $1" - shift 1; COUNT=$(($COUNT+1)) - ;; - -D*) - properties="$properties $1=$2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m $properties -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.LicenseVerificationTool "$@" +exec "$JAVA" $JAVA_OPTS -Xmx64m -Xms16m -cp "$LICENSE_CLASSPATH" -Des.path.home="`pwd`" org.elasticsearch.license.licensor.tools.LicenseVerificationTool "$@" diff --git a/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java b/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java index 01e3d580999..f4ba4af817a 100644 --- a/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java +++ b/elasticsearch/license/licensor/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java @@ -5,22 +5,21 @@ */ package org.elasticsearch.license.licensor.tools; -import java.io.File; +import joptsimple.OptionSet; +import joptsimple.OptionSpec; +import org.elasticsearch.cli.Command; +import org.elasticsearch.cli.ExitCodes; +import org.elasticsearch.cli.Terminal; +import org.elasticsearch.cli.UserError; +import org.elasticsearch.common.SuppressForbidden; +import org.elasticsearch.common.io.PathUtils; + import java.nio.file.Files; import java.nio.file.Path; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.SecureRandom; -import joptsimple.OptionSet; -import joptsimple.OptionSpec; -import org.elasticsearch.cli.Command; -import org.elasticsearch.cli.ExitCodes; -import org.elasticsearch.cli.UserError; -import org.elasticsearch.cli.Terminal; -import org.elasticsearch.common.SuppressForbidden; -import org.elasticsearch.common.io.PathUtils; - import static org.elasticsearch.license.core.CryptUtils.writeEncryptedPrivateKey; import static org.elasticsearch.license.core.CryptUtils.writeEncryptedPublicKey; diff --git a/elasticsearch/qa/build.gradle b/elasticsearch/qa/build.gradle index 4be011cce50..3c9541e6178 100644 --- a/elasticsearch/qa/build.gradle +++ b/elasticsearch/qa/build.gradle @@ -1,5 +1,5 @@ subprojects { - project.afterEvaluate { + tasks.withType(org.elasticsearch.gradle.precommit.LicenseHeadersTask) { // someone figure out what the x-plugins logic should be project.licenseHeaders.enabled = false } diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/build.gradle b/elasticsearch/qa/smoke-test-monitoring-with-shield/build.gradle index 85715ca77ce..5c9b4d832d6 100644 --- a/elasticsearch/qa/smoke-test-monitoring-with-shield/build.gradle +++ b/elasticsearch/qa/smoke-test-monitoring-with-shield/build.gradle @@ -1,41 +1,50 @@ -apply plugin: 'elasticsearch.rest-test' +subprojects { + apply plugin: 'elasticsearch.rest-test' -dependencies { - testCompile project(path: ':x-plugins:elasticsearch:x-pack', configuration: 'runtime') -} + dependencies { + testCompile project(path: ':x-plugins:elasticsearch:x-pack', configuration: 'runtime') + } -// bring in monitoring rest test suite -task copyMonitoringRestTests(type: Copy) { - into project.sourceSets.test.output.resourcesDir - from project(':x-plugins:elasticsearch:x-pack').sourceSets.test.resources.srcDirs - include 'rest-api-spec/test/monitoring/**' -} + // bring in monitoring rest test suite + task copyMonitoringRestTests(type: Copy) { + into project.sourceSets.test.output.resourcesDir + from project(':x-plugins:elasticsearch:x-pack').sourceSets.test.resources.srcDirs + include 'rest-api-spec/test/monitoring/**' + } -integTest { - dependsOn copyMonitoringRestTests + integTest { + dependsOn copyMonitoringRestTests - cluster { - systemProperty 'es.logger.level', 'TRACE' - plugin 'x-pack', project(':x-plugins:elasticsearch:x-pack') - setting 'xpack.monitoring.agent.interval', '3s' - extraConfigFile 'x-pack/roles.yml', 'roles.yml' - setupCommand 'setupTestAdminUser', - 'bin/x-pack/users', 'useradd', 'test_admin', '-p', 'changeme', '-r', 'superuser' - setupCommand 'setupMonitoredSystemUser', - 'bin/x-pack/users', 'useradd', 'monitored_system', '-p', 'changeme', '-r', 'monitored_system,required_for_test' - setupCommand 'setupPowerlessUser', - 'bin/x-pack/users', 'useradd', 'no_monitored_system', '-p', 'changeme', '-r', 'required_for_test' + cluster { + systemProperty 'es.logger.level', 'TRACE' + plugin 'x-pack', project(':x-plugins:elasticsearch:x-pack') + setting 'xpack.monitoring.agent.interval', '3s' + extraConfigFile 'x-pack/roles.yml', '../roles.yml' + setupCommand 'setupTestAdminUser', + 'bin/x-pack/users', 'useradd', 'test_admin', '-p', 'changeme', '-r', 'superuser' + setupCommand 'setupMonitoredSystemUser', + 'bin/x-pack/users', 'useradd', 'monitoring_system', '-p', 'changeme', '-r', 'monitoring_system,monitoring_without_bulk' + setupCommand 'setupPowerlessUser', + 'bin/x-pack/users', 'useradd', 'not_monitoring_system', '-p', 'changeme', '-r', 'monitoring_without_bulk' - waitCondition = { node, ant -> - File tmpFile = new File(node.cwd, 'wait.success') - ant.get(src: "http://${node.httpUri()}", - dest: tmpFile.toString(), - username: 'test_admin', - password: 'changeme', - ignoreerrors: true, - retries: 10) - return tmpFile.exists() + waitCondition = { node, ant -> + File tmpFile = new File(node.cwd, 'wait.success') + ant.get(src: "http://${node.httpUri()}", + dest: tmpFile.toString(), + username: 'test_admin', + password: 'changeme', + ignoreerrors: true, + retries: 10) + return tmpFile.exists() + } } } } +/** + * Allow {@code integTest} to be invoked on this project to run both Monitoring+Security smoke tests. + */ +task integTest { + dependsOn subprojects.integTest +} + diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/insufficient-rights/build.gradle b/elasticsearch/qa/smoke-test-monitoring-with-shield/insufficient-rights/build.gradle new file mode 100644 index 00000000000..e69de29bb2d diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/insufficient-rights/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldInsufficientRoleIT.java b/elasticsearch/qa/smoke-test-monitoring-with-shield/insufficient-rights/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldInsufficientRoleIT.java new file mode 100644 index 00000000000..f4879d2a53d --- /dev/null +++ b/elasticsearch/qa/smoke-test-monitoring-with-shield/insufficient-rights/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldInsufficientRoleIT.java @@ -0,0 +1,61 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ +package org.elasticsearch.smoketest; + +import com.carrotsearch.randomizedtesting.annotations.Name; +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.util.concurrent.ThreadContext; +import org.elasticsearch.shield.authc.support.SecuredString; +import org.elasticsearch.test.rest.ESRestTestCase; +import org.elasticsearch.test.rest.RestTestCandidate; +import org.elasticsearch.test.rest.parser.RestTestParseException; + +import java.io.IOException; + +import static org.elasticsearch.shield.authc.support.UsernamePasswordToken.basicAuthHeaderValue; +import static org.hamcrest.Matchers.containsString; + +public class MonitoringWithShieldInsufficientRoleIT extends ESRestTestCase { + + public MonitoringWithShieldInsufficientRoleIT(@Name("yaml") RestTestCandidate testCandidate) { + super(testCandidate); + } + + @ParametersFactory + public static Iterable parameters() throws IOException, RestTestParseException { + return ESRestTestCase.createParameters(0, 1); + } + + @Override + protected Settings restClientSettings() { + String token = basicAuthHeaderValue("not_monitoring_system", new SecuredString("changeme".toCharArray())); + return Settings.builder() + .put(ThreadContext.PREFIX + ".Authorization", token) + .build(); + } + + @Override + protected Settings restAdminSettings() { + String token = basicAuthHeaderValue("test_admin", new SecuredString("changeme".toCharArray())); + return Settings.builder() + .put(ThreadContext.PREFIX + ".Authorization", token) + .build(); + } + + @Override + public void test() throws IOException { + try { + super.test(); + fail("should have failed because of missing role"); + } catch(AssertionError ae) { + assertThat(ae.getMessage(), containsString("action [cluster:admin/xpack/monitoring/bulk]")); + assertThat(ae.getMessage(), containsString("returned [403 Forbidden]")); + assertThat(ae.getMessage(), containsString("is unauthorized for user [not_monitoring_system]")); + } + } + +} diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/roles.yml b/elasticsearch/qa/smoke-test-monitoring-with-shield/roles.yml index 308a49051b7..e317cb01295 100644 --- a/elasticsearch/qa/smoke-test-monitoring-with-shield/roles.yml +++ b/elasticsearch/qa/smoke-test-monitoring-with-shield/roles.yml @@ -1,15 +1,7 @@ -admin: - cluster: - - all - indices: - - names: '*' - privileges: - - all - -monitored_system: +monitoring_system: cluster: [ 'cluster:admin/xpack/monitoring/bulk' ] -required_for_test: +monitoring_without_bulk: cluster: [ 'monitor' ] indices: - names: '.monitoring-*' diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldInsufficientRoleIT.java b/elasticsearch/qa/smoke-test-monitoring-with-shield/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldInsufficientRoleIT.java deleted file mode 100644 index e08a0e992a1..00000000000 --- a/elasticsearch/qa/smoke-test-monitoring-with-shield/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldInsufficientRoleIT.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ -package org.elasticsearch.smoketest; - -import com.carrotsearch.randomizedtesting.annotations.Name; -import org.elasticsearch.test.rest.RestTestCandidate; - -import java.io.IOException; - -import static org.hamcrest.Matchers.containsString; - -public class MonitoringWithShieldInsufficientRoleIT extends MonitoringWithShieldIT { - - public MonitoringWithShieldInsufficientRoleIT(@Name("yaml") RestTestCandidate testCandidate) { - super(testCandidate); - } - - public void test() throws IOException { - try { - super.test(); - fail("should have failed because of missing role"); - } catch(AssertionError ae) { - assertThat(ae.getMessage(), containsString("action [cluster:admin/xpack/monitoring/bulk]")); - assertThat(ae.getMessage(), containsString("returned [403 Forbidden]")); - assertThat(ae.getMessage(), containsString("is unauthorized for user [no_monitored_system]")); - } - } - - @Override - protected String[] getCredentials() { - return new String[]{"no_monitored_system", "changeme"}; - } -} diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/sufficient-rights/build.gradle b/elasticsearch/qa/smoke-test-monitoring-with-shield/sufficient-rights/build.gradle new file mode 100644 index 00000000000..e69de29bb2d diff --git a/elasticsearch/qa/smoke-test-monitoring-with-shield/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldIT.java b/elasticsearch/qa/smoke-test-monitoring-with-shield/sufficient-rights/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldIT.java similarity index 76% rename from elasticsearch/qa/smoke-test-monitoring-with-shield/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldIT.java rename to elasticsearch/qa/smoke-test-monitoring-with-shield/sufficient-rights/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldIT.java index 8c803d543f6..b6f4f47282b 100644 --- a/elasticsearch/qa/smoke-test-monitoring-with-shield/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldIT.java +++ b/elasticsearch/qa/smoke-test-monitoring-with-shield/sufficient-rights/src/test/java/org/elasticsearch/smoketest/MonitoringWithShieldIT.java @@ -20,9 +20,6 @@ import static org.elasticsearch.shield.authc.support.UsernamePasswordToken.basic public class MonitoringWithShieldIT extends ESRestTestCase { - private final static String TEST_ADMIN_USERNAME = "test_admin"; - private final static String TEST_ADMIN_PASSWORD = "changeme"; - public MonitoringWithShieldIT(@Name("yaml") RestTestCandidate testCandidate) { super(testCandidate); } @@ -32,14 +29,9 @@ public class MonitoringWithShieldIT extends ESRestTestCase { return ESRestTestCase.createParameters(0, 1); } - protected String[] getCredentials() { - return new String[]{"monitored_system", "changeme"}; - } - @Override protected Settings restClientSettings() { - String[] creds = getCredentials(); - String token = basicAuthHeaderValue(creds[0], new SecuredString(creds[1].toCharArray())); + String token = basicAuthHeaderValue("monitoring_system", new SecuredString("changeme".toCharArray())); return Settings.builder() .put(ThreadContext.PREFIX + ".Authorization", token) .build(); @@ -47,7 +39,7 @@ public class MonitoringWithShieldIT extends ESRestTestCase { @Override protected Settings restAdminSettings() { - String token = basicAuthHeaderValue(TEST_ADMIN_USERNAME, new SecuredString(TEST_ADMIN_PASSWORD.toCharArray())); + String token = basicAuthHeaderValue("test_admin", new SecuredString("changeme".toCharArray())); return Settings.builder() .put(ThreadContext.PREFIX + ".Authorization", token) .build(); diff --git a/elasticsearch/qa/smoke-test-plugins-ssl/build.gradle b/elasticsearch/qa/smoke-test-plugins-ssl/build.gradle index 20b91d468f4..8581f51a63c 100644 --- a/elasticsearch/qa/smoke-test-plugins-ssl/build.gradle +++ b/elasticsearch/qa/smoke-test-plugins-ssl/build.gradle @@ -177,6 +177,9 @@ integTest { setupCommand 'setupMarvelUser', 'bin/x-pack/users', 'useradd', 'monitoring_agent', '-p', 'changeme', '-r', 'remote_monitoring_agent' + // Required to detect that the monitoring agent service has started + systemProperty 'es.logger.level', 'DEBUG' + waitCondition = { node, ant -> // HTTPS check is tricky to do, so we wait for the log file to indicate that the node is started String waitForNodeStartProp = "waitForNodeStart${name}" diff --git a/elasticsearch/x-dev-tools/smoke_test_xpack_rc.py b/elasticsearch/x-dev-tools/smoke_test_xpack_rc.py index c6715f9e915..fd4efcd7e2e 100644 --- a/elasticsearch/x-dev-tools/smoke_test_xpack_rc.py +++ b/elasticsearch/x-dev-tools/smoke_test_xpack_rc.py @@ -112,7 +112,7 @@ def smoke_test_release(release, files, expected_hash): print(' Starting elasticsearch daemon from [%s]' % es_dir) try: - run('%s; %s -Ees.node.name=smoke_tester -Ees.cluster.name=prepare_release -Ees.script.inline=true -Ees.script.stored=true -Ees.repositories.url.allowed_urls=http://snapshot.test* %s -Ees.pidfile=%s -Ees.node.portsfile=true' + run('%s; %s -Enode.name=smoke_tester -Ecluster.name=prepare_release -Escript.inline=true -Escript.stored=true -Erepositories.url.allowed_urls=http://snapshot.test* %s -Epidfile=%s -Enode.portsfile=true' % (java_exe(), es_run_path, '-d', os.path.join(es_dir, 'es-smoke.pid'))) if not wait_for_node_startup(es_dir, headers=headers): print("elasticsearch logs:") diff --git a/elasticsearch/x-pack/bin/x-pack/extension b/elasticsearch/x-pack/bin/x-pack/extension index db00c964d8b..505559d0bcc 100755 --- a/elasticsearch/x-pack/bin/x-pack/extension +++ b/elasticsearch/x-pack/bin/x-pack/extension @@ -70,38 +70,6 @@ elif [ -f "/etc/default/elasticsearch" ]; then . "/etc/default/elasticsearch" fi -# Parse any long getopt options and put them into properties before calling getopt below -# Be dash compatible to make sure running under ubuntu works -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - --*=*) properties="$properties -Des.${1#--}" - shift 1; COUNT=$(($COUNT+1)) - ;; - --*) properties="$properties -Des.${1#--}=$2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -# check if properties already has a config file or config dir -if [ -e "$CONF_DIR" ]; then - case "$properties" in - *-Des.default.path.conf=*) ;; - *) - if [ ! -d "$CONF_DIR/x-pack" ]; then - echo "ERROR: The configuration directory [$CONF_DIR/x-pack] does not exist. The extension tool expects security configuration files in that location." - echo "The plugin may not have been installed with the correct configuration path. If [$ES_HOME/config/x-pack] exists, please copy the 'x-pack' directory to [$CONF_DIR]" - exit 1 - fi - properties="$properties -Des.default.path.conf=$CONF_DIR" - ;; - esac -fi - export HOSTNAME=`hostname -s` # include x-pack jars in classpath @@ -113,8 +81,14 @@ if [ ! -z "$CONF_FILE" ]; then exit 1 fi +declare -a args=("$@") + +if [ -e "$CONF_DIR" ]; then + args=("${args[@]}" -Edefault.path.conf="$CONF_DIR") +fi + cd "$ES_HOME" > /dev/null -"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" $properties org.elasticsearch.xpack.extensions.XPackExtensionCli "$@" +"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" org.elasticsearch.xpack.extensions.XPackExtensionCli "${args[@]}" status=$? cd - > /dev/null exit $status diff --git a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequest.java b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequest.java index 62c02ed360c..69f7f16de7c 100644 --- a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequest.java +++ b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequest.java @@ -15,7 +15,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregatorBuilder; +import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator; @@ -39,7 +39,7 @@ public class GraphExploreRequest extends ActionRequest impl private String routing; private TimeValue timeout; - private int sampleSize = SamplerAggregatorBuilder.DEFAULT_SHARD_SAMPLE_SIZE; + private int sampleSize = SamplerAggregationBuilder.DEFAULT_SHARD_SAMPLE_SIZE; private String sampleDiversityField; private int maxDocsPerDiversityValue; private boolean useSignificance = true; @@ -199,7 +199,7 @@ public class GraphExploreRequest extends ActionRequest impl /** * The number of top-matching documents that are considered during each hop (default is - * {@link SamplerAggregatorBuilder#DEFAULT_SHARD_SAMPLE_SIZE} + * {@link SamplerAggregationBuilder#DEFAULT_SHARD_SAMPLE_SIZE} * Very small values (less than 50) may not provide sufficient weight-of-evidence to identify * significant connections between terms. *

Very large values (many thousands) are not recommended with loosely defined queries (fuzzy queries or those diff --git a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequestBuilder.java b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequestBuilder.java index dee78a445dc..9177a12b02d 100644 --- a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequestBuilder.java +++ b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/GraphExploreRequestBuilder.java @@ -11,7 +11,7 @@ import org.elasticsearch.client.ElasticsearchClient; import org.elasticsearch.common.Nullable; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilder; -import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregatorBuilder; +import org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregator; @@ -138,7 +138,7 @@ public class GraphExploreRequestBuilder extends ActionRequestBuilder Very large values (many thousands) are not recommended with loosely defined queries (fuzzy queries or diff --git a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/TransportGraphExploreAction.java b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/TransportGraphExploreAction.java index 74263e9b500..5323002ea62 100644 --- a/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/TransportGraphExploreAction.java +++ b/elasticsearch/x-pack/graph/src/main/java/org/elasticsearch/graph/action/TransportGraphExploreAction.java @@ -29,14 +29,14 @@ import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.license.plugin.core.LicenseUtils; import org.elasticsearch.search.aggregations.AggregationBuilders; -import org.elasticsearch.search.aggregations.AggregatorBuilder; -import org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregatorBuilder; +import org.elasticsearch.search.aggregations.AggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.sampler.Sampler; import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms; import org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket; -import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregatorBuilder; +import org.elasticsearch.search.aggregations.bucket.significant.SignificantTermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.Terms; -import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregatorBuilder; +import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.threadpool.ThreadPool; @@ -189,9 +189,9 @@ public class TransportGraphExploreAction extends HandledTransportAction { @Override protected void doStart() { - logger.info("monitoring service started"); + // Please don't remove this log message since it can be used in integration tests + logger.debug("monitoring service started"); for (Collector collector : collectors) { collector.start(); diff --git a/elasticsearch/x-pack/shield/bin/x-pack/syskeygen b/elasticsearch/x-pack/shield/bin/x-pack/syskeygen index 2c9d2a0766c..aaf2da9ab8f 100755 --- a/elasticsearch/x-pack/shield/bin/x-pack/syskeygen +++ b/elasticsearch/x-pack/shield/bin/x-pack/syskeygen @@ -70,38 +70,6 @@ elif [ -f "/etc/default/elasticsearch" ]; then . "/etc/default/elasticsearch" fi -# Parse any long getopt options and put them into properties before calling getopt below -# Be dash compatible to make sure running under ubuntu works -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - --*=*) properties="$properties -Des.${1#--}" - shift 1; COUNT=$(($COUNT+1)) - ;; - --*) properties="$properties -Des.${1#--}=$2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -# check if properties already has a config file or config dir -if [ -e "$CONF_DIR" ]; then - case "$properties" in - *-Des.default.path.conf=*) ;; - *) - if [ ! -d "$CONF_DIR/x-pack" ]; then - echo "ERROR: The configuration directory [$CONF_DIR/x-pack] does not exist. The syskeygen tool expects security configuration files in that location." - echo "The plugin may not have been installed with the correct configuration path. If [$ES_HOME/config/x-pack] exists, please copy the 'x-pack' directory to [$CONF_DIR]" - exit 1 - fi - properties="$properties -Des.default.path.conf=$CONF_DIR" - ;; - esac -fi - export HOSTNAME=`hostname -s` # include shield jars in classpath @@ -121,8 +89,14 @@ if [ ! -z "$CONF_FILE" ]; then exit 1 fi +declare -a args=("$@") + +if [ -e "$CONF_DIR" ]; then + args=("${args[@]}" -Edefault.path.conf="$CONF_DIR") +fi + cd "$ES_HOME" > /dev/null -"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" $properties org.elasticsearch.shield.crypto.tool.SystemKeyTool "$@" +"$JAVA" $ES_JAVA_OPTS -Des.path.home="$ES_HOME" -cp "$ES_CLASSPATH" org.elasticsearch.shield.crypto.tool.SystemKeyTool $properties "{args[@]}" status=$? cd - > /dev/null exit $status diff --git a/elasticsearch/x-pack/shield/bin/x-pack/users b/elasticsearch/x-pack/shield/bin/x-pack/users index 67c9f150272..8efde509c3e 100755 --- a/elasticsearch/x-pack/shield/bin/x-pack/users +++ b/elasticsearch/x-pack/shield/bin/x-pack/users @@ -70,38 +70,6 @@ elif [ -f "/etc/default/elasticsearch" ]; then . "/etc/default/elasticsearch" fi -# Parse any long getopt options and put them into properties before calling getopt below -# Be dash compatible to make sure running under ubuntu works -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - --*=*) properties="$properties -Des.${1#--}" - shift 1; COUNT=$(($COUNT+1)) - ;; - --*) properties="$properties -Des.${1#--}=$2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -# check if properties already has a config file or config dir -if [ -e "$CONF_DIR" ]; then - case "$properties" in - *-Des.default.path.conf=*) ;; - *) - if [ ! -d "$CONF_DIR/x-pack" ]; then - echo "ERROR: The configuration directory [$CONF_DIR/x-pack] does not exist. The users tool expects security configuration files in that location." - echo "The plugin may not have been installed with the correct configuration path. If [$ES_HOME/config/x-pack] exists, please copy the 'x-pack' directory to [$CONF_DIR]" - exit 1 - fi - properties="$properties -Des.default.path.conf=$CONF_DIR" - ;; - esac -fi - export HOSTNAME=`hostname -s` # include shield jars in classpath @@ -121,8 +89,14 @@ if [ ! -z "$CONF_FILE" ]; then exit 1 fi +declare -a args=("$@") + +if [ -e "$CONF_DIR" ]; then + args=("${args[@]}" -Edefault.path.conf="$CONF_DIR") +fi + cd "$ES_HOME" > /dev/null -"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" $properties org.elasticsearch.shield.authc.file.tool.UsersTool "$@" +"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" org.elasticsearch.shield.authc.file.tool.UsersTool "${args[@]}" status=$? cd - > /dev/null exit $status diff --git a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/tool/UsersTool.java b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/tool/UsersTool.java index 0dc39b6ddad..f9a54452358 100644 --- a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/tool/UsersTool.java +++ b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/authc/file/tool/UsersTool.java @@ -5,26 +5,14 @@ */ package org.elasticsearch.shield.authc.file.tool; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; - import joptsimple.OptionSet; import joptsimple.OptionSpec; -import org.elasticsearch.cli.Command; import org.elasticsearch.cli.ExitCodes; import org.elasticsearch.cli.MultiCommand; +import org.elasticsearch.cli.SettingCommand; +import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserError; import org.elasticsearch.common.Strings; -import org.elasticsearch.cli.Terminal; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.env.Environment; @@ -40,32 +28,41 @@ import org.elasticsearch.shield.support.FileAttributesChecker; import org.elasticsearch.shield.support.Validation; import org.elasticsearch.shield.support.Validation.Users; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + public class UsersTool extends MultiCommand { public static void main(String[] args) throws Exception { - Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, Terminal.DEFAULT); - exit(new UsersTool(env).main(args, Terminal.DEFAULT)); + exit(new UsersTool().main(args, Terminal.DEFAULT)); } - UsersTool(Environment env) { + UsersTool() { super("Manages elasticsearch native users"); - subcommands.put("useradd", new AddUserCommand(env)); - subcommands.put("userdel", new DeleteUserCommand(env)); - subcommands.put("passwd", new PasswordCommand(env)); - subcommands.put("roles", new RolesCommand(env)); - subcommands.put("list", new ListCommand(env)); + subcommands.put("useradd", new AddUserCommand()); + subcommands.put("userdel", new DeleteUserCommand()); + subcommands.put("passwd", new PasswordCommand()); + subcommands.put("roles", new RolesCommand()); + subcommands.put("list", new ListCommand()); } - static class AddUserCommand extends Command { + static class AddUserCommand extends SettingCommand { - private final Environment env; private final OptionSpec passwordOption; private final OptionSpec rolesOption; private final OptionSpec arguments; - AddUserCommand(Environment env) { + AddUserCommand() { super("Adds a native user"); - this.env = env; this.passwordOption = parser.acceptsAll(Arrays.asList("p", "password"), "The user password") .withRequiredArg(); @@ -87,7 +84,7 @@ public class UsersTool extends MultiCommand { } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { String username = parseUsername(arguments.values(options)); Validation.Error validationError = Users.validateUsername(username); if (validationError != null) { @@ -95,6 +92,7 @@ public class UsersTool extends MultiCommand { } char[] password = parsePassword(terminal, passwordOption.value(options)); + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); String[] roles = parseRoles(terminal, env, rolesOption.value(options)); Settings fileSettings = Realms.fileRealmSettings(env.settings()); @@ -120,14 +118,12 @@ public class UsersTool extends MultiCommand { } } - static class DeleteUserCommand extends Command { + static class DeleteUserCommand extends SettingCommand { - private final Environment env; private final OptionSpec arguments; - DeleteUserCommand(Environment env) { + DeleteUserCommand() { super("Deletes a file based user"); - this.env = env; this.arguments = parser.nonOptions("username"); } @@ -143,8 +139,9 @@ public class UsersTool extends MultiCommand { } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { String username = parseUsername(arguments.values(options)); + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); Settings fileSettings = Realms.fileRealmSettings(env.settings()); Path passwordFile = FileUserPasswdStore.resolveFile(fileSettings, env); Path rolesFile = FileUserRolesStore.resolveFile(fileSettings, env); @@ -173,15 +170,13 @@ public class UsersTool extends MultiCommand { } } - static class PasswordCommand extends Command { + static class PasswordCommand extends SettingCommand { - private final Environment env; private final OptionSpec passwordOption; private final OptionSpec arguments; - PasswordCommand(Environment env) { + PasswordCommand() { super("Changes the password of an existing file based user"); - this.env = env; this.passwordOption = parser.acceptsAll(Arrays.asList("p", "password"), "The user password") .withRequiredArg(); @@ -200,10 +195,11 @@ public class UsersTool extends MultiCommand { } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { String username = parseUsername(arguments.values(options)); char[] password = parsePassword(terminal, passwordOption.value(options)); + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); Settings fileSettings = Realms.fileRealmSettings(env.settings()); Path file = FileUserPasswdStore.resolveFile(fileSettings, env); FileAttributesChecker attributesChecker = new FileAttributesChecker(file); @@ -218,16 +214,14 @@ public class UsersTool extends MultiCommand { } } - static class RolesCommand extends Command { + static class RolesCommand extends SettingCommand { - private final Environment env; private final OptionSpec addOption; private final OptionSpec removeOption; private final OptionSpec arguments; - RolesCommand(Environment env) { + RolesCommand() { super("Edit roles of an existing user"); - this.env = env; this.addOption = parser.acceptsAll(Arrays.asList("a", "add"), "Adds supplied roles to the specified user") .withRequiredArg().defaultsTo(""); @@ -246,8 +240,9 @@ public class UsersTool extends MultiCommand { } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { String username = parseUsername(arguments.values(options)); + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); String[] addRoles = parseRoles(terminal, env, addOption.value(options)); String[] removeRoles = parseRoles(terminal, env, removeOption.value(options)); @@ -290,14 +285,12 @@ public class UsersTool extends MultiCommand { } } - static class ListCommand extends Command { + static class ListCommand extends SettingCommand { - private final Environment env; private final OptionSpec arguments; - ListCommand(Environment env) { + ListCommand() { super("List existing file based users and their corresponding roles"); - this.env = env; this.arguments = parser.nonOptions("username"); } @@ -308,11 +301,12 @@ public class UsersTool extends MultiCommand { } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { String username = null; if (options.has(arguments)) { username = arguments.value(options); } + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); listUsersAndRoles(terminal, env, username); } } diff --git a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/crypto/tool/SystemKeyTool.java b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/crypto/tool/SystemKeyTool.java index 8973afe7f22..15eb07390ab 100644 --- a/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/crypto/tool/SystemKeyTool.java +++ b/elasticsearch/x-pack/shield/src/main/java/org/elasticsearch/shield/crypto/tool/SystemKeyTool.java @@ -5,21 +5,14 @@ */ package org.elasticsearch.shield.crypto.tool; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardOpenOption; -import java.nio.file.attribute.PosixFileAttributeView; -import java.nio.file.attribute.PosixFilePermission; -import java.util.List; -import java.util.Locale; -import java.util.Set; - import joptsimple.OptionSet; import joptsimple.OptionSpec; +import joptsimple.util.KeyValuePair; import org.elasticsearch.cli.Command; import org.elasticsearch.cli.ExitCodes; -import org.elasticsearch.cli.UserError; +import org.elasticsearch.cli.SettingCommand; import org.elasticsearch.cli.Terminal; +import org.elasticsearch.cli.UserError; import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.settings.Settings; @@ -28,28 +21,47 @@ import org.elasticsearch.env.Environment; import org.elasticsearch.node.internal.InternalSettingsPreparer; import org.elasticsearch.shield.crypto.InternalCryptoService; -public class SystemKeyTool extends Command { +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.PosixFileAttributeView; +import java.nio.file.attribute.PosixFilePermission; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Set; + +public class SystemKeyTool extends SettingCommand { + + private final OptionSpec arguments; + + SystemKeyTool() { + super("system key tool"); + arguments = parser.nonOptions("key path"); + } public static final Set PERMISSION_OWNER_READ_WRITE = Sets.newHashSet(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE); public static void main(String[] args) throws Exception { - Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, Terminal.DEFAULT); - exit(new SystemKeyTool(env).main(args, Terminal.DEFAULT)); + final SystemKeyTool tool = new SystemKeyTool(); + int status = main(tool, args, Terminal.DEFAULT); + if (status != ExitCodes.OK) { + exit(status); + } } - private final Environment env; - private final OptionSpec arguments; - - public SystemKeyTool(Environment env) { - super("Generates the system key"); - this.env = env; - this.arguments = parser.nonOptions("key path"); + static int main(SystemKeyTool tool, String[] args, Terminal terminal) throws Exception { + return tool.main(args, terminal); } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { final Path keyPath; + + final Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); + if (options.hasArgument(arguments)) { List args = arguments.values(options); if (args.size() > 1) { @@ -79,4 +91,5 @@ public class SystemKeyTool extends Command { private static Path parsePath(String path) { return PathUtils.get(path); } + } diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/tool/UsersToolTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/tool/UsersToolTests.java index afd8bb601ee..8e18018dac6 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/tool/UsersToolTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/authc/file/tool/UsersToolTests.java @@ -5,16 +5,6 @@ */ package org.elasticsearch.shield.authc.file.tool; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.FileSystem; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Objects; - import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; import org.apache.lucene.util.IOUtils; @@ -35,17 +25,28 @@ import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.FileSystem; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Objects; + public class UsersToolTests extends CommandTestCase { // the mock filesystem we use so permissions/users/groups can be modified static FileSystem jimfs; + String pathHomeParameter; + String fileTypeParameter; // the config dir for each test to use Path confDir; // settings used to create an Environment for tools - Settings.Builder settingsBuilder; - + Settings settings; @BeforeClass public static void setupJimfs() throws IOException { @@ -78,9 +79,13 @@ public class UsersToolTests extends CommandTestCase { "test_r2:", " cluster: all" ), StandardCharsets.UTF_8); - settingsBuilder = Settings.builder() - .put("path.home", homeDir) - .put("xpack.security.authc.realms.file.type", "file"); + settings = + Settings.builder() + .put("path.home", homeDir) + .put("xpack.security.authc.realms.file.type", "file") + .build(); + pathHomeParameter = "-Epath.home=" + homeDir; + fileTypeParameter = "-Expack.security.authc.realms.file.type=file"; } @AfterClass @@ -92,7 +97,7 @@ public class UsersToolTests extends CommandTestCase { @Override protected Command newCommand() { - return new UsersTool(new Environment(settingsBuilder.build())); + return new UsersTool(); } /** checks the user exists with the given password */ @@ -219,7 +224,7 @@ public class UsersToolTests extends CommandTestCase { } public void testParseUnknownRole() throws Exception { - UsersTool.parseRoles(terminal, new Environment(settingsBuilder.build()), "test_r1,r2,r3"); + UsersTool.parseRoles(terminal, new Environment(settings), "test_r1,r2,r3"); String output = terminal.getOutput(); assertTrue(output, output.contains("The following roles [r2,r3] are not in the [")); } @@ -227,21 +232,21 @@ public class UsersToolTests extends CommandTestCase { public void testParseReservedRole() throws Exception { final String reservedRoleName = randomFrom(ReservedRolesStore.names().toArray(Strings.EMPTY_ARRAY)); String rolesArg = randomBoolean() ? "test_r1," + reservedRoleName : reservedRoleName; - UsersTool.parseRoles(terminal, new Environment(settingsBuilder.build()), rolesArg); + UsersTool.parseRoles(terminal, new Environment(settings), rolesArg); String output = terminal.getOutput(); assertTrue(output, output.isEmpty()); } public void testParseInvalidRole() throws Exception { UserError e = expectThrows(UserError.class, () -> { - UsersTool.parseRoles(terminal, new Environment(settingsBuilder.build()), "$345"); + UsersTool.parseRoles(terminal, new Environment(settings), "$345"); }); assertEquals(ExitCodes.DATA_ERROR, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("Invalid role [$345]")); } public void testParseMultipleRoles() throws Exception { - String[] roles = UsersTool.parseRoles(terminal, new Environment(settingsBuilder.build()), "test_r1,test_r2"); + String[] roles = UsersTool.parseRoles(terminal, new Environment(settings), "test_r1,test_r2"); assertEquals(Objects.toString(roles), 2, roles.length); assertEquals("test_r1", roles[0]); assertEquals("test_r2", roles[1]); @@ -250,18 +255,18 @@ public class UsersToolTests extends CommandTestCase { public void testUseraddNoPassword() throws Exception { terminal.addSecretInput("changeme"); terminal.addSecretInput("changeme"); - execute("useradd", "username"); + execute("useradd", pathHomeParameter, fileTypeParameter, "username"); assertUser("username", "changeme"); } public void testUseraddPasswordOption() throws Exception { - execute("useradd", "username", "-p", "changeme"); + execute("useradd", pathHomeParameter, fileTypeParameter, "username", "-p", "changeme"); assertUser("username", "changeme"); } public void testUseraddUserExists() throws Exception { UserError e = expectThrows(UserError.class, () -> { - execute("useradd", "existing_user", "-p", "changeme"); + execute("useradd", pathHomeParameter, fileTypeParameter, "existing_user", "-p", "changeme"); }); assertEquals(ExitCodes.CODE_ERROR, e.exitCode); assertEquals("User [existing_user] already exists", e.getMessage()); @@ -270,27 +275,27 @@ public class UsersToolTests extends CommandTestCase { public void testUseraddNoRoles() throws Exception { Files.delete(confDir.resolve("users_roles")); Files.createFile(confDir.resolve("users_roles")); - execute("useradd", "username", "-p", "changeme"); + execute("useradd", pathHomeParameter, fileTypeParameter, "username", "-p", "changeme"); List lines = Files.readAllLines(confDir.resolve("users_roles"), StandardCharsets.UTF_8); assertTrue(lines.toString(), lines.isEmpty()); } public void testUserdelUnknownUser() throws Exception { UserError e = expectThrows(UserError.class, () -> { - execute("userdel", "unknown"); + execute("userdel", pathHomeParameter, fileTypeParameter, "unknown"); }); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } public void testUserdel() throws Exception { - execute("userdel", "existing_user"); + execute("userdel", pathHomeParameter, fileTypeParameter, "existing_user"); assertNoUser("existing_user"); } public void testPasswdUnknownUser() throws Exception { UserError e = expectThrows(UserError.class, () -> { - execute("passwd", "unknown", "-p", "changeme"); + execute("passwd", pathHomeParameter, fileTypeParameter, "unknown", "-p", "changeme"); }); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); @@ -299,64 +304,64 @@ public class UsersToolTests extends CommandTestCase { public void testPasswdNoPasswordOption() throws Exception { terminal.addSecretInput("newpassword"); terminal.addSecretInput("newpassword"); - execute("passwd", "existing_user"); + execute("passwd", pathHomeParameter, fileTypeParameter, "existing_user"); assertUser("existing_user", "newpassword"); assertRole("test_admin", "existing_user", "existing_user2"); // roles unchanged } public void testPasswd() throws Exception { - execute("passwd", "existing_user", "-p", "newpassword"); + execute("passwd", pathHomeParameter, fileTypeParameter, "existing_user", "-p", "newpassword"); assertUser("existing_user", "newpassword"); assertRole("test_admin", "existing_user"); // roles unchanged } public void testRolesUnknownUser() throws Exception { UserError e = expectThrows(UserError.class, () -> { - execute("roles", "unknown"); + execute("roles", pathHomeParameter, fileTypeParameter, "unknown"); }); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } public void testRolesAdd() throws Exception { - execute("roles", "existing_user", "-a", "test_r1"); + execute("roles", pathHomeParameter, fileTypeParameter, "existing_user", "-a", "test_r1"); assertRole("test_admin", "existing_user"); assertRole("test_r1", "existing_user"); } public void testRolesRemove() throws Exception { - execute("roles", "existing_user", "-r", "test_admin"); + execute("roles", pathHomeParameter, fileTypeParameter, "existing_user", "-r", "test_admin"); assertRole("test_admin", "existing_user2"); } public void testRolesAddAndRemove() throws Exception { - execute("roles", "existing_user", "-a", "test_r1", "-r", "test_admin"); + execute("roles", pathHomeParameter, fileTypeParameter, "existing_user", "-a", "test_r1", "-r", "test_admin"); assertRole("test_admin", "existing_user2"); assertRole("test_r1", "existing_user"); } public void testRolesRemoveLeavesExisting() throws Exception { - execute("useradd", "username", "-p", "changeme", "-r", "test_admin"); - execute("roles", "existing_user", "-r", "test_admin"); + execute("useradd", pathHomeParameter, fileTypeParameter, "username", "-p", "changeme", "-r", "test_admin"); + execute("roles", pathHomeParameter, fileTypeParameter, "existing_user", "-r", "test_admin"); assertRole("test_admin", "username"); } public void testRolesNoAddOrRemove() throws Exception { - String output = execute("roles", "existing_user"); + String output = execute("roles", pathHomeParameter, fileTypeParameter, "existing_user"); assertTrue(output, output.contains("existing_user")); assertTrue(output, output.contains("test_admin")); } public void testListUnknownUser() throws Exception { UserError e = expectThrows(UserError.class, () -> { - execute("list", "unknown"); + execute("list", pathHomeParameter, fileTypeParameter, "unknown"); }); assertEquals(ExitCodes.NO_USER, e.exitCode); assertTrue(e.getMessage(), e.getMessage().contains("User [unknown] doesn't exist")); } public void testListAllUsers() throws Exception { - String output = execute("list"); + String output = execute("list", pathHomeParameter, fileTypeParameter); assertTrue(output, output.contains("existing_user")); assertTrue(output, output.contains("test_admin")); assertTrue(output, output.contains("existing_user2")); @@ -367,7 +372,7 @@ public class UsersToolTests extends CommandTestCase { } public void testListSingleUser() throws Exception { - String output = execute("list", "existing_user"); + String output = execute("list", pathHomeParameter, fileTypeParameter, "existing_user"); assertTrue(output, output.contains("existing_user")); assertTrue(output, output.contains("test_admin")); assertFalse(output, output.contains("existing_user2")); @@ -378,8 +383,8 @@ public class UsersToolTests extends CommandTestCase { } public void testListUnknownRoles() throws Exception { - execute("useradd", "username", "-p", "changeme", "-r", "test_r1,r2,r3"); - String output = execute("list", "username"); + execute("useradd", pathHomeParameter, fileTypeParameter, "username", "-p", "changeme", "-r", "test_r1,r2,r3"); + String output = execute("list", pathHomeParameter, fileTypeParameter, "username"); assertTrue(output, output.contains("username")); assertTrue(output, output.contains("r2*,r3*,test_r1")); } @@ -389,14 +394,14 @@ public class UsersToolTests extends CommandTestCase { Files.createFile(confDir.resolve("users")); Files.delete(confDir.resolve("users_roles")); Files.createFile(confDir.resolve("users_roles")); - String output = execute("list"); + String output = execute("list", pathHomeParameter, fileTypeParameter); assertTrue(output, output.contains("No users found")); } public void testListUserWithoutRoles() throws Exception { - String output = execute("list", "existing_user3"); + String output = execute("list", pathHomeParameter, fileTypeParameter, "existing_user3"); assertTrue(output, output.contains("existing_user3")); - output = execute("list"); + output = execute("list", pathHomeParameter, fileTypeParameter); assertTrue(output, output.contains("existing_user3")); // output should not contain '*' which indicates unknown role diff --git a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/crypto/tool/SystemKeyToolTests.java b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/crypto/tool/SystemKeyToolTests.java index 8065e097250..636605c18fd 100644 --- a/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/crypto/tool/SystemKeyToolTests.java +++ b/elasticsearch/x-pack/shield/src/test/java/org/elasticsearch/shield/crypto/tool/SystemKeyToolTests.java @@ -7,12 +7,12 @@ package org.elasticsearch.shield.crypto.tool; import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; +import org.apache.lucene.util.IOUtils; import org.elasticsearch.cli.Command; import org.elasticsearch.cli.CommandTestCase; import org.elasticsearch.common.io.PathUtilsForTesting; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.env.Environment; import org.elasticsearch.shield.crypto.InternalCryptoService; +import org.junit.After; import java.nio.file.FileSystem; import java.nio.file.Files; @@ -23,32 +23,33 @@ import java.util.Set; public class SystemKeyToolTests extends CommandTestCase { private FileSystem jimfs; - private Settings.Builder settingsBuilder; - private Path homeDir; - private void initFileSystem(boolean needsPosix) throws Exception { + private Path initFileSystem(boolean needsPosix) throws Exception { String view = needsPosix ? "posix" : randomFrom("basic", "posix"); Configuration conf = Configuration.unix().toBuilder().setAttributeViews(view).build(); jimfs = Jimfs.newFileSystem(conf); PathUtilsForTesting.installMock(jimfs); - homeDir = jimfs.getPath("eshome"); + return jimfs.getPath("eshome"); + } - settingsBuilder = Settings.builder() - .put(Environment.PATH_HOME_SETTING.getKey(), homeDir); + @After + public void tearDown() throws Exception { + IOUtils.close(jimfs); + super.tearDown(); } @Override protected Command newCommand() { - return new SystemKeyTool(new Environment(settingsBuilder.build())); + return new SystemKeyTool(); } public void testGenerate() throws Exception { - initFileSystem(true); + final Path homeDir = initFileSystem(true); Path path = jimfs.getPath(randomAsciiOfLength(10)).resolve("key"); Files.createDirectory(path.getParent()); - execute(path.toString()); + execute("-Epath.home=" + homeDir, path.toString()); byte[] bytes = Files.readAllBytes(path); // TODO: maybe we should actually check the key is...i dunno...valid? assertEquals(InternalCryptoService.KEY_SIZE / 8, bytes.length); @@ -60,35 +61,35 @@ public class SystemKeyToolTests extends CommandTestCase { } public void testGeneratePathInSettings() throws Exception { - initFileSystem(false); + final Path homeDir = initFileSystem(false); Path path = jimfs.getPath(randomAsciiOfLength(10)).resolve("key"); Files.createDirectories(path.getParent()); - settingsBuilder.put(InternalCryptoService.FILE_SETTING.getKey(), path.toAbsolutePath().toString()); - execute(); + execute("-Epath.home=" + homeDir.toString(), "-Expack.security.system_key.file=" + path.toAbsolutePath().toString()); byte[] bytes = Files.readAllBytes(path); assertEquals(InternalCryptoService.KEY_SIZE / 8, bytes.length); } public void testGenerateDefaultPath() throws Exception { - initFileSystem(false); + final Path homeDir = initFileSystem(false); Path keyPath = homeDir.resolve("config/x-pack/system_key"); Files.createDirectories(keyPath.getParent()); - execute(); + execute("-Epath.home=" + homeDir.toString()); byte[] bytes = Files.readAllBytes(keyPath); assertEquals(InternalCryptoService.KEY_SIZE / 8, bytes.length); } public void testThatSystemKeyMayOnlyBeReadByOwner() throws Exception { - initFileSystem(true); + final Path homeDir = initFileSystem(true); Path path = jimfs.getPath(randomAsciiOfLength(10)).resolve("key"); Files.createDirectories(path.getParent()); - execute(path.toString()); + execute("-Epath.home=" + homeDir, path.toString()); Set perms = Files.getPosixFilePermissions(path); assertTrue(perms.toString(), perms.contains(PosixFilePermission.OWNER_READ)); assertTrue(perms.toString(), perms.contains(PosixFilePermission.OWNER_WRITE)); assertEquals(perms.toString(), 2, perms.size()); } + } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommand.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommand.java index 3f97ea13546..cf161e65012 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommand.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommand.java @@ -8,17 +8,18 @@ package org.elasticsearch.xpack.extensions; import joptsimple.OptionSet; import joptsimple.OptionSpec; import org.apache.lucene.util.IOUtils; - import org.elasticsearch.bootstrap.JarHell; -import org.elasticsearch.cli.Command; import org.elasticsearch.cli.ExitCodes; +import org.elasticsearch.cli.SettingCommand; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserError; import org.elasticsearch.common.io.FileSystemUtils; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.node.internal.InternalSettingsPreparer; -import java.io.InputStream; import java.io.IOException; +import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.net.URLDecoder; @@ -26,13 +27,14 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; -import java.util.List; import java.util.Arrays; +import java.util.List; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; -import static org.elasticsearch.xpack.XPackPlugin.resolveXPackExtensionsFile; import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE; +import static org.elasticsearch.xpack.XPackPlugin.resolveXPackExtensionsFile; /** * A command for the extension cli to install an extension into x-pack. @@ -49,22 +51,20 @@ import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE; *

  • Jar hell does not exist, either between the extension's own jars or with the parent classloader (elasticsearch + x-pack)
  • * */ -class InstallXPackExtensionCommand extends Command { +class InstallXPackExtensionCommand extends SettingCommand { - private final Environment env; private final OptionSpec batchOption; private final OptionSpec arguments; - InstallXPackExtensionCommand(Environment env) { + InstallXPackExtensionCommand() { super("Install a plugin"); - this.env = env; this.batchOption = parser.acceptsAll(Arrays.asList("b", "batch"), "Enable batch mode explicitly, automatic confirmation of security permission"); this.arguments = parser.nonOptions("plugin id"); } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { // TODO: in jopt-simple 5.0 we can enforce a min/max number of positional args List args = arguments.values(options); if (args.size() != 1) { @@ -72,12 +72,13 @@ class InstallXPackExtensionCommand extends Command { } String extensionURL = args.get(0); boolean isBatch = options.has(batchOption) || System.console() == null; - execute(terminal, extensionURL, isBatch); + execute(terminal, extensionURL, isBatch, settings); } // pkg private for testing - void execute(Terminal terminal, String extensionId, boolean isBatch) throws Exception { + void execute(Terminal terminal, String extensionId, boolean isBatch, Map properties) throws Exception { + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, properties); if (Files.exists(resolveXPackExtensionsFile(env)) == false) { terminal.println("xpack extensions directory [" + resolveXPackExtensionsFile(env) + "] does not exist. Creating..."); Files.createDirectories(resolveXPackExtensionsFile(env)); diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommand.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommand.java index ccf7768fb95..204c7b2eebc 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommand.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommand.java @@ -6,32 +6,33 @@ package org.elasticsearch.xpack.extensions; import joptsimple.OptionSet; - -import org.elasticsearch.cli.Command; +import org.elasticsearch.cli.SettingCommand; import org.elasticsearch.cli.Terminal; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.node.internal.InternalSettingsPreparer; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Map; -import static org.elasticsearch.xpack.XPackPlugin.resolveXPackExtensionsFile; import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE; +import static org.elasticsearch.xpack.XPackPlugin.resolveXPackExtensionsFile; /** * A command for the extension cli to list extensions installed in x-pack. */ -class ListXPackExtensionCommand extends Command { - private final Environment env; +class ListXPackExtensionCommand extends SettingCommand { - ListXPackExtensionCommand(Environment env) { + ListXPackExtensionCommand() { super("Lists installed x-pack extensions"); - this.env = env; } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); if (Files.exists(resolveXPackExtensionsFile(env)) == false) { throw new IOException("Extensions directory missing: " + resolveXPackExtensionsFile(env)); } @@ -43,4 +44,5 @@ class ListXPackExtensionCommand extends Command { } } } + } diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommand.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommand.java index b700a8d62ad..1da0e61f863 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommand.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommand.java @@ -7,51 +7,53 @@ package org.elasticsearch.xpack.extensions; import joptsimple.OptionSet; import joptsimple.OptionSpec; - import org.apache.lucene.util.IOUtils; -import org.elasticsearch.cli.Command; import org.elasticsearch.cli.ExitCodes; +import org.elasticsearch.cli.SettingCommand; import org.elasticsearch.cli.Terminal; import org.elasticsearch.cli.UserError; import org.elasticsearch.common.Strings; +import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; +import org.elasticsearch.node.internal.InternalSettingsPreparer; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.List; +import java.util.Map; -import static org.elasticsearch.xpack.XPackPlugin.resolveXPackExtensionsFile; import static org.elasticsearch.cli.Terminal.Verbosity.VERBOSE; +import static org.elasticsearch.xpack.XPackPlugin.resolveXPackExtensionsFile; /** * A command for the extension cli to remove an extension from x-pack. */ -class RemoveXPackExtensionCommand extends Command { - private final Environment env; +class RemoveXPackExtensionCommand extends SettingCommand { private final OptionSpec arguments; - RemoveXPackExtensionCommand(Environment env) { + RemoveXPackExtensionCommand() { super("Removes an extension from x-pack"); - this.env = env; this.arguments = parser.nonOptions("extension name"); } @Override - protected void execute(Terminal terminal, OptionSet options) throws Exception { + protected void execute(Terminal terminal, OptionSet options, Map settings) throws Exception { + // TODO: in jopt-simple 5.0 we can enforce a min/max number of positional args List args = arguments.values(options); if (args.size() != 1) { throw new UserError(ExitCodes.USAGE, "Must supply a single extension id argument"); } - execute(terminal, args.get(0)); + execute(terminal, args.get(0), settings); } // pkg private for testing - void execute(Terminal terminal, String extensionName) throws Exception { + void execute(Terminal terminal, String extensionName, Map settings) throws Exception { terminal.println("-> Removing " + Strings.coalesceToEmpty(extensionName) + "..."); + Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, terminal, settings); Path extensionDir = resolveXPackExtensionsFile(env).resolve(extensionName); if (Files.exists(extensionDir) == false) { throw new UserError(ExitCodes.USAGE, diff --git a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java index f18d3db3a02..c17ae66e589 100644 --- a/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java +++ b/elasticsearch/x-pack/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java @@ -9,25 +9,22 @@ import org.apache.log4j.BasicConfigurator; import org.apache.log4j.varia.NullAppender; import org.elasticsearch.cli.MultiCommand; import org.elasticsearch.cli.Terminal; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.env.Environment; -import org.elasticsearch.node.internal.InternalSettingsPreparer; /** * A cli tool for adding, removing and listing extensions for x-pack. */ public class XPackExtensionCli extends MultiCommand { - public XPackExtensionCli(Environment env) { + public XPackExtensionCli() { super("A tool for managing installed x-pack extensions"); - subcommands.put("list", new ListXPackExtensionCommand(env)); - subcommands.put("install", new InstallXPackExtensionCommand(env)); - subcommands.put("remove", new RemoveXPackExtensionCommand(env)); + subcommands.put("list", new ListXPackExtensionCommand()); + subcommands.put("install", new InstallXPackExtensionCommand()); + subcommands.put("remove", new RemoveXPackExtensionCommand()); } public static void main(String[] args) throws Exception { BasicConfigurator.configure(new NullAppender()); - Environment env = InternalSettingsPreparer.prepareEnvironment(Settings.EMPTY, Terminal.DEFAULT); - exit(new XPackExtensionCli(env).main(args, Terminal.DEFAULT)); + exit(new XPackExtensionCli().main(args, Terminal.DEFAULT)); } + } diff --git a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommandTests.java b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommandTests.java index 33d96ccbb03..fe04b60844f 100644 --- a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommandTests.java +++ b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/InstallXPackExtensionCommandTests.java @@ -12,34 +12,37 @@ import org.elasticsearch.cli.UserError; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; +import org.junit.Before; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; import java.nio.file.DirectoryStream; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.StandardCopyOption; -import java.nio.file.FileVisitResult; -import java.nio.file.NoSuchFileException; import java.nio.file.attribute.BasicFileAttributes; +import java.util.HashMap; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @LuceneTestCase.SuppressFileSystems("*") public class InstallXPackExtensionCommandTests extends ESTestCase { - /** - * Creates a test environment with plugins and xpack extensions directories. - */ - static Environment createEnv() throws IOException { - Path home = createTempDir(); + + Path home; + Environment env; + + @Before + public void setUp() throws Exception { + super.setUp(); + home = createTempDir(); Files.createDirectories(home.resolve("org/elasticsearch/xpack/extensions").resolve("xpack").resolve("extensions")); - Settings settings = Settings.builder() - .put("path.home", home) - .build(); - return new Environment(settings); + env = new Environment(Settings.builder().put("path.home", home.toString()).build()); } /** @@ -84,9 +87,11 @@ public class InstallXPackExtensionCommandTests extends ESTestCase { return writeZip(structure); } - static MockTerminal installExtension(String extensionUrl, Environment env) throws Exception { + static MockTerminal installExtension(String extensionUrl, Path home) throws Exception { + Map settings = new HashMap<>(); + settings.put("path.home", home.toString()); MockTerminal terminal = new MockTerminal(); - new InstallXPackExtensionCommand(env).execute(terminal, extensionUrl, true); + new InstallXPackExtensionCommand().execute(terminal, extensionUrl, true, settings); return terminal; } @@ -108,77 +113,65 @@ public class InstallXPackExtensionCommandTests extends ESTestCase { } public void testSomethingWorks() throws Exception { - Environment env = createEnv(); Path extDir = createTempDir(); String extZip = createExtension("fake", extDir); - installExtension(extZip, env); + installExtension(extZip, home); assertExtension("fake", extDir, env); } public void testSpaceInUrl() throws Exception { - Environment env = createEnv(); Path extDir = createTempDir(); String extZip = createExtension("fake", extDir); Path extZipWithSpaces = createTempFile("foo bar", ".zip"); try (InputStream in = new URL(extZip).openStream()) { Files.copy(in, extZipWithSpaces, StandardCopyOption.REPLACE_EXISTING); } - installExtension(extZipWithSpaces.toUri().toURL().toString(), env); + installExtension(extZipWithSpaces.toUri().toURL().toString(), home); assertExtension("fake", extDir, env); } public void testMalformedUrlNotMaven() throws Exception { // has two colons, so it appears similar to maven coordinates MalformedURLException e = expectThrows(MalformedURLException.class, () -> { - installExtension("://host:1234", createEnv()); + installExtension("://host:1234", home); }); assertTrue(e.getMessage(), e.getMessage().contains("no protocol")); } public void testJarHell() throws Exception { - Environment env = createEnv(); Path extDir = createTempDir(); writeJar(extDir.resolve("other.jar"), "FakeExtension"); String extZip = createExtension("fake", extDir); // adds extension.jar with FakeExtension - IllegalStateException e = expectThrows(IllegalStateException.class, () -> { - installExtension(extZip, env); - }); + IllegalStateException e = expectThrows(IllegalStateException.class, () -> installExtension(extZip, home)); assertTrue(e.getMessage(), e.getMessage().contains("jar hell")); assertInstallCleaned(env); } public void testIsolatedExtension() throws Exception { - Environment env = createEnv(); // these both share the same FakeExtension class Path extDir1 = createTempDir(); String extZip1 = createExtension("fake1", extDir1); - installExtension(extZip1, env); + installExtension(extZip1, home); Path extDir2 = createTempDir(); String extZip2 = createExtension("fake2", extDir2); - installExtension(extZip2, env); + installExtension(extZip2, home); assertExtension("fake1", extDir1, env); assertExtension("fake2", extDir2, env); } public void testExistingExtension() throws Exception { - Environment env = createEnv(); String extZip = createExtension("fake", createTempDir()); - installExtension(extZip, env); - UserError e = expectThrows(UserError.class, () -> { - installExtension(extZip, env); - }); + installExtension(extZip, home); + UserError e = expectThrows(UserError.class, () -> installExtension(extZip, home)); assertTrue(e.getMessage(), e.getMessage().contains("already exists")); assertInstallCleaned(env); } public void testMissingDescriptor() throws Exception { - Environment env = createEnv(); Path extDir = createTempDir(); Files.createFile(extDir.resolve("fake.yml")); String extZip = writeZip(extDir); - NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> { - installExtension(extZip, env); - }); + NoSuchFileException e = expectThrows(NoSuchFileException.class, () -> installExtension(extZip, home)); assertTrue(e.getMessage(), e.getMessage().contains("x-pack-extension-descriptor.properties")); assertInstallCleaned(env); } diff --git a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java index c4f484daa9e..87ef2d85536 100644 --- a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java +++ b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/ListXPackExtensionCommandTests.java @@ -11,6 +11,7 @@ import org.elasticsearch.cli.MockTerminal; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; +import org.junit.Before; import java.io.IOException; import java.nio.file.Files; @@ -19,60 +20,55 @@ import java.nio.file.Path; @LuceneTestCase.SuppressFileSystems("*") public class ListXPackExtensionCommandTests extends ESTestCase { - Environment createEnv() throws IOException { - Path home = createTempDir(); - Settings settings = Settings.builder() - .put("path.home", home) - .build(); - return new Environment(settings); + private Path home; + + @Before + public void setUp() throws Exception { + super.setUp(); + home = createTempDir(); } - Path createExtensionDir(Environment env) throws IOException { - Path path = env.pluginsFile().resolve("x-pack").resolve("extensions"); + private static Path createExtensionDir(final Path home) throws IOException { + final Environment env = new Environment(Settings.builder().put("path.home", home.toString()).build()); + final Path path = env.pluginsFile().resolve("x-pack").resolve("extensions"); return Files.createDirectories(path); } - static MockTerminal listExtensions(Environment env) throws Exception { + static MockTerminal listExtensions(Path home) throws Exception { MockTerminal terminal = new MockTerminal(); - String[] args = {}; - int status = new ListXPackExtensionCommand(env).main(args, terminal); + int status = new ListXPackExtensionCommand().main(new String[] { "-Epath.home=" + home }, terminal); assertEquals(ExitCodes.OK, status); return terminal; } public void testExtensionsDirMissing() throws Exception { - Environment env = createEnv(); - Path extDir = createExtensionDir(env); + Path extDir = createExtensionDir(home); Files.delete(extDir); - IOException e = expectThrows(IOException.class, () -> { - listExtensions(env); - }); + IOException e = expectThrows(IOException.class, () -> listExtensions(home)); assertTrue(e.getMessage(), e.getMessage().contains("Extensions directory missing")); } public void testNoExtensions() throws Exception { - Environment env = createEnv(); - createExtensionDir(env); - MockTerminal terminal = listExtensions(env); + createExtensionDir(home); + MockTerminal terminal = listExtensions(home); assertTrue(terminal.getOutput(), terminal.getOutput().isEmpty()); } public void testOneExtension() throws Exception { - Environment env = createEnv(); - Path extDir = createExtensionDir(env); + Path extDir = createExtensionDir(home); Files.createDirectory(extDir.resolve("fake")); - MockTerminal terminal = listExtensions(env); + MockTerminal terminal = listExtensions(home); assertTrue(terminal.getOutput(), terminal.getOutput().contains("fake")); } public void testTwoExtensions() throws Exception { - Environment env = createEnv(); - Path extDir = createExtensionDir(env); + Path extDir = createExtensionDir(home); Files.createDirectory(extDir.resolve("fake1")); Files.createDirectory(extDir.resolve("fake2")); - MockTerminal terminal = listExtensions(env); + MockTerminal terminal = listExtensions(home); String output = terminal.getOutput(); assertTrue(output, output.contains("fake1")); assertTrue(output, output.contains("fake2")); } + } diff --git a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommandTests.java b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommandTests.java index fec7b42e86f..fda0b00f3e3 100644 --- a/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommandTests.java +++ b/elasticsearch/x-pack/src/test/java/org/elasticsearch/xpack/extensions/RemoveXPackExtensionCommandTests.java @@ -11,22 +11,26 @@ import org.elasticsearch.cli.UserError; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.env.Environment; import org.elasticsearch.test.ESTestCase; +import org.junit.Before; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; @LuceneTestCase.SuppressFileSystems("*") public class RemoveXPackExtensionCommandTests extends ESTestCase { - /** Creates a test environment with bin, config and plugins directories. */ - static Environment createEnv() throws IOException { - Path home = createTempDir(); - Settings settings = Settings.builder() - .put("path.home", home) - .build(); - return new Environment(settings); + private Path home; + private Environment env; + + @Before + public void setUp() throws Exception { + super.setUp(); + home = createTempDir(); + env = new Environment(Settings.builder().put("path.home", home.toString()).build()); } Path createExtensionDir(Environment env) throws IOException { @@ -34,9 +38,11 @@ public class RemoveXPackExtensionCommandTests extends ESTestCase { return Files.createDirectories(path); } - static MockTerminal removeExtension(String name, Environment env) throws Exception { + static MockTerminal removeExtension(String name, Path home) throws Exception { + Map settings = new HashMap<>(); + settings.put("path.home", home.toString()); MockTerminal terminal = new MockTerminal(); - new RemoveXPackExtensionCommand(env).execute(terminal, name); + new RemoveXPackExtensionCommand().execute(terminal, name, settings); return terminal; } @@ -51,25 +57,22 @@ public class RemoveXPackExtensionCommandTests extends ESTestCase { } public void testMissing() throws Exception { - Environment env = createEnv(); Path extDir = createExtensionDir(env); - UserError e = expectThrows(UserError.class, () -> { - removeExtension("dne", env); - }); + UserError e = expectThrows(UserError.class, () -> removeExtension("dne", home)); assertTrue(e.getMessage(), e.getMessage().contains("Extension dne not found")); assertRemoveCleaned(extDir); } public void testBasic() throws Exception { - Environment env = createEnv(); Path extDir = createExtensionDir(env); Files.createDirectory(extDir.resolve("fake")); Files.createFile(extDir.resolve("fake").resolve("extension.jar")); Files.createDirectory(extDir.resolve("fake").resolve("subdir")); Files.createDirectory(extDir.resolve("other")); - removeExtension("fake", env); + removeExtension("fake", home); assertFalse(Files.exists(extDir.resolve("fake"))); assertTrue(Files.exists(extDir.resolve("other"))); assertRemoveCleaned(extDir); } + } diff --git a/elasticsearch/x-pack/watcher/bin/x-pack/croneval b/elasticsearch/x-pack/watcher/bin/x-pack/croneval index edfe1041755..4194b2d0dc6 100755 --- a/elasticsearch/x-pack/watcher/bin/x-pack/croneval +++ b/elasticsearch/x-pack/watcher/bin/x-pack/croneval @@ -70,29 +70,6 @@ elif [ -f "/etc/default/elasticsearch" ]; then . "/etc/default/elasticsearch" fi -# Parse any long getopt options and put them into properties -ARGCOUNT=$# -COUNT=0 -while [ $COUNT -lt $ARGCOUNT ] -do - case $1 in - --*) properties="$properties $1 $2" - shift ; shift; COUNT=$(($COUNT+2)) - ;; - *) set -- "$@" "$1"; shift; COUNT=$(($COUNT+1)) - esac -done - -# check if properties already has a config file or config dir -if [ -e "$CONF_DIR" ]; then - case "$properties" in - *-Des.default.path.conf=*) ;; - *) - properties="$properties -Des.default.path.conf=$CONF_DIR" - ;; - esac -fi - export HOSTNAME=`hostname -s` # include watcher jars in classpath @@ -112,8 +89,14 @@ if [ ! -z "$CONF_FILE" ]; then exit 1 fi +declare -a args=("$@") + +if [ -e "$CONF_DIR" ]; then + args=("${args[@]}" -Edefault.path.conf="$CONF_DIR") +fi + cd "$ES_HOME" > /dev/null -"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool "$@" $properties +"$JAVA" $ES_JAVA_OPTS -cp "$ES_CLASSPATH" -Des.path.home="$ES_HOME" org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool "${args[@]}" status=$? cd - > /dev/null exit $status diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java index bcbb7560590..540d6c4f875 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/WatcherService.java @@ -64,7 +64,7 @@ public class WatcherService extends AbstractComponent { public void start(ClusterState clusterState) throws Exception { if (state.compareAndSet(WatcherState.STOPPED, WatcherState.STARTING)) { try { - logger.info("starting watch service..."); + logger.debug("starting watch service..."); watcherIndexTemplateRegistry.addTemplatesIfMissing(); watchLockService.start(); @@ -74,7 +74,7 @@ public class WatcherService extends AbstractComponent { triggerService.start(watchStore.activeWatches()); state.set(WatcherState.STARTED); - logger.info("watch service has started"); + logger.debug("watch service has started"); } catch (Exception e) { state.set(WatcherState.STOPPED); throw e; @@ -90,7 +90,7 @@ public class WatcherService extends AbstractComponent { public void stop() { if (state.compareAndSet(WatcherState.STARTED, WatcherState.STOPPING)) { - logger.info("stopping watch service..."); + logger.debug("stopping watch service..."); triggerService.stop(); executionService.stop(); try { @@ -100,7 +100,7 @@ public class WatcherService extends AbstractComponent { } watchStore.stop(); state.set(WatcherState.STOPPED); - logger.info("watch service has stopped"); + logger.debug("watch service has stopped"); } else { logger.debug("not stopping watcher, because its state is [{}] while [{}] is expected", state, WatcherState.STARTED); } diff --git a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleModule.java b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleModule.java index 7c6170fbf93..407362814cf 100644 --- a/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleModule.java +++ b/elasticsearch/x-pack/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/ScheduleModule.java @@ -36,7 +36,7 @@ public class ScheduleModule extends AbstractModule { public static Class triggerEngineType(Settings nodeSettings) { Engine engine = Engine.resolve(nodeSettings); - Loggers.getLogger(ScheduleModule.class, nodeSettings).info("using [{}] schedule trigger engine", + Loggers.getLogger(ScheduleModule.class, nodeSettings).debug("using [{}] schedule trigger engine", engine.name().toLowerCase(Locale.ROOT)); return engine.engineType(); }