Skip to content

Commit 4f9483a

Browse files
committed
load mmdb from official URL with accountId + licenseKey
1 parent 90d3a2e commit 4f9483a

File tree

3 files changed

+54
-30
lines changed

3 files changed

+54
-30
lines changed

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/DatabaseReaderFactory.java

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
package org.prebid.server.hooks.modules.greenbids.real.time.data.config;
22

3-
import com.google.cloud.storage.Blob;
43
import com.google.cloud.storage.Storage;
5-
import com.google.cloud.storage.StorageException;
4+
import com.maxmind.db.Reader;
65
import io.vertx.core.Promise;
76
import io.vertx.core.Vertx;
87
import com.maxmind.geoip2.DatabaseReader;
8+
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
9+
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
910
import org.prebid.server.exception.PreBidException;
1011
import org.prebid.server.vertx.Initializable;
1112

12-
import java.io.FileOutputStream;
1313
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.net.HttpURLConnection;
16+
import java.net.URL;
1417
import java.nio.file.Files;
1518
import java.nio.file.Path;
16-
import java.util.Optional;
19+
import java.nio.file.StandardCopyOption;
20+
import java.util.Base64;
1721
import java.util.concurrent.atomic.AtomicReference;
22+
import java.util.zip.GZIPInputStream;
1823

1924
public class DatabaseReaderFactory implements Initializable {
2025

21-
private final String gcsBucketName;
22-
23-
private final String geoLiteCountryPath;
26+
private final GreenbidsRealTimeDataProperties properties;
2427

2528
private final Vertx vertx;
2629

27-
private final Storage storage;
28-
2930
private final AtomicReference<DatabaseReader> databaseReaderRef = new AtomicReference<>();
3031

31-
public DatabaseReaderFactory(String gcsBucketName, String geoLiteCountryPath, Vertx vertx, Storage storage) {
32-
this.gcsBucketName = gcsBucketName;
33-
this.geoLiteCountryPath = geoLiteCountryPath;
32+
public DatabaseReaderFactory(GreenbidsRealTimeDataProperties properties, Vertx vertx, Storage storage) {
33+
this.properties = properties;
3434
this.vertx = vertx;
35-
this.storage = storage;
3635
}
3736

3837
@Override
3938
public void initialize(Promise<Void> initializePromise) {
4039
vertx.executeBlocking(() -> {
4140
try {
42-
final Blob blob = getBlob();
43-
final Path databasePath = Files.createTempFile("GeoLite2-Country", ".mmdb");
44-
45-
try (FileOutputStream outputStream = new FileOutputStream(databasePath.toFile())) {
46-
outputStream.write(blob.getContent());
47-
}
48-
49-
databaseReaderRef.set(new DatabaseReader.Builder(databasePath.toFile()).build());
41+
final Path tarGzPath = downloadFile(
42+
properties.geoLiteCountryPath,
43+
properties.maxMindAccountId,
44+
properties.maxMindLicenseKey);
45+
databaseReaderRef.set(extractMMDB(tarGzPath));
5046
} catch (IOException e) {
5147
throw new PreBidException("Failed to initialize DatabaseReader from URL", e);
5248
}
@@ -55,13 +51,38 @@ public void initialize(Promise<Void> initializePromise) {
5551
.onComplete(initializePromise);
5652
}
5753

58-
private Blob getBlob() {
59-
try {
60-
return Optional.ofNullable(storage.get(gcsBucketName))
61-
.map(bucket -> bucket.get(geoLiteCountryPath))
62-
.orElseThrow(() -> new PreBidException("Bucket not found: " + gcsBucketName));
63-
} catch (StorageException e) {
64-
throw new PreBidException("Error accessing GCS artefact for model: ", e);
54+
private Path downloadFile(String url, String accountId, String licenseKey) throws IOException {
55+
final URL downloadUrl = new URL(url + "&account_id=" + accountId + "&license_key=" + licenseKey);
56+
final HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection();
57+
connection.setRequestMethod("GET");
58+
59+
final String auth = accountId + ":" + licenseKey;
60+
final String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
61+
connection.setRequestProperty("Authorization", "Basic " + encodedAuth);
62+
63+
final Path tempFile = Files.createTempFile("geolite2", ".tar.gz");
64+
try (InputStream inputStream = connection.getInputStream()) {
65+
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
66+
}
67+
return tempFile;
68+
}
69+
70+
private DatabaseReader extractMMDB(Path tarGzPath) throws IOException {
71+
try (GZIPInputStream gis = new GZIPInputStream(Files.newInputStream(tarGzPath));
72+
TarArchiveInputStream tarInput = new TarArchiveInputStream(gis)) {
73+
74+
TarArchiveEntry currentEntry;
75+
while ((currentEntry = tarInput.getNextTarEntry()) != null) {
76+
if (currentEntry.getName().contains("GeoLite2-Country.mmdb")) {
77+
break;
78+
}
79+
}
80+
81+
final DatabaseReader databaseReader = new DatabaseReader.Builder(tarInput)
82+
.fileMode(Reader.FileMode.MEMORY).build();
83+
return databaseReader;
84+
} catch (IOException e) {
85+
throw new RuntimeException("Failed to extract MMDB file", e);
6586
}
6687
}
6788

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public class GreenbidsRealTimeDataConfiguration {
3333
@Bean
3434
DatabaseReaderFactory databaseReaderFactory(
3535
GreenbidsRealTimeDataProperties properties, Vertx vertx, Storage storage) {
36-
return new DatabaseReaderFactory(
37-
properties.getGcsBucketName(), properties.getGeoLiteCountryPath(), vertx, storage);
36+
return new DatabaseReaderFactory(properties, vertx, storage);
3837
}
3938

4039
@Bean

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataProperties.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ public class GreenbidsRealTimeDataProperties {
1111

1212
String geoLiteCountryPath;
1313

14+
String maxMindAccountId;
15+
16+
String maxMindLicenseKey;
17+
1418
String gcsBucketName;
1519

1620
Integer cacheExpirationMinutes;

0 commit comments

Comments
 (0)