Fix reconstituting version string from components (#117213)

* Fix reconstituting version string from components

Co-authored-by: Joe Gallo <joegallo@gmail.com>
This commit is contained in:
Stanislav Malyshev 2024-12-03 17:32:28 -07:00 committed by GitHub
parent 4a90903192
commit 28eda97ddd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 45 additions and 25 deletions

View file

@ -0,0 +1,6 @@
pr: 117213
summary: Fix reconstituting version string from components
area: Ingest Node
type: bug
issues:
- 116950

View file

@ -9,6 +9,7 @@
package org.elasticsearch.ingest.useragent;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.util.Maps;
import org.elasticsearch.core.UpdateForV10;
import org.elasticsearch.ingest.AbstractProcessor;
@ -95,19 +96,8 @@ public class UserAgentProcessor extends AbstractProcessor {
}
break;
case VERSION:
StringBuilder version = new StringBuilder();
if (uaClient.userAgent() != null && uaClient.userAgent().major() != null) {
version.append(uaClient.userAgent().major());
if (uaClient.userAgent().minor() != null) {
version.append(".").append(uaClient.userAgent().minor());
if (uaClient.userAgent().patch() != null) {
version.append(".").append(uaClient.userAgent().patch());
if (uaClient.userAgent().build() != null) {
version.append(".").append(uaClient.userAgent().build());
}
}
}
uaDetails.put("version", version.toString());
uaDetails.put("version", versionToString(uaClient.userAgent()));
}
break;
case OS:
@ -115,20 +105,10 @@ public class UserAgentProcessor extends AbstractProcessor {
Map<String, String> osDetails = Maps.newMapWithExpectedSize(3);
if (uaClient.operatingSystem().name() != null) {
osDetails.put("name", uaClient.operatingSystem().name());
StringBuilder sb = new StringBuilder();
if (uaClient.operatingSystem().major() != null) {
sb.append(uaClient.operatingSystem().major());
if (uaClient.operatingSystem().minor() != null) {
sb.append(".").append(uaClient.operatingSystem().minor());
if (uaClient.operatingSystem().patch() != null) {
sb.append(".").append(uaClient.operatingSystem().patch());
if (uaClient.operatingSystem().build() != null) {
sb.append(".").append(uaClient.operatingSystem().build());
}
}
}
osDetails.put("version", sb.toString());
osDetails.put("full", uaClient.operatingSystem().name() + " " + sb.toString());
String version = versionToString(uaClient.operatingSystem());
osDetails.put("version", version);
osDetails.put("full", uaClient.operatingSystem().name() + " " + version);
}
uaDetails.put("os", osDetails);
}
@ -160,6 +140,23 @@ public class UserAgentProcessor extends AbstractProcessor {
return ingestDocument;
}
private static String versionToString(final UserAgentParser.VersionedName version) {
final StringBuilder versionString = new StringBuilder();
if (Strings.hasLength(version.major())) {
versionString.append(version.major());
if (Strings.hasLength(version.minor())) {
versionString.append(".").append(version.minor());
if (Strings.hasLength(version.patch())) {
versionString.append(".").append(version.patch());
if (Strings.hasLength(version.build())) {
versionString.append(".").append(version.build());
}
}
}
}
return versionString.toString();
}
@Override
public String getType() {
return TYPE;

View file

@ -345,4 +345,21 @@ public class UserAgentProcessorTests extends ESTestCase {
assertThat(changed, is(false));
assertThat(config, is(Map.of("field", "user-agent")));
}
// From https://github.com/elastic/elasticsearch/issues/116950
@SuppressWarnings("unchecked")
public void testFirefoxVersion() {
Map<String, Object> document = new HashMap<>();
document.put("source_field", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0");
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random(), document);
processor.execute(ingestDocument);
Map<String, Object> data = ingestDocument.getSourceAndMetadata();
assertThat(data, hasKey("target_field"));
Map<String, Object> target = (Map<String, Object>) data.get("target_field");
assertThat(target.get("name"), is("Firefox"));
assertThat(target.get("version"), is("128.0"));
}
}