3

I want to test my AWS code locally so I have to set a proxy to a AWS client.

There is a proxy host (http://user@pass:my-corporate-proxy.com:8080) set in my environment via a variable HTTPS_PROXY.

I didn't find a way how to set the proxy as whole so I came up with this code:

AmazonSNS sns = AmazonSNSClientBuilder.standard()
    .withClientConfiguration(clientConfig(System.getenv("HTTPS_PROXY")))
    .withRegion(Regions.fromName(System.getenv("AWS_REGION")))
    .withCredentials(new DefaultAWSCredentialsProviderChain())
    .build();

ClientConfiguration clientConfig(String proxy) {
    ClientConfiguration configuration = new ClientConfiguration();
    if (proxy != null && !proxy.isEmpty()) {
        Matcher matcher = Pattern.compile("(\\w{3,5})://((\\w+):(\\w+)@)?(.+):(\\d{1,5})").matcher(proxy);
        if (!matcher.matches()) {
            throw new IllegalArgumentException("Proxy not valid: " + proxy);
        }
        configuration.setProxyHost(matcher.group(5));
        configuration.setProxyPort(Integer.parseInt(matcher.group(6)));
        configuration.setProxyUsername(matcher.group(3));
        configuration.setProxyPassword(matcher.group(4));
    }
    return configuration;
}

The whole method clientConfig is only boilerplate code.

Is there any elegant way how to achieve this?

Barney
  • 797
  • 1
  • 11
  • 20
  • Honestly given your environment this sounds about as good as it's going to get. I'm not sure what sending an empty `ClientConfiguration` in the event that the proxy is not set does as I've not used this but I'm assuming it is ignored in that case. – stdunbar Feb 08 '19 at 16:13
  • a method like `clientConfiguration.setProxy(String proxy)` would be much better... – Barney Feb 09 '19 at 09:30

2 Answers2

1

As far as I can tell while using AWS SDK V1 (1.11.840), if you have environment variables such as HTTP(S)_PROXY or http(s)_proxy set at runtime, or properties like http(s).proxyHost, proxyPort, proxyUser, and proxyPassword passed to your application, you don't have to set any of that. It gets automatically read into the newly created ClientConfigiration.

As, such you'd only want to set the ProxyAuthenticationMethod, if needed.

ClientConfiguration clientConfig(ProxyAuthenticationMethod authMethod) {
    ClientConfiguration conf = new ClientConfiguration();
    List<ProxyAuthenticationMethod> proxyAuthentication = new ArrayList<>(1);
    proxyAuthentication.add(authMethod);
    conf.setProxyAuthenticationMethods(proxyAuthentication);
    return conf;
}

ProxyAuthenticationMethod can be ProxyAuthenticationMethod.BASIC or DIGEST or KERBEROS or NTLM or SPNEGO

kfkhalili
  • 996
  • 1
  • 11
  • 24
1

I can confirm setting the parameters "http(s).proxyHost" (and others) work out of the box, you need however to specify a port, otherwise AWS SDK (1) will not pick it up.

java -Dhttps.proxyHost=proxy.company.com -Dhttps.proxyPort=8080 -Dhttps.proxyUser=myUsername -Dhttps.proxyPassword=myPassword <app>

Username & passsword are optional.

See for more info: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/doc-files/net-properties.html

and

What Java properties to pass to a Java app to authenticate with a http proxy

RBT
  • 24,161
  • 21
  • 159
  • 240