首页 > 解决方案 > Kubernetes 中的 OIDC 反向代理 sidecar

问题描述

我正在尝试保护 kubernetes 上的 java 应用程序。

对于一个带有 permitAll 的简单 Springboot 应用程序,我选择了带有 lua-resty-openidc 的 openresty (nginx) 作为反向代理。

一个例子主要说明了我正在尝试做的事情:https ://medium.com/@lukas.eichler/securing-pods-with-sidecar-proxies-d84f8d34be3e

它在 localhost 中“有效”,但在 kubernetes 上无效。

这是我的 nginx.conf :

worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  debug;

events {
    worker_connections  1024;
}

http {
    lua_package_path '~/lua/?.lua;;';

    resolver ${dns.ip};

    lua_ssl_trusted_certificate /ssl/certs/chain.pem;
    lua_ssl_verify_depth 5;

    lua_shared_dict discovery 1m;
    lua_shared_dict jwks 1m;
    lua_shared_dict introspection 10m;

    lua_shared_dict sessions 10m;

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        server_name  localhost;
        listen       80;

        location /OAuth2Client {
            access_by_lua_block {
                local opts = {
                    discovery = "${openam-provider}/.well-known/openid-configuration",
                    redirect_uri = "http://localhost:8080/OAuth2Client/authorization-code/callback",
                    client_id = "myClientId",
                    client_secret = "myClientSecret",
                    scope = "openid profile email",
                }

                local res, err = require("resty.openidc").authenticate(opts)

                if err then
                    ngx.status = 500
                    ngx.say(err)
                    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
                end

                ngx.req.set_header("Authorization", "Bearer " .. res.access_token)
                ngx.req.set_header("X-USER", res.id_token.sub)
            }

            proxy_pass  http://localhost:8080/OAuth2Client;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

所以在本地,当我的 nginx 和我的 springboot 应用程序在本地主机上运行时,重定向正在工作。

现在,当我使用它在 kubernetes 上部署它时,浏览器不会将 localhost 与内部容器 ip 映射。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: oauth2-client-deployment
spec:
  selector:
    matchLabels:
      app: OAuth2Client
  replicas: 2
  template:
    metadata:
      labels:
        app: OAuth2Client
    spec:
      #hostAliases:
      #- ip: "127.0.0.1"
      #  hostnames:
      #  - "oauth2-client.local"
      containers:
      - name: oauth2-client-container
        image: repo/oauth2-client-springboot:latest
        env:
          - name: SPRING_PROFILES_ACTIVE
            value: dev
        envFrom:
          - secretRef: 
              name: openam-client-secret
          - secretRef: 
              name: keystore-java-opts
        volumeMounts:
          - name: oauth2-client-keystore
            mountPath: "/projected"
            readOnly: true
        ports:
          - containerPort: 8080
      - name: oauth2-sidecar
        image: repo/oauth2-sidecar:latest
        ports:
          - containerPort: 80
      volumes:
        - name: oauth2-client-keystore
          projected:
            sources:
              - secret:
                   name: keystore-secret
                   items:
                    - key: keystore.jks
                      path: keystore.jks
              - secret:
                   name: truststore-secret
                   items:
                    - key: truststore.jks
                      path: truststore.jks
      imagePullSecrets:
      - name: regcred

---

apiVersion: v1
kind: Service
metadata:
  name: oauth2-client-service-sidecar
spec:
  selector:
    app: OAuth2Client
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

那么我如何映射这个 localhost 呢?我不希望应用程序容器被暴露,因为它没有安全性,这就是为什么我使用 nginx 作为 sidecar 并且服务只针对它。如何告诉 nginx redirect_uri 和 proxypass 到应用程序容器 ip ?

和附属问题:由于 nginx 不接受 env 变量,我应该如何使其通用,所以应用程序可以提供自己的 redirect_uri 应该在 nginx.conf 中使用?

另一个附属问题:命令 ngx.req.set_header("Authorization", "Bearer" .. res.access_token) 似乎不起作用,因为我在我的应用程序的请求中没有看到任何授权标头...

标签: nginxkubernetesoauth-2.0openid-connectlua-resty-openidc

解决方案


将您的服务配置ClusterIP只能在内部访问,然后在您的服务中使用 fqdn 来访问没有 IP 依赖​​关系的服务。

apiVersion: v1
kind: Service
metadata:
  name: oauth2-client-service-sidecar
spec:
  selector:
    app: OAuth2Client
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP

然后oauth2-client-service-sidecar.<namespacen>.cluster.local在您的 nginx 配置中使用以访问该服务:

proxy_pass  http://oauth2-client-service-sidecar.<namespacen>.cluster.local/OAuth2Client;

推荐阅读