Skip to content

Commit dc5cc1c

Browse files
authored
Convert processor supports validation of IPv4/IPv6 addresses (#69989) (#71285)
1 parent 4c04803 commit dc5cc1c

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
package org.elasticsearch.ingest.common;
1010

11+
import org.elasticsearch.common.network.InetAddresses;
1112
import org.elasticsearch.ingest.AbstractProcessor;
1213
import org.elasticsearch.ingest.ConfigurationUtils;
1314
import org.elasticsearch.ingest.IngestDocument;
@@ -83,6 +84,13 @@ public Object convert(Object value) {
8384
throw new IllegalArgumentException("[" + value + "] is not a boolean value, cannot convert to boolean");
8485
}
8586
}
87+
}, IP {
88+
@Override
89+
public Object convert(Object value) {
90+
// IllegalArgumentException is thrown if unable to convert
91+
InetAddresses.forString((String) value);
92+
return value;
93+
}
8694
}, STRING {
8795
@Override
8896
public Object convert(Object value) {

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/ConvertProcessorTests.java

+76-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88

99
package org.elasticsearch.ingest.common;
1010

11+
import org.elasticsearch.ingest.IngestDocument;
12+
import org.elasticsearch.ingest.Processor;
13+
import org.elasticsearch.ingest.RandomDocumentPicks;
14+
import org.elasticsearch.test.ESTestCase;
15+
1116
import java.util.ArrayList;
1217
import java.util.Collections;
1318
import java.util.HashMap;
1419
import java.util.List;
1520
import java.util.Locale;
1621
import java.util.Map;
1722

18-
import org.elasticsearch.ingest.IngestDocument;
19-
import org.elasticsearch.ingest.Processor;
20-
import org.elasticsearch.ingest.RandomDocumentPicks;
21-
import org.elasticsearch.test.ESTestCase;
22-
2323
import static org.elasticsearch.ingest.IngestDocumentMatcher.assertIngestDocument;
2424
import static org.elasticsearch.ingest.common.ConvertProcessor.Type;
2525
import static org.hamcrest.Matchers.containsString;
@@ -307,6 +307,77 @@ public void testConvertBooleanError() throws Exception {
307307
}
308308
}
309309

310+
public void testConvertIpV4() throws Exception {
311+
// valid ipv4 address
312+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
313+
String fieldName = RandomDocumentPicks.randomFieldName(random());
314+
String targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random()));
315+
String validIpV4 = "192.168.1.1";
316+
ingestDocument.setFieldValue(fieldName, validIpV4);
317+
318+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false);
319+
processor.execute(ingestDocument);
320+
assertThat(ingestDocument.getFieldValue(targetField, String.class), equalTo(validIpV4));
321+
322+
// invalid ipv4 address
323+
IngestDocument ingestDocument2 = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
324+
fieldName = RandomDocumentPicks.randomFieldName(random());
325+
targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random()));
326+
String invalidIpV4 = "192.168.1.256";
327+
ingestDocument2.setFieldValue(fieldName, invalidIpV4);
328+
329+
Processor processor2 = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false);
330+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(ingestDocument2));
331+
assertThat(e.getMessage(), containsString("'" + invalidIpV4 + "' is not an IP string literal."));
332+
}
333+
334+
public void testConvertIpV6() throws Exception {
335+
// valid ipv6 address
336+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
337+
String fieldName = RandomDocumentPicks.randomFieldName(random());
338+
String targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random()));
339+
String validIpV6 = "2001:db8:3333:4444:5555:6666:7777:8888";
340+
ingestDocument.setFieldValue(fieldName, validIpV6);
341+
342+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false);
343+
processor.execute(ingestDocument);
344+
assertThat(ingestDocument.getFieldValue(targetField, String.class), equalTo(validIpV6));
345+
346+
// invalid ipv6 address
347+
IngestDocument ingestDocument2 = RandomDocumentPicks.randomIngestDocument(random(), new HashMap<>());
348+
fieldName = RandomDocumentPicks.randomFieldName(random());
349+
targetField = randomValueOtherThan(fieldName, () -> RandomDocumentPicks.randomFieldName(random()));
350+
String invalidIpV6 = "2001:db8:3333:4444:5555:6666:7777:88888";
351+
ingestDocument2.setFieldValue(fieldName, invalidIpV6);
352+
353+
Processor processor2 = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, targetField, Type.IP, false);
354+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor2.execute(ingestDocument2));
355+
assertThat(e.getMessage(), containsString("'" + invalidIpV6 + "' is not an IP string literal."));
356+
}
357+
358+
public void testConvertIpList() throws Exception {
359+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
360+
int numItems = randomIntBetween(1, 10);
361+
List<String> fieldValue = new ArrayList<>();
362+
List<String> expectedList = new ArrayList<>();
363+
for (int j = 0; j < numItems; j++) {
364+
String value;
365+
if (randomBoolean()) {
366+
// ipv4 value
367+
value = "192.168.1." + randomIntBetween(0, 255);
368+
} else {
369+
// ipv6 value
370+
value = "2001:db8:3333:4444:5555:6666:7777:" + Long.toString(randomLongBetween(0, 65535), 16);
371+
}
372+
fieldValue.add(value);
373+
expectedList.add(value);
374+
}
375+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, fieldValue);
376+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, fieldName, Type.IP, false);
377+
processor.execute(ingestDocument);
378+
assertThat(ingestDocument.getFieldValue(fieldName, List.class), equalTo(expectedList));
379+
}
380+
310381
public void testConvertString() throws Exception {
311382
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
312383
Object fieldValue;

0 commit comments

Comments
 (0)