# Credential Injection from Environment Variables
#
# Injects per-service API credentials read from environment
# variables, so no secret is written into the config. One listener
# fronts several services: a path prefix picks the service, the
# prefix is stripped before forwarding, and the upstream gets that
# service's credential and Host header. Client-provided values for
# the credential header are overwritten.
#
# The variables are read once, when the filter is built, from the
# proxy's own environment. If any of them is unset the proxy refuses
# to start (`praxis -t` reports which one), and rotating a credential
# means restarting the proxy with the new value.
#
# Usage:
#   GITHUB_TOKEN=... OPENAI_API_KEY=... INTERNAL_API_KEY=... \
#     cargo run -p praxis-proxy -- \
#     -c examples/configs/security/credential-injection-env-vars.yaml
#
# Exercise:
#   # Upstream sees GET /user with Authorization: Bearer $GITHUB_TOKEN
#   # and Host: api.github.com
#   curl http://localhost:8080/github/user
#
#   # Upstream sees GET /v1/models with Authorization: Bearer
#   # $OPENAI_API_KEY and Host: api.openai.com
#   curl http://localhost:8080/openai/v1/models
#
#   # Upstream sees GET /data with x-api-key: $INTERNAL_API_KEY
#   curl http://localhost:8080/internal/data
#
#   # Client-provided Authorization is replaced
#   curl -H "Authorization: Bearer spoofed" \
#     http://localhost:8080/github/user
#
# The endpoints below are local plain-HTTP stand-ins so the example
# runs anywhere. To reach a real API, give the cluster a hostname
# endpoint on 443 and TLS, with SNI matching the Host override
# (`tls.sni` is configured separately from `http.authority`):
#
#   - name: github
#     endpoints:
#       - "api.github.com:443"
#     tls:
#       sni: "api.github.com"
#     http:
#       authority: "api.github.com"

listeners:
  - name: default
    address: "127.0.0.1:8080"
    filter_chains:
      - main

filter_chains:
  - name: main
    filters:
      - filter: router
        routes:
          - path_prefix: "/github"
            cluster: github
          - path_prefix: "/openai"
            cluster: openai
          - path_prefix: "/internal"
            cluster: internal

      # The conditions are mutually exclusive, so only one rewrite
      # fires per request; allow_rewrite_override tells pipeline
      # validation that the extra rewrite filters are intended.
      - filter: path_rewrite
        strip_prefix: "/github"
        conditions:
          - when:
              path_prefix: "/github"

      - filter: path_rewrite
        strip_prefix: "/openai"
        allow_rewrite_override: true
        conditions:
          - when:
              path_prefix: "/openai"

      - filter: path_rewrite
        strip_prefix: "/internal"
        allow_rewrite_override: true
        conditions:
          - when:
              path_prefix: "/internal"

      - filter: credential_injection
        clusters:
          - name: github
            header: Authorization
            env_var: GITHUB_TOKEN
            header_prefix: "Bearer "
          - name: openai
            header: Authorization
            env_var: OPENAI_API_KEY
            header_prefix: "Bearer "
          - name: internal
            header: x-api-key
            env_var: INTERNAL_API_KEY

      - filter: load_balancer
        clusters:
          - name: github
            endpoints:
              - "127.0.0.1:3001"
            http:
              authority: "api.github.com"
          - name: openai
            endpoints:
              - "127.0.0.1:3002"
            http:
              authority: "api.openai.com"
          - name: internal
            endpoints:
              - "127.0.0.1:3003"
            http:
              authority: "internal.example.com"

insecure_options:
  allow_private_endpoints: true # example proxies to local backends
