Search in sources :

Example 21 with SettingsException

use of org.opensearch.common.settings.SettingsException in project OpenSearch by opensearch-project.

the class InternalSettingsPreparer method prepareEnvironment.

/**
 * Prepares the settings by gathering all opensearch system properties, optionally loading the configuration settings.
 *
 * @param input      the custom settings to use; these are not overwritten by settings in the configuration file
 * @param properties map of properties key/value pairs (usually from the command-line)
 * @param configPath path to config directory; (use null to indicate the default)
 * @param defaultNodeName supplier for the default node.name if the setting isn't defined
 * @return the {@link Environment}
 */
public static Environment prepareEnvironment(Settings input, Map<String, String> properties, Path configPath, Supplier<String> defaultNodeName) {
    // 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(), configPath);
    // start with a fresh output
    output = Settings.builder();
    Path path = environment.configFile().resolve("opensearch.yml");
    if (Files.exists(path)) {
        try {
            output.loadFromPath(path);
        } catch (IOException e) {
            throw new SettingsException("Failed to load settings from " + path.toString(), e);
        }
    }
    // re-initialize settings now that the config file has been loaded
    initializeSettings(output, input, properties);
    checkSettingsForTerminalDeprecation(output);
    finalizeSettings(output, defaultNodeName);
    return new Environment(output.build(), configPath);
}
Also used : Path(java.nio.file.Path) Environment(org.opensearch.env.Environment) IOException(java.io.IOException) SettingsException(org.opensearch.common.settings.SettingsException) Settings(org.opensearch.common.settings.Settings)

Example 22 with SettingsException

use of org.opensearch.common.settings.SettingsException in project OpenSearch by opensearch-project.

the class InternalSettingsPreparerTests method testGarbageIsNotSwallowed.

public void testGarbageIsNotSwallowed() throws IOException {
    try {
        InputStream garbage = getClass().getResourceAsStream("/config/garbage/garbage.yml");
        Path home = createTempDir();
        Path config = home.resolve("config");
        Files.createDirectory(config);
        Files.copy(garbage, config.resolve("opensearch.yml"));
        InternalSettingsPreparer.prepareEnvironment(Settings.builder().put(baseEnvSettings).build(), emptyMap(), null, () -> "default_node_name");
    } catch (SettingsException e) {
        assertEquals("Failed to load settings from [opensearch.yml]", e.getMessage());
    }
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) SettingsException(org.opensearch.common.settings.SettingsException)

Example 23 with SettingsException

use of org.opensearch.common.settings.SettingsException in project OpenSearch by opensearch-project.

the class CorsHandlerTests method testCorsConfigWithBadRegex.

public void testCorsConfigWithBadRegex() {
    final Settings settings = Settings.builder().put(SETTING_CORS_ENABLED.getKey(), true).put(SETTING_CORS_ALLOW_ORIGIN.getKey(), "/[*/").put(SETTING_CORS_ALLOW_CREDENTIALS.getKey(), true).build();
    SettingsException e = expectThrows(SettingsException.class, () -> CorsHandler.buildConfig(settings));
    assertThat(e.getMessage(), containsString("Bad regex in [http.cors.allow-origin]: [/[*/]"));
    assertThat(e.getCause(), instanceOf(PatternSyntaxException.class));
}
Also used : SettingsException(org.opensearch.common.settings.SettingsException) Settings(org.opensearch.common.settings.Settings) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

SettingsException (org.opensearch.common.settings.SettingsException)23 Settings (org.opensearch.common.settings.Settings)15 MockSecureSettings (org.opensearch.common.settings.MockSecureSettings)14 SecureString (org.opensearch.common.settings.SecureString)4 BlobServiceClient (com.azure.storage.blob.BlobServiceClient)3 GoogleCloudStorageClientSettings.getClientSettings (org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.getClientSettings)3 IOException (java.io.IOException)2 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 Path (java.nio.file.Path)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Proxy (java.net.Proxy)1 HashMap (java.util.HashMap)1 PatternSyntaxException (java.util.regex.PatternSyntaxException)1 ToXContent (org.opensearch.common.xcontent.ToXContent)1 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)1