1010package org .elasticsearch .ingest .common ;
1111
1212import org .elasticsearch .cluster .metadata .ProjectId ;
13- import org .elasticsearch .common .network .CIDRUtils ;
14- import org .elasticsearch .common .network .InetAddresses ;
13+ import org .elasticsearch .common .network .NetworkDirectionUtils ;
1514import org .elasticsearch .ingest .AbstractProcessor ;
1615import org .elasticsearch .ingest .ConfigurationUtils ;
1716import org .elasticsearch .ingest .IngestDocument ;
1817import org .elasticsearch .ingest .Processor ;
1918import org .elasticsearch .script .ScriptService ;
2019import org .elasticsearch .script .TemplateScript ;
2120
22- import java .net .InetAddress ;
2321import java .util .ArrayList ;
24- import java .util .Arrays ;
2522import java .util .List ;
2623import java .util .Map ;
2724
2825import static org .elasticsearch .ingest .ConfigurationUtils .newConfigurationException ;
2926import static org .elasticsearch .ingest .ConfigurationUtils .readBooleanProperty ;
3027
3128public class NetworkDirectionProcessor extends AbstractProcessor {
32- static final byte [] UNDEFINED_IP4 = new byte [] { 0 , 0 , 0 , 0 };
33- static final byte [] UNDEFINED_IP6 = new byte [] { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };
34- static final byte [] BROADCAST_IP4 = new byte [] { (byte ) 0xff , (byte ) 0xff , (byte ) 0xff , (byte ) 0xff };
3529
3630 public static final String TYPE = "network_direction" ;
3731
@@ -40,17 +34,6 @@ public class NetworkDirectionProcessor extends AbstractProcessor {
4034 public static final String DIRECTION_INBOUND = "inbound" ;
4135 public static final String DIRECTION_OUTBOUND = "outbound" ;
4236
43- private static final String LOOPBACK_NAMED_NETWORK = "loopback" ;
44- private static final String GLOBAL_UNICAST_NAMED_NETWORK = "global_unicast" ;
45- private static final String UNICAST_NAMED_NETWORK = "unicast" ;
46- private static final String LINK_LOCAL_UNICAST_NAMED_NETWORK = "link_local_unicast" ;
47- private static final String INTERFACE_LOCAL_NAMED_NETWORK = "interface_local_multicast" ;
48- private static final String LINK_LOCAL_MULTICAST_NAMED_NETWORK = "link_local_multicast" ;
49- private static final String MULTICAST_NAMED_NETWORK = "multicast" ;
50- private static final String UNSPECIFIED_NAMED_NETWORK = "unspecified" ;
51- private static final String PRIVATE_NAMED_NETWORK = "private" ;
52- private static final String PUBLIC_NAMED_NETWORK = "public" ;
53-
5437 private final String sourceIpField ;
5538 private final String destinationIpField ;
5639 private final String targetField ;
@@ -140,8 +123,8 @@ private String getDirection(IngestDocument d) throws Exception {
140123 return null ;
141124 }
142125
143- boolean sourceInternal = isInternal (networks , sourceIpAddrString );
144- boolean destinationInternal = isInternal (networks , destIpAddrString );
126+ boolean sourceInternal = NetworkDirectionUtils . isInternal (networks , sourceIpAddrString );
127+ boolean destinationInternal = NetworkDirectionUtils . isInternal (networks , destIpAddrString );
145128
146129 if (sourceInternal && destinationInternal ) {
147130 return DIRECTION_INTERNAL ;
@@ -155,83 +138,6 @@ private String getDirection(IngestDocument d) throws Exception {
155138 return DIRECTION_EXTERNAL ;
156139 }
157140
158- private static boolean isInternal (List <String > networks , String ip ) {
159- for (String network : networks ) {
160- if (inNetwork (ip , network )) {
161- return true ;
162- }
163- }
164- return false ;
165- }
166-
167- private static boolean inNetwork (String ip , String network ) {
168- InetAddress address = InetAddresses .forString (ip );
169- return switch (network ) {
170- case LOOPBACK_NAMED_NETWORK -> isLoopback (address );
171- case GLOBAL_UNICAST_NAMED_NETWORK , UNICAST_NAMED_NETWORK -> isUnicast (address );
172- case LINK_LOCAL_UNICAST_NAMED_NETWORK -> isLinkLocalUnicast (address );
173- case INTERFACE_LOCAL_NAMED_NETWORK -> isInterfaceLocalMulticast (address );
174- case LINK_LOCAL_MULTICAST_NAMED_NETWORK -> isLinkLocalMulticast (address );
175- case MULTICAST_NAMED_NETWORK -> isMulticast (address );
176- case UNSPECIFIED_NAMED_NETWORK -> isUnspecified (address );
177- case PRIVATE_NAMED_NETWORK -> isPrivate (ip );
178- case PUBLIC_NAMED_NETWORK -> isPublic (ip );
179- default -> CIDRUtils .isInRange (ip , network );
180- };
181- }
182-
183- private static boolean isLoopback (InetAddress ip ) {
184- return ip .isLoopbackAddress ();
185- }
186-
187- private static boolean isUnicast (InetAddress ip ) {
188- return Arrays .equals (ip .getAddress (), BROADCAST_IP4 ) == false
189- && isUnspecified (ip ) == false
190- && isLoopback (ip ) == false
191- && isMulticast (ip ) == false
192- && isLinkLocalUnicast (ip ) == false ;
193- }
194-
195- private static boolean isLinkLocalUnicast (InetAddress ip ) {
196- return ip .isLinkLocalAddress ();
197- }
198-
199- private static boolean isInterfaceLocalMulticast (InetAddress ip ) {
200- return ip .isMCNodeLocal ();
201- }
202-
203- private static boolean isLinkLocalMulticast (InetAddress ip ) {
204- return ip .isMCLinkLocal ();
205- }
206-
207- private static boolean isMulticast (InetAddress ip ) {
208- return ip .isMulticastAddress ();
209- }
210-
211- private static boolean isUnspecified (InetAddress ip ) {
212- var address = ip .getAddress ();
213- return Arrays .equals (UNDEFINED_IP4 , address ) || Arrays .equals (UNDEFINED_IP6 , address );
214- }
215-
216- private static boolean isPrivate (String ip ) {
217- return CIDRUtils .isInRange (ip , "10.0.0.0/8" , "172.16.0.0/12" , "192.168.0.0/16" , "fd00::/8" );
218- }
219-
220- private static boolean isPublic (String ip ) {
221- return isLocalOrPrivate (ip ) == false ;
222- }
223-
224- private static boolean isLocalOrPrivate (String ip ) {
225- var address = InetAddresses .forString (ip );
226- return isPrivate (ip )
227- || isLoopback (address )
228- || isUnspecified (address )
229- || isLinkLocalUnicast (address )
230- || isLinkLocalMulticast (address )
231- || isInterfaceLocalMulticast (address )
232- || Arrays .equals (address .getAddress (), BROADCAST_IP4 );
233- }
234-
235141 @ Override
236142 public String getType () {
237143 return TYPE ;
0 commit comments