Search in sources :

Example 6 with SettingsException

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

the class AzureStorageServiceTests method testReinitClientSettings.

public void testReinitClientSettings() throws IOException {
    final MockSecureSettings secureSettings1 = new MockSecureSettings();
    secureSettings1.setString("azure.client.azure1.account", "myaccount11");
    secureSettings1.setString("azure.client.azure1.key", encodeKey("mykey11"));
    secureSettings1.setString("azure.client.azure2.account", "myaccount12");
    secureSettings1.setString("azure.client.azure2.key", encodeKey("mykey12"));
    final Settings settings1 = Settings.builder().setSecureSettings(secureSettings1).build();
    final MockSecureSettings secureSettings2 = new MockSecureSettings();
    secureSettings2.setString("azure.client.azure1.account", "myaccount21");
    secureSettings2.setString("azure.client.azure1.key", encodeKey("mykey21"));
    secureSettings2.setString("azure.client.azure3.account", "myaccount23");
    secureSettings2.setString("azure.client.azure3.key", encodeKey("mykey23"));
    final Settings settings2 = Settings.builder().setSecureSettings(secureSettings2).build();
    try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings1)) {
        final AzureStorageService azureStorageService = plugin.azureStoreService;
        final BlobServiceClient client11 = azureStorageService.client("azure1").v1();
        assertThat(client11.getAccountUrl(), equalTo("https://myaccount11.blob.core.windows.net"));
        final BlobServiceClient client12 = azureStorageService.client("azure2").v1();
        assertThat(client12.getAccountUrl(), equalTo("https://myaccount12.blob.core.windows.net"));
        // client 3 is missing
        final SettingsException e1 = expectThrows(SettingsException.class, () -> azureStorageService.client("azure3"));
        assertThat(e1.getMessage(), is("Unable to find client with name [azure3]"));
        // update client settings
        plugin.reload(settings2);
        // old client 1 not changed
        assertThat(client11.getAccountUrl(), equalTo("https://myaccount11.blob.core.windows.net"));
        // new client 1 is changed
        final BlobServiceClient client21 = azureStorageService.client("azure1").v1();
        assertThat(client21.getAccountUrl(), equalTo("https://myaccount21.blob.core.windows.net"));
        // old client 2 not changed
        assertThat(client12.getAccountUrl(), equalTo("https://myaccount12.blob.core.windows.net"));
        // new client2 is gone
        final SettingsException e2 = expectThrows(SettingsException.class, () -> azureStorageService.client("azure2"));
        assertThat(e2.getMessage(), is("Unable to find client with name [azure2]"));
        // client 3 emerged
        final BlobServiceClient client23 = azureStorageService.client("azure3").v1();
        assertThat(client23.getAccountUrl(), equalTo("https://myaccount23.blob.core.windows.net"));
    }
}
Also used : BlobServiceClient(com.azure.storage.blob.BlobServiceClient) SettingsException(org.opensearch.common.settings.SettingsException) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) Settings(org.opensearch.common.settings.Settings)

Example 7 with SettingsException

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

the class AzureStorageServiceTests method testReinitClientWrongSettings.

public void testReinitClientWrongSettings() throws IOException {
    final MockSecureSettings secureSettings1 = new MockSecureSettings();
    secureSettings1.setString("azure.client.azure1.account", "myaccount1");
    secureSettings1.setString("azure.client.azure1.key", encodeKey("mykey11"));
    final Settings settings1 = Settings.builder().setSecureSettings(secureSettings1).build();
    final MockSecureSettings secureSettings2 = new MockSecureSettings();
    secureSettings2.setString("azure.client.azure1.account", "myaccount1");
    // missing key
    final Settings settings2 = Settings.builder().setSecureSettings(secureSettings2).build();
    final MockSecureSettings secureSettings3 = new MockSecureSettings();
    secureSettings3.setString("azure.client.azure1.account", "myaccount3");
    secureSettings3.setString("azure.client.azure1.key", encodeKey("mykey33"));
    secureSettings3.setString("azure.client.azure1.sas_token", encodeKey("mysasToken33"));
    final Settings settings3 = Settings.builder().setSecureSettings(secureSettings3).build();
    try (AzureRepositoryPlugin plugin = pluginWithSettingsValidation(settings1)) {
        final AzureStorageService azureStorageService = plugin.azureStoreService;
        final BlobServiceClient client11 = azureStorageService.client("azure1").v1();
        assertThat(client11.getAccountUrl(), equalTo("https://myaccount1.blob.core.windows.net"));
        final SettingsException e1 = expectThrows(SettingsException.class, () -> plugin.reload(settings2));
        assertThat(e1.getMessage(), is("Neither a secret key nor a shared access token was set."));
        final SettingsException e2 = expectThrows(SettingsException.class, () -> plugin.reload(settings3));
        assertThat(e2.getMessage(), is("Both a secret as well as a shared access token were set."));
        // existing client untouched
        assertThat(client11.getAccountUrl(), equalTo("https://myaccount1.blob.core.windows.net"));
    }
}
Also used : BlobServiceClient(com.azure.storage.blob.BlobServiceClient) SettingsException(org.opensearch.common.settings.SettingsException) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) Settings(org.opensearch.common.settings.Settings)

