Skip to content

Commit

Permalink
-download task support for geoip (opensearch-project#3373)
Browse files Browse the repository at this point in the history
* -download task support for geoip
Signed-off-by: rajeshLovesToCode <[email protected]>

* -download task support for geoip
Signed-off-by: rajeshLovesToCode <[email protected]>

* -download task support for geoip
Signed-off-by: rajeshLovesToCode <[email protected]>

* -fix for geoip IP constant
Signed-off-by: rajeshLovesToCode <[email protected]>
  • Loading branch information
rajeshLovesToCode authored Oct 3, 2023
1 parent b69f81b commit 56b74be
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
74 changes: 74 additions & 0 deletions data-prepper-plugins/geoip-processor/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
plugins{
id 'de.undercouch.download' version '4.1.2'
}
apply plugin: 'de.undercouch.download'

import de.undercouch.gradle.tasks.download.Download

/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
Expand All @@ -23,8 +30,75 @@ dependencies {
testImplementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml'
testImplementation project(':data-prepper-test-common')
}
def geoIP2='GeoIP2'
def geoLite2= 'GeoLite2'
task downloadFile(type: Download) {

def urls = [
'https://raw.githubusercontent.com/maxmind/MaxMind-DB/main/test-data/GeoIP2-City-Test.mmdb',
'https://raw.githubusercontent.com/maxmind/MaxMind-DB/main/test-data/GeoIP2-Country-Test.mmdb',
'https://raw.githubusercontent.com/maxmind/MaxMind-DB/main/test-data/GeoLite2-ASN-Test.mmdb'
]
def mmdbFileExtension = '.mmdb'
def baseDirPath = 'src/test/resources/mmdb-file/geo-lite2/'

urls.each { url ->
src(url)
dest(baseDirPath)
doLast {

def testFileName = url.substring(url.lastIndexOf('/') + 1)
def testMmdbSubString = testFileName.substring(testFileName.lastIndexOf('-'))
def fileName = testFileName.substring(0, testFileName.length() - testMmdbSubString.length())

if(fileName.contains(geoIP2)) {
fileName = fileName.replace(geoIP2, geoLite2)
}
File sourceFile = file(baseDirPath+testFileName)
File destinationFile = file( baseDirPath+fileName+mmdbFileExtension)
sourceFile.renameTo(destinationFile)

}

}


}
task downloadEnterpriseFile(type: Download) {
dependsOn downloadFile
def urls = [
'https://raw.githubusercontent.com/maxmind/MaxMind-DB/main/test-data/GeoIP2-Enterprise-Test.mmdb'
]
def mmdbFileExtension = '.mmdb'
def baseDirPath = 'src/test/resources/mmdb-file/geo-enterprise/'

urls.each { url ->
src(url)
def testFileName = url.substring(url.lastIndexOf('/') + 1)
def testMmdbSubString = testFileName.substring(testFileName.lastIndexOf('-'))
def fileName = testFileName.substring(0, testFileName.length() - testMmdbSubString.length())

dest(baseDirPath+testFileName)
doLast {
if(fileName.contains(geoIP2)) {
fileName = fileName.replace(geoIP2, geoLite2)
}
File sourceFile = file(baseDirPath+testFileName)
File destinationFile = file( baseDirPath+fileName+mmdbFileExtension)
sourceFile.renameTo(destinationFile)
}

}

}

/*task processTestResources(type: Copy) {
dependsOn downloadEnterpriseFile
from 'src/test/resources' // Source directory containing test resources
into 'build/resources/test' // Destination directory for processed test resources
}*/
tasks.test.dependsOn 'processTestResources'
tasks.processTestResources.dependsOn 'downloadEnterpriseFile'
test {
useJUnitPlatform()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public Map<String, Object> getGeoData(InetAddress inetAddress, List<String> attr
enrichData(geoData, ORGANIZATION_NAME, organizationName);
break;
case NETWORK:
enrichData(geoData, NETWORK, network.toString());
enrichData(geoData, NETWORK,network!=null? network.toString():null);
break;
}
}
Expand All @@ -204,7 +204,7 @@ public Map<String, Object> getGeoData(InetAddress inetAddress, List<String> attr
}

enrichData(geoData, ORGANIZATION_NAME, organizationName);
enrichData(geoData, NETWORK, network.toString());
enrichData(geoData, NETWORK,network!=null? network.toString():null);
}
} catch (Exception ex) {
throw new EnrichFailedException("Enrichment failed exception" + ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class GetGeoLite2DataTest {

private static final String PATH = "./src/test/resources/mmdb-file/geo-lite2";
public static final String IP = "2001:4860:4860::8888";
public static final String IP = "2a02:ec00:0:0:0:0:0:0";
private static final String PREFIX_DIR = "first_database";
private String tempFolderPath = System.getProperty("java.io.tmpdir") + File.separator + "GeoIP";
@Mock
Expand Down Expand Up @@ -64,8 +64,8 @@ void getGeoDataTest_without_attributes() throws UnknownHostException {
GeoIPProcessorService.downloadReady = false;
Map<String, Object> geoData = getGeoLite2Data.getGeoData(inetAddress, attributes, tempFolderPath);
Assertions.assertNotNull(geoData);
assertThat(geoData.get("country_iso_code"), equalTo("US"));
assertThat(geoData.get("ip"), equalTo("2001:4860:4860:0:0:0:0:8888"));
assertThat(geoData.get("country_iso_code"), equalTo("FR"));
assertThat(geoData.get("ip"), equalTo(IP));
assertDoesNotThrow(() -> {
getGeoLite2Data.closeReader();
});
Expand All @@ -88,8 +88,8 @@ void getGeoDataTest_with_attributes() throws UnknownHostException {
GeoIPProcessorService.downloadReady = false;
Map<String, Object> geoData = getGeoLite2Data.getGeoData(inetAddress, attributes, tempFolderPath);
Assertions.assertNotNull(geoData);
assertThat(geoData.get("country_name"), equalTo("United States"));
assertThat(geoData.get("ip"), equalTo("2001:4860:4860:0:0:0:0:8888"));
assertThat(geoData.get("country_name"), equalTo("France"));
assertThat(geoData.get("ip"), equalTo(IP));
assertDoesNotThrow(() -> {
getGeoLite2Data.closeReader();
});
Expand Down
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 56b74be

Please sign in to comment.