diff --git a/pom.xml b/pom.xml index 177c0032da..e448fb2bb2 100644 --- a/pom.xml +++ b/pom.xml @@ -440,6 +440,16 @@ + + + src/main/resources + + + + src/main/resources-filtered + true + + io.quarkus.platform diff --git a/src/main/java/io/github/hectorvent/floci/config/FlociVersion.java b/src/main/java/io/github/hectorvent/floci/config/FlociVersion.java new file mode 100644 index 0000000000..fdc68d3fe8 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/config/FlociVersion.java @@ -0,0 +1,65 @@ +package io.github.hectorvent.floci.config; + +import org.jboss.logging.Logger; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +/** + * The version this Floci build reports on {@code /health}, {@code /_floci/health} and in its + * certificate metadata. + * + *

{@code FLOCI_VERSION} wins when set, so an image can still override it. Otherwise the + * version stamped into {@code floci/version.properties} by the Maven build is used, which is what + * a standalone native binary reports. {@code "dev"} remains only for a build that carries no + * stamped version, such as an IDE run without resource processing. + * + *

Plain static code on purpose: {@link TlsConfigSource} runs while the configuration is still + * being assembled, before CDI or {@code ConfigProvider} can be used. + */ +public final class FlociVersion { + + private static final Logger LOG = Logger.getLogger(FlociVersion.class); + + static final String RESOURCE = "floci/version.properties"; + static final String FALLBACK = "dev"; + + private static final String BUILD_VERSION = loadBuildVersion(); + + private FlociVersion() { + } + + public static String current() { + return resolve(System.getenv("FLOCI_VERSION"), BUILD_VERSION); + } + + static String resolve(String override, String buildVersion) { + if (override != null && !override.isBlank()) { + return override; + } + if (buildVersion != null && !buildVersion.isBlank()) { + return buildVersion; + } + return FALLBACK; + } + + private static String loadBuildVersion() { + try (InputStream in = FlociVersion.class.getClassLoader().getResourceAsStream(RESOURCE)) { + if (in == null) { + return null; + } + Properties properties = new Properties(); + properties.load(in); + String version = properties.getProperty("version"); + // An unfiltered copy (resources not processed by Maven) still holds the placeholder. + if (version == null || version.startsWith("${")) { + return null; + } + return version.trim(); + } catch (IOException e) { + LOG.debugv("Could not read {0}: {1}", RESOURCE, e.getMessage()); + return null; + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java b/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java index 43aebec9d5..cdc2124538 100644 --- a/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java +++ b/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java @@ -206,7 +206,7 @@ public class TlsCertificateManager implements Resettable { CertificateGenerator.GeneratedCertificate issued = ca.issueServerCertificate("localhost", sans, keyAlgorithm, keyPair); Path dir = tlsDir(); - CertificateMetadata metadata = CertificateMetadata.create(configuredHostnames, resolveFlociVersion()); + CertificateMetadata metadata = CertificateMetadata.create(configuredHostnames, FlociVersion.current()); metadata.setLearnedHostnames(new ArrayList<>(learned)); writeAtomically(dir.resolve(CERT_NAME), issued.certificatePem()); writeAtomically(dir.resolve(METADATA_NAME), OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(metadata)); @@ -302,9 +302,4 @@ public class TlsCertificateManager implements Resettable { Files.deleteIfExists(tmp); } } - - private static String resolveFlociVersion() { - String env = System.getenv("FLOCI_VERSION"); - return env == null || env.isBlank() ? "dev" : env; - } } diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java index 04df6f244f..49eb361684 100644 --- a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java +++ b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java @@ -533,7 +533,7 @@ public class TlsConfigSource implements ConfigSource { private void persistMetadata(Path tlsDir, List hostnames, List learnedHostnames) { Path metadataFile = tlsDir.resolve(SERVER_METADATA_NAME); try { - String version = resolveFlociVersion(); + String version = FlociVersion.current(); CertificateMetadata metadata = CertificateMetadata.create(hostnames, version); metadata.setLearnedHostnames(learnedHostnames); @@ -547,19 +547,6 @@ public class TlsConfigSource implements ConfigSource { } } - /** - * Resolves the Floci version from environment variable or defaults to "dev". - * - * @return Floci version string - */ - private static String resolveFlociVersion() { - String env = System.getenv("FLOCI_VERSION"); - if (env != null && !env.isBlank()) { - return env; - } - return "dev"; - } - /** * Checks if the hostname configuration has changed since the last certificate generation. * Loads the metadata file and compares the hostnames with the current configuration. diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java index 8820dd75c0..cc071ca274 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoController.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.lifecycle; import io.github.hectorvent.floci.config.ContainerCaBundle; import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.config.FlociCertificateAuthority; +import io.github.hectorvent.floci.config.FlociVersion; import io.github.hectorvent.floci.core.common.ContainerTeardown; import io.github.hectorvent.floci.core.common.ContainerTeardowns; import io.github.hectorvent.floci.core.common.Resettable; @@ -63,7 +64,7 @@ public class EmulatorInfoController { this.certificateAuthority = certificateAuthority; this.config = config; this.dynamoDbBackendSelector = dynamoDbBackendSelector; - this.version = resolveVersion(); + this.version = FlociVersion.current(); } @GET @@ -207,12 +208,4 @@ public class EmulatorInfoController { throw failure; } } - - static String resolveVersion() { - String env = System.getenv("FLOCI_VERSION"); - if (env != null && !env.isBlank()) { - return env; - } - return "dev"; - } } diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/HealthController.java b/src/main/java/io/github/hectorvent/floci/lifecycle/HealthController.java index 88c666b7b6..dcb46a10fa 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/HealthController.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/HealthController.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.lifecycle; +import io.github.hectorvent.floci.config.FlociVersion; import io.github.hectorvent.floci.core.common.ServiceRegistry; import jakarta.inject.Inject; import jakarta.ws.rs.GET; @@ -20,7 +21,7 @@ public class HealthController { @Inject public HealthController(ServiceRegistry serviceRegistry) { this.serviceRegistry = serviceRegistry; - this.version = resolveVersion(); + this.version = FlociVersion.current(); } @GET @@ -31,12 +32,4 @@ public class HealthController { "original_edition", "floci-always-free", "version", version)).build(); } - - static String resolveVersion() { - String env = System.getenv("FLOCI_VERSION"); - if (env != null && !env.isBlank()) { - return env; - } - return "dev"; - } } diff --git a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyTlsCertificates.java b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyTlsCertificates.java index d9c97bd407..4131ac9fda 100644 --- a/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyTlsCertificates.java +++ b/src/main/java/io/github/hectorvent/floci/services/rds/proxy/RdsProxyTlsCertificates.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.rds.proxy; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.config.CertificateMetadata; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.FlociVersion; import io.github.hectorvent.floci.core.common.Pem; import io.github.hectorvent.floci.services.acm.CertificateGenerator; import io.github.hectorvent.floci.services.acm.model.KeyAlgorithm; @@ -225,13 +226,8 @@ public class RdsProxyTlsCertificates { } private void writeMetadata(Path metadataFile, List sans) throws IOException { - CertificateMetadata metadata = CertificateMetadata.create(sans, resolveFlociVersion()); + CertificateMetadata metadata = CertificateMetadata.create(sans, FlociVersion.current()); String json = OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(metadata); Files.writeString(metadataFile, json); } - - private static String resolveFlociVersion() { - String env = System.getenv("FLOCI_VERSION"); - return (env != null && !env.isBlank()) ? env : "dev"; - } } diff --git a/src/main/resources-filtered/floci/version.properties b/src/main/resources-filtered/floci/version.properties new file mode 100644 index 0000000000..defbd48204 --- /dev/null +++ b/src/main/resources-filtered/floci/version.properties @@ -0,0 +1 @@ +version=${project.version} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9e9f081f21..56849e3fc4 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -76,6 +76,7 @@ quarkus: - cloudformation/*.json - redshift/*.sql - i18n/*.properties + - floci/version.properties - org/apache/velocity/runtime/defaults/*.properties # DriverManager finds the JDBC drivers through ServiceLoader, and the MySQL # driver reads its time zone table at connect time. diff --git a/src/test/java/io/github/hectorvent/floci/config/FlociVersionTest.java b/src/test/java/io/github/hectorvent/floci/config/FlociVersionTest.java new file mode 100644 index 0000000000..a96814b6a9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/config/FlociVersionTest.java @@ -0,0 +1,36 @@ +package io.github.hectorvent.floci.config; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.matchesPattern; +import static org.hamcrest.Matchers.not; + +class FlociVersionTest { + + @Test + void overrideWinsOverBuildVersion() { + assertThat(FlociVersion.resolve("nightly-10152026", "2.3.0"), equalTo("nightly-10152026")); + } + + @Test + void blankOverrideFallsBackToBuildVersion() { + assertThat(FlociVersion.resolve(" ", "2.3.0"), equalTo("2.3.0")); + assertThat(FlociVersion.resolve(null, "2.3.0"), equalTo("2.3.0")); + } + + @Test + void devOnlyWhenNoVersionIsKnown() { + assertThat(FlociVersion.resolve(null, null), equalTo("dev")); + assertThat(FlociVersion.resolve("", " "), equalTo("dev")); + } + + @Test + void buildStampsTheProjectVersion() { + // Only meaningful when FLOCI_VERSION is unset, which is how the suite runs. + String version = FlociVersion.current(); + assertThat(version, not(equalTo("dev"))); + assertThat(version, matchesPattern("\\d+\\.\\d+\\.\\d+.*")); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceMetadataPersistenceTest.java b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceMetadataPersistenceTest.java index 39ffe437f6..ebc582fdb5 100644 --- a/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceMetadataPersistenceTest.java +++ b/src/test/java/io/github/hectorvent/floci/config/TlsConfigSourceMetadataPersistenceTest.java @@ -179,8 +179,8 @@ class TlsConfigSourceMetadataPersistenceTest { assertNotNull(metadata.getFlociVersion(), "Metadata should contain flociVersion"); - assertEquals("dev", metadata.getFlociVersion(), - "Version should default to 'dev' when FLOCI_VERSION not set"); + assertEquals(FlociVersion.current(), metadata.getFlociVersion(), + "Metadata should record the version this build reports"); } /** diff --git a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerIntegrationTest.java index 5776099ea4..45d82fd359 100644 --- a/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/lifecycle/EmulatorInfoControllerIntegrationTest.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.lifecycle; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.config.FlociVersion; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.ValidatableResponse; @@ -57,7 +58,7 @@ class EmulatorInfoControllerIntegrationTest { JsonNode tree = MAPPER.readTree(body); assertEquals("community", tree.get("edition").asText()); assertEquals("floci-always-free", tree.get("original_edition").asText()); - assertEquals("dev", tree.get("version").asText()); + assertEquals(FlociVersion.current(), tree.get("version").asText()); JsonNode services = tree.get("services"); assertNotNull(services, "services field must be present"); @@ -94,7 +95,7 @@ class EmulatorInfoControllerIntegrationTest { .statusCode(200) .contentType("application/json") .body("edition", equalTo("community")) - .body("version", notNullValue()) + .body("version", allOf(equalTo(FlociVersion.current()), not(equalTo("dev")))) .body("dynamodb_backend", equalTo("native")); }