diff --git a/.gitea/workflows/publish-auth.yml b/.gitea/workflows/publish-auth.yml index c481002..7660a1e 100644 --- a/.gitea/workflows/publish-auth.yml +++ b/.gitea/workflows/publish-auth.yml @@ -52,6 +52,7 @@ jobs: BRANCH="$(echo "$BRANCH" | tr '[:upper:]' '[:lower:]')" echo "branch=$BRANCH" >> "$GITHUB_OUTPUT" echo "sha=${GITHUB_SHA::12}" >> "$GITHUB_OUTPUT" + echo "build_time=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> "$GITHUB_OUTPUT" - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -69,6 +70,9 @@ jobs: with: context: . push: true + build-args: | + GIT_SHA=${{ github.sha }} + BUILD_TIME=${{ steps.prep.outputs.build_time }} tags: | git.andreas-dahm.eu/lobo_corp/tbp-auth-service:${{ steps.prep.outputs.branch }} git.andreas-dahm.eu/lobo_corp/tbp-auth-service:${{ steps.prep.outputs.branch }}-${{ steps.prep.outputs.sha }} diff --git a/Dockerfile b/Dockerfile index 4de8f6a..51c3379 100644 --- a/Dockerfile +++ b/Dockerfile @@ -25,6 +25,11 @@ FROM eclipse-temurin:25-jre-alpine WORKDIR /app +# Passed by the CI workflow; shown by GET /api/version +ARG GIT_SHA=dev +ARG BUILD_TIME= +ENV APP_BUILD_COMMIT=$GIT_SHA APP_BUILD_TIME=$BUILD_TIME + # Expose the port your Spring Boot app runs on EXPOSE 8080 diff --git a/readme.md b/readme.md index fcc63e3..2daee34 100644 --- a/readme.md +++ b/readme.md @@ -56,6 +56,7 @@ To Be Played is split into three repositories: | `PUT` | `/api/users/settings/profile-stats` (`{groups}`: `PILE`, `TOP_GAMES`, `TASTE`, `HABITS`, `FINISHED`, `TREND`; `204`) | access token | | `GET` | `/api/public/profiles/{displayName}` | public | | `GET` | `/api/stats/public` | public | +| `GET` | `/api/version` (`{commit, builtAt}` of the running image, from the CI build; `dev` locally) | public | Endpoints use Spring's API versioning with a query parameter: `?version=1.0` (default `1.0`). @@ -80,6 +81,7 @@ layers: | Persistence | `persistence` | JPA entities, Spring Data repositories, `EntityMapper`. Entities never reach the API layer. | `profile` and `stats` only read data. They have no entities and use `JdbcTemplate` queries in a `repo` package. +`version` has only an `api` package: `GET /api/version` answers the commit and build time of the running image. ArchUnit enforces these rules in `src/test/java/auth_service/architecture/ArchitectureTest.java`, so a layer violation fails the build. It also keeps the modules independent of each other and free of cycles. Test base diff --git a/src/main/java/auth_service/configuration/security/SecurityConfig.java b/src/main/java/auth_service/configuration/security/SecurityConfig.java index 14c22c6..c5686f1 100644 --- a/src/main/java/auth_service/configuration/security/SecurityConfig.java +++ b/src/main/java/auth_service/configuration/security/SecurityConfig.java @@ -44,6 +44,7 @@ public class SecurityConfig { "/api/steam-link/callback", // Steam's redirect back; bound to the browser by a cookie, see SteamLinkService "/actuator/health", // Docker health check; details are off "/.well-known/jwks.json", // Public verify keys for Core + "/api/version", // Build of the running image, shown in the settings "/error" // Default Spring Boot error page routing }; diff --git a/src/main/java/auth_service/modules/version/api/VersionController.java b/src/main/java/auth_service/modules/version/api/VersionController.java new file mode 100644 index 0000000..fbc03f8 --- /dev/null +++ b/src/main/java/auth_service/modules/version/api/VersionController.java @@ -0,0 +1,22 @@ +package auth_service.modules.version.api; + +import auth_service.modules.version.api.dto.VersionDto; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class VersionController { + + private final VersionDto version; + + public VersionController(@Value("${app.build.commit}") String commit, @Value("${app.build.time}") String builtAt) { + this.version = new VersionDto(commit.isBlank() ? "dev" : commit, builtAt.isBlank() ? null : builtAt); + } + + @GetMapping("/api/version") + public ResponseEntity getVersion() { + return ResponseEntity.ok(version); + } +} diff --git a/src/main/java/auth_service/modules/version/api/dto/VersionDto.java b/src/main/java/auth_service/modules/version/api/dto/VersionDto.java new file mode 100644 index 0000000..b11a553 --- /dev/null +++ b/src/main/java/auth_service/modules/version/api/dto/VersionDto.java @@ -0,0 +1,7 @@ +package auth_service.modules.version.api.dto; + +/** + * @param commit full git hash the image was built from, "dev" outside the CI build + * @param builtAt ISO-8601 UTC build time, null outside the CI build + */ +public record VersionDto(String commit, String builtAt) {} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 534fcf0..e2412bd 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -45,6 +45,10 @@ security: # Custom properties for CORS app: + build: + # Filled from the CI run by the Dockerfile. + commit: ${APP_BUILD_COMMIT:dev} + time: ${APP_BUILD_TIME:} cors: # Production default: strictly allow only the frontend domain allowed-origins: "https://tbp.andreas-dahm.eu" diff --git a/src/test/java/auth_service/configuration/security/PublicEndpointsTest.java b/src/test/java/auth_service/configuration/security/PublicEndpointsTest.java index b118b55..4c8d758 100644 --- a/src/test/java/auth_service/configuration/security/PublicEndpointsTest.java +++ b/src/test/java/auth_service/configuration/security/PublicEndpointsTest.java @@ -42,4 +42,11 @@ class PublicEndpointsTest extends AbstractIntegrationTest { int status = mockMvc.perform(get("/actuator/prometheus")).andReturn().getResponse().getStatus(); assertTrue(Set.of(401, 403).contains(status), "/actuator/prometheus answered " + status); } + + @Test + void versionIsPublic() throws Exception { + mockMvc.perform(get("/api/version")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.commit").isString()); + } } diff --git a/src/test/java/auth_service/modules/version/api/VersionControllerTest.java b/src/test/java/auth_service/modules/version/api/VersionControllerTest.java new file mode 100644 index 0000000..d5113c9 --- /dev/null +++ b/src/test/java/auth_service/modules/version/api/VersionControllerTest.java @@ -0,0 +1,24 @@ +package auth_service.modules.version.api; + +import auth_service.modules.version.api.dto.VersionDto; +import auth_service.shared.test.AbstractUnitTest; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class VersionControllerTest extends AbstractUnitTest { + + @Test + void answersTheBuildOfTheImage() { + VersionDto version = new VersionController("4ec47f1aa0b1", "2026-09-27T08:05:00Z").getVersion().getBody(); + + assertEquals(new VersionDto("4ec47f1aa0b1", "2026-09-27T08:05:00Z"), version); + } + + @Test + void emptyBuildArgsCountAsLocalBuild() { + VersionDto version = new VersionController("", "").getVersion().getBody(); + + assertEquals(new VersionDto("dev", null), version); + } +}