Skip to content

Commit 4d61aae

Browse files
committed
Replace auth cache key generation Lua impl with NJS impl
1 parent ceabf5b commit 4d61aae

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ http {
7474

7575
init_worker_by_lua_file /etc/nginx/lua/ngx_conf_init_worker.lua;
7676

77+
js_import /etc/nginx/js/nginx/ngx_conf_rewrite_auth.js;
78+
79+
js_set $njs_cache_key ngx_conf_rewrite_auth.cache_key;
80+
7781
{{/* Enable the real_ip module only if we use either X-Forwarded headers or Proxy Protocol. */}}
7882
{{/* we use the value of the real IP for the geo_ip module */}}
7983
{{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }}
@@ -988,17 +992,14 @@ stream {
988992

989993
{{ if $externalAuth.AuthCacheKey }}
990994
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;
994995

995996
proxy_cache auth_cache;
996997

997998
{{- range $dur := $externalAuth.AuthCacheDuration }}
998999
proxy_cache_valid {{ $dur }};
9991000
{{- end }}
10001001

1001-
proxy_cache_key "$cache_key";
1002+
proxy_cache_key "$njs_cache_key";
10021003
{{ end }}
10031004

10041005
# ngx_auth_request module overrides variables in the parent request,

test/e2e/settings/global_external_auth.go

+1-5
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,12 +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) &&
177-
strings.Contains(server, `proxy_cache_valid 200 201 401 30m;`)
173+
return strings.Contains(server, `proxy_cache_valid 200 201 401 30m;`)
178174
})
179175

180176
f.HTTPTestClient().

0 commit comments

Comments
 (0)