Search in sources :

Example 41 with Environment

use of org.elasticsearch.env.Environment in project elasticsearch by elastic.

the class InternalSettingsPreparerTests method testSecureSettings.

public void testSecureSettings() {
    MockSecureSettings secureSettings = new MockSecureSettings();
    secureSettings.setString("foo", "secret");
    Settings input = Settings.builder().put(baseEnvSettings).setSecureSettings(secureSettings).build();
    Environment env = InternalSettingsPreparer.prepareEnvironment(input, null);
    Setting<SecureString> fakeSetting = SecureSetting.secureString("foo", null, false);
    assertEquals("secret", fakeSetting.get(env.settings()).toString());
}
Also used : Environment(org.elasticsearch.env.Environment) MockSecureSettings(org.elasticsearch.common.settings.MockSecureSettings) MockSecureSettings(org.elasticsearch.common.settings.MockSecureSettings) Settings(org.elasticsearch.common.settings.Settings) SecureString(org.elasticsearch.common.settings.SecureString)

Example 42 with Environment

use of org.elasticsearch.env.Environment in project elasticsearch by elastic.

the class InternalSettingsPreparerTests method testEmptySettings.

public void testEmptySettings() {
    Settings settings = InternalSettingsPreparer.prepareSettings(Settings.EMPTY);
    // a name was not set
    assertNull(settings.get("node.name"));
    // a cluster name was set
    assertNotNull(settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()));
    int size = settings.names().size();
    Environment env = InternalSettingsPreparer.prepareEnvironment(baseEnvSettings, null);
    settings = env.settings();
    // a name was not set
    assertNull(settings.get("node.name"));
    // a cluster name was set
    assertNotNull(settings.get(ClusterName.CLUSTER_NAME_SETTING.getKey()));
    assertEquals(settings.toString(), size + 1, /* path.home is in the base settings */
    settings.names().size());
    String home = Environment.PATH_HOME_SETTING.get(baseEnvSettings);
    String configDir = env.configFile().toString();
    assertTrue(configDir, configDir.startsWith(home));
}
Also used : Environment(org.elasticsearch.env.Environment) SecureString(org.elasticsearch.common.settings.SecureString) Matchers.containsString(org.hamcrest.Matchers.containsString) MockSecureSettings(org.elasticsearch.common.settings.MockSecureSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 43 with Environment

use of org.elasticsearch.env.Environment in project elasticsearch by elastic.

the class InternalSettingsPreparer method prepareEnvironment.

/**
     * Prepares the settings by gathering all elasticsearch system properties, optionally loading the configuration settings,
     * and then replacing all property placeholders. If a {@link Terminal} is provided and configuration settings are loaded,
     * settings with a value of <code>${prompt.text}</code> or <code>${prompt.secret}</code> will result in a prompt for
     * the setting to the user.
     * @param input The custom settings to use. These are not overwritten by settings in the configuration file.
     * @param terminal the Terminal to use for input/output
     * @param properties Map of properties key/value pairs (usually from the command-line)
     * @return the {@link Settings} and {@link Environment} as a {@link Tuple}
     */
public static Environment prepareEnvironment(Settings input, Terminal terminal, Map<String, String> properties) {
    // just create enough settings to build the environment, to get the config dir
    Settings.Builder output = Settings.builder();
    initializeSettings(output, input, properties);
    Environment environment = new Environment(output.build());
    // start with a fresh output
    output = Settings.builder();
    boolean settingsFileFound = false;
    Set<String> foundSuffixes = new HashSet<>();
    for (String allowedSuffix : ALLOWED_SUFFIXES) {
        Path path = environment.configFile().resolve("elasticsearch" + allowedSuffix);
        if (Files.exists(path)) {
            if (!settingsFileFound) {
                try {
                    output.loadFromPath(path);
                } catch (IOException e) {
                    throw new SettingsException("Failed to load settings from " + path.toString(), e);
                }
            }
            settingsFileFound = true;
            foundSuffixes.add(allowedSuffix);
        }
    }
    if (foundSuffixes.size() > 1) {
        throw new SettingsException("multiple settings files found with suffixes: " + Strings.collectionToDelimitedString(foundSuffixes, ","));
    }
    // re-initialize settings now that the config file has been loaded
    initializeSettings(output, input, properties);
    finalizeSettings(output, terminal);
    environment = new Environment(output.build());
    // we put back the path.logs so we can use it in the logging configuration file
    output.put(Environment.PATH_LOGS_SETTING.getKey(), cleanPath(environment.logsFile().toAbsolutePath().toString()));
    return new Environment(output.build());
}
Also used : Path(java.nio.file.Path) Strings.cleanPath(org.elasticsearch.common.Strings.cleanPath) Environment(org.elasticsearch.env.Environment) IOException(java.io.IOException) SettingsException(org.elasticsearch.common.settings.SettingsException) Settings(org.elasticsearch.common.settings.Settings) HashSet(java.util.HashSet)

Example 44 with Environment

use of org.elasticsearch.env.Environment in project elasticsearch by elastic.

the class HunspellServiceTests method testLocaleDirectoryWithNodeLevelConfig.

public void testLocaleDirectoryWithNodeLevelConfig() throws Exception {
    Settings settings = Settings.builder().put(Environment.PATH_CONF_SETTING.getKey(), getDataPath("/indices/analyze/conf_dir")).put(HUNSPELL_LAZY_LOAD.getKey(), randomBoolean()).put(HUNSPELL_IGNORE_CASE.getKey(), true).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).build();
    Dictionary dictionary = new HunspellService(settings, new Environment(settings), emptyMap()).getDictionary("en_US");
    assertThat(dictionary, notNullValue());
    assertTrue(dictionary.getIgnoreCase());
}
Also used : Dictionary(org.apache.lucene.analysis.hunspell.Dictionary) HunspellService(org.elasticsearch.indices.analysis.HunspellService) Environment(org.elasticsearch.env.Environment) Settings(org.elasticsearch.common.settings.Settings)

