2016-03-03 15:10:26 +03:00
|
|
|
local cjson = require("cjson")
|
|
|
|
local time = require("os")
|
|
|
|
local ltn12 = require("ltn12")
|
|
|
|
local io = require("io")
|
2016-03-02 11:36:36 +03:00
|
|
|
|
|
|
|
-- Load configuration
|
2016-03-03 15:10:26 +03:00
|
|
|
function readConf()
|
|
|
|
local file = '/etc/nginx/etc/swift.json'
|
2016-03-02 11:36:36 +03:00
|
|
|
local f, err = io.open(file, "rb")
|
|
|
|
local content = f:read("*all")
|
|
|
|
f:close()
|
2016-03-03 15:10:26 +03:00
|
|
|
|
|
|
|
local conf = cjson.decode(content)
|
|
|
|
return conf
|
2016-03-02 11:36:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Print nested tables
|
|
|
|
function content_from_table(tbl)
|
|
|
|
for i, v in pairs(tbl) do
|
|
|
|
if type(v) == "table" then
|
|
|
|
content_from_table(v)
|
|
|
|
else
|
|
|
|
ngx.print(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-03 15:10:26 +03:00
|
|
|
-- Write to log
|
|
|
|
function write_to_logfile(string)
|
|
|
|
local conf = readConf('/etc/nginx/etc/swift.json')
|
|
|
|
local l, err = io.open(conf.logfile, "a")
|
|
|
|
l:write(string .. "\n")
|
|
|
|
l:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
local conf = readConf()
|
2016-03-02 11:36:36 +03:00
|
|
|
|
|
|
|
-- Make authentication request to Ceph
|
|
|
|
headers_t = {}
|
|
|
|
headers_t["X-Auth-User"] = conf.user
|
|
|
|
headers_t["X-Auth-Key"] = conf.secret_key
|
|
|
|
|
|
|
|
local http = require("socket.http")
|
|
|
|
local r, c, h = http.request{
|
|
|
|
url = conf.auth_uri,
|
|
|
|
headers = headers_t,
|
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
|
|
|
|
-- Return 403 if not authorized
|
2016-03-03 15:10:26 +03:00
|
|
|
if c ~= 204 then
|
|
|
|
write_to_logfile(time.date() .. ' 403')
|
2016-03-02 11:36:36 +03:00
|
|
|
ngx.exit(ngx.HTTP_FORBIDDEN)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- OK, we get auth token now
|
|
|
|
local auth_token = ''
|
|
|
|
auth_token = h["x-auth-token"]
|
|
|
|
|
|
|
|
content_headers= {}
|
|
|
|
content_headers["X-Auth-Token"] = auth_token
|
|
|
|
|
2016-03-03 15:10:26 +03:00
|
|
|
write_to_logfile(time.date() .. " Auth OK. Token is: " .. auth_token)
|
|
|
|
|
2016-03-02 11:36:36 +03:00
|
|
|
|
|
|
|
t = {}
|
|
|
|
|
|
|
|
local resp, code, http_st = http.request{
|
|
|
|
method = "GET",
|
|
|
|
headers = content_headers,
|
|
|
|
url = conf.rados_uri .. conf.bucket .. ngx.var.path,
|
|
|
|
sink = ltn12.sink.table(t)
|
|
|
|
}
|
|
|
|
|
2016-03-03 15:10:26 +03:00
|
|
|
write_to_logfile(time.date() .. " URI: " .. conf.rados_uri .. conf.bucket .. ngx.var.path)
|
|
|
|
|
2016-03-02 11:36:36 +03:00
|
|
|
-- Return 404 if object not found in Ceph
|
|
|
|
if code ~= 200 then
|
2016-03-03 15:10:26 +03:00
|
|
|
write_to_logfile(time.date() .. " Not found")
|
2016-03-02 11:36:36 +03:00
|
|
|
ngx.exit(ngx.HTTP_NOT_FOUND)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Print object
|
|
|
|
content_from_table(t)
|