Search in sources :

Example 1 with SettingsException

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

the class ImportYmlConfigTask method writeSettings.

/**
 * Write settings to the config file on the file system. It uses the {@link XContentBuilder}
 * to build the YAML content and write it to the output stream.
 *
 * @param configYml path to a yml file where config will be written to.
 * @param settings  the settings to write
 * @throws IOException exception during writing to the output stream.
 */
private void writeSettings(final Path configYml, final Settings settings) throws IOException {
    try (OutputStream os = Files.newOutputStream(configYml, StandardOpenOption.APPEND);
        XContentBuilder builder = new XContentBuilder(YamlXContent.yamlXContent, os)) {
        builder.startObject();
        final Map<String, String> params = new HashMap<>();
        params.put("flat_settings", "true");
        settings.toXContent(builder, new ToXContent.MapParams(params));
        builder.endObject();
        builder.flush();
    } catch (Exception e) {
        throw new SettingsException("Failed to write settings to " + configYml.toString(), e);
    }
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) SettingsException(org.opensearch.common.settings.SettingsException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException) SettingsException(org.opensearch.common.settings.SettingsException)

Example 2 with SettingsException

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

the class GoogleCloudStorageClientSettingsTests method testProxyWrongHost.

public void testProxyWrongHost() {
    final Settings settings = Settings.builder().put("gcs.client.default.proxy.type", randomFrom("socks", "http")).put("gcs.client.default.proxy.host", "thisisnotavalidhostorwehavebeensuperunlucky").put("gcs.client.default.proxy.port", 8080).build();
    final SettingsException e = expectThrows(SettingsException.class, () -> GoogleCloudStorageClientSettings.load(settings));
    assertEquals("Google Cloud Storage proxy host is unknown.", e.getMessage());
}
Also used : SettingsException(org.opensearch.common.settings.SettingsException) GoogleCloudStorageClientSettings.getClientSettings(org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.getClientSettings) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) Settings(org.opensearch.common.settings.Settings)

Example 3 with SettingsException

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

the class GoogleCloudStorageClientSettingsTests method testProxyHostNotSet.

public void testProxyHostNotSet() {
    final Settings settings = Settings.builder().put("gcs.client.default.proxy.port", 8080).put("gcs.client.default.proxy.type", randomFrom("socks", "http")).build();
    final SettingsException e = expectThrows(SettingsException.class, () -> GoogleCloudStorageClientSettings.load(settings));
    assertEquals("Google Cloud Storage proxy type has been set but proxy host or port is not defined.", e.getMessage());
}
Also used : SettingsException(org.opensearch.common.settings.SettingsException) GoogleCloudStorageClientSettings.getClientSettings(org.opensearch.repositories.gcs.GoogleCloudStorageClientSettings.getClientSettings) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) Settings(org.opensearch.common.settings.Settings)

Example 4 with SettingsException

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

the class AzureStorageSettings method validateAndCreateProxySettings.

static ProxySettings validateAndCreateProxySettings(final Settings settings, final String clientName) {
    final ProxySettings.ProxyType proxyType = getConfigValue(settings, clientName, PROXY_TYPE_SETTING);
    final String proxyHost = getConfigValue(settings, clientName, PROXY_HOST_SETTING);
    final int proxyPort = getConfigValue(settings, clientName, PROXY_PORT_SETTING);
    final SecureString proxyUserName = getConfigValue(settings, clientName, PROXY_USERNAME_SETTING);
    final SecureString proxyPassword = getConfigValue(settings, clientName, PROXY_PASSWORD_SETTING);
    // Validate proxy settings
    if (proxyType == ProxySettings.ProxyType.DIRECT && (proxyPort != 0 || Strings.hasText(proxyHost) || Strings.hasText(proxyUserName) || Strings.hasText(proxyPassword))) {
        throw new SettingsException("Azure proxy port or host or username or password have been set but proxy type is not defined.");
    }
    if (proxyType != ProxySettings.ProxyType.DIRECT && (proxyPort == 0 || Strings.isEmpty(proxyHost))) {
        throw new SettingsException("Azure proxy type has been set but proxy host or port is not defined.");
    }
    if (proxyType == ProxySettings.ProxyType.DIRECT) {
        return ProxySettings.NO_PROXY_SETTINGS;
    }
    try {
        final InetAddress proxyHostAddress = InetAddress.getByName(proxyHost);
        return new ProxySettings(proxyType, proxyHostAddress, proxyPort, proxyUserName.toString(), proxyPassword.toString());
    } catch (final UnknownHostException e) {
        throw new SettingsException("Azure proxy host is unknown.", e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) SecureString(org.opensearch.common.settings.SecureString) SettingsException(org.opensearch.common.settings.SettingsException) InetAddress(java.net.InetAddress) SecureString(org.opensearch.common.settings.SecureString)

Example 5 with SettingsException

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

the class AzureStorageServiceTests method testProxyNoType.

public void testProxyNoType() {
    final Settings settings = Settings.builder().setSecureSettings(buildSecureSettings()).put("azure.client.azure1.proxy.host", "127.0.0.1").put("azure.client.azure1.proxy.port", 8080).build();
    final SettingsException e = expectThrows(SettingsException.class, () -> storageServiceWithSettingsValidation(settings));
    assertEquals("Azure proxy port or host or username or password have been set but proxy type is not defined.", e.getMessage());
}
Also used : SettingsException(org.opensearch.common.settings.SettingsException) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) Settings(org.opensearch.common.settings.Settings)

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