Skip to content

NGINX: Migrate auth cache key to NJS. #12447

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions rootfs/etc/nginx/js/nginx/ngx_conf_rewrite_auth.js
Original file line number Diff line number Diff line change
@@ -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 };
11 changes: 7 additions & 4 deletions rootfs/etc/nginx/template/nginx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# setup custom paths that do not require root access
pid {{ .PID }};

load_module /etc/nginx/modules/ngx_http_js_module.so;

{{ if $cfg.UseGeoIP2 }}
load_module /etc/nginx/modules/ngx_http_geoip2_module.so;
{{ end }}
Expand Down Expand Up @@ -74,6 +76,10 @@ http {

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. */}}
{{/* we use the value of the real IP for the geo_ip module */}}
{{ if or (or $cfg.UseForwardedHeaders $cfg.UseProxyProtocol) $cfg.EnableRealIP }}
Expand Down Expand Up @@ -988,17 +994,14 @@ stream {

{{ if $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;

{{- range $dur := $externalAuth.AuthCacheDuration }}
proxy_cache_valid {{ $dur }};
{{- end }}

proxy_cache_key "$cache_key";
proxy_cache_key "$njs_cache_key";
{{ end }}

# ngx_auth_request module overrides variables in the parent request,
Expand Down
5 changes: 1 addition & 4 deletions test/e2e/annotations/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"fmt"
"net/http"
"net/url"
"regexp"
"strings"

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

cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)

f.WaitForNginxServer(host,
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;`)
})
})
Expand Down
5 changes: 1 addition & 4 deletions test/e2e/settings/global_external_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"fmt"
"net/http"
"regexp"
"strings"

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

cacheRegex := regexp.MustCompile(`\$cache_key.*foo`)

f.WaitForNginxServer(host,
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;`)
})

Expand Down
Loading