Add keepalive support for auth requests (#8219)
* Add keepalive support for auth requests * Fix typo * Address PR comments * Log warning when auth-url contains variable in its host:port * Generate upstream name without replacing dots to underscores in server name * Add comment in the nginx template when the keepalive upstream block is referenced * Workaround for auth_request module ignores keepalive in upstream block * The `auth_request` module does not support HTTP keepalives in upstream block: https://trac.nginx.org/nginx/ticket/1579 * As a workaround we use ngx.location.capture but unfortunately it does not support HTTP/2 so `use-http2` configuration parameter is needed. * Handle PR comments * Address PR comments * Handle invalid values for int parameters * Handle PR comments * Fix e2e test
This commit is contained in:
parent
5e322f79a1
commit
83ce21b4dd
7 changed files with 570 additions and 12 deletions
|
|
@ -610,7 +610,25 @@ http {
|
|||
{{ end }}
|
||||
|
||||
{{ range $server := $servers }}
|
||||
{{ range $location := $server.Locations }}
|
||||
{{ $applyGlobalAuth := shouldApplyGlobalAuth $location $all.Cfg.GlobalExternalAuth.URL }}
|
||||
{{ $applyAuthUpstream := shouldApplyAuthUpstream $location $all.Cfg }}
|
||||
{{ if and (eq $applyAuthUpstream true) (eq $applyGlobalAuth false) }}
|
||||
## start auth upstream {{ $server.Hostname }}{{ $location.Path }}
|
||||
upstream {{ buildAuthUpstreamName $location $server.Hostname }} {
|
||||
{{- $externalAuth := $location.ExternalAuth }}
|
||||
server {{ extractHostPort $externalAuth.URL }};
|
||||
|
||||
keepalive {{ $externalAuth.KeepaliveConnections }};
|
||||
keepalive_requests {{ $externalAuth.KeepaliveRequests }};
|
||||
keepalive_timeout {{ $externalAuth.KeepaliveTimeout }}s;
|
||||
}
|
||||
## end auth upstream {{ $server.Hostname }}{{ $location.Path }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ range $server := $servers }}
|
||||
## start server {{ $server.Hostname }}
|
||||
server {
|
||||
server_name {{ buildServerName $server.Hostname }} {{range $server.Aliases }}{{ . }} {{ end }};
|
||||
|
|
@ -995,6 +1013,7 @@ stream {
|
|||
{{ $proxySetHeader := proxySetHeader $location }}
|
||||
{{ $authPath := buildAuthLocation $location $all.Cfg.GlobalExternalAuth.URL }}
|
||||
{{ $applyGlobalAuth := shouldApplyGlobalAuth $location $all.Cfg.GlobalExternalAuth.URL }}
|
||||
{{ $applyAuthUpstream := shouldApplyAuthUpstream $location $all.Cfg }}
|
||||
|
||||
{{ $externalAuth := $location.ExternalAuth }}
|
||||
{{ if eq $applyGlobalAuth true }}
|
||||
|
|
@ -1074,7 +1093,6 @@ stream {
|
|||
proxy_buffer_size {{ $location.Proxy.BufferSize }};
|
||||
proxy_buffers {{ $location.Proxy.BuffersNumber }} {{ $location.Proxy.BufferSize }};
|
||||
proxy_request_buffering {{ $location.Proxy.RequestBuffering }};
|
||||
proxy_http_version {{ $location.Proxy.ProxyHTTPVersion }};
|
||||
|
||||
proxy_ssl_server_name on;
|
||||
proxy_pass_request_headers on;
|
||||
|
|
@ -1103,7 +1121,19 @@ stream {
|
|||
{{ $externalAuth.AuthSnippet }}
|
||||
{{ end }}
|
||||
|
||||
{{ if and (eq $applyAuthUpstream true) (eq $applyGlobalAuth false) }}
|
||||
{{ $authUpstreamName := buildAuthUpstreamName $location $server.Hostname }}
|
||||
# The target is an upstream with HTTP keepalive, that is why the
|
||||
# Connection header is cleared and the HTTP version is set to 1.1 as
|
||||
# the Nginx documentation suggests:
|
||||
# http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Connection "";
|
||||
set $target {{ changeHostPort $externalAuth.URL $authUpstreamName }};
|
||||
{{ else }}
|
||||
proxy_http_version {{ $location.Proxy.ProxyHTTPVersion }};
|
||||
set $target {{ $externalAuth.URL }};
|
||||
{{ end }}
|
||||
proxy_pass $target;
|
||||
}
|
||||
{{ end }}
|
||||
|
|
@ -1208,13 +1238,37 @@ stream {
|
|||
{{ if not (isLocationInLocationList $location $all.Cfg.NoAuthLocations) }}
|
||||
{{ if $authPath }}
|
||||
# this location requires authentication
|
||||
{{ if and (eq $applyAuthUpstream true) (eq $applyGlobalAuth false) }}
|
||||
set $auth_cookie '';
|
||||
add_header Set-Cookie $auth_cookie;
|
||||
{{- range $line := buildAuthResponseHeaders $proxySetHeader $externalAuth.ResponseHeaders true }}
|
||||
{{ $line }}
|
||||
{{- end }}
|
||||
# `auth_request` module does not support HTTP keepalives in upstream block:
|
||||
# https://trac.nginx.org/nginx/ticket/1579
|
||||
access_by_lua_block {
|
||||
local res = ngx.location.capture('{{ $authPath }}', { method = ngx.HTTP_GET, body = '' })
|
||||
if res.status == ngx.HTTP_OK then
|
||||
ngx.var.auth_cookie = res.header['Set-Cookie']
|
||||
{{- range $line := buildAuthUpstreamLuaHeaders $externalAuth.ResponseHeaders }}
|
||||
{{ $line }}
|
||||
{{- end }}
|
||||
return
|
||||
end
|
||||
if res.status == ngx.HTTP_FORBIDDEN then
|
||||
ngx.exit(res.status)
|
||||
end
|
||||
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
|
||||
}
|
||||
{{ else }}
|
||||
auth_request {{ $authPath }};
|
||||
auth_request_set $auth_cookie $upstream_http_set_cookie;
|
||||
add_header Set-Cookie $auth_cookie;
|
||||
{{- range $line := buildAuthResponseHeaders $proxySetHeader $externalAuth.ResponseHeaders }}
|
||||
{{- range $line := buildAuthResponseHeaders $proxySetHeader $externalAuth.ResponseHeaders false }}
|
||||
{{ $line }}
|
||||
{{- end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ if $externalAuth.SigninURL }}
|
||||
set_escape_uri $escaped_request_uri $request_uri;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue