# Credvane quickstart: Credvane, Postgres, OpenBao and Keycloak on one machine.
# For trying Credvane out, not for production: OpenBao runs in dev mode, in memory, so values are lost when it stops.
# Create .env first (see the quickstart), then: docker compose up -d
name: credvane-quickstart

services:
  postgres:
    image: postgres:17
    environment:
      POSTGRES_DB: credvane
      POSTGRES_USER: credvane
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?run the .env step of the quickstart first}
    volumes: [postgres-data:/var/lib/postgresql/data]
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U credvane -d credvane"]
      interval: 3s
      retries: 20

  # Where Credvane keeps every credential value. Port 8200 is published on this machine only, to look inside.
  openbao:
    image: openbao/openbao:2.4.1
    command: ["server", "-dev", "-dev-listen-address=0.0.0.0:8200"]
    environment:
      BAO_DEV_ROOT_TOKEN_ID: ${BAO_TOKEN:?run the .env step of the quickstart first}
    ports: ["127.0.0.1:8200:8200"]
    healthcheck:
      test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8200/v1/sys/health | grep -q '\"sealed\":false'"]
      interval: 3s
      retries: 20

  # Sign-in. The realm file adds the Credvane client, the credvane-admins group and one admin.
  # The name keycloak.localhost reaches this container from Credvane and from your browser alike.
  keycloak:
    image: quay.io/keycloak/keycloak:26.4
    command: ["start-dev", "--http-port=8180", "--import-realm"]
    environment:
      KC_BOOTSTRAP_ADMIN_USERNAME: admin
      KC_BOOTSTRAP_ADMIN_PASSWORD: ${ADMIN_PASSWORD:?run the .env step of the quickstart first}
      KC_HEALTH_ENABLED: "true"
      OAUTH_CLIENT_SECRET: ${OAUTH_CLIENT_SECRET:?run the .env step of the quickstart first}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}
    volumes: ["./credvane-realm.json:/opt/keycloak/data/import/credvane-realm.json:ro"]
    ports: ["127.0.0.1:8180:8180"]
    networks:
      default:
        aliases: [keycloak.localhost]
    healthcheck:
      test: ["CMD-SHELL", "exec 3<>/dev/tcp/127.0.0.1/9000; printf 'GET /health/ready HTTP/1.0\\r\\n\\r\\n' >&3; grep -q '\"UP\"' <&3"]
      interval: 5s
      retries: 30

  credvane:
    image: ghcr.io/commitwisedev/credvane:latest
    depends_on:
      postgres: { condition: service_healthy }
      openbao: { condition: service_healthy }
      keycloak: { condition: service_healthy }
    ports: ["127.0.0.1:9091:9091"]
    environment:
      CREDVANE_PUBLIC_URL: http://localhost:9091
      DATASOURCES_DEFAULT_URL: jdbc:postgresql://postgres:5432/credvane?currentSchema=credvane
      DATASOURCES_DEFAULT_USERNAME: credvane
      DATASOURCES_DEFAULT_PASSWORD: ${POSTGRES_PASSWORD}
      # Credvane's own secret store: the OpenBao above, KV version 2 at secret/, under credvane/.
      CREDVANE_SECRET_STORE_PLATFORM_TYPE: bao
      CREDVANE_SECRET_STORE_ADDRESS: http://openbao:8200
      CREDVANE_SECRET_STORE_TOKEN: ${BAO_TOKEN}
      # OpenBao and Keycloak are on this machine's private network and speak plain http. Credvane refuses that
      # unless told otherwise; leave it off in production.
      CREDVANE_SOURCES_ALLOW_PRIVATE_HOSTS: "true"
      OAUTH_ISSUER: http://keycloak.localhost:8180/realms/credvane
      OAUTH_CLIENT_ID: credvane
      OAUTH_CLIENT_SECRET: ${OAUTH_CLIENT_SECRET}
      CREDVANE_ADMIN_GROUPS: credvane-admins
      CREDVANE_ORG_NAME: Example

volumes:
  postgres-data:
