Working and debianized

This commit is contained in:
Difrex 2016-03-02 11:36:36 +03:00
commit 1d43131855
12 changed files with 206 additions and 0 deletions

5
Makefile Normal file
View File

@ -0,0 +1,5 @@
build:
luac5.1 -o swift swift.lua
clean:
rm -f swift

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# nginx-lua-swift
## Sample nginx configuration
```
location ~ /ceph(.*)$ {
set $path $1;
content_by_lua_file "/etc/nginx/lua/swift";
error_page 404 @404;
}
location @404 {
echo 'Do what you want';
}
```

5
debian/changelog vendored Normal file
View File

@ -0,0 +1,5 @@
nginx-lua-swift (0.1-1) jessie; urgency=low
* Initial release
-- Denis Zheleztsov <difrex.punk@gmail.com> Wed, 02 Mar 2016 11:09:52 +0300

1
debian/compat vendored Normal file
View File

@ -0,0 +1 @@
9

12
debian/control vendored Normal file
View File

@ -0,0 +1,12 @@
Source: nginx-lua-swift
Section: web
Priority: optional
Maintainer: Denis Zheleztsov <difrex.punk@gmail.com>
Build-Depends: debhelper (>= 9)
Standards-Version: 3.9.5
Vcs-Git: https://github.com/Difrex/nginx-lua-swift.git
Package: nginx-lua-swift
Architecture: amd64
Depends: nginx-extras, lua-cjson
Description: Nginx lua module to get objects from Swift storage

34
debian/copyright vendored Normal file
View File

@ -0,0 +1,34 @@
Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: nginx-lua-swift
Source: https://github.com/Difrex/nginx-lua-swift.git
Files: *
Copyright: 2016 Denis Zheleztsov <difrex.punk@gmail.com>
License: BSD 3-clause
Copyright (c) 2016, Denis Zheleztsov <difrex.punk@gmail.com>
All rights reserved.
.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
.
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
.
* Neither the name of nginx-lua-swift nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

2
debian/docs vendored Normal file
View File

@ -0,0 +1,2 @@
README.md
nginx.sample

2
debian/nginx-lua-swift.install vendored Normal file
View File

@ -0,0 +1,2 @@
swift etc/nginx/lua
swift.json etc/nginx/etc

38
debian/rules vendored Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#DH_VERBOSE = 1
# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/*
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk
# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
override_dh_auto_clean:
make clean
override_dh_auto_build:
make build
# main packaging script based on dh7 syntax
%:
dh $@
# debmake generated override targets
# This is example for Cmake (See http://bugs.debian.org/641051 )
#override_dh_auto_configure:
# dh_auto_configure -- \
# -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

16
nginx.sample Normal file
View File

@ -0,0 +1,16 @@
server {
listen 9999;
server_name _;
location ~ /ceph(.*)$ {
set $path $1;
content_by_lua_file "/etc/nginx/lua/swift.lua";
error_page 404 @404;
}
location @404 {
echo 'This is OK';
}
}

7
swift.json Normal file
View File

@ -0,0 +1,7 @@
{
"user": "user:swift",
"secret_key": "you_secret_key",
"auth_uri": "http://radosgw/auth/1.0",
"rados_uri": "http://radosgw/swift/v1/",
"bucket": "public"
}

67
swift.lua Normal file
View File

@ -0,0 +1,67 @@
local cjson = require "cjson"
-- Load configuration
function readConf(file)
local f, err = io.open(file, "rb")
local content = f:read("*all")
f:close()
return content
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
local json = readConf('/etc/nginx/etc/swift.json')
local conf = cjson.decode(json)
-- 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
if c ~= 200 then
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
local ltn12 = require("ltn12")
local io = require("io")
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)
}
-- Return 404 if object not found in Ceph
if code ~= 200 then
ngx.exit(ngx.HTTP_NOT_FOUND)
end
-- Print object
content_from_table(t)