Replace auth cache key generation Lua impl with NJS impl

This commit is contained in:
Elizabeth Martin Campos 2024-11-23 18:53:18 +01:00
parent bc4b3170ff
commit 00774274ec
No known key found for this signature in database
GPG key ID: 5338FD45A0CE142D
4 changed files with 16 additions and 12 deletions

View file

@ -0,0 +1,7 @@
const crypto = require('crypto');
function cache_key(req) {
return crypto.createHash('sha1').update(req.variables.tmp_cache_key).digest('base64');
}
export default { cache_key };

View file

@ -12,6 +12,8 @@
# setup custom paths that do not require root access # setup custom paths that do not require root access
pid {{ .PID }}; pid {{ .PID }};
load_module /etc/nginx/modules/ngx_http_js_module.so;
{{ if $cfg.UseGeoIP2 }} {{ if $cfg.UseGeoIP2 }}
load_module /etc/nginx/modules/ngx_http_geoip2_module.so; load_module /etc/nginx/modules/ngx_http_geoip2_module.so;
{{ end }} {{ end }}
@ -74,6 +76,10 @@ http {
init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua; init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua;
js_import /etc/nginx/js/nginx/ngx_conf_rewrite_auth.js;
js_set $njs_cache_key ngx_conf_rewrite_auth.cache_key;
{{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}} {{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}}
{{/* we use the value of the real IP for the geo_ip module */}} {{/* we use the value of the real IP for the geo_ip module */}}
{{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }} {{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }}
@ -988,9 +994,6 @@ stream {
{{ if $externalAuth.AuthCacheKey }} {{ if $externalAuth.AuthCacheKey }}
set $tmp_cache_key '{{ $server.Hostname }}{{ $authPath }}{{ $externalAuth.AuthCacheKey }}'; set $tmp_cache_key '{{ $server.Hostname }}{{ $authPath }}{{ $externalAuth.AuthCacheKey }}';
set $cache_key '';
rewrite_by_lua_file /etc/nginx/lua/nginx/ngx_conf_rewrite_auth.lua;
proxy_cache auth_cache; proxy_cache auth_cache;
@ -998,7 +1001,7 @@ stream {
proxy_cache_valid {{ $dur }}; proxy_cache_valid {{ $dur }};
{{- end }} {{- end }}
proxy_cache_key "$cache_key"; proxy_cache_key "$njs_cache_key";
{{ end }} {{ end }}
# ngx_auth_request module overrides variables in the parent request, # ngx_auth_request module overrides variables in the parent request,

View file

@ -21,7 +21,6 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
"regexp"
"strings" "strings"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -341,11 +340,9 @@ var _ = framework.DescribeAnnotation("auth-*", func() {
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations) ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
f.EnsureIngress(ing) f.EnsureIngress(ing)
cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)
f.WaitForNginxServer(host, f.WaitForNginxServer(host,
func(server string) bool { func(server string) bool {
return cacheRegex.MatchString(server) && return strings.Contains(server, "proxy_cache_key \"$njs_cache_key\";") &&
strings.Contains(server, `proxy_cache_valid 200 202 401 30m;`) strings.Contains(server, `proxy_cache_valid 200 202 401 30m;`)
}) })
}) })

View file

@ -20,7 +20,6 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"regexp"
"strings" "strings"
"github.com/onsi/ginkgo/v2" "github.com/onsi/ginkgo/v2"
@ -169,11 +168,9 @@ var _ = framework.DescribeSetting("[Security] global-auth-url", func() {
globalExternalAuthURLSetting: globalExternalAuthURL, globalExternalAuthURLSetting: globalExternalAuthURL,
}) })
cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)
f.WaitForNginxServer(host, f.WaitForNginxServer(host,
func(server string) bool { func(server string) bool {
return cacheRegex.MatchString(server) && return strings.Contains(server, "proxy_cache_key \"$njs_cache_key\";") &&
strings.Contains(server, `proxy_cache_valid 200 201 401 30m;`) strings.Contains(server, `proxy_cache_valid 200 201 401 30m;`)
}) })