From 5036d853bfc253c9e43924be088687bcf62ae36b Mon Sep 17 00:00:00 2001 From: sirius Date: Tue, 3 Nov 2026 18:16:03 +0000 Subject: [PATCH] =?UTF-8?q?SGS=20portal:=20nginx=20login=20gate=20?= =?UTF-8?q?=E2=80=94=20rate-limit=20/api/portal/login=20(12r/m,=20burst=20?= =?UTF-8?q?6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sirius-growth/configmap-portal-gate.yaml | 69 +++++++++++++++++++ .../sirius-growth/deployment-portal-gate.yaml | 53 ++++++++++++++ apps/base/sirius-growth/httproute.yaml | 7 ++ apps/base/sirius-growth/kustomization.yaml | 3 + .../sirius-growth/service-portal-gate.yaml | 14 ++++ 5 files changed, 146 insertions(+) create mode 100644 apps/base/sirius-growth/configmap-portal-gate.yaml create mode 100644 apps/base/sirius-growth/deployment-portal-gate.yaml create mode 100644 apps/base/sirius-growth/service-portal-gate.yaml diff --git a/apps/base/sirius-growth/configmap-portal-gate.yaml b/apps/base/sirius-growth/configmap-portal-gate.yaml new file mode 100644 index 0000000..5e6a0b0 --- /dev/null +++ b/apps/base/sirius-growth/configmap-portal-gate.yaml @@ -0,0 +1,69 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: sirius-growth-portal-gate + namespace: sirius-growth +data: + default.conf: | + # Sirius Growth portal login gate. One job: cap credential attempts per client + # IP before they reach the API. The HTTPRoute sends only /api/portal/login* here + # (login, plus a trailing-slash variant); everything else goes straight to the + # portal service, so this pod can never break uploads, downloads or the admin API. + + # The TCP peer is always cluster ingress, so $remote_addr is meaningless until + # the forwarded chain is resolved: Envoy (hostNetwork, siriussec) appends the + # caller to X-Forwarded-For. Trusting the pod / service / node ranges makes + # $remote_addr the real client. A caller cannot spoof it by sending its own + # X-Forwarded-For, because Envoy appends the true address to the right of + # whatever the client supplied and nginx walks the chain from the right. + set_real_ip_from 10.244.0.0/16; + set_real_ip_from 10.96.0.0/12; + set_real_ip_from 10.10.0.0/24; + real_ip_header X-Forwarded-For; + real_ip_recursive on; + + # 12 attempts/min per IP with 6 in hand: a fat-fingered operator gets several + # tries, a password guesser gets nowhere. Tune the two numbers here only. + limit_req_zone $binary_remote_addr zone=portal_login:10m rate=12r/m; + limit_req_status 429; + limit_req_log_level warn; + + server { + listen 8080; + server_name _; + default_type application/json; + + client_max_body_size 8k; + client_body_timeout 10s; + + location ^~ /api/portal/login { + limit_req zone=portal_login burst=6 nodelay; + + # JSON, not nginx's HTML error page: portal.js shows the body's `error` + # string on the sign-in page instead of "Portal API is not reachable". + error_page 429 @login_limited; + + proxy_pass http://sirius-growth-portal:8091; + proxy_http_version 1.1; + proxy_set_header Host $host; + # Pass the chain through verbatim; uvicorn reads the leftmost entry. + proxy_set_header X-Forwarded-For $http_x_forwarded_for; + proxy_set_header X-Real-IP $remote_addr; + proxy_connect_timeout 5s; + proxy_read_timeout 15s; + proxy_send_timeout 15s; + proxy_buffering off; + } + + location @login_limited { + internal; + default_type application/json; + add_header Cache-Control "no-store" always; + return 429 '{"error":"too many sign-in attempts - wait a minute and try again"}'; + } + + # Nothing else is routed to this pod. An honest 404 beats a proxied surprise. + location / { + return 404; + } + } diff --git a/apps/base/sirius-growth/deployment-portal-gate.yaml b/apps/base/sirius-growth/deployment-portal-gate.yaml new file mode 100644 index 0000000..32714f6 --- /dev/null +++ b/apps/base/sirius-growth/deployment-portal-gate.yaml @@ -0,0 +1,53 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sirius-growth-portal-gate + namespace: sirius-growth +spec: + replicas: 1 + selector: + matchLabels: + app: sirius-growth-portal-gate + template: + metadata: + labels: + app: sirius-growth-portal-gate + spec: + tolerations: + - key: dedicated + operator: Exists + effect: PreferNoSchedule + nodeSelector: + kubernetes.io/hostname: siriussec + containers: + - name: nginx + image: nginx:1.27-alpine + imagePullPolicy: IfNotPresent + ports: + - containerPort: 8080 + name: http + volumeMounts: + - name: conf + mountPath: /etc/nginx/conf.d/default.conf + subPath: default.conf + readOnly: true + readinessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 2 + periodSeconds: 10 + livenessProbe: + tcpSocket: + port: 8080 + initialDelaySeconds: 10 + periodSeconds: 20 + resources: + requests: + cpu: 10m + memory: 16Mi + limits: + memory: 64Mi + volumes: + - name: conf + configMap: + name: sirius-growth-portal-gate diff --git a/apps/base/sirius-growth/httproute.yaml b/apps/base/sirius-growth/httproute.yaml index 07edc7b..4651c53 100644 --- a/apps/base/sirius-growth/httproute.yaml +++ b/apps/base/sirius-growth/httproute.yaml @@ -18,6 +18,13 @@ spec: backendRefs: - name: sirius-growth-contact port: 8090 + - matches: + - path: + type: PathPrefix + value: /api/portal/login + backendRefs: + - name: sirius-growth-portal-gate + port: 8080 - matches: - path: type: PathPrefix diff --git a/apps/base/sirius-growth/kustomization.yaml b/apps/base/sirius-growth/kustomization.yaml index ab9f9d8..b3e796c 100644 --- a/apps/base/sirius-growth/kustomization.yaml +++ b/apps/base/sirius-growth/kustomization.yaml @@ -5,9 +5,12 @@ resources: - deployment.yaml - deployment-api.yaml - deployment-portal.yaml + - deployment-portal-gate.yaml + - configmap-portal-gate.yaml - service.yaml - service-api.yaml - service-portal.yaml + - service-portal-gate.yaml - httproute.yaml - cluster.yaml - podmonitor.yaml diff --git a/apps/base/sirius-growth/service-portal-gate.yaml b/apps/base/sirius-growth/service-portal-gate.yaml new file mode 100644 index 0000000..4ceadee --- /dev/null +++ b/apps/base/sirius-growth/service-portal-gate.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Service +metadata: + name: sirius-growth-portal-gate + namespace: sirius-growth +spec: + type: ClusterIP + selector: + app: sirius-growth-portal-gate + ports: + - name: http + port: 8080 + targetPort: 8080 + protocol: TCP