Skip to content

Commit 04caf20

Browse files
committed
maxmind integration (#1924)
1 parent 809f885 commit 04caf20

File tree

9 files changed

+287
-199
lines changed

9 files changed

+287
-199
lines changed

.github/workflows/prod.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ jobs:
6666
wget -O filetypes.json https://raw.githubusercontent.com/akto-api-security/akto/master/pii-types/filetypes.json
6767
wget -O automated_api_groups.csv https://raw.githubusercontent.com/akto-api-security/akto/master/automated-api-groups/automated-api-groups.csv
6868
69+
70+
- name: Create maxmind directory
71+
run: mkdir -p ./apps/threat-detection-backend/src/main/resources/maxmind
72+
- name: Download Maxmind Country database
73+
working-directory: ./apps/threat-detection-backend/src/main/resources/maxmind
74+
run: |
75+
wget -O Geo-Country.mmdb https://raw.githubusercontent.com/akto-api-security/tests-library/refs/heads/master/resources/Geo-Country.mmdb
76+
6977
- name: Prepare Dashboard polaris UI
7078
working-directory: ./apps/dashboard/web/polaris_web
7179
run: npm install && export RELEASE_VERSION=${{github.event.inputs.release_version}} && npm run build

.github/workflows/staging.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ jobs:
4141
wget -O general.json https://raw.githubusercontent.com/akto-api-security/pii-types/master/general.json
4242
wget -O fintech.json https://raw.githubusercontent.com/akto-api-security/akto/master/pii-types/fintech.json
4343
wget -O filetypes.json https://raw.githubusercontent.com/akto-api-security/akto/master/pii-types/filetypes.json
44+
- name: Create maxmind directory
45+
run: mkdir -p ./apps/threat-detection-backend/src/main/resources/maxmind
46+
- name: Download Maxmind Country database
47+
working-directory: ./apps/threat-detection-backend/src/main/resources/maxmind
48+
run: |
49+
wget -O Geo-Country.mmdb https://raw.githubusercontent.com/akto-api-security/tests-library/refs/heads/master/resources/Geo-Country.mmdb
4450
4551
- name: Configure AWS Credentials
4652
uses: aws-actions/configure-aws-credentials@v1
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.mmdb

apps/threat-detection-backend/pom.xml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@
7979
<version>${vertex.version}</version>
8080
</dependency>
8181

82+
<!-- Maxmind GeoIP2 -->
83+
<dependency>
84+
<groupId>com.maxmind.geoip2</groupId>
85+
<artifactId>geoip2</artifactId>
86+
<version>2.15.0</version>
87+
</dependency>
88+
8289
</dependencies>
8390
<build>
8491
<plugins>
@@ -121,10 +128,6 @@
121128
<resources>
122129
<resource>
123130
<directory>src/main/resources</directory>
124-
<filtering>true</filtering>
125-
<includes>
126-
<include>**/version.txt</include>
127-
</includes>
128131
</resource>
129132
</resources>
130133
</build>
@@ -204,4 +207,4 @@
204207
</profile>
205208
</profiles>
206209

207-
</project>
210+
</project>

apps/threat-detection-backend/src/main/java/com/akto/threat/backend/Main.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.akto.kafka.KafkaConfig;
88
import com.akto.kafka.KafkaConsumerConfig;
99
import com.akto.kafka.KafkaProducerConfig;
10+
import com.akto.threat.backend.client.IPLookupClient;
1011
import com.akto.threat.backend.service.MaliciousEventService;
1112
import com.akto.threat.backend.service.ThreatActorService;
1213
import com.akto.threat.backend.service.ThreatApiService;
@@ -17,6 +18,10 @@
1718
import com.mongodb.WriteConcern;
1819
import com.mongodb.client.MongoClient;
1920
import com.mongodb.client.MongoClients;
21+
import java.io.File;
22+
import java.io.FileOutputStream;
23+
import java.io.IOException;
24+
import org.apache.commons.io.IOUtils;
2025
import org.bson.codecs.configuration.CodecRegistry;
2126
import org.bson.codecs.pojo.PojoCodecProvider;
2227

@@ -53,14 +58,28 @@ public static void main(String[] args) throws Exception {
5358
KafkaProducerConfig.newBuilder().setBatchSize(100).setLingerMs(1000).build())
5459
.build();
5560

61+
IPLookupClient ipLookupClient = new IPLookupClient(getMaxmindFile());
62+
5663
new FlushMessagesToDB(internalKafkaConfig, threatProtectionMongo).run();
5764

5865
MaliciousEventService maliciousEventService =
59-
new MaliciousEventService(internalKafkaConfig, threatProtectionMongo);
66+
new MaliciousEventService(internalKafkaConfig, threatProtectionMongo, ipLookupClient);
6067

6168
ThreatActorService threatActorService = new ThreatActorService(threatProtectionMongo);
6269
ThreatApiService threatApiService = new ThreatApiService(threatProtectionMongo);
6370

6471
new BackendVerticle(maliciousEventService, threatActorService, threatApiService).start();
6572
}
73+
74+
private static File getMaxmindFile() throws IOException {
75+
File maxmindTmpFile = File.createTempFile("tmp-geo-country", ".mmdb");
76+
maxmindTmpFile.deleteOnExit();
77+
78+
try (FileOutputStream fos = new FileOutputStream(maxmindTmpFile)) {
79+
IOUtils.copy(
80+
Main.class.getClassLoader().getResourceAsStream("maxmind/Geo-Country.mmdb"), fos);
81+
}
82+
83+
return maxmindTmpFile;
84+
}
6685
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.akto.threat.backend.client;
2+
3+
import com.maxmind.geoip2.DatabaseReader;
4+
import com.maxmind.geoip2.model.CountryResponse;
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.net.InetAddress;
8+
import java.util.Optional;
9+
10+
public class IPLookupClient {
11+
private final DatabaseReader db;
12+
13+
public IPLookupClient(File dbFile) throws IOException {
14+
this.db = new DatabaseReader.Builder(dbFile).build();
15+
}
16+
17+
public Optional<String> getCountryISOCodeGivenIp(String ip) {
18+
try {
19+
InetAddress ipAddr = InetAddress.getByName(ip);
20+
CountryResponse resp = db.country(ipAddr);
21+
return Optional.of(resp.getCountry().getIsoCode());
22+
} catch (Exception e) {
23+
return Optional.empty();
24+
}
25+
}
26+
}

