Skip to content

Commit cfa33e8

Browse files
author
AncaGhenade
committed
add submodule
1 parent 352b98e commit cfa33e8

File tree

7 files changed

+217
-2
lines changed

7 files changed

+217
-2
lines changed

.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,5 @@ setup/terraform/.terraform.lock.hcl
6363
setup/terraform/terraform.tfstate.backup
6464

6565
# lambda module
66-
shipment-picture-lambda-validator/target
67-
shipment-picture-lambda-validator/.idea
66+
/shipment-picture-lambda-validator/.idea
67+
/shipment-picture-lambda-validator/target/
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>dev.ancaghenade</groupId>
8+
<artifactId>shipment-picture-lambda-validator</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.amazonaws</groupId>
19+
<artifactId>aws-lambda-java-events</artifactId>
20+
<version>2.0.2</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.amazonaws</groupId>
24+
<artifactId>aws-lambda-java-core</artifactId>
25+
<version>1.1.0</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.amazonaws</groupId>
29+
<artifactId>aws-java-sdk-s3</artifactId>
30+
<version>1.12.387</version>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-shade-plugin</artifactId>
39+
<version>2.4.3</version>
40+
<configuration>
41+
<createDependencyReducedPom>false</createDependencyReducedPom>
42+
</configuration>
43+
<executions>
44+
<execution>
45+
<phase>package</phase>
46+
<goals>
47+
<goal>shade</goal>
48+
</goals>
49+
</execution>
50+
</executions>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package dev.ancaghenade.shipmentpicturelambdavalidator;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.util.Properties;
7+
8+
public class PropertiesProvider {
9+
10+
InputStream inputStream;
11+
12+
public Properties values() throws IOException {
13+
try {
14+
Properties properties = new java.util.Properties();
15+
inputStream = getClass().getClassLoader().getResourceAsStream("config.properties");
16+
if (inputStream != null) {
17+
properties.load(inputStream);
18+
} else {
19+
throw new FileNotFoundException("Property file not found in the classpath.");
20+
}
21+
return properties;
22+
} catch (Exception e) {
23+
System.out.println("Exception: " + e);
24+
} finally {
25+
inputStream.close();
26+
}
27+
return null;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dev.ancaghenade.shipmentpicturelambdavalidator;
2+
3+
import com.amazonaws.auth.AWSCredentials;
4+
import com.amazonaws.auth.AWSStaticCredentialsProvider;
5+
import com.amazonaws.auth.BasicAWSCredentials;
6+
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;
7+
import com.amazonaws.services.s3.AmazonS3;
8+
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
9+
import java.io.IOException;
10+
11+
public class S3ClientHelper {
12+
13+
private static final String ENVIRONMENT = System.getenv("ENVIRONMENT");
14+
15+
public static AmazonS3 getS3Client() throws IOException {
16+
PropertiesProvider properties = new PropertiesProvider();
17+
18+
if (properties.values().getProperty("environment.dev").equals(ENVIRONMENT)) {
19+
AWSCredentials awsCredentials = new BasicAWSCredentials(
20+
"",
21+
""
22+
);
23+
AmazonS3ClientBuilder amazonS3ClientBuilder = AmazonS3ClientBuilder
24+
.standard()
25+
.withEndpointConfiguration(
26+
new EndpointConfiguration(properties.values().getProperty("s3.url"),
27+
properties.values().getProperty("s3.region")));
28+
29+
return amazonS3ClientBuilder.withCredentials(
30+
new AWSStaticCredentialsProvider(awsCredentials))
31+
.build();
32+
} else {
33+
return AmazonS3ClientBuilder.defaultClient();
34+
}
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package dev.ancaghenade.shipmentpicturelambdavalidator;
2+
3+
import com.amazonaws.services.lambda.runtime.Context;
4+
import com.amazonaws.services.lambda.runtime.LambdaLogger;
5+
import com.amazonaws.services.lambda.runtime.RequestHandler;
6+
import com.amazonaws.services.lambda.runtime.events.S3Event;
7+
import com.amazonaws.services.s3.AmazonS3;
8+
import com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord;
9+
import com.amazonaws.services.s3.model.ObjectMetadata;
10+
import com.amazonaws.services.s3.model.PutObjectRequest;
11+
import com.amazonaws.services.s3.model.S3Object;
12+
import java.io.IOException;
13+
import java.io.InputStream;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
import org.apache.http.entity.ContentType;
17+
18+
// Should be something more interesting
19+
20+
public class ServiceHandler implements RequestHandler<S3Event, Void> {
21+
22+
public Void handleRequest(S3Event event, Context context) {
23+
24+
AmazonS3 s3Client = null;
25+
try {
26+
s3Client = S3ClientHelper.getS3Client();
27+
} catch (IOException e) {
28+
throw new RuntimeException(e);
29+
}
30+
31+
LambdaLogger logger = context.getLogger();
32+
boolean isValid = true;
33+
34+
// check if record is there
35+
if (event.getRecords().isEmpty()) {
36+
logger.log("No records received.");
37+
return null;
38+
}
39+
40+
for (S3EventNotificationRecord record : event.getRecords()) {
41+
String bucketName = record.getS3().getBucket().getName();
42+
String objectKey = record.getS3().getObject().getKey();
43+
44+
S3Object s3Object = s3Client.getObject(bucketName, objectKey);
45+
46+
if (!List.of(ContentType.IMAGE_JPEG.getMimeType(),
47+
ContentType.IMAGE_PNG.getMimeType(),
48+
ContentType.IMAGE_BMP.getMimeType())
49+
.contains(s3Object.getObjectMetadata().getUserMetadata().get("content-type"))) {
50+
51+
isValid = false;
52+
logger.log(
53+
"File format not accepted. This will be replaced with a standard placeholder.");
54+
}
55+
if (isValid) {
56+
byte[] magicNumbers = new byte[4];
57+
InputStream objectData = s3Object.getObjectContent();
58+
try {
59+
objectData.read(magicNumbers, 0, 4);
60+
} catch (IOException e) {
61+
throw new RuntimeException(e);
62+
}
63+
if (Arrays.equals(magicNumbers, new byte[]{(byte) 0x7f, 'E', 'L', 'F'})) {
64+
logger.log("The object is an ELF executable file.");
65+
isValid = false;
66+
67+
} else if (Arrays.equals(magicNumbers, new byte[]{'M', 'Z'})) {
68+
logger.log("The object is a Windows executable file.");
69+
isValid = false;
70+
71+
}
72+
}
73+
if (!isValid) {
74+
s3Client.deleteObject(bucketName, objectKey);
75+
76+
InputStream is = ServiceHandler.class.getResourceAsStream("/resources/placeholder.jpg");
77+
if (is == null) {
78+
is = ServiceHandler.class.getClassLoader().getResourceAsStream("placeholder.jpg");
79+
}
80+
81+
s3Client.putObject(new PutObjectRequest(bucketName, objectKey, is, new ObjectMetadata()));
82+
83+
} else {
84+
logger.log(
85+
"Found image with content type: " + s3Object.getObjectMetadata().getUserMetadata()
86+
.get("content-type") + " that is correct.");
87+
}
88+
89+
}
90+
return null;
91+
}
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s3.url=https://s3.localhost.localstack.cloud:4566
2+
s3.region=eu-central-1
3+
environment.dev=dev
Loading

0 commit comments

Comments
 (0)