Example 45 with Environment

use of org.elasticsearch.env.Environment in project elasticsearch by elastic.

the class FileScriptTests method makeScriptService.

ScriptService makeScriptService(Settings settings) throws Exception {
    Path homeDir = createTempDir();
    Path scriptsDir = homeDir.resolve("config").resolve("scripts");
    Files.createDirectories(scriptsDir);
    Path mockscript = scriptsDir.resolve("script1.mockscript");
    String scriptSource = "1";
    Files.write(mockscript, scriptSource.getBytes("UTF-8"));
    settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), homeDir).put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false).put(settings).build();
    MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, Collections.singletonMap(scriptSource, script -> "1"));
    ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singleton(scriptEngine));
    ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList());
    ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry);
    return new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, scriptSettings);
}
Also used : Path(java.nio.file.Path) Settings(org.elasticsearch.common.settings.Settings) Files(java.nio.file.Files) MockCompiledScript(org.elasticsearch.script.MockScriptEngine.MockCompiledScript) Environment(org.elasticsearch.env.Environment) ESTestCase(org.elasticsearch.test.ESTestCase) Path(java.nio.file.Path) Collections(java.util.Collections) Environment(org.elasticsearch.env.Environment)

Aggregations

Environment (org.elasticsearch.env.Environment)103 Settings (org.elasticsearch.common.settings.Settings)63 Path (java.nio.file.Path)60 Matchers.containsString (org.hamcrest.Matchers.containsString)28 IndexSettings (org.elasticsearch.index.IndexSettings)20 UserException (org.elasticsearch.cli.UserException)14 IOException (java.io.IOException)11 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)8 ScriptService (org.elasticsearch.script.ScriptService)8 AnalysisModule (org.elasticsearch.indices.analysis.AnalysisModule)7 ScriptContextRegistry (org.elasticsearch.script.ScriptContextRegistry)7 ScriptEngineRegistry (org.elasticsearch.script.ScriptEngineRegistry)7 ScriptSettings (org.elasticsearch.script.ScriptSettings)7 Collections (java.util.Collections)5 Before (org.junit.Before)5 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Logger (org.apache.logging.log4j.Logger)4 MockScriptEngine (org.elasticsearch.script.MockScriptEngine)4