Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
<artifactId>aws-java-sdk</artifactId>
<version>${aws.sdk.version}</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-kinesisvideo</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
Expand Down Expand Up @@ -115,6 +121,28 @@
<version>${aws.sdk.version}</version>
</dependency>

<!-- AWS SDK version 2 libs -->
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sns</artifactId>
<version>2.13.36</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>sqs</artifactId>
<version>2.13.36</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>kinesis</artifactId>
<version>2.13.36</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>netty-nio-client</artifactId>
<version>2.13.36</version>
</dependency>


<!-- test dependencies -->
<dependency>
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/cloud/localstack/CommonUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cloud.localstack;

import cloud.localstack.Constants;
import cloud.localstack.Localstack;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.lang.reflect.Field;
import java.nio.channels.FileChannel;
import java.util.stream.Stream;

/**
* Common utility methods
*/
public class CommonUtils {

private static final String[] EXCLUDED_DIRECTORIES = {
".github", ".git", ".idea", ".venv", "target", "node_modules"
};

public static void disableSslCertChecking() {
System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
}

public static void setEnv(String key, String value) {
Map<String, String> newEnv = new HashMap<String, String>(System.getenv());
newEnv.put(key, value);
setEnv(newEnv);
}

protected static void setEnv(Map<String, String> newEnv) {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newEnv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newEnv);
} catch (NoSuchFieldException e) {
try {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for (Class cl : classes) {
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newEnv);
}
}
} catch (Exception e2) {
e2.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}

public static void copyFolder(Path src, Path dest) throws IOException {
try(Stream<Path> stream = Files.walk(src)) {
stream.forEach(source -> {
boolean isExcluded = Arrays.stream(EXCLUDED_DIRECTORIES)
.anyMatch( excluded -> source.toAbsolutePath().toString().contains(excluded));
if (!isExcluded) {
copy(source, dest.resolve(src.relativize(source)));
}
});
}
}

