Skip to content

Commit

Permalink
the default credentials according to the localstack are test , when w…
Browse files Browse the repository at this point in the history
…e do getAccessKey and getSecretKey we are checking the env map if we have the keys AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY respectively and fallback to the defaults
  • Loading branch information
fokion committed Oct 26, 2023
1 parent 0c52c41 commit 46ac860
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public class LocalStackContainer extends GenericContainer<LocalStackContainer> {

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

private static final String DEFAULT_AWS_ACCESS_KEY_ID = "test";

private static final String DEFAULT_AWS_SECRET_ACCESS_KEY = "test";

@Deprecated
public static final String VERSION = DEFAULT_TAG;

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

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

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@
import software.amazon.awssdk.services.s3.S3Client;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -451,4 +455,53 @@ private String runAwsCliAgainstDockerNetworkContainer(String command) throws Exc
return logs;
}
}

public static class WithEnvironmentVariables {

@ClassRule
public static LocalStackContainer localstack = new LocalStackContainer(LocalstackTestImages.LOCALSTACK_LATEST)
.withServices(Service.S3)
.withEnv("S3_SKIP_SIGNATURE_VALIDATION", "0");

@Test
public void s3TestWithPresignedURL() throws IOException {
AmazonS3 s3 = AmazonS3ClientBuilder
.standard()
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(
localstack.getEndpoint().toString(),
localstack.getRegion()
)
)
.withCredentials(
new AWSStaticCredentialsProvider(
new BasicAWSCredentials(localstack.getAccessKey(), localstack.getSecretKey())
)
)
.build();

final String bucketName = "foo";

s3.createBucket(bucketName);

s3.putObject(bucketName, "bar", "baz");

final List<Bucket> buckets = s3.listBuckets();
final Optional<Bucket> maybeBucket = buckets
.stream()
.filter(b -> b.getName().equals(bucketName))
.findFirst();
assertThat(maybeBucket).as("The created bucket is present").isPresent();

URL presignedUrl = s3.generatePresignedUrl(
bucketName,
"bar",
Date.from(Instant.now().plus(5, ChronoUnit.MINUTES))
);

assertThat(presignedUrl).as("The presigned url is valid").isNotNull();
final String content = IOUtils.toString(presignedUrl, StandardCharsets.UTF_8);
assertThat(content).as("The object can be retrieved").isEqualTo("baz");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ public interface LocalstackTestImages {
DockerImageName LOCALSTACK_0_11_IMAGE = LOCALSTACK_IMAGE.withTag("0.11.3");
DockerImageName LOCALSTACK_0_12_IMAGE = LOCALSTACK_IMAGE.withTag("0.12.8");
DockerImageName LOCALSTACK_0_13_IMAGE = LOCALSTACK_IMAGE.withTag("0.13.0");

DockerImageName LOCALSTACK_LATEST = LOCALSTACK_IMAGE.withTag("latest");
DockerImageName AWS_CLI_IMAGE = DockerImageName.parse("amazon/aws-cli:2.7.27");
}

0 comments on commit 46ac860

Please sign in to comment.