# SecTools MISP 教學實驗 — 單機、虛構資料、僅綁定本機迴路位址。
# 依官方 MISP/misp-docker 精簡整理，不是官方 Compose 原檔，也不是正式部署模板。
# 先執行 python3 init-env.py 產生 .env；不要把 .env 加入版本控制。
name: sectools-misp-lab

services:
  redis:
    image: ghcr.io/valkey-io/valkey:7.2.16
    restart: unless-stopped
    command: ["sh", "-c", "exec valkey-server --requirepass \"$$REDIS_PASSWORD\""]
    environment:
      REDIS_PASSWORD: ${REDIS_PASSWORD:?run init-env.py first}
    healthcheck:
      test: ["CMD-SHELL", "valkey-cli -a \"$$REDIS_PASSWORD\" --no-auth-warning ping | grep -q PONG"]
      interval: 5s
      timeout: 3s
      retries: 10
    volumes:
      - cache_data:/data

  db:
    image: ghcr.io/mariadb/mariadb:10.11.20
    restart: unless-stopped
    environment:
      MYSQL_DATABASE: misp
      MYSQL_USER: misp
      MYSQL_PASSWORD: ${MYSQL_PASSWORD:?run init-env.py first}
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:?run init-env.py first}
    command: >-
      --innodb-buffer-pool-size=1024M
      --innodb-change-buffering=none
      --innodb-io-capacity=1000
      --innodb-io-capacity-max=2000
      --innodb-log-file-size=600M
      --innodb-read-io-threads=16
      --innodb-stats-persistent=ON
      --innodb-write-io-threads=4
    cap_add:
      - SYS_NICE
    healthcheck:
      test: ["CMD-SHELL", "mariadb-admin --user=\"$$MYSQL_USER\" --password=\"$$MYSQL_PASSWORD\" status"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 30s
    volumes:
      - mysql_data:/var/lib/mysql
      - backups:/backups

  misp-modules:
    image: ghcr.io/misp/misp-docker/misp-modules:v3.1.6
    restart: unless-stopped
    healthcheck:
      test: ["CMD-SHELL", "bash -c '</dev/tcp/localhost/6666'"]
      interval: 5s
      timeout: 3s
      retries: 20
      start_period: 10s

  misp-core:
    image: ghcr.io/misp/misp-docker/misp-core:v2.5.53
    restart: unless-stopped
    depends_on:
      redis:
        condition: service_healthy
      db:
        condition: service_healthy
      misp-modules:
        condition: service_healthy
    healthcheck:
      test: ["CMD-SHELL", "bash -c 'echo > /dev/tcp/127.0.0.1/9002'"]
      interval: 10s
      timeout: 3s
      retries: 30
      start_period: 120s
    cap_add:
      - AUDIT_WRITE
    environment:
      BASE_URL: http://localhost:${MISP_HTTP_PORT:-18081}
      ADMIN_EMAIL: ${ADMIN_EMAIL:?run init-env.py first}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD:?run init-env.py first}
      ADMIN_KEY: ${ADMIN_KEY:?run init-env.py first}
      ADMIN_ORG: ${ADMIN_ORG:-SecTools Lab}
      MISP_EMAIL: ${ADMIN_EMAIL}
      MISP_CONTACT: ${ADMIN_EMAIL}
      DISABLE_PRINTING_PLAINTEXT_CREDENTIALS: "true"
      ENCRYPTION_KEY: ${ENCRYPTION_KEY:?run init-env.py first}
      GPG_PASSPHRASE: ${GPG_PASSPHRASE:?run init-env.py first}
      CRON_USER_ID: "1"
      MYSQL_HOST: db
      MYSQL_PORT: "3306"
      MYSQL_USER: misp
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
      MYSQL_DATABASE: misp
      REDIS_HOST: redis
      REDIS_PORT: "6379"
      REDIS_PASSWORD: ${REDIS_PASSWORD}
      SUPERVISOR_HOST: 127.0.0.1
      SUPERVISOR_USERNAME: supervisor
      SUPERVISOR_PASSWORD: ${SUPERVISOR_PASSWORD:?run init-env.py first}
      FASTCGI_LISTEN_STATUS: misp-core:9003
      PHP_MEMORY_LIMIT: 1024M
      TZ: Asia/Taipei
    volumes:
      - misp_config:/var/www/MISP/app/Config
      - misp_logs:/var/www/MISP/app/tmp/logs
      - misp_files:/var/www/MISP/app/files
      - misp_gnupg:/var/www/MISP/.gnupg

  misp-nginx:
    image: ghcr.io/misp/misp-docker/misp-nginx:v2.5.53
    restart: unless-stopped
    depends_on:
      misp-core:
        condition: service_healthy
    # 教學實驗只發布 HTTP 到 127.0.0.1；正式環境請改用 HTTPS 與受信任憑證。
    ports:
      - "127.0.0.1:${MISP_HTTP_PORT:-18081}:8080"
    tmpfs:
      - /etc/nginx/conf.d:mode=0755,uid=101,gid=101
      - /var/cache/nginx:mode=0755,uid=101,gid=101
      - /tmp:mode=1777
    user: nginx
    read_only: true
    security_opt:
      - no-new-privileges:true
    cap_drop:
      - ALL
    environment:
      BASE_URL: http://localhost:${MISP_HTTP_PORT:-18081}
      FASTCGI_LISTEN: misp-core:9002
      FASTCGI_LISTEN_STATUS: misp-core:9003
      NGINX_CLIENT_MAX_BODY_SIZE: 50M
      TZ: Asia/Taipei
    healthcheck:
      test: ["CMD-SHELL", "curl -fsS -o /dev/null http://127.0.0.1:8080/users/login || exit 1"]
      interval: 10s
      timeout: 5s
      retries: 10
      start_period: 20s

volumes:
  mysql_data:
  cache_data:
  backups:
  misp_config:
  misp_logs:
  misp_files:
  misp_gnupg:
