Skip to content

Commit b9a7b81

Browse files
committed
Replace auth cache key generation Lua impl with NJS impl
1 parent bc4b317 commit b9a7b81

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const crypto = require('crypto');
2+
3+
function cache_key(req) {
4+
return crypto.createHash('sha1').update(req.variables.tmp_cache_key).digest('base64');
5+
}
6+
7+
export default { cache_key };

rootfs/etc/nginx/template/nginx.tmpl

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# setup custom paths that do not require root access
1313
pid {{ .PID }};
1414

15+
load_module modules/ngx_http_js_module.so;
16+
1517
{{ if $cfg.UseGeoIP2 }}
1618
load_module /etc/nginx/modules/ngx_http_geoip2_module.so;
1719
{{ end }}
@@ -74,6 +76,10 @@ http {
7476

7577
init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua;
7678

79+
js_import /etc/nginx/js/nginx/ngx_conf_rewrite_auth.js;
80+
81+
js_set $njs_cache_key ngx_conf_rewrite_auth.cache_key;
82+
7783
{{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}}
7884
{{/* we use the value of the real IP for the geo_ip module */}}
7985
{{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }}
@@ -988,17 +994,14 @@ stream {
988994

989995
{{ if $externalAuth.AuthCacheKey }}
990996
set $tmp_cache_key '{{ $server.Hostname }}{{ $authPath }}{{ $externalAuth.AuthCacheKey }}';
991-
set $cache_key '';
992-
993-
rewrite_by_lua_file /etc/nginx/lua/nginx/ngx_conf_rewrite_auth.lua;
994997

995998
proxy_cache auth_cache;
996999

9971000
{{- range $dur := $externalAuth.AuthCacheDuration }}
9981001
proxy_cache_valid {{ $dur }};
9991002
{{- end }}
10001003

1001-
proxy_cache_key "$cache_key";
1004+
proxy_cache_key "$njs_cache_key";
10021005
{{ end }}
10031006

10041007
# ngx_auth_request module overrides variables in the parent request,

test/e2e/annotations/auth.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"net/http"
2323
"net/url"
24-
"regexp"
2524
"strings"
2625

2726
"golang.org/x/crypto/bcrypt"
@@ -341,11 +340,9 @@ var _ = framework.DescribeAnnotation("auth-*", func() {
341340
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, annotations)
342341
f.EnsureIngress(ing)
343342

344-
cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)
345-
346343
f.WaitForNginxServer(host,
347344
func(server string) bool {
348-
return cacheRegex.MatchString(server) &&
345+
return strings.Contains(server, "proxy_cache_key \"$njs_cache_key\";") &&
349346
strings.Contains(server, `proxy_cache_valid 200 202 401 30m;`)
350347
})
351348
})

test/e2e/settings/global_external_auth.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"context"
2121
"fmt"
2222
"net/http"
23-
"regexp"
2423
"strings"
2524

2625
"github.com/onsi/ginkgo/v2"
@@ -169,11 +168,9 @@ var _ = framework.DescribeSetting("[Security] global-auth-url", func() {
169168
globalExternalAuthURLSetting: globalExternalAuthURL,
170169
})
171170

172-
cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)
173-
174171
f.WaitForNginxServer(host,
175172
func(server string) bool {
176-
return cacheRegex.MatchString(server) &&
173+
return strings.Contains(server, "proxy_cache_key \"$njs_cache_key\";") &&
177174
strings.Contains(server, `proxy_cache_valid 200 201 401 30m;`)
178175
})
179176

0 commit comments

Comments
 (0)