Example 8 with SettingsException

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

the class S3ClientSettings method validateAndCreateProxySettings.

static ProxySettings validateAndCreateProxySettings(final Settings settings, final String clientName, final Protocol awsProtocol) {
    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);
    if (awsProtocol != Protocol.HTTPS && proxyType == ProxySettings.ProxyType.DIRECT && Strings.hasText(proxyHost)) {
        // This is backward compatibility for the current behaviour.
        // The default value for Protocol settings is HTTPS,
        // The expectation of ex-developers that protocol is the same as the proxy protocol
        // which is a separate setting for AWS SDK.
        // In this case, proxy type should be the same as a protocol,
        // when proxy host and port have been set
        proxyType = ProxySettings.ProxyType.valueOf(awsProtocol.name());
        deprecationLogger.deprecate(PROTOCOL_SETTING.getConcreteSettingForNamespace(clientName).getKey(), "Using of " + PROTOCOL_SETTING.getConcreteSettingForNamespace(clientName).getKey() + " as proxy type is deprecated and will be removed in future releases. Please use " + PROXY_TYPE_SETTING.getConcreteSettingForNamespace(clientName).getKey() + " instead to specify proxy type.");
    }
    // Validate proxy settings
    if (proxyType == ProxySettings.ProxyType.DIRECT && (proxyPort != 80 || Strings.hasText(proxyHost) || Strings.hasText(proxyUserName) || Strings.hasText(proxyPassword))) {
        throw new SettingsException("S3 proxy port or host or username or password have been set but proxy type is not defined.");
    }
    if (proxyType != ProxySettings.ProxyType.DIRECT && Strings.isEmpty(proxyHost)) {
        throw new SettingsException("S3 proxy type has been set but proxy host or port is not defined.");
    }
    if (proxyType == ProxySettings.ProxyType.DIRECT) {
        return ProxySettings.NO_PROXY_SETTINGS;
    }
    if (awsProtocol == Protocol.HTTP && proxyType == ProxySettings.ProxyType.SOCKS) {
        throw new SettingsException("SOCKS proxy is not supported for HTTP protocol");
    }
    validateInetAddressFor(proxyHost);
    return new ProxySettings(proxyType, proxyHost, proxyPort, proxyUserName.toString(), proxyPassword.toString());
}
Also used : SecureString(org.opensearch.common.settings.SecureString) SettingsException(org.opensearch.common.settings.SettingsException) SecureString(org.opensearch.common.settings.SecureString)

Example 9 with SettingsException

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

the class S3ClientSettingsTests method testProxyTypeNotSet.

public void testProxyTypeNotSet() {
    final Settings hostPortSettings = Settings.builder().put("s3.client.default.proxy.host", "127.0.0.1").put("s3.client.default.proxy.port", 8080).build();
    SettingsException e = expectThrows(SettingsException.class, () -> S3ClientSettings.load(hostPortSettings));
    assertEquals("S3 proxy port or host or username or password have been set but proxy type is not defined.", e.getMessage());
    final MockSecureSettings secureSettings = new MockSecureSettings();
    secureSettings.setString("s3.client.default.proxy.username", "aaaa");
    secureSettings.setString("s3.client.default.proxy.password", "bbbb");
    final Settings usernamePasswordSettings = Settings.builder().setSecureSettings(secureSettings).build();
    e = expectThrows(SettingsException.class, () -> S3ClientSettings.load(usernamePasswordSettings));
    assertEquals("S3 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) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings)

Example 10 with SettingsException

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

the class AwsEc2ServiceImplTests method testRejectionOfLoneSessionToken.

public void testRejectionOfLoneSessionToken() {
    final MockSecureSettings secureSettings = new MockSecureSettings();
    secureSettings.setString("discovery.ec2.session_token", "aws_session_token");
    SettingsException e = expectThrows(SettingsException.class, () -> AwsEc2ServiceImpl.buildCredentials(logger, Ec2ClientSettings.getClientSettings(Settings.builder().setSecureSettings(secureSettings).build())));
    assertThat(e.getMessage(), is("Setting [discovery.ec2.session_token] is set but [discovery.ec2.access_key] and [discovery.ec2.secret_key] are not"));
}
Also used : SettingsException(org.opensearch.common.settings.SettingsException) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings)

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