Search in sources :

Example 1 with HttpProxyOptions

use of software.amazon.awssdk.crt.http.HttpProxyOptions in project aws-greengrass-nucleus by aws-greengrass.

the class ProxyUtils method getHttpProxyOptions.

/**
 * Provides a software.amazon.awssdk.crt.http.HttpProxyOptions object that can be used when building various
 * CRT library clients (like mqtt and http)
 *
 * @param deviceConfiguration contains user specified system proxy values
 * @param tlsContext contains TLS options for proxy connection if an HTTPS proxy is used
 * @return httpProxyOptions containing user proxy settings, if specified. If not, httpProxyOptions is null.
 */
@Nullable
public static HttpProxyOptions getHttpProxyOptions(DeviceConfiguration deviceConfiguration, @NonNull ClientTlsContext tlsContext) {
    String proxyUrl = deviceConfiguration.getProxyUrl();
    if (Utils.isEmpty(proxyUrl)) {
        return null;
    }
    HttpProxyOptions httpProxyOptions = new HttpProxyOptions();
    httpProxyOptions.setHost(ProxyUtils.getHostFromProxyUrl(proxyUrl));
    httpProxyOptions.setPort(ProxyUtils.getPortFromProxyUrl(proxyUrl));
    if ("https".equalsIgnoreCase(getSchemeFromProxyUrl(proxyUrl))) {
        httpProxyOptions.setTlsContext(tlsContext);
    }
    String proxyUsername = getProxyUsername(proxyUrl, deviceConfiguration.getProxyUsername());
    if (Utils.isNotEmpty(proxyUsername)) {
        httpProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.Basic);
        httpProxyOptions.setAuthorizationUsername(proxyUsername);
        httpProxyOptions.setAuthorizationPassword(getProxyPassword(proxyUrl, deviceConfiguration.getProxyPassword()));
    }
    return httpProxyOptions;
}
Also used : HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) Nullable(javax.annotation.Nullable)

Example 2 with HttpProxyOptions

use of software.amazon.awssdk.crt.http.HttpProxyOptions in project aws-greengrass-nucleus by aws-greengrass.

the class ProxyUtilsTest method testGetHttpProxyOptions.

@Test
void testGetHttpProxyOptions() {
    when(deviceConfiguration.getProxyUrl()).thenReturn("https://myproxy:8080");
    when(deviceConfiguration.getProxyUsername()).thenReturn("test-user");
    when(deviceConfiguration.getProxyPassword()).thenReturn("itsasecret");
    HttpProxyOptions httpProxyOptions = ProxyUtils.getHttpProxyOptions(deviceConfiguration, new ClientTlsContext(TlsContextOptions.createDefaultClient()));
    assertEquals("myproxy", httpProxyOptions.getHost());
    assertEquals(8080, httpProxyOptions.getPort());
    assertEquals(HttpProxyOptions.HttpProxyAuthorizationType.Basic, httpProxyOptions.getAuthorizationType());
    assertEquals("test-user", httpProxyOptions.getAuthorizationUsername());
    assertEquals("itsasecret", httpProxyOptions.getAuthorizationPassword());
}
Also used : HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) Test(org.junit.jupiter.api.Test)

Example 3 with HttpProxyOptions

use of software.amazon.awssdk.crt.http.HttpProxyOptions in project aws-crt-java by awslabs.

the class ProxyTest method buildDirectMqttConnection.

private MqttClientConnection buildDirectMqttConnection(ProxyTestType testType, ProxyAuthType authType) {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        TlsContext tlsContext = createX509TlsContext(null);
        MqttClient mqttClient = new MqttClient(bootstrap, tlsContext);
        MqttConnectionConfig connectionConfig = new MqttConnectionConfig();
        TlsContext proxyTlsContext = createProxyTlsContext(testType)) {
        HttpProxyOptions proxyOptions = buildProxyOptions(testType, authType, proxyTlsContext);
        String clientId = PROXY_TEST_CLIENTID + (UUID.randomUUID()).toString();
        connectionConfig.setMqttClient(mqttClient);
        connectionConfig.setEndpoint(MQTT_ENDPOINT);
        connectionConfig.setHttpProxyOptions(proxyOptions);
        connectionConfig.setCleanSession(true);
        connectionConfig.setClientId(clientId);
        connectionConfig.setPort(MQTT_DIRECT_PORT);
        connectionConfig.setProtocolOperationTimeoutMs(60000);
        return new MqttClientConnection(connectionConfig);
    }
}
Also used : MqttClient(software.amazon.awssdk.crt.mqtt.MqttClient) HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) MqttClientConnection(software.amazon.awssdk.crt.mqtt.MqttClientConnection) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) MqttConnectionConfig(software.amazon.awssdk.crt.mqtt.MqttConnectionConfig) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) TlsContext(software.amazon.awssdk.crt.io.TlsContext) HostResolver(software.amazon.awssdk.crt.io.HostResolver)

