Skip to content

Commit 28eda97

Browse files
smalyshevjoegallo
andauthored
Fix reconstituting version string from components (#117213)
* Fix reconstituting version string from components Co-authored-by: Joe Gallo <[email protected]>
1 parent 4a90903 commit 28eda97

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

docs/changelog/117213.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 117213
2+
summary: Fix reconstituting version string from components
3+
area: Ingest Node
4+
type: bug
5+
issues:
6+
- 116950

modules/ingest-user-agent/src/main/java/org/elasticsearch/ingest/useragent/UserAgentProcessor.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
package org.elasticsearch.ingest.useragent;
1111

12+
import org.elasticsearch.common.Strings;
1213
import org.elasticsearch.common.util.Maps;
1314
import org.elasticsearch.core.UpdateForV10;
1415
import org.elasticsearch.ingest.AbstractProcessor;
@@ -95,40 +96,19 @@ public IngestDocument execute(IngestDocument ingestDocument) {
9596
}
9697
break;
9798
case VERSION:
98-
StringBuilder version = new StringBuilder();
9999
if (uaClient.userAgent() != null && uaClient.userAgent().major() != null) {
100-
version.append(uaClient.userAgent().major());
101-
if (uaClient.userAgent().minor() != null) {
102-
version.append(".").append(uaClient.userAgent().minor());
103-
if (uaClient.userAgent().patch() != null) {
104-
version.append(".").append(uaClient.userAgent().patch());
105-
if (uaClient.userAgent().build() != null) {
106-
version.append(".").append(uaClient.userAgent().build());
107-
}
108-
}
109-
}
110-
uaDetails.put("version", version.toString());
100+
uaDetails.put("version", versionToString(uaClient.userAgent()));
111101
}
112102
break;
113103
case OS:
114104
if (uaClient.operatingSystem() != null) {
115105
Map<String, String> osDetails = Maps.newMapWithExpectedSize(3);
116106
if (uaClient.operatingSystem().name() != null) {
117107
osDetails.put("name", uaClient.operatingSystem().name());
118-
StringBuilder sb = new StringBuilder();
119108
if (uaClient.operatingSystem().major() != null) {
120-
sb.append(uaClient.operatingSystem().major());
121-
if (uaClient.operatingSystem().minor() != null) {
122-
sb.append(".").append(uaClient.operatingSystem().minor());
123-
if (uaClient.operatingSystem().patch() != null) {
124-
sb.append(".").append(uaClient.operatingSystem().patch());
125-
if (uaClient.operatingSystem().build() != null) {
126-
sb.append(".").append(uaClient.operatingSystem().build());
127-
}
128-
}
129-
}
130-
osDetails.put("version", sb.toString());
131-
osDetails.put("full", uaClient.operatingSystem().name() + " " + sb.toString());
109+
String version = versionToString(uaClient.operatingSystem());
110+
osDetails.put("version", version);
111+
osDetails.put("full", uaClient.operatingSystem().name() + " " + version);
132112
}
133113
uaDetails.put("os", osDetails);
134114
}
@@ -160,6 +140,23 @@ public IngestDocument execute(IngestDocument ingestDocument) {
160140
return ingestDocument;
161141
}
162142

143+
private static String versionToString(final UserAgentParser.VersionedName version) {
144+
final StringBuilder versionString = new StringBuilder();
145+
if (Strings.hasLength(version.major())) {
146+
versionString.append(version.major());
147+
if (Strings.hasLength(version.minor())) {
148+
versionString.append(".").append(version.minor());
149+
if (Strings.hasLength(version.patch())) {
150+
versionString.append(".").append(version.patch());
151+
if (Strings.hasLength(version.build())) {
152+
versionString.append(".").append(version.build());
153+
}
154+
}
155+
}
156+
}
157+
return versionString.toString();
158+
}
159+
163160
@Override
164161
public String getType() {
165162
return TYPE;

modules/ingest-user-agent/src/test/java/org/elasticsearch/ingest/useragent/UserAgentProcessorTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,21 @@ public void testMaybeUpgradeConfig_doesNothingIfEcsAbsent() {
345345
assertThat(changed, is(false));
346346
assertThat(config, is(Map.of("field", "user-agent")));
347347
}
348+
349+
// From https://github.com/elastic/elasticsearch/issues/116950
350+
@SuppressWarnings("unchecked")
351+
public void testFirefoxVersion() {
352+
Map<String, Object> document = new HashMap<>();
353+
document.put("source_field", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0");
354+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
355+
356+
processor.execute(ingestDocument);
357+
Map<String, Object> data = ingestDocument.getSourceAndMetadata();
358+
359+
assertThat(data, hasKey("target_field"));
360+
Map<String, Object> target = (Map<String, Object>) data.get("target_field");
361+
362+
assertThat(target.get("name"), is("Firefox"));
363+
assertThat(target.get("version"), is("128.0"));
364+
}
348365
}

0 commit comments

Comments
 (0)