diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2773e39..e8d0969 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -84,17 +84,33 @@ jobs: - name: Smoke test container shell: bash run: | + config_repo_fixture="$(mktemp -d)" + cp -R src/test/resources/config-repo/. "${config_repo_fixture}/" + git -C "${config_repo_fixture}" init --initial-branch=main + git -C "${config_repo_fixture}" config user.name "Config Server CI" + git -C "${config_repo_fixture}" config user.email "config-server-ci@localhost" + git -C "${config_repo_fixture}" add . + git -C "${config_repo_fixture}" commit --message "Create CI configuration fixture" + chmod -R a+rwX "${config_repo_fixture}" + + cleanup() { + docker rm --force config-server-ci > /dev/null 2>&1 || true + rm -rf "${config_repo_fixture}" + } + trap cleanup EXIT + docker run --detach \ --name config-server-ci \ --publish 8888:8888 \ + --env CONFIG_REPO_URI=file:/platform-config \ + --env CONFIG_REPO_DEFAULT_LABEL=main \ + --volume "${config_repo_fixture}:/platform-config" \ digital-bank-java/config-server:ci - trap 'docker rm --force config-server-ci' EXIT - ready=false for attempt in {1..30}; do if curl --fail --silent \ - http://localhost:8888/actuator/health > /dev/null; then + http://localhost:8888/actuator/health/liveness > /dev/null; then ready=true break fi @@ -107,5 +123,5 @@ jobs: fi curl --fail --silent \ - http://localhost:8888/customer-service/local \ - | grep --quiet '"name":"customer-service"' + http://localhost:8888/test-service/default \ + | grep --quiet '"test.message":"config-server-integration-test"' diff --git a/Dockerfile b/Dockerfile index a073e21..0058ea9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -17,11 +17,11 @@ RUN groupadd --gid 10001 spring \ WORKDIR /app +ENV XDG_CONFIG_HOME=/tmp/.config + COPY --from=builder --chown=10001:10001 \ /workspace/target/config-server-*.jar app.jar -COPY --chown=10001:10001 config-repo/ config-repo/ - USER 10001:10001 EXPOSE 8888 diff --git a/README.md b/README.md index 8167217..b2205e3 100644 --- a/README.md +++ b/README.md @@ -17,23 +17,24 @@ Centralized configuration service for the Digital Bank Java platform, built with ## Configuration Model -The service currently uses the Spring Cloud Config native backend and reads configuration from the local `config-repo` directory. +The service uses the Spring Cloud Config Git backend. Client configuration is owned by the separate `platform-config` repository so configuration can change independently of the Config Server image. ### Config Server Runtime Configuration `src/main/resources/application.yml` configures the Config Server process itself, including: - HTTP port. -- Active Spring profile. -- Native configuration search locations. +- Git repository URI and default branch. +- Service-specific configuration search paths. +- Git authentication supplied through runtime environment variables. - Actuator endpoint exposure. ### Client Configuration Repository -`config-repo` contains configuration served to client services: +The sibling `platform-config` repository contains configuration served to client services: ```text -config-repo/ +platform-config/ ├── application.yml ├── application-local.yml └── customer-service/ @@ -68,7 +69,15 @@ helm version --short ## Run Locally -Run commands from the repository root so the native backend can resolve `./config-repo`. +Clone `platform-config` beside this repository. The default local repository URI is `file:../platform-config`: + +```text +digital-bank-java/ +├── config-server/ +└── platform-config/ +``` + +Run commands from the `config-server` repository root. Execute the test suite: @@ -116,13 +125,13 @@ curl --fail http://localhost:8888/customer-service/local The local response combines configuration from: ```text -config-repo/customer-service/customer-service-local.yml -config-repo/application-local.yml -config-repo/customer-service/customer-service.yml -config-repo/application.yml +platform-config/customer-service/customer-service-local.yml +platform-config/application-local.yml +platform-config/customer-service/customer-service.yml +platform-config/application.yml ``` -More specific profile and service configuration takes precedence over shared defaults. +More specific profile and service configuration takes precedence over shared defaults. The response `version` identifies the exact `platform-config` Git commit used. ## Run With Docker @@ -130,18 +139,29 @@ Build the image from the repository root: ```bash docker build \ - --tag digital-bank-java/config-server:0.0.1 \ + --tag digital-bank-java/config-server:0.0.2 \ . ``` -Run the container as a non-root user: +Create a disposable writable copy of the local configuration repository. JGit requires write access for checkout metadata, so the real working repository is not mounted directly: + +```bash +CONFIG_REPO_FIXTURE="$(mktemp -d)" +cp -R ../platform-config/. "$CONFIG_REPO_FIXTURE/" +chmod -R a+rwX "$CONFIG_REPO_FIXTURE" +``` + +Run the container as a non-root user with the fixture mounted as its Git repository: ```bash docker run --detach \ --rm \ --name digital-bank-java-config-server \ --publish 8888:8888 \ - digital-bank-java/config-server:0.0.1 + --env CONFIG_REPO_URI=file:/platform-config \ + --env CONFIG_REPO_DEFAULT_LABEL=main \ + --volume "$CONFIG_REPO_FIXTURE:/platform-config" \ + digital-bank-java/config-server:0.0.2 ``` Verify the container and stop it: @@ -150,9 +170,11 @@ Verify the container and stop it: curl --fail http://localhost:8888/actuator/health curl --fail http://localhost:8888/customer-service/local docker stop digital-bank-java-config-server +test -n "$CONFIG_REPO_FIXTURE" && rm -rf "$CONFIG_REPO_FIXTURE" +unset CONFIG_REPO_FIXTURE ``` -The runtime image uses the numeric non-root user `10001:10001`. The native `config-repo` directory is included in the image only for the current local configuration phase. +The runtime image uses the numeric non-root user `10001:10001`. Client configuration and credentials are not included in the image. ## Deploy To Local SIT @@ -172,6 +194,24 @@ helm template config-server helm | kubectl apply --dry-run=client -f - ``` +Create the namespace and an opaque Secret containing a repository-scoped, read-only GitHub credential. Never commit the token or place it in Helm values: + +```bash +kubectl create namespace digital-bank-sit --dry-run=client -o yaml | + kubectl apply -f - + +read -s CONFIG_REPO_TOKEN +printf %s "$CONFIG_REPO_TOKEN" | + kubectl create secret generic config-server-git-credentials \ + --namespace digital-bank-sit \ + --from-literal=username=YOUR_GITHUB_USERNAME \ + --from-file=token=/dev/stdin \ + --dry-run=client \ + --output yaml | + kubectl apply -f - +unset CONFIG_REPO_TOKEN +``` + Install or upgrade the release in the SIT namespace: ```bash @@ -216,6 +256,7 @@ The Kubernetes deployment: - Uses a read-only root filesystem with bounded temporary storage. - Does not mount the default Kubernetes service account token. - Exposes Config Server only through an internal `ClusterIP` Service. +- Reads Git credentials from an existing Kubernetes Secret rather than Helm values. ## CI Validation @@ -223,7 +264,7 @@ Pull requests and changes to `main` run independent CI jobs that: - Execute Maven verification with Java 21. - Lint and render the Helm chart with Helm 4.2.0. -- Build the container image and smoke-test its health and configuration endpoints. +- Build the container image and smoke-test it against a disposable Git repository fixture. Third-party GitHub Actions are pinned to immutable commit SHAs. @@ -231,7 +272,7 @@ Third-party GitHub Actions are pinned to immutable commit SHAs. The same container and Helm chart are intended to move through SIT, UAT, and PROD without application rebuilds. Hosted environments will provide environment-specific image repositories, immutable image tags, resource sizing, and infrastructure configuration through the deployment pipeline. -The current native configuration backend is suitable for local development and SIT bootstrap. Migration to the dedicated Git-backed `platform-config` repository is required before the hosted UAT and PROD deployment. Secrets must remain outside Git and Helm values and be supplied through the environment's approved secret-management integration. +Configuration is promoted independently through the dedicated Git-backed `platform-config` repository. Secrets must remain outside Git and Helm values and be supplied through the environment's approved secret-management integration. AWS environments will replace the manually created SIT Secret with managed secret delivery and rotation. ## Development Workflow diff --git a/config-repo/application-local.yml b/config-repo/application-local.yml deleted file mode 100644 index b65b6b7..0000000 --- a/config-repo/application-local.yml +++ /dev/null @@ -1,4 +0,0 @@ -digital-bank: - platform: - environment: local - region: local \ No newline at end of file diff --git a/config-repo/application.yml b/config-repo/application.yml deleted file mode 100644 index d29d27f..0000000 --- a/config-repo/application.yml +++ /dev/null @@ -1,3 +0,0 @@ -digital-bank: - platform: - name: digital-bank-java \ No newline at end of file diff --git a/config-repo/customer-service/customer-service-local.yml b/config-repo/customer-service/customer-service-local.yml deleted file mode 100644 index fa506a0..0000000 --- a/config-repo/customer-service/customer-service-local.yml +++ /dev/null @@ -1,3 +0,0 @@ -digital-bank: - service: - runtime-profile: local \ No newline at end of file diff --git a/config-repo/customer-service/customer-service.yml b/config-repo/customer-service/customer-service.yml deleted file mode 100644 index a0c4d0c..0000000 --- a/config-repo/customer-service/customer-service.yml +++ /dev/null @@ -1,11 +0,0 @@ -server: - port: 8081 - -spring: - application: - name: customer-service - -digital-bank: - service: - name: customer-service - tier: core-banking \ No newline at end of file diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml index f2f2e26..405d8e3 100644 --- a/helm/templates/deployment.yaml +++ b/helm/templates/deployment.yaml @@ -30,6 +30,21 @@ spec: - name: {{ include "config-server.name" . }} image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" imagePullPolicy: {{ .Values.image.pullPolicy }} + env: + - name: CONFIG_REPO_URI + value: {{ .Values.configRepository.uri | quote }} + - name: CONFIG_REPO_DEFAULT_LABEL + value: {{ .Values.configRepository.defaultLabel | quote }} + - name: CONFIG_REPO_USERNAME + valueFrom: + secretKeyRef: + name: {{ .Values.configRepository.credentialsSecret.name }} + key: {{ .Values.configRepository.credentialsSecret.usernameKey }} + - name: CONFIG_REPO_TOKEN + valueFrom: + secretKeyRef: + name: {{ .Values.configRepository.credentialsSecret.name }} + key: {{ .Values.configRepository.credentialsSecret.tokenKey }} securityContext: {{- toYaml .Values.containerSecurityContext | nindent 12 }} ports: diff --git a/helm/values.yaml b/helm/values.yaml index af030ae..28befa3 100644 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -5,9 +5,17 @@ revisionHistoryLimit: 3 image: repository: digital-bank-java/config-server - tag: "0.0.1" + tag: "0.0.2" pullPolicy: IfNotPresent +configRepository: + uri: https://github.com/digital-bank-java/platform-config.git + defaultLabel: main + credentialsSecret: + name: config-server-git-credentials + usernameKey: username + tokenKey: token + service: type: ClusterIP port: 8888 @@ -22,7 +30,7 @@ resources: probes: startup: - path: /actuator/health + path: /actuator/health/liveness failureThreshold: 30 periodSeconds: 5 liveness: diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6c59000..6049d0d 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,15 +4,28 @@ server: spring: application: name: config-server - profiles: - active: native cloud: config: server: - native: - search-locations: - - file:./config-repo - - file:./config-repo/{application} + accept-empty: false + git: + uri: "${CONFIG_REPO_URI:file:../platform-config}" + default-label: "${CONFIG_REPO_DEFAULT_LABEL:main}" + try-master-branch: false + search-paths: + - "{application}" + clone-on-start: true + force-pull: true + delete-untracked-branches: true + timeout: 10 + username: "${CONFIG_REPO_USERNAME:}" + password: "${CONFIG_REPO_TOKEN:}" + health: + repositories: + platform-config: + name: customer-service + profiles: local + label: "${CONFIG_REPO_DEFAULT_LABEL:main}" management: endpoints: diff --git a/src/test/java/com/digitalbank/configserver/ConfigServerIntegrationTests.java b/src/test/java/com/digitalbank/configserver/ConfigServerIntegrationTests.java index 61b68e6..65d806a 100644 --- a/src/test/java/com/digitalbank/configserver/ConfigServerIntegrationTests.java +++ b/src/test/java/com/digitalbank/configserver/ConfigServerIntegrationTests.java @@ -1,36 +1,86 @@ package com.digitalbank.configserver; +import java.io.IOException; + +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.server.LocalServerPort; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.client.RestTestClient; -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { - "spring.cloud.config.server.native.search-locations=classpath:/config-repo" }) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class ConfigServerIntegrationTests { + private static final GitTestRepository TEST_REPOSITORY = GitTestRepository.create(); + @LocalServerPort private int port; private RestTestClient client; + @DynamicPropertySource + static void configureGitRepository(DynamicPropertyRegistry registry) { + registry.add("spring.cloud.config.server.git.uri", TEST_REPOSITORY::uri); + registry.add("spring.cloud.config.server.git.default-label", () -> "main"); + registry.add("spring.cloud.config.server.git.try-master-branch", () -> false); + registry.add("spring.cloud.config.server.git.search-paths", () -> "{application}"); + registry.add("spring.cloud.config.server.git.clone-on-start", () -> true); + registry.add("spring.cloud.config.server.accept-empty", () -> false); + registry.add( + "spring.cloud.config.server.health.repositories.integration-test.name", + () -> "test-service"); + registry.add( + "spring.cloud.config.server.health.repositories.integration-test.profiles", + () -> "default"); + registry.add( + "spring.cloud.config.server.health.repositories.integration-test.label", + () -> "main"); + } + @BeforeEach void setUp() { - client = RestTestClient.bindToServer().baseUrl("http://localhost:" + port).build(); + client = RestTestClient.bindToServer() + .baseUrl("http://localhost:" + port) + .build(); + } + + @AfterAll + static void deleteGitRepository() throws IOException { + TEST_REPOSITORY.delete(); } @Test void healthEndpointReportsUp() { - client.get().uri("/actuator/health").exchange().expectStatus().isOk().expectBody().jsonPath("$.status") - .isEqualTo("UP"); + client.get() + .uri("/actuator/health") + .exchange() + .expectStatus().isOk() + .expectBody() + .jsonPath("$.status").isEqualTo("UP"); } @Test - void configurationEndpointReturnsFixtureProperty() { - client.get().uri("/test-service/default").exchange().expectStatus().isOk().expectBody().jsonPath("$.name") - .isEqualTo("test-service").jsonPath("$.profiles[0]").isEqualTo("default") - .jsonPath("$.propertySources[0].source['test.message']").isEqualTo("config-server-integration-test"); + void configurationEndpointReturnsGitFixture() { + client.get() + .uri("/test-service/default") + .exchange() + .expectStatus().isOk() + .expectBody() + .jsonPath("$.name").isEqualTo("test-service") + .jsonPath("$.profiles[0]").isEqualTo("default") + .jsonPath("$.version").isEqualTo(TEST_REPOSITORY.revision()) + .jsonPath("$.propertySources[0].source['test.message']") + .isEqualTo("config-server-integration-test"); } -} + @Test + void missingConfigurationReturnsNotFound() { + client.get() + .uri("/missing-service/default") + .exchange() + .expectStatus().isNotFound(); + } +} \ No newline at end of file diff --git a/src/test/java/com/digitalbank/configserver/GitTestRepository.java b/src/test/java/com/digitalbank/configserver/GitTestRepository.java new file mode 100644 index 0000000..c72ebe2 --- /dev/null +++ b/src/test/java/com/digitalbank/configserver/GitTestRepository.java @@ -0,0 +1,52 @@ +package com.digitalbank.configserver; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.GitAPIException; +import org.springframework.core.io.ClassPathResource; +import org.springframework.util.FileSystemUtils; + +record GitTestRepository(Path directory, String revision) { + + static GitTestRepository create() { + try { + Path directory = Files.createTempDirectory( + "config-server-test-repository-").toRealPath(); + try (InputStream fixture = new ClassPathResource( + "config-repo/test-service.yml").getInputStream()) { + Files.copy(fixture, directory.resolve("test-service.yml")); + } + + try (Git git = Git.init() + .setDirectory(directory.toFile()) + .setInitialBranch("main") + .call()) { + + git.add().addFilepattern(".").call(); + + String revision = git.commit() + .setMessage("Add test configuration") + .setAuthor("Config Server Test", "test@digital-bank.local") + .setCommitter("Config Server Test", "test@digital-bank.local") + .call() + .getName(); + + return new GitTestRepository(directory, revision); + } + } catch (IOException | GitAPIException exception) { + throw new IllegalStateException("Failed to create Git test repository", exception); + } + } + + String uri() { + return directory.toUri().toString(); + } + + void delete() throws IOException { + FileSystemUtils.deleteRecursively(directory); + } +} \ No newline at end of file