Skip to content

Commit 26feb3b

Browse files
committed
GeoIP interfaces for the Logstash bridge where elastic_integration plugin utilizes.
1 parent 8fe1bc4 commit 26feb3b

File tree

8 files changed

+207
-2
lines changed

8 files changed

+207
-2
lines changed

libs/logstash-bridge/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dependencies {
2424
compileOnly project(':x-pack:plugin:redact')
2525
compileOnly project(':x-pack:plugin:spatial')
2626
compileOnly project(':x-pack:plugin:wildcard')
27+
28+
compileOnly('com.maxmind.db:maxmind-db:3.1.1')
2729
}
2830

2931
tasks.named('forbiddenApisMain').configure {

libs/logstash-bridge/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
requires org.elasticsearch.redact;
2424
requires org.elasticsearch.spatial;
2525
requires org.elasticsearch.wildcard;
26+
requires org.elasticsearch.ingest.geoip;
27+
requires com.maxmind.db;
2628

2729
exports org.elasticsearch.logstashbridge;
2830
exports org.elasticsearch.logstashbridge.common;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.logstashbridge.geoip;
10+
11+
import org.elasticsearch.ingest.geoip.GeoIpProcessor;
12+
import org.elasticsearch.ingest.geoip.IpDatabaseProvider;
13+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
14+
15+
/**
16+
* An external bridge for {@link GeoIpProcessor}
17+
*/
18+
public interface GeoIpProcessorBridge {
19+
20+
class Factory extends StableBridgeAPI.ProxyInternal<GeoIpProcessor.Factory> {
21+
22+
public Factory(String type, IpDatabaseProvider ipDatabaseProvider) {
23+
super(new GeoIpProcessor.Factory(type, ipDatabaseProvider));
24+
}
25+
}
26+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.logstashbridge.geoip;
10+
11+
import org.elasticsearch.ingest.geoip.IpDatabase;
12+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* An external bridge for {@link IpDatabase}
18+
*/
19+
public interface IpDatabaseBridge {
20+
21+
String getDatabaseType() throws IOException;
22+
23+
class ProxyInternal extends StableBridgeAPI.ProxyInternal<IpDatabase> implements IpDatabaseBridge {
24+
25+
public ProxyInternal(final IpDatabase delegate) {
26+
super(delegate);
27+
}
28+
29+
@Override
30+
public String getDatabaseType() throws IOException {
31+
return toInternal().getDatabaseType();
32+
}
33+
}
34+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.logstashbridge.geoip;
10+
11+
import org.elasticsearch.cluster.metadata.ProjectId;
12+
import org.elasticsearch.core.FixForMultiProject;
13+
import org.elasticsearch.ingest.Processor;
14+
import org.elasticsearch.ingest.geoip.IpDatabase;
15+
import org.elasticsearch.ingest.geoip.IpDatabaseProvider;
16+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
17+
18+
/**
19+
* An external bridge for {@link Processor}
20+
*/
21+
public interface IpDatabaseProviderBridge extends StableBridgeAPI<IpDatabaseProvider> {
22+
23+
@FixForMultiProject(description = "What ProjectId should be resolved for multi-project case?")
24+
default Boolean isValid(String name) {
25+
return toInternal().isValid(ProjectId.DEFAULT, name);
26+
}
27+
28+
@FixForMultiProject(description = "What ProjectId should be resolved for multi-project case?")
29+
default IpDatabaseBridge getDatabase(String name) {
30+
return new IpDatabaseBridge.ProxyInternal(toInternal().getDatabase(ProjectId.DEFAULT, name));
31+
}
32+
33+
static IpDatabaseProviderBridge fromInternal(final IpDatabaseProvider internalProvider) {
34+
if (internalProvider instanceof IpDatabaseProviderBridge.AbstractExternal.ProxyExternal externalProxy) {
35+
return externalProxy.getIpDatabaseProviderBridge();
36+
}
37+
return new IpDatabaseProviderBridge.ProxyInternal(internalProvider);
38+
}
39+
40+
/**
41+
* The {@code IpDatabaseProviderBridge.AbstractExternal} is an abstract base class for implementing
42+
* the {@link IpDatabaseProviderBridge} externally to the Elasticsearch code-base. It takes care of
43+
* the details of maintaining a singular internal-form implementation of {@link IpDatabaseProvider}
44+
* that proxies calls through the external implementation.
45+
*/
46+
abstract class AbstractExternal implements IpDatabaseProviderBridge {
47+
private AbstractExternal.ProxyExternal internalProcessor;
48+
49+
public IpDatabaseProvider toInternal() {
50+
if (internalProcessor == null) {
51+
internalProcessor = new AbstractExternal.ProxyExternal();
52+
}
53+
return internalProcessor;
54+
}
55+
56+
private class ProxyExternal implements IpDatabaseProvider {
57+
58+
private AbstractExternal getIpDatabaseProviderBridge() {
59+
return AbstractExternal.this;
60+
}
61+
62+
@Override
63+
public Boolean isValid(ProjectId projectId, String name) {
64+
return IpDatabaseProviderBridge.AbstractExternal.this.toInternal().isValid(projectId, name);
65+
}
66+
67+
@Override
68+
public IpDatabase getDatabase(ProjectId projectId, String name) {
69+
return IpDatabaseProviderBridge.AbstractExternal.this.toInternal().getDatabase(projectId, name);
70+
}
71+
}
72+
}
73+
74+
/**
75+
* An implementation of {@link IpDatabaseProviderBridge} that proxies to an internal {@link IpDatabaseProvider}
76+
*/
77+
class ProxyInternal extends StableBridgeAPI.ProxyInternal<IpDatabaseProvider> implements IpDatabaseProviderBridge {
78+
public ProxyInternal(final IpDatabaseProvider delegate) {
79+
super(delegate);
80+
}
81+
}
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
package org.elasticsearch.logstashbridge.geoip;
10+
11+
import org.elasticsearch.core.SuppressForbidden;
12+
import org.elasticsearch.logstashbridge.StableBridgeAPI;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
17+
public interface MaxMindDbBridge {
18+
19+
class Reader extends StableBridgeAPI.ProxyInternal<com.maxmind.db.Reader> {
20+
21+
@SuppressForbidden(reason = "Maxmind Reader constructor requires java.io.File")
22+
public Reader(final File databasePath, final NodeCache nodeCache) throws IOException {
23+
super(new com.maxmind.db.Reader(databasePath, nodeCache.toInternal()));
24+
}
25+
26+
protected Reader(final com.maxmind.db.Reader internalDelegate) {
27+
super(internalDelegate);
28+
}
29+
30+
@Override
31+
public com.maxmind.db.Reader toInternal() {
32+
return internalDelegate;
33+
}
34+
35+
public String getDatabaseType() {
36+
return toInternal().getMetadata().getDatabaseType();
37+
}
38+
39+
public void close() throws IOException {
40+
toInternal().close();
41+
}
42+
}
43+
44+
class NodeCache extends StableBridgeAPI.ProxyInternal<com.maxmind.db.NodeCache> {
45+
46+
protected NodeCache(final com.maxmind.db.NodeCache internalDelegate) {
47+
super(internalDelegate);
48+
}
49+
50+
public static NodeCache get(final int capacity) {
51+
return new NodeCache(new com.maxmind.db.CHMCache(capacity));
52+
}
53+
54+
public static NodeCache getInstance() {
55+
return new NodeCache(com.maxmind.db.NoCache.getInstance());
56+
}
57+
}
58+
59+
}

libs/logstash-bridge/src/main/java/org/elasticsearch/logstashbridge/script/ScriptServiceBridge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void close() throws IOException {
117117
@FixForMultiProject
118118
// Logstash resolves and runs ingest pipelines based on the datastream.
119119
// How should ProjectIdResolverBridge behave in this case?
120-
// In other words, it looks we need to find a way to figure out which ingest pipeline belongs to which project.
120+
// In other words, it looks we need to find a way to figure out which ingest pipeline belongs to which project.
121121
static class ProjectIdResolverBridge implements ProjectResolver {
122122

123123
public static final ProjectIdResolverBridge INSTANCE = new ProjectIdResolverBridge();

modules/ingest-geoip/src/main/java/module-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919
exports org.elasticsearch.ingest.geoip.direct to org.elasticsearch.server;
2020
exports org.elasticsearch.ingest.geoip.stats to org.elasticsearch.server;
2121

22-
exports org.elasticsearch.ingest.geoip to com.maxmind.db;
22+
exports org.elasticsearch.ingest.geoip to com.maxmind.db, org.elasticsearch.logstashbridge;
2323
}

0 commit comments

Comments
 (0)