kubernetes 中的 OIDC 反向代理 sidecar

OIDC reverse proxy sidecar in kubernetes

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

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

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

它 "works" 在本地主机上,但在 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

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

还有一个附属问题:由于 nginx 不接受 env 变量,我应该怎么做才能使其通用,以便应用程序可以提供自己的 redirect_uri 应该在 nginx.conf 中使用?

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

将类型为 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

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

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