Skip to content

Commit 95849a5

Browse files
authored
Fix default LocalStack credentials (#7718)
Fixes #7623
1 parent 44847d8 commit 95849a5

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public class LocalStackContainer extends GenericContainer<LocalStackContainer> {
4747

4848
private static final String DEFAULT_REGION = "us-east-1";
4949

50+
private static final String DEFAULT_AWS_ACCESS_KEY_ID = "test";
51+
52+
private static final String DEFAULT_AWS_SECRET_ACCESS_KEY = "test";
53+
5054
@Deprecated
5155
public static final String VERSION = DEFAULT_TAG;
5256

@@ -293,6 +297,7 @@ private int getServicePort(EnabledService service) {
293297

294298
/**
295299
* Provides a default access key that is preconfigured to communicate with a given simulated service.
300+
* <a href="https://github.com/localstack/localstack/blob/master/doc/interaction/README.md?plain=1#L32">AWS Access Key</a>
296301
* The access key can be used to construct AWS SDK v2 clients:
297302
* <pre><code>S3Client s3 = S3Client
298303
.builder()
@@ -306,11 +311,12 @@ private int getServicePort(EnabledService service) {
306311
* @return a default access key
307312
*/
308313
public String getAccessKey() {
309-
return "accesskey";
314+
return this.getEnvMap().getOrDefault("AWS_ACCESS_KEY_ID", DEFAULT_AWS_ACCESS_KEY_ID);
310315
}
311316

312317
/**
313318
* Provides a default secret key that is preconfigured to communicate with a given simulated service.
319+
* <a href="https://github.com/localstack/localstack/blob/master/doc/interaction/README.md?plain=1#L32">AWS Secret Key</a>
314320
* The secret key can be used to construct AWS SDK v2 clients:
315321
* <pre><code>S3Client s3 = S3Client
316322
.builder()
@@ -324,7 +330,7 @@ public String getAccessKey() {
324330
* @return a default secret key
325331
*/
326332
public String getSecretKey() {
327-
return "secretkey";
333+
return this.getEnvMap().getOrDefault("AWS_SECRET_ACCESS_KEY", DEFAULT_AWS_SECRET_ACCESS_KEY);
328334
}
329335

330336
/**

modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackContainerTest.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
import software.amazon.awssdk.services.s3.S3Client;
3838

3939
import java.io.IOException;
40+
import java.net.URL;
4041
import java.nio.charset.StandardCharsets;
42+
import java.time.Instant;
43+
import java.time.temporal.ChronoUnit;
44+
import java.util.Date;
4145
import java.util.List;
4246
import java.util.Optional;
4347

@@ -451,4 +455,54 @@ private String runAwsCliAgainstDockerNetworkContainer(String command) throws Exc
451455
return logs;
452456
}
453457
}
458+
459+
public static class S3SkipSignatureValidation {
460+
461+
@ClassRule
462+
public static LocalStackContainer localstack = new LocalStackContainer(
463+
LocalstackTestImages.LOCALSTACK_2_3_IMAGE
464+
)
465+
.withEnv("S3_SKIP_SIGNATURE_VALIDATION", "0");
466+
467+
@Test
468+
public void shouldBeAccessibleWithCredentials() throws IOException {
469+
AmazonS3 s3 = AmazonS3ClientBuilder
470+
.standard()
471+
.withEndpointConfiguration(
472+
new AwsClientBuilder.EndpointConfiguration(
473+
localstack.getEndpoint().toString(),
474+
localstack.getRegion()
475+
)
476+
)
477+
.withCredentials(
478+
new AWSStaticCredentialsProvider(
479+
new BasicAWSCredentials(localstack.getAccessKey(), localstack.getSecretKey())
480+
)
481+
)
482+
.build();
483+
484+
final String bucketName = "foo";
485+
486+
s3.createBucket(bucketName);
487+
488+
s3.putObject(bucketName, "bar", "baz");
489+
490+
final List<Bucket> buckets = s3.listBuckets();
491+
final Optional<Bucket> maybeBucket = buckets
492+
.stream()
493+
.filter(b -> b.getName().equals(bucketName))
494+
.findFirst();
495+
assertThat(maybeBucket).as("The created bucket is present").isPresent();
496+
497+
URL presignedUrl = s3.generatePresignedUrl(
498+
bucketName,
499+
"bar",
500+
Date.from(Instant.now().plus(5, ChronoUnit.MINUTES))
501+
);
502+
503+
assertThat(presignedUrl).as("The presigned url is valid").isNotNull();
504+
final String content = IOUtils.toString(presignedUrl, StandardCharsets.UTF_8);
505+
assertThat(content).as("The object can be retrieved").isEqualTo("baz");
506+
}
507+
}
454508
}

modules/localstack/src/test/java/org/testcontainers/containers/localstack/LocalstackTestImages.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,7 @@ public interface LocalstackTestImages {
99
DockerImageName LOCALSTACK_0_11_IMAGE = LOCALSTACK_IMAGE.withTag("0.11.3");
1010
DockerImageName LOCALSTACK_0_12_IMAGE = LOCALSTACK_IMAGE.withTag("0.12.8");
1111
DockerImageName LOCALSTACK_0_13_IMAGE = LOCALSTACK_IMAGE.withTag("0.13.0");
12+
13+
DockerImageName LOCALSTACK_2_3_IMAGE = LOCALSTACK_IMAGE.withTag("2.3");
1214
DockerImageName AWS_CLI_IMAGE = DockerImageName.parse("amazon/aws-cli:2.7.27");
1315
}

0 commit comments

Comments
 (0)