From 6d29d3d5a29dbcd513b15c9f89b5666cabb32d34 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 17 Oct 2024 10:15:20 -0400 Subject: [PATCH 1/3] fix(semver): handle unparseable version strings by returning UNKNOWN instead of throwing --- .../java/io/cryostat/agent/VersionInfo.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/cryostat/agent/VersionInfo.java b/src/main/java/io/cryostat/agent/VersionInfo.java index b6f98aee..f86c1918 100644 --- a/src/main/java/io/cryostat/agent/VersionInfo.java +++ b/src/main/java/io/cryostat/agent/VersionInfo.java @@ -24,6 +24,9 @@ import io.cryostat.agent.util.ResourcesUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class VersionInfo { private static final String RESOURCE_LOCATION = "versions.properties"; @@ -80,6 +83,8 @@ public boolean validateServerVersion(Semver actual) { public static class Semver implements Comparable { + private static Logger log = LoggerFactory.getLogger(Semver.class); + public static final Semver UNKNOWN = new Semver(0, 0, 0) { @Override @@ -103,10 +108,15 @@ public static Semver fromString(String in) { if (parts.length != 3) { throw new IllegalArgumentException(); } - return new Semver( - Integer.parseInt(parts[0]), - Integer.parseInt(parts[1]), - Integer.parseInt(parts[2])); + try { + return new Semver( + Integer.parseInt(parts[0]), + Integer.parseInt(parts[1]), + Integer.parseInt(parts[2])); + } catch (NumberFormatException nfe) { + log.error(String.format("Unable to parse input string \"%s\"", in), nfe); + return UNKNOWN; + } } public int getMajor() { From 3787dfb9cff7061b45adb7e05b2484fb35dccb4e Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 17 Oct 2024 10:21:08 -0400 Subject: [PATCH 2/3] refactor --- .../java/io/cryostat/agent/VersionInfo.java | 22 +++++++++++++------ .../io/cryostat/agent/model/ServerHealth.java | 17 +------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/java/io/cryostat/agent/VersionInfo.java b/src/main/java/io/cryostat/agent/VersionInfo.java index f86c1918..84564fc6 100644 --- a/src/main/java/io/cryostat/agent/VersionInfo.java +++ b/src/main/java/io/cryostat/agent/VersionInfo.java @@ -21,6 +21,8 @@ import java.util.Map; import java.util.Objects; import java.util.Properties; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import io.cryostat.agent.util.ResourcesUtil; @@ -83,6 +85,11 @@ public boolean validateServerVersion(Semver actual) { public static class Semver implements Comparable { + public static final Pattern VERSION_PATTERN = + Pattern.compile( + "^v?(?[\\d]+)\\.(?[\\d]+)\\.(?[\\d]+)(?:-[a-z0-9\\._-]*)?", + Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); + private static Logger log = LoggerFactory.getLogger(Semver.class); public static final Semver UNKNOWN = @@ -104,15 +111,16 @@ public Semver(int major, int minor, int patch) { } public static Semver fromString(String in) { - String[] parts = in.split("\\."); - if (parts.length != 3) { - throw new IllegalArgumentException(); - } try { + Matcher m = VERSION_PATTERN.matcher(in); + if (!m.matches()) { + log.warn("Input version \"{}\" did not match expected semver pattern", in); + return UNKNOWN; + } return new Semver( - Integer.parseInt(parts[0]), - Integer.parseInt(parts[1]), - Integer.parseInt(parts[2])); + Integer.parseInt(m.group("major")), + Integer.parseInt(m.group("minor")), + Integer.parseInt(m.group("patch"))); } catch (NumberFormatException nfe) { log.error(String.format("Unable to parse input string \"%s\"", in), nfe); return UNKNOWN; diff --git a/src/main/java/io/cryostat/agent/model/ServerHealth.java b/src/main/java/io/cryostat/agent/model/ServerHealth.java index 2ec06f70..9e7fa219 100644 --- a/src/main/java/io/cryostat/agent/model/ServerHealth.java +++ b/src/main/java/io/cryostat/agent/model/ServerHealth.java @@ -15,9 +15,6 @@ */ package io.cryostat.agent.model; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - import io.cryostat.agent.VersionInfo.Semver; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -25,11 +22,6 @@ @SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) public class ServerHealth { - private static final Pattern VERSION_PATTERN = - Pattern.compile( - "^v(?[\\d]+)\\.(?[\\d]+)\\.(?[\\d]+)(?:-[a-z0-9\\._-]*)?", - Pattern.CASE_INSENSITIVE | Pattern.MULTILINE); - private String cryostatVersion; private BuildInfo build; @@ -53,14 +45,7 @@ public String cryostatVersion() { } public Semver cryostatSemver() { - Matcher m = VERSION_PATTERN.matcher(cryostatVersion()); - if (!m.matches()) { - return Semver.fromString("0.0.0"); - } - return new Semver( - Integer.parseInt(m.group("major")), - Integer.parseInt(m.group("minor")), - Integer.parseInt(m.group("patch"))); + return Semver.fromString(cryostatVersion()); } public BuildInfo buildInfo() { From 117395f16e7f4304dd67b327bc64471e0a138d81 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Thu, 17 Oct 2024 10:23:47 -0400 Subject: [PATCH 3/3] add test cases --- src/test/java/io/cryostat/agent/SemverTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/io/cryostat/agent/SemverTest.java b/src/test/java/io/cryostat/agent/SemverTest.java index 74e8ef90..6316e88f 100644 --- a/src/test/java/io/cryostat/agent/SemverTest.java +++ b/src/test/java/io/cryostat/agent/SemverTest.java @@ -35,6 +35,9 @@ public class SemverTest { "2.0.0, 1.1.0, 1", "1.0.1, 1.1.0, -1", "1.1.1, 1.0.1, 1", + "1.0.0-SNAPSHOT, 1.0.0, 0", + "v1.0.0-SNAPSHOT, 1.0.0, 0", + "v1.0.0, 1.0.0, 0", }) public void test(String first, String second, int result) { Semver a = Semver.fromString(first);