Skip to content

Commit

Permalink
Revamped and corrected LUA files addition, and revamped dockerfile to…
Browse files Browse the repository at this point in the history
… reduce image size
  • Loading branch information
acaranta committed Oct 16, 2020
1 parent b16ce81 commit 75a331a
Show file tree
Hide file tree
Showing 3 changed files with 909 additions and 15 deletions.
26 changes: 11 additions & 15 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,24 @@ MAINTAINER arthur@caranta.com
ENV DEBIAN_FRONTEND noninteractive
ENV INITRD No

#RUN echo "deb http://ppa.launchpad.net/vbernat/haproxy-1.5/ubuntu trusty main" >/etc/apt/sources.list.d/haproxy1.5.list
#RUN echo "deb-src http://ppa.launchpad.net/vbernat/haproxy-1.5/ubuntu trusty main" >>/etc/apt/sources.list.d/haproxy1.5.list

RUN apt-get update -y
#RUN apt-get install --force-yes -y supervisor haproxy inotify-tools python-pip
RUN apt-get install --force-yes -y haproxy inotify-tools python-pip curl lua-socket
RUN pip install envtpl supervisor supervisor-logging
ADD supervisord.conf.tpl /etc/supervisor/supervisord.conf.tpl
ADD dir-prereqs.sh /dir-prereqs.sh

#ADD supervisor.conf /etc/supervisor/conf.d/supervisor.conf
RUN apt-get install --force-yes -y haproxy inotify-tools python-pip curl lua-socket lua-json lua-http && \
pip install envtpl supervisor supervisor-logging && \
apt-get -y remove build-essential ".*-dev" && \
apt-get -y autoremove

ADD . /app
WORKDIR /app

RUN mkdir -p /etc/supervisor && cp /app/supervisord.conf.tpl /etc/supervisor/supervisord.conf.tpl
RUN cp /app/dir-prereqs.sh /dir-prereqs.sh
RUN cp /app/haproxy.cfg /etc/haproxy
RUN curl https://raw.githubusercontent.com/TimWolla/haproxy-auth-request/master/auth-request.lua -o /etc/haproxy/auth-request.lua
RUN apt-get -y remove build-essential ".*-dev"
RUN apt-get -y autoremove
#Source : https://raw.githubusercontent.com/haproxytech/haproxy-lua-http/master/http.lua
RUN cp /app/lua/haproxy-lua-http.lua /usr/share/lua/5.3/haproxy-lua-http.lua
#Source : https://raw.githubusercontent.com/TimWolla/haproxy-auth-request/master/auth-request.lua
RUN cp /app/lua/auth-request.lua /etc/haproxy/auth-request.lua

ENV TPLFILES /etc/supervisor/supervisord.conf

ENV HASVC hapconf.cfg
ENV SYSLOG_SERVER 127.0.0.1
ENV SYSLOG_PORT 514
Expand All @@ -34,5 +31,4 @@ VOLUME ["/hacfg"]

EXPOSE 80


CMD . ./dir-prereqs.sh && for FILE in $TPLFILES; do envtpl --keep-template --allow-missing $FILE.tpl; done && ./run.sh
119 changes: 119 additions & 0 deletions lua/auth-request.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
-- The MIT License (MIT)
--
-- Copyright (c) 2018 Tim Düsterhus
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.

local http = require("haproxy-lua-http")

function set_var_pre_2_2(txn, var, value)
return txn:set_var(var, value)
end
function set_var_post_2_2(txn, var, value)
return txn:set_var(var, value, true)
end

set_var = function(txn, var, value)
local success = pcall(set_var_post_2_2, txn, var, value)
if success then
set_var = set_var_post_2_2
else
set_var = set_var_pre_2_2
end

return set_var(txn, var, value)
end

function sanitize_header_for_variable(header)
return header:gsub("[^a-zA-Z0-9]", "_")
end


core.register_action("auth-request", { "http-req" }, function(txn, be, path)
set_var(txn, "txn.auth_response_successful", false)

-- Check whether the given backend exists.
if core.backends[be] == nil then
txn:Alert("Unknown auth-request backend '" .. be .. "'")
set_var(txn, "txn.auth_response_code", 500)
return
end

-- Check whether the given backend has servers that
-- are not `DOWN`.
local addr = nil
for name, server in pairs(core.backends[be].servers) do
local status = server:get_stats()['status']
if status == "no check" or status:find("UP") == 1 then
addr = server:get_addr()
break
end
end
if addr == nil then
txn:Warning("No servers available for auth-request backend: '" .. be .. "'")
set_var(txn, "txn.auth_response_code", 500)
return
end

-- Transform table of request headers from haproxy's to
-- socket.http's format.
local headers = {}
for header, values in pairs(txn.http:req_get_headers()) do
if header ~= 'content-length' then
for i, v in pairs(values) do
if headers[header] == nil then
headers[header] = v
else
headers[header] = headers[header] .. ", " .. v
end
end
end
end

-- Make request to backend.
local response, err = http.head {
url = "http://" .. addr .. path,
headers = headers,
}

-- Check whether we received a valid HTTP response.
if response == nil then
txn:Warning("Failure in auth-request backend '" .. be .. "': " .. err)
set_var(txn, "txn.auth_response_code", 500)
return
end

set_var(txn, "txn.auth_response_code", response.status_code)

for header, value in response:get_headers(true) do
set_var(txn, "req.auth_response_header." .. sanitize_header_for_variable(header), value)
end

-- 2xx: Allow request.
if 200 <= response.status_code and response.status_code < 300 then
set_var(txn, "txn.auth_response_successful", true)
-- Don't allow other codes.
-- Codes with Location: Passthrough location at redirect.
elseif response.status_code == 301 or response.status_code == 302 or response.status_code == 303 or response.status_code == 307 or response.status_code == 308 then
set_var(txn, "txn.auth_response_location", response:get_header("location", "last"))
-- 401 / 403: Do nothing, everything else: log.
elseif response.status_code ~= 401 and response.status_code ~= 403 then
txn:Warning("Invalid status code in auth-request backend '" .. be .. "': " .. response.status_code)
end
end, 2)
Loading

0 comments on commit 75a331a

Please sign in to comment.