services:

  unit-lint:
    build:
      context: .
      target: unit-lint
    environment:
      - DD_TRACE_ENABLED=0
    volumes:
      - ./pdp:/var/app/pdp
      - ./tests:/var/app/tests
      - ./build:/var/app/build

  pdp-dynamodb:
    image: amazon/dynamodb-local:2.4.0
    # healthcheck:
    #   test:
    #     [
    #       "CMD-SHELL",
    #       'if [ "$(curl -s -o /dev/null -I -w ''%{http_code}'' http://localhost:8000)" == "400" ]; then exit 0; else exit 1; fi',
    #     ]
    #   interval: 5s
    #   timeout: 5s
    #   retries: 10
    user: 0:0
    restart: always
    volumes:
      - pdp-dynamodb-data:/home/dynamodblocal/data
    command: "-jar DynamoDBLocal.jar -sharedDb -dbPath ./data"
    working_dir: /home/dynamodblocal

  pdp-redis:
    image: redis:7.2.3
    healthcheck:
      test: [ "CMD-SHELL", "redis-cli ping | grep PONG" ]
      interval: 5s
      timeout: 5s
      retries: 10
    environment:
      REDIS_HOST: pdp-redis
    volumes:
      - pdp-redis-data:/home/redis/data

  pdp-cerbos:
    image: 086679231553.dkr.ecr.us-east-1.amazonaws.com/cerbos:latest
    environment:
      - AUDIT_ENABLED=true
      - DECISION_LOGS_ENABLED=true
      - CERBOS_ADMIN_API_ENABLED=true
      - CERBOS_PLAYGROUND_ENABLED=true
      - CERBOS_WATCH_FOR_CHANGES=true
      - CERBOS_BATCH_SIZE=25
    volumes:
      - ./cerbos/policies:/policies

  start-db:
    build:
      context: .
      target: start-db
    depends_on:
      pdp-dynamodb:
        condition: service_started
    links:
      - pdp-dynamodb
    environment:
      - APP_DEBUG=true
      - Environment=dev
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_ACCESS_KEY_ID=fakeMyKeyId
      - AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey
      - DYNAMODB_ENDPOINT_URL=http://pdp-dynamodb:8000
      - DD_TRACE_ENABLED=0
    volumes:
      - ./pdp:/var/app/pdp

  # Seeds the cerbos_policy_metadata Redis entry before ows-pdp starts.
  # The pdp container reads this key on startup and will not start without it.
  seed-policy-metadata:
    build:
      context: .
      target: pdpcli
    depends_on:
      pdp-redis:
        condition: service_healthy
    links:
      - pdp-redis
    environment:
      - Environment=dev
      - DD_TRACE_ENABLED=0
      - CACHE_USE_REDIS=true
      - REDIS_URL=redis://pdp-redis:6379
    command: ["cerbos", "seed_policy_metadata_cache"]
    volumes:
      - ./cerbos:/var/app/cerbos

  ows-pdp:
    stdin_open: true
    tty: true
    build:
      context: .
      target: dev
    depends_on:
      pdp-dynamodb:
        condition: service_started
      pdp-cerbos:
        condition: service_healthy
      pdp-redis:
        condition: service_healthy
      seed-policy-metadata:
        condition: service_completed_successfully
      start-db:
        condition: service_completed_successfully
    links:
      - pdp-dynamodb
      - pdp-cerbos
      - pdp-redis
    environment:
      - APP_DEBUG=true
      - PORT=8080
      - Environment=dev
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_ACCESS_KEY_ID=fakeMyKeyId
      - AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey
      - DD_ENV=dev
      - DD_SERVICE=ows-pdp
      - DD_REDIS_SERVICE=ows-pdp-redis
      - DD_AGENT_HOST=dev-datadog-agent.dev.theorchard.io
      - DD_TRACE_ENABLED=1
      - DD_PROFILING_ENABLED=1
      - DD_TRACE_WRAP_SPAN_NAME_INCLUDE_CLASS=true
      # PP-1487: dump a native C/Python traceback to stderr on segfault (exit 139)
      - PYTHONFAULTHANDLER=1
      - DYNAMODB_ENDPOINT_URL=http://pdp-dynamodb:8000
      - DYNAMODB_TCP_KEEP_ALIVE=True
      - DYNAMODB_BATCH_UNPROCESSED_ITEMS_MAX_ATTEMPTS=3
      - CERBOS_SERVER_HOST=http://pdp-cerbos:3592
      - CERBOS_BATCH_SIZE=25
      - CERBOS_TIMEOUT_SECS=5
      - AUTH_ISSUERS=https://qa-orchard.auth0.com/
      - THEORCHARD_API_AUDIENCE=https://qa-ows.theorchard.io
      - M2M_API_AUDIENCE=https://workstation.qaorch.com/api
      - JWT_AUTH_ENABLED=1
      - AUTH_LEEWAY=0
      - SPLITIO_API_KEY=localhost
      - SPLIT_FILE_PATH=/var/app/tests/.split
      - OWSREQUEST_SERVICE_MAP={"ows-permissions":"https://qa-ows-permissions.theorchard.io", "ows-account":"https://qa-ows-account.theorchard.io", "ows-participant":"https://qa-ows-participant.theorchard.io"}
      - CACHE_USE_REDIS=true
      - REDIS_URL=redis://pdp-redis:6379
      - TOMBSTONE_TTL_SECONDS=86400
    volumes:
      - ./pdp:/var/app/pdp
      - ./dev.py:/var/app/dev.py
      - ./tests:/var/app/tests

  ows-pdp-integration:
    image: 086679231553.dkr.ecr.us-east-1.amazonaws.com/ows-pdp:latest
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8080/hello/"]
      interval: 5s
      timeout: 5s
      retries: 10
    depends_on:
      pdp-dynamodb:
        condition: service_started
      pdp-cerbos:
        condition: service_healthy
      pdp-redis:
        condition: service_healthy
    links:
      - pdp-dynamodb
      - pdp-cerbos
      - pdp-redis
    environment:
      - APP_DEBUG=true
      - PORT=8080
      - Environment=test
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_ACCESS_KEY_ID=fakeMyKeyId
      - AWS_SECRET_ACCESS_KEY=fakeSecretAccessKey
      - DD_ENV=dev
      - DD_SERVICE=ows-pdp
      - DD_AGENT_HOST=dev-datadog-agent.dev.theorchard.io
      - DD_TRACE_ENABLED=1
      - DD_PROFILING_ENABLED=1
      - DD_TRACE_WRAP_SPAN_NAME_INCLUDE_CLASS=true
      # PP-1487: dump a native C/Python traceback to stderr on segfault (exit 139)
      - PYTHONFAULTHANDLER=1
      - DYNAMODB_ENDPOINT_URL=http://pdp-dynamodb:8000
      - DYNAMODB_TABLE_IDENTITY=test_pp_identity
      - DYNAMODB_TCP_KEEP_ALIVE=True
      - DYNAMODB_BATCH_UNPROCESSED_ITEMS_MAX_ATTEMPTS=3
      - CERBOS_SERVER_HOST=http://pdp-cerbos:3592
      - CERBOS_BATCH_SIZE=25
      - CERBOS_TIMEOUT_SECS=5
      - AUTH_ISSUERS=https://qa-orchard.auth0.com/
      - THEORCHARD_API_AUDIENCE=https://qa-ows.theorchard.io
      - M2M_API_AUDIENCE=https://workstation.qaorch.com/api
      - JWT_AUTH_ENABLED=1
      - SPLITIO_API_KEY=localhost
      - SPLIT_FILE_PATH=/.split
      - OWSREQUEST_SERVICE_MAP={"ows-permissions":"https://qa-ows-permissions.theorchard.io", "ows-account":"https://qa-ows-account.theorchard.io", "ows-participant":"https://qa-ows-participant.theorchard.io"}
      - CACHE_USE_REDIS=true
      - REDIS_URL=redis://pdp-redis:6379
      - TOMBSTONE_TTL_SECONDS=86400
    volumes:
      - ${PWD}/tests/.split:/.split

  integration-test:
    build:
      context: .
      target: integration-test
    depends_on:
      ows-pdp-integration:
        condition: service_healthy
    environment:
      - AWS_ACCESS_KEY_ID
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_SECRET_ACCESS_KEY
      - AWS_SESSION_TOKEN
      - DYNAMODB_ENDPOINT_URL=http://pdp-dynamodb:8000
      - DYNAMODB_TABLE_IDENTITY=test_pp_identity
      - Environment=test
      - QA_BASE_URL=http://ows-pdp-integration:8080
      - CACHE_USE_REDIS=true
      - REDIS_URL=redis://pdp-redis:6379
      - DD_TRACE_ENABLED=0
      - DD_TRACE_WRAP_SPAN_NAME_INCLUDE_CLASS=true
      - SPLITIO_API_KEY=localhost
      - SPLIT_FILE_PATH=/var/app/tests/.split
    volumes:
      - ./pdp:/var/app/pdp
      - ./tests:/var/app/tests
      # Note: We need to mount the policies
      # for the CerbosPolicyParser integration tests
      - ./cerbos/policies:/var/app/cerbos/policies

  pdpcli:
    build:
      context: .
      target: pdpcli
    environment:
      - Environment
      - AWS_ACCESS_KEY_ID
      - AWS_DEFAULT_REGION=us-east-1
      - AWS_SECRET_ACCESS_KEY
      - AWS_SESSION_TOKEN
      - DYNAMODB_TABLE_IDENTITY
      - DYNAMODB_ENDPOINT_URL
      - QA_REFRESH_IDENTITY_UUID
      - DD_TRACE_ENABLED=0
      - CACHE_USE_REDIS
      - REDIS_URL
    volumes:
      - ./pdp:/var/app/pdp
      - ./pdp_qa_refresh:/var/app/pdp_qa_refresh
      - ./cerbos:/var/app/cerbos

volumes:
  pdp-dynamodb-data:
    driver: local
  pdp-redis-data:
    driver: local
