# NemoClaw policy preset: opens api.openai.com / cdn.openai.com for the
# OpenClaw gateway when running OpenAI as the inference provider.
#
# Why this is required:
#   By default the sandbox egress policy doesn't include any OpenAI host.
#   Without this preset, requests fail in /var/log/openshell.*.log with:
#       DENIED /usr/local/bin/node(...) -> api.openai.com:443
#       [policy:- engine:opa]
#       [reason:endpoint api.openai.com:443 not in policy '...']
#
# Apply with:
#   nemoclaw nemo policy-add --yes \
#       --from-file scripts/openai.policy.yaml \
#       "$(docker ps --format '{{.Names}}' | grep '^openshell-nemo-')"
#
# After applying, the gateway hot-reloads automatically — no need to
# `nemoclaw nemo recover` (and that command's probe gives false negatives
# in this sandbox anyway; see docs/nemoclaw.md troubleshooting).
#
# Note 1: unlike the rosbridge preset, no `allowed_ips` block is needed —
# api.openai.com resolves to PUBLIC IPs, so the OpenShell SSRF guard
# (which only default-denies private/RFC1918 destinations) doesn't fire.
#
# Note 2: NO `binaries:` allowlist on this preset, and that is intentional.
# The actual code path is:
#   OpenClaw (node) → CONNECT inference.local:443 via OPA proxy
#                  → openshell-router (Rust) → api.openai.com:443
# The hop that crosses api.openai.com is made by the openshell-router
# binary (/opt/openshell/bin/openshell-sandbox), not Node. If you restrict
# `binaries:` to openclaw/node, OPA can't attribute the router's outbound
# connection to any allowed binary and denies the request with
#   DENIED -(0) -> api.openai.com:443
#   [reason:failed to resolve peer binary: No ESTABLISHED TCP connection
#    found for port ...]
# Removing the binaries clause lets any process inside the sandbox
# netns reach the listed hosts — fine because the hosts themselves are
# narrowly scoped to OpenAI's public endpoints.

preset:
  name: openai
  description: "OpenAI API access for inference (api.openai.com + cdn.openai.com)"

network_policies:
  openai:
    name: openai
    endpoints:
      # Chat completions, embeddings, files, images — everything in the
      # standard OpenAI REST surface goes through api.openai.com.
      # We use the REST-aware shape (protocol/rules) that the built-in
      # `managed_inference` and `local_inference` policies use, NOT the
      # raw L4 `access: full` shape. The REST-aware shape tells OpenShell
      # to terminate TLS at its proxy, inspect the HTTP layer, and route
      # the request itself — which lets the openshell-router make the
      # outbound api.openai.com hop on behalf of the sandbox without
      # tripping OPA's peer-binary attribution check (which fails for
      # router-originated connections — see "OpenAI inference fails with
      # 'failed to resolve peer binary'" in docs/nemoclaw.md).
      - host: api.openai.com
        port: 443
        protocol: rest
        enforcement: enforce
        rules:
          - allow:
              method: GET
              path: /**
          - allow:
              method: POST
              path: /**
      # OpenAI's CDN host for binary downloads (file responses, etc.).
      - host: cdn.openai.com
        port: 443
        protocol: rest
        enforcement: enforce
        rules:
          - allow:
              method: GET
              path: /**
