Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to configure proxy for SQS in Spring Cloud AWS 3 #1184

Closed
ifeLawal opened this issue Jul 23, 2024 Discussed in #1027 · 4 comments
Closed

How to configure proxy for SQS in Spring Cloud AWS 3 #1184

ifeLawal opened this issue Jul 23, 2024 Discussed in #1027 · 4 comments

Comments

@ifeLawal
Copy link

ifeLawal commented Jul 23, 2024

Discussed in #1027

Originally posted by ghughal January 22, 2024
With 2.4.x version, we were using ClientConfiguration to configure proxy (and other properties like max retry, max connection etc.) here is a sample below:

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.profile.ProfileCredentialsProvider;
import com.amazonaws.services.sqs.AmazonSQSAsync;
import org.springframework.beans.factory.annotation.Value;
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder;

public class SqsConfig {

    @Value("${cloud.aws.client.proxy-host}")
    public String proxyHost;
    
    @Value("${cloud.aws.client.proxy-port}")
    public String proxyPort;
    
    @Value("${cloud.aws.client.non-proxy-hosts}")
    public String nonProxyHost;
    
    // Value is populated by the region code in the cloud environment.
    @Value("${cloud.aws.region.static}")
    public String region;

    public ClientConfiguration clientConfiguration() {
      final ClientConfiguration config = new ClientConfiguration();
      config.setProxyHost(proxyHost);
      config.setProxyPort(Integer.parseInt(proxyPort));
      config.setNonProxyHosts(nonProxyHost);
      return config;
    }

    public AmazonSQSAsync localamazonSqsAsync() {
          String awsProfileName = "adfs";
          return AmazonSQSAsyncClientBuilder
            .standard()
            .withRegion(region)
            .withCredentials(new ProfileCredentialsProvider(awsProfileName))
            .withClientConfiguration(clientConfiguration())
            .build();
      }

}

but it's not clear how to do that programmatically with 3.1.x version and it was not clear from documentation. Thank you.

Here is what I have for the migration to 3.1.x.

import com.amazonaws.ClientConfiguration;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.sqs.SqsAsyncClient;

public class SqsConfig {

    @Value("${cloud.aws.client.proxy-host}")
    public String proxyHost;
    
    @Value("${cloud.aws.client.proxy-port}")
    public String proxyPort;
    
    @Value("${cloud.aws.client.non-proxy-hosts}")
    public String nonProxyHost;
    
    // Value is populated by the region code in the cloud environment.
    @Value("${cloud.aws.region.static}")
    public String region;

    public ClientConfiguration clientConfiguration() {
      final ClientConfiguration config = new ClientConfiguration();
      config.setProxyHost(proxyHost);
      config.setProxyPort(Integer.parseInt(proxyPort));
      config.setNonProxyHosts(nonProxyHost);
      return config;
    }

    public SqsAsyncClient localamazonSqsAsync() {
      String awsProfileName = "adfs";
      return SqsAsyncClient.builder()
          .region(Region.of(region))
          .credentialsProvider(ProfileCredentialsProvider.create(awsProfileName))
          // .asyncConfiguration(clientConfiguration())  -- tried using asyncConfiguration but this does not take the client configuration
          .build();
    }

Any information on how to incorporate the clientConfiguration?

@tomazfernandes
Copy link
Contributor

Hmm, not sure about this one, @maciejwalkowiak @MatejNedic do you have any ideas?

@MatejNedic
Copy link
Member

@tomazfernandes will take a look

@jonathansloman
Copy link

jonathansloman commented Sep 5, 2024

Having battled this issue for a while, this is what worked for me:

@Configuration
public class AwsClientConfiguration {
    @Value("${proxy.host}")
    private String proxyHost;

    @Value("${proxy.port}")
    private int proxyPort;

  @Bean
    AwsClientCustomizer<SqsAsyncClientBuilder> sqsAsyncClientBuilderAwsClientConfigurer() {
        LOG.info("Creating sqs async client config with proxy: " + proxyHost);
        return new SqsAsyncAwsClientClientConfigurer(proxyHost, proxyPort);
    }

    static class SqsAsyncAwsClientClientConfigurer implements AwsClientCustomizer<SqsAsyncClientBuilder> {
        private String proxyHost;
        private int proxyPort;

        public SqsAsyncAwsClientClientConfigurer(final String proxyHost, final int proxyPort) {
            this.proxyHost = proxyHost;
            this.proxyPort = proxyPort;
        }

        @Override
        public SdkAsyncHttpClient asyncHttpClient() {
            return NettyNioAsyncHttpClient.builder().proxyConfiguration(ProxyConfiguration.builder()
                            .host(proxyHost).port(proxyPort).build())
                    .build();
        }
    }
}

@ifeLawal
Copy link
Author

ifeLawal commented Sep 6, 2024

This is workable for our needs. So closing this out on my end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants