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

Changing default localstack credentials #7718

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved

@ClassRule
public static LocalStackContainer localstack = new LocalStackContainer(LocalstackTestImages.LOCALSTACK_LATEST)
.withServices(Service.S3)
fokion marked this conversation as resolved.
Show resolved Hide resolved
.withEnv("S3_SKIP_SIGNATURE_VALIDATION", "0");

@Test
public void s3TestWithPresignedURL() throws IOException {
fokion marked this conversation as resolved.
Show resolved Hide resolved
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");
fokion marked this conversation as resolved.
Show resolved Hide resolved
DockerImageName AWS_CLI_IMAGE = DockerImageName.parse("amazon/aws-cli:2.7.27");
}