# services/oauth2-proxy/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: oauth2-proxy
  namespace: sso
  labels:
    app: oauth2-proxy
spec:
  replicas: 2
  selector:
    matchLabels:
      app: oauth2-proxy
  template:
    metadata:
      labels:
        app: oauth2-proxy
      annotations:
        vault.hashicorp.com/agent-inject: "true"
        vault.hashicorp.com/role: "sso"
        vault.hashicorp.com/agent-inject-secret-oidc-env: "kv/data/atlas/sso/oauth2-proxy-oidc"
        vault.hashicorp.com/agent-inject-template-oidc-env: |
          {{- with secret "kv/data/atlas/sso/oauth2-proxy-oidc" -}}
          export OAUTH2_PROXY_CLIENT_ID="{{ .Data.data.client_id }}"
          export OAUTH2_PROXY_CLIENT_SECRET="{{ .Data.data.client_secret }}"
          export OAUTH2_PROXY_COOKIE_SECRET="{{ .Data.data.cookie_secret }}"
          {{- end -}}
    spec:
      serviceAccountName: sso-vault
      imagePullSecrets:
        - name: harbor-regcred
      nodeSelector:
        node-role.kubernetes.io/worker: "true"
      affinity:
        nodeAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
            - weight: 90
              preference:
                matchExpressions:
                  - key: hardware
                    operator: In
                    values: ["rpi5","rpi4"]
      containers:
        - name: oauth2-proxy
          image: registry.bstein.dev/tools/oauth2-proxy-vault:v7.6.0
          imagePullPolicy: IfNotPresent
          command: ["/entrypoint.sh"]
          args:
            - /bin/oauth2-proxy
            - --provider=oidc
            - --redirect-url=https://auth.bstein.dev/oauth2/callback
            - --oidc-issuer-url=https://sso.bstein.dev/realms/atlas
            - --scope=openid profile email groups
            - --email-domain=*
            - --set-xauthrequest=true
            - --pass-access-token=true
            - --set-authorization-header=true
            - --cookie-secure=true
            - --cookie-samesite=lax
            - --cookie-refresh=20m
            - --cookie-expire=168h
            - --upstream=static://200
            - --http-address=0.0.0.0:4180
            - --skip-provider-button=true
            - --skip-jwt-bearer-tokens=true
            - --oidc-groups-claim=groups
          env:
            - name: VAULT_ENV_FILE
              value: /vault/secrets/oidc-env
          ports:
            - containerPort: 4180
              name: http
          readinessProbe:
            httpGet:
              path: /ping
              port: 4180
            initialDelaySeconds: 5
            periodSeconds: 10
          livenessProbe:
            httpGet:
              path: /ping
              port: 4180
            initialDelaySeconds: 20
            periodSeconds: 20
