Skip to content

Commit 13fe69d

Browse files
committed
Further refactoring back to keyword return type
1 parent 3129e57 commit 13fe69d

File tree

5 files changed

+50
-30
lines changed

5 files changed

+50
-30
lines changed

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/NetworkDirectionProcessor.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ public class NetworkDirectionProcessor extends AbstractProcessor {
2929

3030
public static final String TYPE = "network_direction";
3131

32-
public static final String DIRECTION_INTERNAL = "internal";
33-
public static final String DIRECTION_EXTERNAL = "external";
34-
public static final String DIRECTION_INBOUND = "inbound";
35-
public static final String DIRECTION_OUTBOUND = "outbound";
36-
3732
private final String sourceIpField;
3833
private final String destinationIpField;
3934
private final String targetField;
@@ -126,16 +121,7 @@ private String getDirection(IngestDocument d) throws Exception {
126121
boolean sourceInternal = NetworkDirectionUtils.isInternal(networks, sourceIpAddrString);
127122
boolean destinationInternal = NetworkDirectionUtils.isInternal(networks, destIpAddrString);
128123

129-
if (sourceInternal && destinationInternal) {
130-
return DIRECTION_INTERNAL;
131-
}
132-
if (sourceInternal) {
133-
return DIRECTION_OUTBOUND;
134-
}
135-
if (destinationInternal) {
136-
return DIRECTION_INBOUND;
137-
}
138-
return DIRECTION_EXTERNAL;
124+
return NetworkDirectionUtils.getDirection(sourceInternal, destinationInternal);
139125
}
140126

141127
@Override

server/src/main/java/org/elasticsearch/common/network/NetworkDirectionUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ public class NetworkDirectionUtils {
3030
private static final String PRIVATE_NAMED_NETWORK = "private";
3131
private static final String PUBLIC_NAMED_NETWORK = "public";
3232

33+
public static final String DIRECTION_INTERNAL = "internal";
34+
public static final String DIRECTION_EXTERNAL = "external";
35+
public static final String DIRECTION_INBOUND = "inbound";
36+
public static final String DIRECTION_OUTBOUND = "outbound";
37+
3338
public static boolean isInternal(List<String> networks, String ip) {
3439
for (String network : networks) {
3540
if (inNetwork(InetAddresses.forString(ip), network)) {
@@ -54,6 +59,19 @@ public static boolean inNetwork(InetAddress address, String network) {
5459
};
5560
}
5661

62+
public static String getDirection(boolean sourceInternal, boolean destinationInternal) {
63+
if (sourceInternal && destinationInternal) {
64+
return DIRECTION_INTERNAL;
65+
}
66+
if (sourceInternal) {
67+
return DIRECTION_OUTBOUND;
68+
}
69+
if (destinationInternal) {
70+
return DIRECTION_INBOUND;
71+
}
72+
return DIRECTION_EXTERNAL;
73+
}
74+
5775
private static boolean isLoopback(InetAddress ip) {
5876
return ip.isLoopbackAddress();
5977
}

x-pack/plugin/esql/src/main/generated/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirectionEvaluator.java

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/ip/NetworkDirection.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class NetworkDirection extends EsqlScalarFunction {
5050
private final Expression internalNetworks;
5151

5252
@FunctionInfo(
53-
returnType = "boolean",
53+
returnType = "keyword",
5454
description = "Returns true if the direction of the source-to-destination-IPs is determined to be inbound, provided a list of internal networks.",
5555
examples = @Example(file = "ip", tag = "cdirMatchMultipleArgs") // TODO make an example
5656
)
@@ -126,7 +126,7 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvalua
126126
}
127127

128128
@Evaluator
129-
static boolean process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock internalNetworks) {
129+
static BytesRef process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) BytesRef scratch, BytesRef sourceIp, BytesRef destinationIp, @Position int position, BytesRefBlock internalNetworks) {
130130
// Pulling the bytes out directly using InetAddress.getByAddress() requires error handling TODO
131131
InetAddress sourceIpAddress = InetAddresses.forString(sourceIp.utf8ToString());
132132
InetAddress destinationIpAddress = InetAddresses.forString(destinationIp.utf8ToString());
@@ -149,14 +149,11 @@ static boolean process(@Fixed(includeInToString=false, scope=THREAD_LOCAL) Bytes
149149
}
150150
}
151151

152-
if (sourceInternal == false) {
153-
return destinationInternal;
154-
}
155-
return false;
152+
return new BytesRef(NetworkDirectionUtils.getDirection(sourceInternal, destinationInternal));
156153
}
157154

158155
@Override
159156
public DataType dataType() {
160-
return DataType.BOOLEAN;
157+
return DataType.KEYWORD;
161158
}
162159
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.ip;
9+
10+
import org.elasticsearch.xpack.esql.core.expression.Expression;
11+
import org.elasticsearch.xpack.esql.core.tree.Source;
12+
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
13+
14+
import java.util.List;
15+
16+
public class NetworkDirectionTests extends AbstractScalarFunctionTestCase {
17+
@Override
18+
protected Expression build(Source source, List<Expression> args) {
19+
return null;
20+
}
21+
}

0 commit comments

Comments
 (0)