feat(keycloak): default-deny the keycloak namespace egress
The keycloak namespace only has the ingress policy the Keycloak Operator creates. Neither the Keycloak pod nor the Operator pod has any egress restriction, unlike every other platform namespace. Add a namespace-wide default deny and allow only what is used: DNS, platform-postgres 5432 (server and realm-import jobs), JGroups between Keycloak pods, and for the Operator the API server and the Keycloak management ports. Keycloak ingress stays owned by the Operator policy. Rejected: allowing public 443 like Gitea does. Neither the repository nor the Keycloak CR configures SMTP or an external identity provider, and the only open TCP connections from keycloak-0 on 2026-11-12 were to PostgreSQL. Adding SMTP or an IdP later needs a new egress rule; the README says so. Risk: a missed dependency would break logins for every service. The README lists the post-apply checks (OIDC discovery, a new TCP connection to Postgres from keycloak-0, Operator logs) and the one-line rollback. Verification: render and kubectl diff only add the five policies.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
# keycloak namespace NetworkPolicy
|
||||
|
||||
2026-11-11 점검 전 `keycloak` namespace에는 Keycloak Operator가 만든 ingress 정책 하나뿐이었다.
|
||||
Keycloak 파드와 Operator 파드의 egress는 막혀 있지 않았다. 다른 플랫폼 namespace처럼 기본 차단에
|
||||
필요한 것만 연다.
|
||||
|
||||
| 정책 | 대상 | 허용 |
|
||||
| --- | --- | --- |
|
||||
| `keycloak-default-deny` | 모든 파드 | ingress·egress 기본 차단 |
|
||||
| `keycloak-allow-dns` | 모든 파드 | kube-dns 53 |
|
||||
| `keycloak-allow-platform-postgres` | 모든 파드(서버와 realm import Job) | `platform-data` Postgres 5432 |
|
||||
| `keycloak-allow-cluster-peers` | Keycloak 파드 | 같은 Keycloak 파드 JGroups 7800·57800 |
|
||||
| `keycloak-operator-allow-egress` | Operator | API 서버(`10.43.0.1:443`, `192.168.0.107:6443`), Keycloak 8080·9000 |
|
||||
|
||||
- Keycloak 파드의 **ingress**는 계속 Keycloak CR `spec.networkPolicy`로 Operator가 만드는
|
||||
`keycloak-network-policy`가 소유한다(Traefik 8080, 관리 9000, JGroups).
|
||||
- 바깥(인터넷)으로 나가는 길은 없다. 2026-11-12에 repo와 Keycloak CR에 SMTP·외부 IdP 설정이
|
||||
없고, Keycloak 파드의 열린 TCP 연결이 Postgres뿐인 것을 확인했다. SMTP나 외부 IdP를 붙이면
|
||||
여기에 egress를 더해야 한다(안 더하면 메일·외부 로그인만 조용히 실패한다).
|
||||
- kubelet probe는 노드에서 오므로 막히지 않는다.
|
||||
|
||||
## 적용과 확인
|
||||
|
||||
```sh
|
||||
bash scripts/validate/render-tenants-and-policies.sh
|
||||
kubectl kustomize infrastructure/networking/keycloak | kubectl diff -f -
|
||||
kubectl apply -k infrastructure/networking/keycloak
|
||||
```
|
||||
|
||||
적용 뒤 확인(모두 통과해야 한다):
|
||||
|
||||
```sh
|
||||
kubectl -n keycloak get pods
|
||||
curl -fsS -o /dev/null -w '%{http_code}\n' https://id.learn.hyeonworks.com/realms/hyeonworks/.well-known/openid-configuration
|
||||
kubectl -n keycloak logs deploy/keycloak-operator --since=5m | grep -i -E 'error|exception|timeout' || echo no-operator-errors
|
||||
```
|
||||
|
||||
되돌리기: `kubectl delete -k infrastructure/networking/keycloak`(Operator 정책은 남는다).
|
||||
@@ -0,0 +1,12 @@
|
||||
apiVersion: kustomize.config.k8s.io/v1beta1
|
||||
kind: Kustomization
|
||||
|
||||
namespace: keycloak
|
||||
|
||||
labels:
|
||||
- pairs:
|
||||
app.kubernetes.io/part-of: platform
|
||||
includeSelectors: false
|
||||
|
||||
resources:
|
||||
- network-policies.yaml
|
||||
@@ -0,0 +1,117 @@
|
||||
# keycloak namespace: 기본 차단 + 필요한 것만 허용.
|
||||
# Keycloak 파드의 ingress는 Keycloak CR(spec.networkPolicy)로 Operator가 만드는
|
||||
# keycloak-network-policy가 계속 소유한다. 여기서는 기본 차단과 egress만 정한다.
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: NetworkPolicy
|
||||
metadata:
|
||||
name: keycloak-default-deny
|
||||
spec:
|
||||
podSelector: {}
|
||||
policyTypes:
|
||||
- Ingress
|
||||
- Egress
|
||||
---
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: NetworkPolicy
|
||||
metadata:
|
||||
name: keycloak-allow-dns
|
||||
spec:
|
||||
podSelector: {}
|
||||
policyTypes:
|
||||
- Egress
|
||||
egress:
|
||||
- to:
|
||||
- namespaceSelector:
|
||||
matchLabels:
|
||||
kubernetes.io/metadata.name: kube-system
|
||||
podSelector:
|
||||
matchLabels:
|
||||
k8s-app: kube-dns
|
||||
ports:
|
||||
- port: 53
|
||||
protocol: UDP
|
||||
- port: 53
|
||||
protocol: TCP
|
||||
---
|
||||
# Keycloak 서버와 realm import Job이 공용 PostgreSQL에 붙는다.
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: NetworkPolicy
|
||||
metadata:
|
||||
name: keycloak-allow-platform-postgres
|
||||
spec:
|
||||
podSelector: {}
|
||||
policyTypes:
|
||||
- Egress
|
||||
egress:
|
||||
- to:
|
||||
- namespaceSelector:
|
||||
matchLabels:
|
||||
kubernetes.io/metadata.name: platform-data
|
||||
podSelector:
|
||||
matchLabels:
|
||||
cnpg.io/cluster: platform-postgres
|
||||
ports:
|
||||
- port: 5432
|
||||
protocol: TCP
|
||||
---
|
||||
# 같은 Keycloak 인스턴스끼리의 JGroups(7800, 57800). Operator ingress 정책과 짝이다.
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: NetworkPolicy
|
||||
metadata:
|
||||
name: keycloak-allow-cluster-peers
|
||||
spec:
|
||||
podSelector:
|
||||
matchLabels:
|
||||
app: keycloak
|
||||
app.kubernetes.io/instance: keycloak
|
||||
app.kubernetes.io/managed-by: keycloak-operator
|
||||
policyTypes:
|
||||
- Egress
|
||||
egress:
|
||||
- to:
|
||||
- podSelector:
|
||||
matchLabels:
|
||||
app: keycloak
|
||||
app.kubernetes.io/instance: keycloak
|
||||
app.kubernetes.io/managed-by: keycloak-operator
|
||||
ports:
|
||||
- port: 7800
|
||||
protocol: TCP
|
||||
- port: 57800
|
||||
protocol: TCP
|
||||
---
|
||||
# Operator: Kubernetes API(ClusterIP와 실제 API 서버 주소)와 Keycloak 관리 포트.
|
||||
apiVersion: networking.k8s.io/v1
|
||||
kind: NetworkPolicy
|
||||
metadata:
|
||||
name: keycloak-operator-allow-egress
|
||||
spec:
|
||||
podSelector:
|
||||
matchLabels:
|
||||
app.kubernetes.io/name: keycloak-operator
|
||||
policyTypes:
|
||||
- Egress
|
||||
egress:
|
||||
- to:
|
||||
- ipBlock:
|
||||
cidr: 10.43.0.1/32
|
||||
ports:
|
||||
- port: 443
|
||||
protocol: TCP
|
||||
- to:
|
||||
- ipBlock:
|
||||
cidr: 192.168.0.107/32
|
||||
ports:
|
||||
- port: 6443
|
||||
protocol: TCP
|
||||
- to:
|
||||
- podSelector:
|
||||
matchLabels:
|
||||
app: keycloak
|
||||
app.kubernetes.io/instance: keycloak
|
||||
app.kubernetes.io/managed-by: keycloak-operator
|
||||
ports:
|
||||
- port: 8080
|
||||
protocol: TCP
|
||||
- port: 9000
|
||||
protocol: TCP
|
||||
Reference in New Issue
Block a user