commit 1d43131855e4abd218d6382a56ad6743ff61bb05 Author: Difrex Date: Wed Mar 2 11:36:36 2016 +0300 Working and debianized diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6b6f46e --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +build: + luac5.1 -o swift swift.lua + +clean: + rm -f swift diff --git a/README.md b/README.md new file mode 100644 index 0000000..35f5e63 --- /dev/null +++ b/README.md @@ -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'; + } +``` diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..cfc70c5 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +nginx-lua-swift (0.1-1) jessie; urgency=low + + * Initial release + + -- Denis Zheleztsov Wed, 02 Mar 2016 11:09:52 +0300 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..ad3193c --- /dev/null +++ b/debian/control @@ -0,0 +1,12 @@ +Source: nginx-lua-swift +Section: web +Priority: optional +Maintainer: Denis Zheleztsov +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 diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..48ff9bd --- /dev/null +++ b/debian/copyright @@ -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 +License: BSD 3-clause + Copyright (c) 2016, Denis Zheleztsov + 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. diff --git a/debian/docs b/debian/docs new file mode 100644 index 0000000..4c8babe --- /dev/null +++ b/debian/docs @@ -0,0 +1,2 @@ +README.md +nginx.sample diff --git a/debian/nginx-lua-swift.install b/debian/nginx-lua-swift.install new file mode 100644 index 0000000..7fc475e --- /dev/null +++ b/debian/nginx-lua-swift.install @@ -0,0 +1,2 @@ +swift etc/nginx/lua +swift.json etc/nginx/etc diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..e78d22b --- /dev/null +++ b/debian/rules @@ -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) + + + + diff --git a/nginx.sample b/nginx.sample new file mode 100644 index 0000000..ae35d25 --- /dev/null +++ b/nginx.sample @@ -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'; + } +} diff --git a/swift.json b/swift.json new file mode 100644 index 0000000..50ab2fa --- /dev/null +++ b/swift.json @@ -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" +} diff --git a/swift.lua b/swift.lua new file mode 100644 index 0000000..d2680b0 --- /dev/null +++ b/swift.lua @@ -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)