【问题标题】:Kubernetes ingress custom JWT authentication cache keyKubernetes 入口自定义 JWT 身份验证缓存密钥
【发布时间】:2021-10-04 09:18:30
【问题描述】:

我们将 Kubernetes 入口与外部服务 JWT authentication 结合使用,并将 auth-url 作为入口的一部分。

现在我们要使用auth-cache-key 注解来控制 JWT 令牌的缓存。目前,我们的外部身份验证服务只需通过查看令牌来回复 200/401。我们所有的组件都是带有rest api的后端微服务。传入的请求可能不是 UI 请求。我们如何为传入的 JWT 令牌填写 `auth-cache-key'。

  annotations:
    nginx.ingress.kubernetes.io/auth-url: http://auth-service/validate
    nginx.ingress.kubernetes.io/auth-response-headers: "authorization"
    nginx.ingress.kubernetes.io/auth-cache-key: '$remote_user$http_authorization'
    nginx.ingress.kubernetes.io/auth-cache-duration: '1m'
    kubernetes.io/ingress.class: "nginx"

看示例,$remote_user$http_authorization 在 K8s 文档中被指定为示例。但是不确定在我们的案例中是否会设置$remote_user。因为这不是外部基本身份验证。在这种情况下,我们如何确定 auth 缓存键?

没有足够的示例/文档。

【问题讨论】:

  • 您使用的是哪个 Kubernetes 和 NGINX Ingress 控制器版本?
  • 您能否解释一下“传入的请求可能不是 UI 请求。”和“在这种情况下,我们如何确定身份验证缓存键?”您的问题到底是什么?你想达到吗?
  • 您能否提供当前设置的最小可重现示例?

标签: authentication kubernetes ingress-nginx


【解决方案1】:

发布一般答案,没有提供进一步的细节和解释。

确实没有那么多文档,所以我决定深入研究NGINX Ingress source code

注解nginx.ingress.kubernetes.io/auth-cache-key中设置的值是变量$externalAuth.AuthCacheKeyin code

{{ if $externalAuth.AuthCacheKey }}
set $tmp_cache_key '{{ $server.Hostname }}{{ $authPath }}{{ $externalAuth.AuthCacheKey }}';
set $cache_key '';

可以看到,$externalAuth.AuthCacheKey 被变量$tmp_cache_key 使用,它被编码为base64 格式并使用lua NGINX module 设置为变量$cache_key

rewrite_by_lua_block {
    ngx.var.cache_key = ngx.encode_base64(ngx.sha1_bin(ngx.var.tmp_cache_key))
}

那么$cache_key用来设置variable $proxy_cache_key which defines a key for caching

proxy_cache_key "$cache_key";

基于上面的代码,我们可以假设我们可以使用任何NGINX variable来设置nginx.ingress.kubernetes.io/auth-cache-key注解。请注意,某些变量仅在 corresponding module is loaded 时可用。

示例 - 我设置了以下 auth-cache-key 注释:

nginx.ingress.kubernetes.io/auth-cache-key: '$proxy_host$request_uri'

然后,在 NGINX Ingress 控制器 pod 上,在文件 /etc/nginx/nginx.conf 中有以下行:

set $tmp_cache_key '{my-host}/_external-auth-Lw-Prefix$proxy_host$request_uri';

如果你将auth-cache-key注解设置为不存在的NGINX变量,NGINX会抛出如下错误:

nginx: [emerg] unknown "nonexistent_variable" variable

取决于你需要哪些变量。

请同时查看以下文章和主题:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-25
    • 2018-02-27
    • 2018-04-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2023-01-22
    相关资源
    最近更新 更多