apps/threat-detection-backend/src/main/java/com/akto/threat/backend/service/MaliciousEventService.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.akto.proto.generated.threat_detection.service.dashboard_service.v1.ListMaliciousRequestsRequest;
1212
import com.akto.proto.generated.threat_detection.service.dashboard_service.v1.ListMaliciousRequestsResponse;
1313
import com.akto.proto.generated.threat_detection.service.malicious_alert_service.v1.RecordMaliciousEventRequest;
14+
import com.akto.threat.backend.client.IPLookupClient;
1415
import com.akto.threat.backend.constants.KafkaTopic;
1516
import com.akto.threat.backend.constants.MongoDBCollection;
1617
import com.akto.threat.backend.db.AggregateSampleMaliciousEventModel;
@@ -33,10 +34,13 @@ public class MaliciousEventService {
3334

3435
private final Kafka kafka;
3536
private MongoClient mongoClient;
37+
private IPLookupClient ipLookupClient;
3638

37-
public MaliciousEventService(KafkaConfig kafkaConfig, MongoClient mongoClient) {
39+
public MaliciousEventService(
40+
KafkaConfig kafkaConfig, MongoClient mongoClient, IPLookupClient ipLookupClient) {
3841
this.kafka = new Kafka(kafkaConfig);
3942
this.mongoClient = mongoClient;
43+
this.ipLookupClient = ipLookupClient;
4044
}
4145

4246
public void recordMaliciousEvent(String accountId, RecordMaliciousEventRequest request) {
@@ -64,7 +68,8 @@ public void recordMaliciousEvent(String accountId, RecordMaliciousEventRequest r
6468
.setLatestApiCollectionId(evt.getLatestApiCollectionId())
6569
.setEventType(maliciousEventType)
6670
.setLatestApiIp(evt.getLatestApiIp())
67-
.setCountry("US")
71+
.setCountry(
72+
this.ipLookupClient.getCountryISOCodeGivenIp(evt.getLatestApiIp()).orElse(""))
6873
.setCategory(evt.getCategory())
6974
.setSubCategory(evt.getSubCategory())
7075
.build();

apps/threat-detection-backend/src/main/java/com/akto/threat/backend/tasks/FlushMessagesToDB.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void run() {
7171
private void processRecords(ConsumerRecords<String, String> records) {
7272
records.forEach(
7373
r -> {
74-
String message = r.value();
7574
try {
75+
String message = r.value();
7676
writeMessage(message);
7777
} catch (JsonProcessingException e) {
7878
System.out.println("Error while parsing message" + e);

0 commit comments

Comments
 (0)