Example 4 with HttpProxyOptions

use of software.amazon.awssdk.crt.http.HttpProxyOptions in project aws-crt-java by awslabs.

the class ProxyTest method buildProxiedX509CredentialsProvider.

private CredentialsProvider buildProxiedX509CredentialsProvider(ProxyTestType testType, ProxyAuthType authType) {
    try (EventLoopGroup eventLoopGroup = new EventLoopGroup(1);
        HostResolver resolver = new HostResolver(eventLoopGroup);
        ClientBootstrap bootstrap = new ClientBootstrap(eventLoopGroup, resolver);
        TlsContext tlsContext = createX509TlsContext(null);
        TlsContext proxyTlsContext = createProxyTlsContext(testType)) {
        HttpProxyOptions proxyOptions = buildProxyOptions(testType, authType, proxyTlsContext);
        X509CredentialsProvider.X509CredentialsProviderBuilder builder = new X509CredentialsProvider.X509CredentialsProviderBuilder();
        builder.withClientBootstrap(bootstrap).withEndpoint(X509_CREDENTIALS_ENDPOINT).withProxyOptions(proxyOptions).withRoleAlias(X509_CREDENTIALS_ROLE_ALIAS).withThingName(X509_CREDENTIALS_THING_NAME).withTlsContext(tlsContext);
        return builder.build();
    }
}
Also used : HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions) EventLoopGroup(software.amazon.awssdk.crt.io.EventLoopGroup) ClientBootstrap(software.amazon.awssdk.crt.io.ClientBootstrap) ClientTlsContext(software.amazon.awssdk.crt.io.ClientTlsContext) TlsContext(software.amazon.awssdk.crt.io.TlsContext) HostResolver(software.amazon.awssdk.crt.io.HostResolver) X509CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.X509CredentialsProvider)

Example 5 with HttpProxyOptions

use of software.amazon.awssdk.crt.http.HttpProxyOptions in project aws-sdk-java-v2 by aws.

the class AwsCrtAsyncHttpClient method buildProxyOptions.

private HttpProxyOptions buildProxyOptions(ProxyConfiguration proxyConfiguration) {
    if (proxyConfiguration == null) {
        return null;
    }
    HttpProxyOptions clientProxyOptions = new HttpProxyOptions();
    clientProxyOptions.setHost(proxyConfiguration.host());
    clientProxyOptions.setPort(proxyConfiguration.port());
    if ("https".equalsIgnoreCase(proxyConfiguration.scheme())) {
        clientProxyOptions.setTlsContext(tlsContext);
    }
    if (proxyConfiguration.username() != null && proxyConfiguration.password() != null) {
        clientProxyOptions.setAuthorizationUsername(proxyConfiguration.username());
        clientProxyOptions.setAuthorizationPassword(proxyConfiguration.password());
        clientProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.Basic);
    } else {
        clientProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.None);
    }
    return clientProxyOptions;
}
Also used : HttpProxyOptions(software.amazon.awssdk.crt.http.HttpProxyOptions)

Aggregations

HttpProxyOptions (software.amazon.awssdk.crt.http.HttpProxyOptions)10 ClientTlsContext (software.amazon.awssdk.crt.io.ClientTlsContext)4 ClientBootstrap (software.amazon.awssdk.crt.io.ClientBootstrap)3 EventLoopGroup (software.amazon.awssdk.crt.io.EventLoopGroup)3 HostResolver (software.amazon.awssdk.crt.io.HostResolver)3 TlsContext (software.amazon.awssdk.crt.io.TlsContext)3 X509CredentialsProvider (software.amazon.awssdk.crt.auth.credentials.X509CredentialsProvider)2 File (java.io.File)1 ExecutionException (java.util.concurrent.ExecutionException)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.jupiter.api.Test)1 CrtRuntimeException (software.amazon.awssdk.crt.CrtRuntimeException)1 HttpClientConnectionManagerOptions (software.amazon.awssdk.crt.http.HttpClientConnectionManagerOptions)1 SocketOptions (software.amazon.awssdk.crt.io.SocketOptions)1 MqttClient (software.amazon.awssdk.crt.mqtt.MqttClient)1 MqttClientConnection (software.amazon.awssdk.crt.mqtt.MqttClientConnection)1 MqttConnectionConfig (software.amazon.awssdk.crt.mqtt.MqttConnectionConfig)1 AwsIotMqttConnectionBuilder (software.amazon.awssdk.iot.AwsIotMqttConnectionBuilder)1 DiscoveryClient (software.amazon.awssdk.iot.discovery.DiscoveryClient)1 DiscoveryClientConfig (software.amazon.awssdk.iot.discovery.DiscoveryClientConfig)1