如何集成OPA Gatekeeper在Kubernetes准入阶段阻断不合规EAP配置?

解读

国内金融、运营商、政务云对“上线即合规”要求极严,JBoss EAP 常以 Operator 方式部署在 Red Hat OpenShift 或国产 K8s 发行版(如麒麟云、统信 UOS 容器平台)上。面试官问“Gatekeeper 如何阻断不合规 EAP 配置”,实质考察三点:

  1. 是否熟悉 EAP 在 Kubernetes 上的配置载体(CR、ConfigMap、环境变量、Secret);
  2. 能否把企业基线(镜像源、资源限额、JVM 参数、安全域、端口、敏感挂载)翻译成 Gatekeeper 可识别的 Rego 策略;
  3. 是否具备落地闭环:策略开发→CI 测试→灰度发布→审计回溯,且不影响现有 CI/CD(如 Jenkins+ArgoCD)。

回答必须体现“国内合规”细节:等保 2.0 三级要求禁止默认账号、强制镜像签名(国密 SM2/SM3)、资源限额防止横向扩展攻击、日志留存 6 个月以上。

知识点

  1. OPA Gatekeeper 架构:Admission Webhook + Audit + Sync + Controller Manager;ConstraintTemplate 与 Constraint 分离设计。
  2. EAP Operator CR 关键字段:
    • spec.applicationImage:镜像地址;
    • spec.env:环境变量(如 JAVA_OPTS);
    • spec.resources:CPU/Memory limit/request;
    • spec.securityContext:runAsNonRoot、fsGroup;
    • spec.configMaps:挂载 standalone.xml 片段;
    • spec.deployments:副本数、调度亲和性。
  3. Rego 语法:deny[msg] 规则,input.review.object 为待审资源,input.parameters 为策略参数。
  4. 国产合规基线示例:
    • 镜像必须来自内部 Harbor 且带 harbor.example.com/eap 前缀;
    • 必须声明 resources.limits.memory ≤ 4Gi,防止容器 OOM 影响宿主机;
    • 禁止环境变量中出现 -Dcom.sun.management.jmxremote(关闭明文 JMX);
    • 必须挂载只读的 /etc/pki/ca-trust 国密根证书卷;
    • 禁止 spec.securityContext.runAsUser=0。
  5. 灰度发布:利用 Gatekeeper 的 enforcementAction: dryrun 先审计再强制;与 ArgoCD 的 SyncWave 配合,保证策略先于 EAP 实例生效。
  6. 审计闭环:Gatekeeper Audit 每 60 秒扫描集群,将违规写入 Prometheus metrics(gatekeeper_violations),由阿里云 SLS 或华为云 LTS 侧车采集,对接内部 SOC 平台。

答案

步骤一:构建 ConstraintTemplate
以“镜像白名单”为例,编写 template.yaml:

apiVersion: templates.gatekeeper.sh/v1beta1 kind: ConstraintTemplate metadata: name: eapimagewhitelist spec: crd: spec: names: kind: EAPImageWhitelist validation: openAPIV3Schema: type: object properties: registries: type: array items: {type: string} targets: - target: admission.k8s.gatekeeper.sh rego: | package eap.imagewhitelist violation[{"msg": msg}] { input.review.kind.kind == "WildFlyServer" image := input.review.object.spec.applicationImage not startswith(image, input.parameters.registries[_]) msg := sprintf("EAP image '%v' is not from approved registry", [image]) }

步骤二:实例化 Constraint
apply constraint.yaml:

apiVersion: constraints.gatekeeper.sh/v1beta1 kind: EAPImageWhitelist metadata: name: eap-must-use-internal-harbor spec: match: kinds: - apiGroups: ["wildfly.org"] kinds: ["WildFlyServer"] parameters: registries: ["harbor.example.com/eap"]

步骤三:覆盖全部基线
再创建三个 ConstraintTemplate:

  1. eapresources:校验 limits.memory≤4Gi、cpu≤2;
  2. eapenvblacklist:禁止 JMX 明文、默认用户;
  3. eapsecuritycontext:runAsNonRoot=true,只读根证书卷。

步骤四:接入 CI
在 GitLab CI 中增加 opa test 阶段,使用 gator test 对样例 CR 做单元测试;MR 合并后,ArgoCD 自动同步 Constraint 到集群,初始 enforcementAction 为 dryrun,观察一周无业务异常后改为 deny。

步骤五:审计与应急
Gatekeeper Audit 生成 Prometheus 指标,Grafana 大盘展示违规趋势;若出现误杀,可在 Constraint 中增加 namespaceSelector 排除测试环境,10 分钟内生效,满足国内“变更窗口≤30 分钟”的运维规范。

拓展思考

  1. 动态参数:利用 Gatekeeper 的 Config 资源,将镜像白名单、资源上限做成 ConfigMap 级联,无需重启 Gatekeeper 即可实时调整,适应“一行两会”季度检查。
  2. 多集群治理:在总部“监管集群”部署 Gatekeeper Policy Manager,通过 KubeFed 或国产“集群联邦”方案,把基线策略下发到 20+ 城商行边缘集群,实现“一次编写、全网强制”。
  3. 性能调优:EAP 启动本就耗时,Gatekeeper 增加 30~50 ms 延迟,可通过 namespaceSelector 限流、启用 failurePolicy: Fail 但 timeout=3s,并配合本地 Katacontainer 池化,保证 P99 延迟 < 100 ms。
  4. 国密场景:若镜像需内置国密 JCE,可在 Rego 中校验镜像 label smcrypto=enabled,并联动 Harbor 的“镜像签名”Webhook,确保摘要与等保办备案一致。