public static void copy(Path source, Path dest) {
try {
CopyOption[] options = new CopyOption[] {StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING};
if(Files.isDirectory(dest)) {
// continue without copying
return;
}
if (Files.exists(dest)) {
try(FileChannel sourceFile = FileChannel.open(source)) {
try (FileChannel destFile = FileChannel.open(dest)) {
if (!Files.getLastModifiedTime(source).equals(Files.getLastModifiedTime(dest))
|| sourceFile.size() != destFile.size()
) {
Files.copy(source, dest, options);
}
}
}
} else {
Files.copy(source, dest, options);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/cloud/localstack/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ public class Constants {

public static final String LOCALHOST_DOMAIN_NAME = "localhost.localstack.cloud";

public static final String DEFAULT_REGION = "us-east-1";
public static final String TEST_ACCESS_KEY = "test";
public static final String TEST_SECRET_KEY = "test";

static {
DEFAULT_PORTS.put("apigateway", 4567);
DEFAULT_PORTS.put("kinesis", 4568);
Expand All @@ -34,4 +38,5 @@ public class Constants {
DEFAULT_PORTS.put("ec2", 4597);
DEFAULT_PORTS.put("kms", 4599);
}

}
5 changes: 3 additions & 2 deletions src/main/java/cloud/localstack/Localstack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cloud.localstack.Constants;
import cloud.localstack.ServiceName;
import cloud.localstack.CommonUtils;
import cloud.localstack.docker.*;
import cloud.localstack.docker.command.*;
import cloud.localstack.docker.annotation.LocalstackDockerConfiguration;
Expand Down Expand Up @@ -54,7 +55,7 @@ public class Localstack {

static {
// make sure we avoid any errors related to locally generated SSL certificates
TestUtils.disableSslCertChecking();
CommonUtils.disableSslCertChecking();
}

private Localstack() { }
Expand Down Expand Up @@ -237,6 +238,6 @@ public static boolean isEnvConfigSet(String configName) {
}

public static String getDefaultRegion() {
return TestUtils.DEFAULT_REGION;
return Constants.DEFAULT_REGION;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cloud.localstack;
package cloud.localstack.awssdkv1;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSCredentialsProvider;
Expand Down Expand Up @@ -33,36 +33,16 @@
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClientBuilder;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.channels.FileChannel;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import cloud.localstack.Constants;
import cloud.localstack.Localstack;
import cloud.localstack.CommonUtils;

@SuppressWarnings("all")
public class TestUtils {

public static final String DEFAULT_REGION = "us-east-1";
public static final String TEST_ACCESS_KEY = "test";
public static final String TEST_SECRET_KEY = "test";
public static final AWSCredentials TEST_CREDENTIALS = new BasicAWSCredentials(TEST_ACCESS_KEY, TEST_SECRET_KEY);

private static final String[] EXCLUDED_DIRECTORIES = {
".github", ".git", ".idea", ".venv", "target", "node_modules"
};

public static void setEnv(String key, String value) {
Map<String, String> newEnv = new HashMap<String, String>(System.getenv());
newEnv.put(key, value);
setEnv(newEnv);
}
public static final AWSCredentials TEST_CREDENTIALS = new BasicAWSCredentials(
Constants.TEST_ACCESS_KEY, Constants.TEST_SECRET_KEY);

public static AmazonSQS getClientSQS() {
return getClientSQS(null);
Expand Down Expand Up @@ -227,87 +207,16 @@ protected static AwsClientBuilder.EndpointConfiguration getEndpointConfiguration
return getEndpointConfiguration(Localstack.INSTANCE.getEndpointStepFunctions());
}

protected static void setEnv(Map<String, String> newEnv) {
try {
Class<?> processEnvironmentClass = Class.forName("java.lang.ProcessEnvironment");
Field theEnvironmentField = processEnvironmentClass.getDeclaredField("theEnvironment");
theEnvironmentField.setAccessible(true);
Map<String, String> env = (Map<String, String>) theEnvironmentField.get(null);
env.putAll(newEnv);
Field theCaseInsensitiveEnvironmentField = processEnvironmentClass
.getDeclaredField("theCaseInsensitiveEnvironment");
theCaseInsensitiveEnvironmentField.setAccessible(true);
Map<String, String> cienv = (Map<String, String>) theCaseInsensitiveEnvironmentField.get(null);
cienv.putAll(newEnv);
} catch (NoSuchFieldException e) {
try {
Class[] classes = Collections.class.getDeclaredClasses();
Map<String, String> env = System.getenv();
for (Class cl : classes) {
if ("java.util.Collections$UnmodifiableMap".equals(cl.getName())) {
Field field = cl.getDeclaredField("m");
field.setAccessible(true);
Object obj = field.get(env);
Map<String, String> map = (Map<String, String>) obj;
map.clear();
map.putAll(newEnv);
}
}
} catch (Exception e2) {
e2.printStackTrace();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}

public static void disableSslCertChecking() {
System.setProperty("com.amazonaws.sdk.disableCertChecking", "true");
}

public static void copyFolder(Path src, Path dest) throws IOException {
try(Stream<Path> stream = Files.walk(src)) {
stream.forEach(source -> {
boolean isExcluded = Arrays.stream(EXCLUDED_DIRECTORIES)
.anyMatch( excluded -> source.toAbsolutePath().toString().contains(excluded));
if (!isExcluded) {
copy(source, dest.resolve(src.relativize(source)));
}
});
}
}

public static void copy(Path source, Path dest) {
try {
CopyOption[] options = new CopyOption[] {StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING};
if(Files.isDirectory(dest)) {
// continue without copying
return;
}
if (Files.exists(dest)) {
try(FileChannel sourceFile = FileChannel.open(source)) {
try (FileChannel destFile = FileChannel.open(dest)) {
if (!Files.getLastModifiedTime(source).equals(Files.getLastModifiedTime(dest))
|| sourceFile.size() != destFile.size()
) {
Files.copy(source, dest, options);
}
}
}
} else {
Files.copy(source, dest, options);
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* UTIL METHODS
*/

public static AWSCredentialsProvider getCredentialsProvider() {
return new AWSStaticCredentialsProvider(TEST_CREDENTIALS);
}

protected static AwsClientBuilder.EndpointConfiguration getEndpointConfiguration(String endpointURL) {
return new AwsClientBuilder.EndpointConfiguration(endpointURL, DEFAULT_REGION);
return new AwsClientBuilder.EndpointConfiguration(endpointURL, Constants.DEFAULT_REGION);
}

}
53 changes: 53 additions & 0 deletions src/main/java/cloud/localstack/awssdkv2/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cloud.localstack.awssdkv2;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.utils.*;
import software.amazon.awssdk.http.*;
import software.amazon.awssdk.services.kinesis.*;
import software.amazon.awssdk.services.sns.*;
import software.amazon.awssdk.services.sqs.*;
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;

import cloud.localstack.Localstack;

import java.net.*;

@SuppressWarnings("all")
public class TestUtils {

/**
* AWS SDK V2 METHODS
*/

public static KinesisAsyncClient getClientKinesisAsyncV2() {
return wrapApiClientV2(KinesisAsyncClient.builder(), Localstack.INSTANCE.getEndpointKinesis()).build();
}

public static SqsAsyncClient getClientSQSAsyncV2() {
return wrapApiClientV2(SqsAsyncClient.builder(), Localstack.INSTANCE.getEndpointSQS()).build();
}

public static SnsAsyncClient getClientSNSAsyncV2() {
return wrapApiClientV2(SnsAsyncClient.builder(), Localstack.INSTANCE.getEndpointSNS()).build();
}

public static <T extends software.amazon.awssdk.core.client.builder.SdkAsyncClientBuilder> T wrapApiClientV2(T builder, String endpointURL) {
try {
return (T) ((software.amazon.awssdk.awscore.client.builder.AwsClientBuilder)builder
.httpClient(NettyNioAsyncHttpClient.builder().buildWithDefaults(
AttributeMap.builder().put(
SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES, java.lang.Boolean.TRUE).build())))
.credentialsProvider(getCredentialsV2())
.region(Region.of(Localstack.INSTANCE.getDefaultRegion()))
.endpointOverride(new URI(endpointURL));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

private static software.amazon.awssdk.auth.credentials.AwsCredentialsProvider getCredentialsV2() throws Exception {
return software.amazon.awssdk.auth.credentials.StaticCredentialsProvider.create(
software.amazon.awssdk.auth.credentials.AwsBasicCredentials.create("access", "secret"));
}

}
3 changes: 2 additions & 1 deletion src/main/java/cloud/localstack/deprecated/Localstack.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import cloud.localstack.Constants;
import cloud.localstack.ServiceName;
import cloud.localstack.CommonUtils;

/**
* Singleton class that automatically downloads, installs, starts,
Expand Down Expand Up @@ -385,7 +386,7 @@ public void setupInfrastructure() {
ensureInstallation(true);

// make sure we avoid any errors related to locally generated SSL certificates
TestUtils.disableSslCertChecking();
CommonUtils.disableSslCertChecking();

if (INFRA_STARTED.get() != null) return;
String[] cmd = new String[]{"make", "-C", TMP_INSTALL_DIR, "infra"};
Expand Down
Loading