Skip to content

Commit 8d5a1d2

Browse files
authoredMar 27, 2024
Convert ingest processor supports ip type (opensearch-project#12818)
* Convert ingest processor supports ip type Signed-off-by: Gao Binlong <gbinlong@amazon.com> * Modify change log Signed-off-by: Gao Binlong <gbinlong@amazon.com> * Add comment Signed-off-by: Gao Binlong <gbinlong@amazon.com> --------- Signed-off-by: Gao Binlong <gbinlong@amazon.com>
1 parent 618782d commit 8d5a1d2

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
103103

104104
## [Unreleased 2.x]
105105
### Added
106+
- Convert ingest processor supports ip type ([#12818](https://github.com/opensearch-project/OpenSearch/pull/12818))
106107
- Add a counter to node stat api to track shard going from idle to non-idle ([#12768](https://github.com/opensearch-project/OpenSearch/pull/12768))
107108
- [Concurrent Segment Search] Perform buildAggregation concurrently and support Composite Aggregations ([#12697](https://github.com/opensearch-project/OpenSearch/pull/12697))
108109

‎modules/ingest-common/src/main/java/org/opensearch/ingest/common/ConvertProcessor.java

+14
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package org.opensearch.ingest.common;
3434

35+
import org.opensearch.common.network.InetAddresses;
3536
import org.opensearch.ingest.AbstractProcessor;
3637
import org.opensearch.ingest.ConfigurationUtils;
3738
import org.opensearch.ingest.IngestDocument;
@@ -118,6 +119,19 @@ public Object convert(Object value) {
118119
return value.toString();
119120
}
120121
},
122+
IP {
123+
@Override
124+
public Object convert(Object value) {
125+
// If the value is a valid ipv4/ipv6 address, we return the original value directly because IpFieldType
126+
// can accept string value, this is simpler than we return an InetAddress object which needs to do more
127+
// work such as serialization
128+
if (value instanceof String && InetAddresses.isInetAddress(value.toString())) {
129+
return value;
130+
} else {
131+
throw new IllegalArgumentException("[" + value + "] is not a valid ipv4/ipv6 address");
132+
}
133+
}
134+
},
121135
AUTO {
122136
@Override
123137
public Object convert(Object value) {

‎modules/ingest-common/src/test/java/org/opensearch/ingest/common/ConvertProcessorTests.java

+25
Original file line numberDiff line numberDiff line change
@@ -550,4 +550,29 @@ public void testTargetField() throws Exception {
550550
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(String.valueOf(randomInt)));
551551
assertThat(ingestDocument.getFieldValue(targetField, Integer.class), equalTo(randomInt));
552552
}
553+
554+
public void testConvertIP() throws Exception {
555+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
556+
String validIPString;
557+
if (randomBoolean()) {
558+
validIPString = "1.2.3.4";
559+
} else {
560+
validIPString = "::1";
561+
}
562+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, validIPString);
563+
564+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, fieldName, Type.IP, false);
565+
processor.execute(ingestDocument);
566+
assertThat(ingestDocument.getFieldValue(fieldName, String.class), equalTo(validIPString));
567+
568+
String invalidIPString = randomAlphaOfLength(10);
569+
fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, invalidIPString);
570+
Processor processorWithInvalidIP = new ConvertProcessor(randomAlphaOfLength(10), null, fieldName, fieldName, Type.IP, false);
571+
try {
572+
processorWithInvalidIP.execute(ingestDocument);
573+
fail("processor execute should have failed");
574+
} catch (IllegalArgumentException e) {
575+
assertThat(e.getMessage(), equalTo("[" + invalidIPString + "] is not a valid ipv4/ipv6 address"));
576+
}
577+
}
553578
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
teardown:
3+
- do:
4+
ingest.delete_pipeline:
5+
id: "1"
6+
ignore: 404
7+
8+
---
9+
"Test convert processor with ip type":
10+
- skip:
11+
version: " - 2.13.99"
12+
reason: "introduced in 2.14.0"
13+
- do:
14+
ingest.put_pipeline:
15+
id: "1"
16+
body: >
17+
{
18+
"processors": [
19+
{
20+
"convert" : {
21+
"field" : "raw_ip",
22+
"type": "ip"
23+
}
24+
}
25+
]
26+
}
27+
- match: { acknowledged: true }
28+
29+
- do:
30+
catch: /\[1.1.1.\] is not a valid ipv4\/ipv6 address/
31+
index:
32+
index: test
33+
id: 1
34+
pipeline: "1"
35+
body: {
36+
raw_ip: "1.1.1."
37+
}
38+
39+
- do:
40+
ingest.put_pipeline:
41+
id: "1"
42+
body: >
43+
{
44+
"processors": [
45+
{
46+
"convert" : {
47+
"field" : "raw_ip",
48+
"target_field" : "ip_field",
49+
"type" : "ip",
50+
"ignore_failure" : true
51+
}
52+
}
53+
]
54+
}
55+
- match: { acknowledged: true }
56+
57+
- do:
58+
index:
59+
index: test
60+
id: 1
61+
pipeline: "1"
62+
body: {
63+
raw_ip: "1.1.1."
64+
}
65+
- do:
66+
get:
67+
index: test
68+
id: 1
69+
- match: { _source: { raw_ip: "1.1.1."} }
70+
71+
- do:
72+
index:
73+
index: test
74+
id: 1
75+
pipeline: "1"
76+
body: {
77+
raw_ip: "1.1.1.1"
78+
}
79+
- do:
80+
get:
81+
index: test
82+
id: 1
83+
- match: { _source: { raw_ip: "1.1.1.1", ip_field: "1.1.1.1"} }

0 commit comments

Comments
 (0)