apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: app-policy
  namespace: vibecarbon
spec:
  podSelector:
    matchLabels:
      app: vibecarbon-app
  policyTypes:
    - Ingress
    - Egress
  ingress:
    # Allow traffic from Traefik ingress controller (runs in same namespace)
    - from:
        - podSelector:
            matchLabels:
              app: vibecarbon-traefik
      ports:
        - protocol: TCP
          port: 3000
  egress:
    # Allow connection to the database: the API server queries Postgres
    # directly (src/server/db/client.ts) through the `db` Service. Selects the
    # db StatefulSet's pod (k8s/base/db/statefulset.yaml); base `labels:` never
    # rewrites selectors. (allow-intra-namespace-egress also admits this; the
    # explicit rule keeps the app's db path visible here and survives a
    # tighter namespace policy.)
    - to:
        - podSelector:
            matchLabels:
              app: vibecarbon-db
      ports:
        - protocol: TCP
          port: 5432
    # Allow access to Kubernetes API server for reading pod logs (admin dashboard)
    # 10.43.0.1:443 = ClusterIP via kube-proxy DNAT (works on worker nodes)
    - to:
        - ipBlock:
            cidr: 10.43.0.1/32
      ports:
        - protocol: TCP
          port: 443
    # 0.0.0.0/0:6443 = apiserver, dial-anywhere on apiserver-protocol port.
    # See carbon/k8s/base/traefik/network-policy.yaml for the full RCA. tl;dr:
    # kube-proxy DNATs 10.43.0.1 to the master's node IP at the OUTPUT chain
    # BEFORE the CNI evaluates egress, so the policy sees the post-NAT IP.
    # With --node-ip=public (master-init.sh) and the CCM HCLOUD_NETWORK patch
    # reverted (kubelet cert SAN), the master IP is public — outside any
    # private CIDR we could list. Allow any destination on 6443; deny-by-
    # default is preserved on every other port.
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
      ports:
        - protocol: TCP
          port: 6443
    # Allow outbound HTTPS (Stripe, OAuth providers, email APIs, external APIs)
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              - 10.0.0.0/8
              - 172.16.0.0/12
              - 192.168.0.0/16
      ports:
        - protocol: TCP
          port: 443
    # Allow outbound SMTP: the app sends its own auth emails (verification,
    # password reset, magic link) and transactional email through SMTP_HOST
    # on SMTP_PORT. 587 (submission/STARTTLS) is the configure default; 465
    # (implicit TLS), 25 and 2525 (the alternate port Resend, Postmark,
    # SendGrid and Mailgun offer where 25/587 are blocked) cover the rest.
    # Public destinations only, like HTTPS above.
    - to:
        - ipBlock:
            cidr: 0.0.0.0/0
            except:
              - 10.0.0.0/8
              - 172.16.0.0/12
              - 192.168.0.0/16
      ports:
        - protocol: TCP
          port: 25
        - protocol: TCP
          port: 465
        - protocol: TCP
          port: 587
        - protocol: TCP
          port: 2525
    # Allow connection to Redis (if configured)
    - to:
        - podSelector:
            matchLabels:
              app: redis
      ports:
        - protocol: TCP
          port: 6379
    # Allow health checks to optional services (n8n, metabase, observability)
    # These are only used for the admin services-status API; unused rules are harmless.
    - to:
        - podSelector:
            matchLabels:
              app: vibecarbon-n8n
      ports:
        - protocol: TCP
          port: 5678
    - to:
        - podSelector:
            matchLabels:
              app: metabase
      ports:
        - protocol: TCP
          port: 3000
    - to:
        - podSelector:
            matchLabels:
              app: vibecarbon-prometheus
      ports:
        - protocol: TCP
          port: 9090
    - to:
        - podSelector:
            matchLabels:
              app: vibecarbon-grafana
      ports:
        - protocol: TCP
          port: 3000
    - to:
        - podSelector:
            matchLabels:
              app: vibecarbon-loki
      ports:
        - protocol: TCP
          port: 3100
