Skip to content
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

[WIP] perf: optimizations #13

Closed
wants to merge 10 commits into from
14 changes: 13 additions & 1 deletion conf/nginx.conf.tpl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
worker_processes 1;
worker_processes 2;

# expose env vars to lua code
env BENTO_DEBUG;
Expand Down Expand Up @@ -39,6 +39,12 @@ stream {
ssl_preread on;
proxy_pass $name;
}

log_format basic '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time "$upstream_addr" '
'"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';
access_log logs/access.log basic;
}
# tpl__tls_yes__end

Expand All @@ -54,6 +60,12 @@ http {
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio" "$uri"';

log_format upstream_time '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"'
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
access_log logs/access.log upstream_time;

# Set up per-server and per-address rate limiter
limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;
limit_req_zone $server_name zone=perserver:10m rate=40r/s;
Expand Down
26 changes: 24 additions & 2 deletions src/proxy_auth_v2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,29 @@ local err_500_and_log = function(detail, err)
end
-- END FUNCTION DEFINITIONS -----–-----–-----–-----–-----–-----–-----–-----–-----–-------

local req = ngx.req
local req_method = req.get_method()
local req_uri = ngx.var.request_uri -- pre-rewrite URI
local uri = ngx.var.uri -- post-rewrite URI

-- BEGIN OPEN ENDPOINT LOGIC ------------------------------------------------------------

-- Pass through all endpoint calls which used to be proxied by bento_public
-- TODO: replace this with properly authorization-compatible services

if req_method == "GET" and (
uri == "/service-info" or
req_uri == "/api/metadata/api/projects" or
req_uri == "/api/metadata/api/public" or
req_uri == "/api/metadata/api/public_overview" or
req_uri == "/api/metadata/api/public_search_fields" or
req_uri == "/api/metadata/api/public_dataset"
) then
goto script_end
end

-- END OPEN ENDPOINT LOGIC --------------------------------------------------------------

-- BEGIN AUTHORIZATION LOGIC ------------------------------------------------------------

local bento_debug = os.getenv("BENTO_DEBUG")
Expand All @@ -54,11 +77,10 @@ local user_role

-- Check bearer token if set
-- Adapted from https://github.com/zmartzone/lua-resty-openidc/issues/266#issuecomment-542771402
local req = ngx.req
local auth_header = req.get_headers()["Authorization"]

-- Tokens can also be passed in the form of POST body form data
if req.get_method() == "POST" then
if req_method == "POST" then
req.read_body()
local req_body = req.get_post_args()
if req_body ~= nil and req_body["token"] then
Expand Down