version: "3.8"
volumes:
  opal:
services:
  redis:
    image: redis
    ports:
      - "6379:6379"
  opal_server_worker:
    image: permitio/opal-server:latest
    environment:
      - OPAL_REDIS_URL=redis://redis:6379
      - OPAL_SCOPES=1
      - OPAL_SERVER_URL=http://opal_server:7002
      - OPAL_WORKER_TOKEN=token
      - OPAL_POLICY_REPO_POLLING_INTERVAL=30
      - OPAL_BASE_DIR=/opal
    volumes:
      - opal:/opal:rw
    depends_on:
      - redis
    command: sh -c "celery -A opal_server.worker worker -B -l info -s /tmp/schedule.db"
  opal_server:
    # by default we run opal-server from latest official image
    image: permitio/opal-server:latest
    environment:
      # the broadcast backbone uri used by opal server workers (see comments above for: broadcast_channel)
      - OPAL_BROADCAST_URI=redis://redis:6379
      # number of uvicorn workers to run inside the opal-server container
      - UVICORN_NUM_WORKERS=1
      - OPAL_REPO_WATCHER_ENABLED=0
      # the git repo hosting our policy
      # - if this repo is not public, you can pass an ssh key via `OPAL_POLICY_REPO_SSH_KEY`)
      # - the repo we pass in this example is *public* and acts as an example repo with dummy rego policy
      # - for more info, see: https://docs.opal.ac/tutorials/track_a_git_repo
      # - OPAL_POLICY_REPO_URL=https://github.com/permitio/opal-example-policy-repo
      # in this example we will use a polling interval of 30 seconds to check for new policy updates (git commits affecting the rego policy).
      # however, it is better to utilize a git *webhook* to trigger the server to check for changes only when the repo has new commits.
      # for more info see: https://docs.opal.ac/tutorials/track_a_git_repo
      # - OPAL_POLICY_REPO_POLLING_INTERVAL=30
      # configures from where the opal client should initially fetch data (when it first goes up, after disconnection, etc).
      # the data sources represents from where the opal clients should get a "complete picture" of the data they need.
      # after the initial sources are fetched, the client will subscribe only to update notifications sent by the server.
      - OPAL_DATA_CONFIG_SOURCES={"config":{"entries":[{"url":"http://opal_server:7002/policy-data","topics":["policy_data"],"dst_path":"/static"}]}}
      - OPAL_LOG_FORMAT_INCLUDE_PID=true
      - OPAL_REDIS_URL=redis://redis:6379
      - OPAL_SCOPES=1
      - OPAL_WORKER_TOKEN=token
      - OPAL_BASE_DIR=/opal
    ports:
      # exposes opal server on the host machine, you can access the server at: http://localhost:7002
      - "7002:7002"
    depends_on:
      - redis
    volumes:
      - opal:/opal:rw
  opal_client:
    # by default we run opal-client from latest official image
    image: permitio/opal-client:latest
    environment:
      - OPAL_SERVER_URL=http://opal_server:7002
      - OPAL_LOG_FORMAT_INCLUDE_PID=true
      - OPAL_INLINE_OPA_LOG_FORMAT=http
      - OPAL_SCOPE_ID=internal
    ports:
      # exposes opal client on the host machine, you can access the client at: http://localhost:7000
      - "7001:7000"
      # exposes the OPA agent (being run by OPAL) on the host machine
      # you can access the OPA api that you know and love at: http://localhost:8181
      # OPA api docs are at: https://www.openpolicyagent.org/docs/latest/rest-api/
      - "8181:8181"
    depends_on:
      - opal_server
    # this command is not necessary when deploying OPAL for real, it is simply a trick for dev environments
    # to make sure that opal-server is already up before starting the client.
    command: sh -c "./wait-for.sh opal_server:7002 --timeout=20 -- ./start.sh"