diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e85b20e --- /dev/null +++ b/.dockerignore @@ -0,0 +1,11 @@ +target/ +.git/ +.github/ +.settings/ +.classpath +.project +.sts4-cache/ +.idea/ +.vscode/ +.DS_Store +README.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..2773e39 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,111 @@ +name: CI + +on: + pull_request: + push: + branches: + - main + +permissions: + contents: read + +concurrency: + group: ci-${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + name: Maven verification + runs-on: ubuntu-latest + timeout-minutes: 10 + + steps: + - name: Check out repository + uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + + - name: Set up Java + uses: actions/setup-java@ad2b38190b15e4d6bdf0c97fb4fca8412226d287 # v5.3.0 + with: + distribution: temurin + java-version: "21" + cache: maven + + - name: Verify application + run: ./mvnw --batch-mode --no-transfer-progress verify + + helm: + name: Helm validation + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Check out repository + uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + + - name: Set up Helm + uses: azure/setup-helm@dda3372f752e03dde6b3237bc9431cdc2f7a02a2 # v5.0.0 + with: + version: v4.2.0 + + - name: Lint chart + run: helm lint helm --strict + + - name: Render chart + run: | + helm template config-server helm \ + --set image.tag="${GITHUB_SHA}" \ + > rendered.yaml + test -s rendered.yaml + container: + name: Container build and smoke test + needs: + - test + - helm + runs-on: ubuntu-latest + timeout-minutes: 15 + + steps: + - name: Check out repository + uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@d7f5e7f509e45cec5c76c4d5afdd7de93d0b3df5 # v4.1.0 + + - name: Build container image + uses: docker/build-push-action@f9f3042f7e2789586610d6e8b85c8f03e5195baf # v7.2.0 + with: + context: . + load: true + push: false + tags: digital-bank-java/config-server:ci + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Smoke test container + shell: bash + run: | + docker run --detach \ + --name config-server-ci \ + --publish 8888:8888 \ + 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 + ready=true + break + fi + sleep 2 + done + + if [[ "${ready}" != "true" ]]; then + docker logs config-server-ci + exit 1 + fi + + curl --fail --silent \ + http://localhost:8888/customer-service/local \ + | grep --quiet '"name":"customer-service"' diff --git a/.gitignore b/.gitignore index 667aaef..36c1992 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ HELP.md +.DS_Store target/ .mvn/wrapper/maven-wrapper.jar !**/src/main/**/target/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a073e21 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM eclipse-temurin:21-jdk-jammy AS builder + +WORKDIR /workspace + +COPY .mvn/ .mvn/ +COPY mvnw pom.xml ./ +RUN chmod +x mvnw && ./mvnw --batch-mode dependency:go-offline + +COPY src/ src/ +RUN ./mvnw --batch-mode clean package + +FROM eclipse-temurin:21-jre-jammy + +RUN groupadd --gid 10001 spring \ + && useradd --uid 10001 --gid spring --no-create-home \ + --shell /usr/sbin/nologin spring + +WORKDIR /app + +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 + +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/README.md b/README.md index da1c26d..8167217 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,8 @@ config-repo/ - Java 21. - Git. - Network access to Maven Central for the initial dependency download. +- Docker Desktop for container builds. +- Docker Desktop Kubernetes and Helm 4 for the local SIT deployment. A global Maven installation is not required because the repository includes the Maven Wrapper. @@ -59,6 +61,9 @@ Verify the environment: ```bash java -version ./mvnw --version +docker version +kubectl config current-context +helm version --short ``` ## Run Locally @@ -119,6 +124,115 @@ config-repo/application.yml More specific profile and service configuration takes precedence over shared defaults. +## Run With Docker + +Build the image from the repository root: + +```bash +docker build \ + --tag digital-bank-java/config-server:0.0.1 \ + . +``` + +Run the container as a non-root user: + +```bash +docker run --detach \ + --rm \ + --name digital-bank-java-config-server \ + --publish 8888:8888 \ + digital-bank-java/config-server:0.0.1 +``` + +Verify the container and stop it: + +```bash +curl --fail http://localhost:8888/actuator/health +curl --fail http://localhost:8888/customer-service/local +docker stop digital-bank-java-config-server +``` + +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. + +## Deploy To Local SIT + +The Helm chart deploys Config Server to Docker Desktop Kubernetes. Confirm that the active context is `docker-desktop` before continuing: + +```bash +kubectl config current-context +kubectl get nodes +``` + +Validate the chart without changing the cluster: + +```bash +helm lint helm + +helm template config-server helm | + kubectl apply --dry-run=client -f - +``` + +Install or upgrade the release in the SIT namespace: + +```bash +helm upgrade --install config-server helm \ + --namespace digital-bank-sit \ + --create-namespace \ + --wait \ + --timeout 5m +``` + +Inspect the release: + +```bash +helm status config-server --namespace digital-bank-sit +kubectl get deployment,pods,service --namespace digital-bank-sit +kubectl logs deployment/config-server --namespace digital-bank-sit +``` + +Forward the internal Kubernetes Service to the workstation: + +```bash +kubectl port-forward \ + service/config-server 8888:8888 \ + --namespace digital-bank-sit +``` + +While port forwarding is active, run the health and configuration requests from another terminal. Stop port forwarding with `Ctrl+C`. + +Remove only this Helm release when cleanup is required: + +```bash +helm uninstall config-server --namespace digital-bank-sit +``` + +## Deployment Security + +The Kubernetes deployment: + +- Runs as numeric non-root user and group `10001`. +- Disables privilege escalation. +- Drops Linux capabilities. +- 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. + +## CI Validation + +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. + +Third-party GitHub Actions are pinned to immutable commit SHAs. + +## Environment Promotion + +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. + ## Development Workflow Changes must be made on a dedicated branch and merged through a pull request. Do not commit directly to `main`. @@ -135,7 +249,8 @@ Before opening a pull request: ```bash git status -./mvnw test +./mvnw verify +helm lint helm --strict git diff --check ``` diff --git a/helm/Chart.yaml b/helm/Chart.yaml new file mode 100644 index 0000000..dd0e1fd --- /dev/null +++ b/helm/Chart.yaml @@ -0,0 +1,6 @@ +apiVersion: v2 +name: config-server +description: Helm chart for the Digital Bank Java Config Server +type: application +version: 0.1.0 +appVersion: "0.0.1" diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl new file mode 100644 index 0000000..5750079 --- /dev/null +++ b/helm/templates/_helpers.tpl @@ -0,0 +1,29 @@ + {{- define "config-server.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{- define "config-server.fullname" -}} +{{- if .Values.fullnameOverride }} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- $name := default .Chart.Name .Values.nameOverride }} +{{- if contains $name .Release.Name }} +{{- .Release.Name | trunc 63 | trimSuffix "-" }} +{{- else }} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" }} +{{- end }} +{{- end }} +{{- end }} + +{{- define "config-server.selectorLabels" -}} +app.kubernetes.io/name: {{ include "config-server.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} + +{{- define "config-server.labels" -}} +helm.sh/chart: {{ printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" }} +{{ include "config-server.selectorLabels" . }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +app.kubernetes.io/part-of: digital-bank-java +{{- end }} \ No newline at end of file diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml new file mode 100644 index 0000000..f2f2e26 --- /dev/null +++ b/helm/templates/deployment.yaml @@ -0,0 +1,65 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "config-server.fullname" . }} + labels: + {{- include "config-server.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + revisionHistoryLimit: {{ .Values.revisionHistoryLimit }} + minReadySeconds: 5 + progressDeadlineSeconds: 300 + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 0 + maxSurge: 1 + selector: + matchLabels: + {{- include "config-server.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "config-server.labels" . | nindent 8 }} + spec: + automountServiceAccountToken: false + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + terminationGracePeriodSeconds: {{ .Values.terminationGracePeriodSeconds }} + containers: + - name: {{ include "config-server.name" . }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + securityContext: + {{- toYaml .Values.containerSecurityContext | nindent 12 }} + ports: + - name: http + containerPort: {{ .Values.service.port }} + protocol: TCP + startupProbe: + httpGet: + path: {{ .Values.probes.startup.path }} + port: http + failureThreshold: {{ .Values.probes.startup.failureThreshold }} + periodSeconds: {{ .Values.probes.startup.periodSeconds }} + livenessProbe: + httpGet: + path: {{ .Values.probes.liveness.path }} + port: http + initialDelaySeconds: {{ .Values.probes.liveness.initialDelaySeconds }} + periodSeconds: {{ .Values.probes.liveness.periodSeconds }} + readinessProbe: + httpGet: + path: {{ .Values.probes.readiness.path }} + port: http + initialDelaySeconds: {{ .Values.probes.readiness.initialDelaySeconds }} + periodSeconds: {{ .Values.probes.readiness.periodSeconds }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: tmp + mountPath: /tmp + volumes: + - name: tmp + emptyDir: + sizeLimit: {{ .Values.temporaryStorage.sizeLimit }} diff --git a/helm/templates/service.yaml b/helm/templates/service.yaml new file mode 100644 index 0000000..c4a3994 --- /dev/null +++ b/helm/templates/service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "config-server.fullname" . }} + labels: + {{- include "config-server.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + ports: + - name: http + port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + selector: + {{- include "config-server.selectorLabels" . | nindent 4 }} diff --git a/helm/values.yaml b/helm/values.yaml new file mode 100644 index 0000000..af030ae --- /dev/null +++ b/helm/values.yaml @@ -0,0 +1,56 @@ +nameOverride: "" +fullnameOverride: "" +replicaCount: 1 +revisionHistoryLimit: 3 + +image: + repository: digital-bank-java/config-server + tag: "0.0.1" + pullPolicy: IfNotPresent + +service: + type: ClusterIP + port: 8888 + +resources: + requests: + cpu: 100m + memory: 256Mi + limits: + cpu: 500m + memory: 512Mi + +probes: + startup: + path: /actuator/health + failureThreshold: 30 + periodSeconds: 5 + liveness: + path: /actuator/health/liveness + initialDelaySeconds: 30 + periodSeconds: 10 + readiness: + path: /actuator/health/readiness + initialDelaySeconds: 10 + periodSeconds: 5 + +podSecurityContext: + runAsNonRoot: true + runAsUser: 10001 + runAsGroup: 10001 + fsGroup: 10001 + fsGroupChangePolicy: OnRootMismatch + seccompProfile: + type: RuntimeDefault + +containerSecurityContext: + allowPrivilegeEscalation: false + readOnlyRootFilesystem: true + capabilities: + drop: + - ALL + +temporaryStorage: + sizeLimit: 64Mi + +terminationGracePeriodSeconds: 30