From f4fb56a83117db492e12f91246d43cd031f5b009 Mon Sep 17 00:00:00 2001 From: Waqar Ahmed Khan Date: Mon, 18 Sep 2023 10:41:17 -0500 Subject: [PATCH] Add ENV Proxy Option to HTTP Connection Manager (#682) --- crt/s2n | 2 +- .../crt/http/HttpClientConnectionManager.java | 18 +++++++++++++ .../HttpClientConnectionManagerOptions.java | 25 +++++++++++++++++++ .../HttpProxyEnvironmentVariableSetting.java | 9 ++++++- src/native/http_connection_manager.c | 15 +++++++++++ 5 files changed, 67 insertions(+), 2 deletions(-) diff --git a/crt/s2n b/crt/s2n index baf094768..f7930e58e 160000 --- a/crt/s2n +++ b/crt/s2n @@ -1 +1 @@ -Subproject commit baf094768e90d10a1f72182a1b04160e31d54c13 +Subproject commit f7930e58e730ad745063785d1d421ecc952aee00 diff --git a/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManager.java b/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManager.java index 764e206b0..de1508b5d 100644 --- a/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManager.java +++ b/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManager.java @@ -93,6 +93,16 @@ private HttpClientConnectionManager(HttpClientConnectionManagerOptions options) proxyAuthorizationPassword = proxyOptions.getAuthorizationPassword(); } + int environmentVariableProxyConnectionType = 0; + TlsConnectionOptions environmentVariableProxyTlsConnectionOptions = null; + int environmentVariableType = 0; + HttpProxyEnvironmentVariableSetting environmentVariableSetting = options.getHttpProxyEnvironmentVariableSetting(); + if (environmentVariableSetting != null) { + environmentVariableProxyConnectionType = environmentVariableSetting.getConnectionType().getValue(); + environmentVariableProxyTlsConnectionOptions = environmentVariableSetting.getTlsConnectionOptions(); + environmentVariableType = environmentVariableSetting.getEnvironmentVariableType().getValue(); + } + HttpMonitoringOptions monitoringOptions = options.getMonitoringOptions(); long monitoringThroughputThresholdInBytesPerSecond = 0; int monitoringFailureIntervalInSeconds = 0; @@ -117,6 +127,11 @@ private HttpClientConnectionManager(HttpClientConnectionManagerOptions options) proxyAuthorizationType, proxyAuthorizationUsername != null ? proxyAuthorizationUsername.getBytes(UTF8) : null, proxyAuthorizationPassword != null ? proxyAuthorizationPassword.getBytes(UTF8) : null, + environmentVariableProxyConnectionType, + environmentVariableProxyTlsConnectionOptions != null + ? environmentVariableProxyTlsConnectionOptions.getNativeHandle() + : 0, + environmentVariableType, options.isManualWindowManagement(), options.getMaxConnectionIdleInMilliseconds(), monitoringThroughputThresholdInBytesPerSecond, @@ -245,6 +260,9 @@ private static native long httpClientConnectionManagerNew(HttpClientConnectionMa int proxyAuthorizationType, byte[] proxyAuthorizationUsername, byte[] proxyAuthorizationPassword, + int environmentVariableProxyConnectionType, + long environmentVariableProxyTlsConnectionOptions, + int environmentVariableSetting, boolean isManualWindowManagement, long maxConnectionIdleInMilliseconds, long monitoringThroughputThresholdInBytesPerSecond, diff --git a/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManagerOptions.java b/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManagerOptions.java index 5b2cc0540..315c13539 100644 --- a/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManagerOptions.java +++ b/src/main/java/software/amazon/awssdk/crt/http/HttpClientConnectionManagerOptions.java @@ -28,6 +28,7 @@ public class HttpClientConnectionManagerOptions { private int port = -1; private int maxConnections = DEFAULT_MAX_CONNECTIONS; private HttpProxyOptions proxyOptions; + private HttpProxyEnvironmentVariableSetting httpProxyEnvironmentVariableSetting; private boolean manualWindowManagement = false; private HttpMonitoringOptions monitoringOptions; private long maxConnectionIdleInMilliseconds = 0; @@ -200,6 +201,30 @@ public HttpClientConnectionManagerOptions withProxyOptions(HttpProxyOptions prox */ public HttpProxyOptions getProxyOptions() { return proxyOptions; } + /** + * Optional. + * Sets how proxy is fetched from the environment. + * Reading proxy configuration from environment is disabled if this is NULL for backward compatibility. + * Only works when proxyOptions is not set. The proxy settings follow the following precedence + * 1. Configured Proxy Setting + * 2. Environment (if enabled) + * 3. No proxy + * @param httpProxyEnvironmentVariableSetting for this connection manager + * @return this + */ + public HttpClientConnectionManagerOptions withProxyEnvironmentVariableSetting( + HttpProxyEnvironmentVariableSetting httpProxyEnvironmentVariableSetting) { + this.httpProxyEnvironmentVariableSetting = httpProxyEnvironmentVariableSetting; + return this; + } + + /** + * @return the proxy environment variable setting + */ + public HttpProxyEnvironmentVariableSetting getHttpProxyEnvironmentVariableSetting() { + return httpProxyEnvironmentVariableSetting; + } + /** * @return true if manual window management is used, false otherwise * @see #withManualWindowManagement diff --git a/src/main/java/software/amazon/awssdk/crt/http/HttpProxyEnvironmentVariableSetting.java b/src/main/java/software/amazon/awssdk/crt/http/HttpProxyEnvironmentVariableSetting.java index 8eacfcf93..e11f9393d 100644 --- a/src/main/java/software/amazon/awssdk/crt/http/HttpProxyEnvironmentVariableSetting.java +++ b/src/main/java/software/amazon/awssdk/crt/http/HttpProxyEnvironmentVariableSetting.java @@ -58,7 +58,8 @@ public HttpProxyEnvironmentVariableSetting() { } /** - * Sets the proxy connection type + * (Optional) + * Sets the proxy connection type. Defaults to HttpProxyConnectionType.Legacy * @param connectionType what kind of connection to establish */ public void setConnectionType(HttpProxyConnectionType connectionType) { @@ -79,11 +80,17 @@ public HttpProxyEnvironmentVariableType getEnvironmentVariableType() { return environmentVariableType; } + /** + * (Optional) + * Enable/Disable reading from environment variable for Proxy config. Defaults to Enabled + * @param environmentVariableType enable or disable env proxy + */ public void setEnvironmentVariableType(HttpProxyEnvironmentVariableType environmentVariableType) { this.environmentVariableType = environmentVariableType; } /** + * (Optional) * Sets the tls connection options for the proxy connection * * @param tlsConnectionOptions tls connection options for the proxy connection diff --git a/src/native/http_connection_manager.c b/src/native/http_connection_manager.c index 94ab8677f..afccfa2cd 100644 --- a/src/native/http_connection_manager.c +++ b/src/native/http_connection_manager.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "http_connection_manager.h" @@ -107,6 +108,9 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnectio jint jni_proxy_authorization_type, jbyteArray jni_proxy_authorization_username, jbyteArray jni_proxy_authorization_password, + jint jni_environment_variable_proxy_connection_type, + jlong jni_environment_variable_proxy_tls_connection_options, + jint jni_environment_variable_type, jboolean jni_manual_window_management, jlong jni_max_connection_idle_in_milliseconds, jlong jni_monitoring_throughput_threshold_in_bytes_per_second, @@ -222,6 +226,17 @@ JNIEXPORT jlong JNICALL Java_software_amazon_awssdk_crt_http_HttpClientConnectio manager_options.proxy_options = &proxy_options; } + struct proxy_env_var_settings proxy_ev_settings; + AWS_ZERO_STRUCT(proxy_ev_settings); + + aws_http_proxy_environment_variable_setting_jni_init( + &proxy_ev_settings, + jni_environment_variable_proxy_connection_type, + jni_environment_variable_type, + (struct aws_tls_connection_options *)jni_environment_variable_proxy_tls_connection_options); + + manager_options.proxy_ev_settings = &proxy_ev_settings; + binding->manager = aws_http_connection_manager_new(allocator, &manager_options); if (binding->manager == NULL) { aws_jni_throw_runtime_exception(