使用 Traefik 中間件處理 Log4J 漏洞
Traefik 的中間件是最讓人喜歡的一個(gè)功能,為了能夠擴(kuò)展中間件,Traefik 官方還推出了 Pilot(https://pilot.traefik.io/) 這樣的中間件 SaaS 服務(wù),和 Traefik 深度集成,我們可以非常方便的使用平臺(tái)上提供的各種中間件,在 Dashboard 上就可以無縫進(jìn)行對接:
Log4Shell(https://github.com/traefik/plugin-log4shell) 就是一個(gè)解決最近很火的 Log4J 漏洞的 Traefik 插件,相關(guān)介紹:https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44228。不過要使用該中間件需要 Traefik >= v.2.5.5 版本。
使用也是非常簡單的,首先通過靜態(tài)配置啟用該插件,可以通過 Traefik 啟動(dòng)參數(shù)配置:
- --pilot.token=xxx # 去 pilot 注冊實(shí)例獲取的 token
- --experimental.plugins.log4shell.modulename=github.com/traefik/plugin-log4shell
- --experimental.plugins.log4shell.version=v0.1.2
或者配置文件:
- pilot:
- token: xxx
- experimental:
- plugins:
- log4shell:
- modulename: github.com/traefik/plugin-log4shell
- version: v0.1.2
為了使用 Log4Shell 插件我們首先需要?jiǎng)?chuàng)建一個(gè)中間件,比如在 Kubernetes 系統(tǒng)中,只需要?jiǎng)?chuàng)建如下所示的資源對象即可:
- apiVersion: traefik.containo.us/v1alpha1
- kind: Middleware
- metadata:
- name: log4shell-foo
- spec:
- plugin:
- log4shell:
- errorCode: 200
然后在 IngressRoute 中關(guān)聯(lián)上上面的中間件即可修復(fù):
- apiVersion: traefik.containo.us/v1alpha1
- kind: IngressRoute
- metadata:
- name: whoami
- spec:
- entryPoints:
- - web
- routes:
- - kind: Rule
- match: Host(`whoami.localhost`)
- middlewares:
- - name: log4shell-foo
- services:
- - kind: Service
- name: whoami-svc
- port: 80
當(dāng)然如果使用的是默認(rèn)的 Ingress 資源對象,則需要通過 annotation 注解來配置:
- apiVersion: networking.k8s.io/v1
- kind: Ingress
- metadata:
- name: myingress
- annotations:
- traefik.ingress.kubernetes.io/router.middlewares: default-log4shell-foo@kubernetescrd
- spec:
- ingressClassName: traefik
- rules:
- - host: whoami.localhost
- http:
- paths:
- - path: /
- pathType: Prefix
- backend:
- service:
- name: whoami
- port:
- number: 80
同樣如果是使用 Docker 環(huán)境則需要通過 labels 標(biāo)簽進(jìn)行配置:
- version: '3.7'
- services:
- whoami:
- image: traefik/whoami:v1.7.1
- labels:
- traefik.enable: 'true'
- traefik.http.routers.app.rule: Host(`whoami.localhost`)
- traefik.http.routers.app.entrypoints: websecure
- traefik.http.routers.app.middlewares: log4shell-foo
- traefik.http.middlewares.log4shell-foo.plugin.log4shell.errorcode: 200