Traefik 的中间件是最让人喜欢的一个功能,为了能够扩展中间件,Traefik 官方还推出了 Pilot(https://pilot.traefik.io/) 这样的中间件 SaaS 服务,和 Traefik 深度集成,我们可以非常方便的使用平台上提供的各种中间件,在 Dashboard 上就可以无缝进行对接:
Log4Shell(https://github.com/traefik/plugin-log4shell) 就是一个解决最近很火的 Log4J 漏洞的 Traefik 插件,相关介绍:https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2021-44228。不过要使用该中间件需要 Traefik >= v.2.5.5 版本。
使用也是非常简单的,首先通过静态配置启用该插件,可以通过 Traefik 启动参数配置:
复制
--pilot.token=xxx # 去 pilot 注册实例获取的 token --experimental.plugins.log4shell.modulename=github.com/traefik/plugin-log4shell --experimental.plugins.log4shell.version=v0.1.2
1.
2.
3.
或者配置文件:
复制
pilot: token: xxx experimental: plugins: log4shell: modulename: github.com/traefik/plugin-log4shell version: v0.1.2
1.
2.
3.
4.
5.
6.
7.
8.
为了使用 Log4Shell 插件我们首先需要创建一个中间件,比如在 Kubernetes 系统中,只需要创建如下所示的资源对象即可:
复制
apiVersion: traefik.containo.us/v1alpha1 kind: Middleware metadata: name: log4shell-foo spec: plugin: log4shell: errorCode: 200
1.
2.
3.
4.
5.
6.
7.
8.
然后在 IngressRoute 中关联上上面的中间件即可修复:
复制
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
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
当然如果使用的是默认的 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
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
同样如果是使用 Docker 环境则需要通过 labels 标签进行配置:
复制
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
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.