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

SQS doesn't work with AWS SDK 2.0 #1318

Closed
highflyerDL opened this issue May 17, 2019 · 4 comments
Closed

SQS doesn't work with AWS SDK 2.0 #1318

highflyerDL opened this issue May 17, 2019 · 4 comments

Comments

@highflyerDL
Copy link

I'm having 2 SQS Java clients, 1 with SDK 1 and the other with the new SDK 2.0. For some reason, the new SDK couldn't work with local SQS because of the following error

[info]   java.util.concurrent.CompletionException: software.amazon.awssdk.services.sqs.model.SqsException: The security token included in the request is invalid. (Service: Sqs, Status Code: 403, Request ID: b013402e-2763-53bb-9b7d-1576a3c7d64a)
[info]   at software.amazon.awssdk.utils.CompletableFutureUtils.errorAsCompletionException(CompletableFutureUtils.java:61)
[info]   at software.amazon.awssdk.core.internal.http.pipeline.stages.AsyncExecutionFailureExceptionReportingStage.lambda$execute$0(AsyncExecutionFailureExceptionReportingStage.java:51)
[info]   at java.util.concurrent.CompletableFuture.uniHandle(CompletableFuture.java:822)
[info]   at java.util.concurrent.CompletableFuture$UniHandle.tryFire(CompletableFuture.java:797)
[info]   at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)
[info]   at java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:1977)
[info]   at software.amazon.awssdk.utils.CompletableFutureUtils.lambda$forwardExceptionTo$0(CompletableFutureUtils.java:75)
[info]   at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:760)
[info]   at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:736)
[info]   at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:474)

even though the credentials being used are the same. Tried to use the solution proposed in #893 (comment) but it didn't work still.

@seanf
Copy link

seanf commented Jun 7, 2019

How did you create the v2 credentials? I got SQS v2 to work with this Scala code:

import java.net.URI

import cloud.localstack.DockerTestUtils
import cloud.localstack.TestUtils.{DEFAULT_REGION, TEST_ACCESS_KEY, TEST_SECRET_KEY}
import cloud.localstack.docker.annotation.LocalstackDockerProperties
import cloud.localstack.docker.{LocalstackDocker, LocalstackDockerTestRunner}
import org.junit.Test
import org.junit.runner.RunWith
import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.sqs.SqsClient

import scala.collection.JavaConverters._

@RunWith(classOf[LocalstackDockerTestRunner])
@LocalstackDockerProperties(imageTag = "0.9.4", randomizePorts = true, services = Array("sqs"))
class LocalstackITCase {

  private val credsV2 = StaticCredentialsProvider.create(AwsBasicCredentials.create(TEST_ACCESS_KEY, TEST_SECRET_KEY))
  private val sqsEndPoint = LocalstackDocker.INSTANCE.getEndpointSQS

  @Test
  def testSQSv2(): Unit = {
    val sqs: SqsClient = SqsClient.builder
      .endpointOverride(new URI(sqsEndPoint))
      .region(Region.of(DEFAULT_REGION))
      .credentialsProvider(credsV2)
      .build()

    println(sqs.listQueues())
  }

}

@highflyerDL
Copy link
Author

@seanf i did just like you, except that localstack is ran as docker container
Dockercompose file:

version: '3'
services:
  localstack:
    image: localstack/localstack
    hostname: localstack
    ports:
    - "4567-4582:4567-4582"
    - "9999:8080"
    environment:
    - SERVICES=sqs
    - DEFAULT_REGION=eu-central-1
    - AWS_CBOR_DISABLE=true
    - USE_SSL=false
    - AWS_ACCESS_KEY_ID=accesskey
    - AWS_SECRET_ACCESS_KEY=secretkey
    - DATA_DIR=/tmp/localstack/data
    - DEBUG=1
    container_name: local-aws-tracing-scala

Scala code

    val credsV2 = StaticCredentialsProvider.create(AwsBasicCredentials.create("accesskey", "secretkey"))
    val sqsClient = SqsAsyncClient
      .builder()
      .region(Region.EU_CENTRAL_1)
      .credentialsProvider(credsV2)
      .build()

@seanf
Copy link

seanf commented Jun 8, 2019

@highflyerDL That JUnit runner uses a Docker container too. I got the same error as you when I added an async test based on your code:

  @Test
  def otherTest(): Unit = {
    val sqsClient = SqsAsyncClient
      .builder()
//      .endpointOverride(new URI(sqsEndPoint)) // without this, you'll get a security token error
      .region(Region.EU_CENTRAL_1)
      .credentialsProvider(credsV2)
      .build()
    println(sqsClient.listQueues().get().toString)
  }

You're just missing the endpoint setting on the SqsAsyncClient, probably http://localhost:4576 in your setup.

@highflyerDL
Copy link
Author

@seanf That works. Thanks !

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

2 participants