question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Not able to override AWS endpoint during Integration test

See original GitHub issue

Type: Bug

Component: Secrets Manager

Describe the bug

I am using the latest 3.0.0-M2 version for spring cloud to pull secrets from the secrets manager.

spring:
  cloud:
    aws:
       endpoint: http://localhost:4566
      credentials:
        profile:
          name: localstack
  config:
    import:
    - aws-secretsmanager:/secret/spring-boot-app;/secret/db-credential

I run a local docker image of localstack , start the application and it all works fines. It pulls the secret and no issues.

Now I am trying to write an integration test using localstack and I am overriding the endpoint properties using @DynamicPropertySource mechanism.

@Testcontainers
@SpringBootTest
class MainApplicationTests {

    @Autowired
    Environment environment;
    @Container
    static LocalStackContainer localStackContainer = new LocalStackContainer(DockerImageName.parse("localstack/localstack"))
            .withCopyFileToContainer(MountableFile.forClasspathResource("script.sh"),
                    "/docker-entrypoint-initaws.d/")
            .withServices(LocalStackContainer.Service.SECRETSMANAGER)
            .withExposedPorts(4566)
            .waitingFor(Wait.forLogMessage(".*localstack.request.aws.*", 2));


    @DynamicPropertySource
    static void properties(DynamicPropertyRegistry registry) {

        registry.add("spring.cloud.aws.secretsmanager.endpoint", () -> localStackContainer.getEndpointOverride(LocalStackContainer.Service.SECRETSMANAGER));
        registry.add("spring.cloud.aws.endpoint", () -> "http://localhost:"+localStackContainer.getMappedPort(4566));
        registry.add("spring.datasource.url", () -> database.getJdbcUrl());
    }

when I run the test, the client is still using the endpoint defined in the properties file ( not the test properties file) and fails with an error that it could not connect to the endpoint http://localhost:4566 ( which is hte url from the main properties file)

Caused by: org.apache.http.conn.HttpHostConnectException: Connect to localhost:4566 [localhost/127.0.0.1] failed: Connection refused
	at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:156)
	at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
	at software.amazon.awssdk.http.apache.internal.conn.ClientConnectionManagerFactory$DelegatingHttpClientConnectionManager.connect(ClientConnectionManagerFactory.java:86)
	at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
	at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
	at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
	at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
	at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)

I debugged the properties fetch for the database properties override with test containers, it is all correct.

I am not sure why the lambda given in the DynamicPropertySource to override the properties does not even get invoked and hence it uses the properties from the main properties file and fails.

I am not sure if I am doing something wrong or missing something.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
amrutprabhucommented, Sep 16, 2022

So I preferred using System.setProperty as @eddumelendez said. And it works and it is cleaner I felt.

But it was nice to know about you can run tests the way that @maciejwalkowiak showed in the example.

I will be writing an article about the AWS Secrets Manager integration using Spring Cloud Secrets Manager soon. Also nice work with the new Spring Cloud AWS 3.0 version. 👍

Also thank you once again @maciejwalkowiak and @eddumelendez.

1reaction
eddumelendezcommented, Sep 16, 2022

System.setProperty can be used and still use the random port from testcontainers. See the following example https://github.com/eddumelendez/testcontainers-localstack/blob/main/secretsmanager/src/test/java/com/example/secretsmanager/SecretsmanagerApplicationTests.java

The same applies for parameterstore

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshoot endpoint URL connection error in Amazon S3
I'm trying to run the cp or sync command on my Amazon Simple Storage Service (Amazon S3) bucket. However, I'm getting the "Could...
Read more >
Use a mapping template to override an API's request and ...
Tutorial: Override an API's request parameters and headers with the API Gateway console · Under APIs, choose the PetStore API. · In the...
Read more >
Resolve connection issues with API Gateway private API ...
I'm having issues connecting to my Amazon API Gateway private API endpoint that's in Amazon Virtual Private Cloud (Amazon VPC).
Read more >
Overriding request/response parameters and response status ...
In this post, I demonstrate an example for overriding the status code without modifying the actual API code. Use the PetStore API, which...
Read more >
Resolve the IAM Error "AWS was not able to validate the ...
"An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found