Skip to content

Commit a074337

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

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.logging.DeprecationCategory;
1314
import org.elasticsearch.common.logging.DeprecationLogger;
1415
import org.elasticsearch.common.util.Maps;
@@ -98,40 +99,19 @@ public IngestDocument execute(IngestDocument ingestDocument) {
9899
}
99100
break;
100101
case VERSION:
101-
StringBuilder version = new StringBuilder();
102102
if (uaClient.userAgent() != null && uaClient.userAgent().major() != null) {
103-
version.append(uaClient.userAgent().major());
104-
if (uaClient.userAgent().minor() != null) {
105-
version.append(".").append(uaClient.userAgent().minor());
106-
if (uaClient.userAgent().patch() != null) {
107-
version.append(".").append(uaClient.userAgent().patch());
108-
if (uaClient.userAgent().build() != null) {
109-
version.append(".").append(uaClient.userAgent().build());
110-
}
111-
}
112-
}
113-
uaDetails.put("version", version.toString());
103+
uaDetails.put("version", versionToString(uaClient.userAgent()));
114104
}
115105
break;
116106
case OS:
117107
if (uaClient.operatingSystem() != null) {
118108
Map<String, String> osDetails = Maps.newMapWithExpectedSize(3);
119109
if (uaClient.operatingSystem().name() != null) {
120110
osDetails.put("name", uaClient.operatingSystem().name());
121-
StringBuilder sb = new StringBuilder();
122111
if (uaClient.operatingSystem().major() != null) {
123-
sb.append(uaClient.operatingSystem().major());
124-
if (uaClient.operatingSystem().minor() != null) {
125-
sb.append(".").append(uaClient.operatingSystem().minor());
126-
if (uaClient.operatingSystem().patch() != null) {
127-
sb.append(".").append(uaClient.operatingSystem().patch());
128-
if (uaClient.operatingSystem().build() != null) {
129-
sb.append(".").append(uaClient.operatingSystem().build());
130-
}
131-
}
132-
}
133-
osDetails.put("version", sb.toString());
134-
osDetails.put("full", uaClient.operatingSystem().name() + " " + sb.toString());
112+
String version = versionToString(uaClient.operatingSystem());
113+
osDetails.put("version", version);
114+
osDetails.put("full", uaClient.operatingSystem().name() + " " + version);
135115
}
136116
uaDetails.put("os", osDetails);
137117
}
@@ -163,6 +143,23 @@ public IngestDocument execute(IngestDocument ingestDocument) {
163143
return ingestDocument;
164144
}
165145

146+
private static String versionToString(final UserAgentParser.VersionedName version) {
147+
final StringBuilder versionString = new StringBuilder();
148+
if (Strings.hasLength(version.major())) {
149+
versionString.append(version.major());
150+
if (Strings.hasLength(version.minor())) {
151+
versionString.append(".").append(version.minor());
152+
if (Strings.hasLength(version.patch())) {
153+
versionString.append(".").append(version.patch());
154+
if (Strings.hasLength(version.build())) {
155+
versionString.append(".").append(version.build());
156+
}
157+
}
158+
}
159+
}
160+
return versionString.toString();
161+
}
162+
166163
@Override
167164
public String getType() {
168165
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
@@ -331,4 +331,21 @@ public void testExtractDeviceTypeDisabled() {
331331
device.put("name", "Other");
332332
assertThat(target.get("device"), is(device));
333333
}
334+
335+
// From https://github.com/elastic/elasticsearch/issues/116950
336+
@SuppressWarnings("unchecked")
337+
public void testFirefoxVersion() {
338+
Map<String, Object> document = new HashMap<>();
339+
document.put("source_field", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0");
340+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
341+
342+
processor.execute(ingestDocument);
343+
Map<String, Object> data = ingestDocument.getSourceAndMetadata();
344+
345+
assertThat(data, hasKey("target_field"));
346+
Map<String, Object> target = (Map<String, Object>) data.get("target_field");
347+
348+
assertThat(target.get("name"), is("Firefox"));
349+
assertThat(target.get("version"), is("128.0"));
350+
}
334351
}

0 commit comments

Comments
 (0)