Skip to content

Commit 7356c4f

Browse files
rikatzGacko
andauthored
Lua: Extract external auth into file. (#12250)
Co-authored-by: Marco Ebert <[email protected]>
1 parent 6608eb2 commit 7356c4f

File tree

5 files changed

+40
-28
lines changed

5 files changed

+40
-28
lines changed

internal/ingress/controller/template/template.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -602,17 +602,12 @@ func buildAuthResponseHeaders(proxySetHeader string, headers []string, lua bool)
602602
return res
603603
}
604604

605-
func buildAuthUpstreamLuaHeaders(headers []string) []string {
606-
res := []string{}
607-
605+
func buildAuthUpstreamLuaHeaders(headers []string) string {
608606
if len(headers) == 0 {
609-
return res
607+
return ""
610608
}
611609

612-
for i, h := range headers {
613-
res = append(res, fmt.Sprintf("ngx.var.authHeader%d = res.header['%s']", i, h))
614-
}
615-
return res
610+
return strings.Join(headers, ",")
616611
}
617612

618613
func buildAuthProxySetHeaders(headers map[string]string) []string {

internal/ingress/controller/template/template_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -537,10 +537,7 @@ func TestBuildAuthResponseHeaders(t *testing.T) {
537537

538538
func TestBuildAuthResponseLua(t *testing.T) {
539539
externalAuthResponseHeaders := []string{"h1", "H-With-Caps-And-Dashes"}
540-
expected := []string{
541-
"ngx.var.authHeader0 = res.header['h1']",
542-
"ngx.var.authHeader1 = res.header['H-With-Caps-And-Dashes']",
543-
}
540+
expected := "h1,H-With-Caps-And-Dashes"
544541

545542
headers := buildAuthUpstreamLuaHeaders(externalAuthResponseHeaders)
546543

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
local auth_path = ngx.var.auth_path
2+
local auth_keepalive_share_vars = ngx.var.auth_keepalive_share_vars
3+
local auth_response_headers = ngx.var.auth_response_headers
4+
local ngx_re_split = require("ngx.re").split
5+
local ipairs = ipairs
6+
local ngx_log = ngx.log
7+
local ngx_ERR = ngx.ERR
8+
9+
local res = ngx.location.capture(auth_path, {
10+
method = ngx.HTTP_GET, body = '',
11+
share_all_vars = auth_keepalive_share_vars })
12+
13+
if res.status == ngx.HTTP_OK then
14+
local header_parts, err = ngx_re_split(auth_response_headers, ",")
15+
if err then
16+
ngx_log(ngx_ERR, err)
17+
return
18+
end
19+
ngx.var.auth_cookie = res.header['Set-Cookie']
20+
for i, header_name in ipairs(header_parts) do
21+
local varname = "authHeader" .. tostring(i)
22+
ngx.var[varname] = res.header[header_name]
23+
end
24+
return
25+
end
26+
27+
if res.status == ngx.HTTP_UNAUTHORIZED or res.status == ngx.HTTP_FORBIDDEN then
28+
ngx.exit(res.status)
29+
end
30+
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)

rootfs/etc/nginx/template/nginx.tmpl

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,20 +1185,10 @@ stream {
11851185
{{- end }}
11861186
# `auth_request` module does not support HTTP keepalives in upstream block:
11871187
# https://trac.nginx.org/nginx/ticket/1579
1188-
access_by_lua_block {
1189-
local res = ngx.location.capture('{{ $authPath }}', { method = ngx.HTTP_GET, body = '', share_all_vars = {{ $externalAuth.KeepaliveShareVars }} })
1190-
if res.status == ngx.HTTP_OK then
1191-
ngx.var.auth_cookie = res.header['Set-Cookie']
1192-
{{- range $line := buildAuthUpstreamLuaHeaders $externalAuth.ResponseHeaders }}
1193-
{{ $line }}
1194-
{{- end }}
1195-
return
1196-
end
1197-
if res.status == ngx.HTTP_UNAUTHORIZED or res.status == ngx.HTTP_FORBIDDEN then
1198-
ngx.exit(res.status)
1199-
end
1200-
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
1201-
}
1188+
set $auth_path '{{ $authPath }}';
1189+
set $auth_keepalive_share_vars {{ $externalAuth.KeepaliveShareVars }};
1190+
set $auth_response_headers '{{ buildAuthUpstreamLuaHeaders $externalAuth.ResponseHeaders }}';
1191+
access_by_lua_file /etc/nginx/lua/nginx/ngx_conf_external_auth.lua;
12021192
{{ else }}
12031193
auth_request {{ $authPath }};
12041194
auth_request_set $auth_cookie $upstream_http_set_cookie;

test/e2e/annotations/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ http {
653653
func(server string) bool {
654654
return strings.Contains(server, `upstream auth-external-auth`) &&
655655
strings.Contains(server, `keepalive 10;`) &&
656-
strings.Contains(server, `share_all_vars = false`)
656+
strings.Contains(server, `set $auth_keepalive_share_vars false;`)
657657
})
658658
})
659659

@@ -673,7 +673,7 @@ http {
673673
func(server string) bool {
674674
return strings.Contains(server, `upstream auth-external-auth`) &&
675675
strings.Contains(server, `keepalive 10;`) &&
676-
strings.Contains(server, `share_all_vars = true`)
676+
strings.Contains(server, `set $auth_keepalive_share_vars true;`)
677677
})
678678
})
679679
})

0 commit comments

Comments
 (0)