-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/N5GEH/n5geh.tutorials.api-p…
- Loading branch information
Showing
12 changed files
with
345 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
FROM kong:latest | ||
|
||
LABEL description="Alpine + Kong + kong-oidc plugin + LUA Plugins" | ||
# Install the js-pluginserver | ||
|
||
USER root | ||
# RUN apk add --update nodejs npm python3 make g++ && rm -rf /var/cache/apk/* | ||
# RUN npm install --unsafe -g [email protected] | ||
|
@@ -12,9 +12,13 @@ RUN apk add --update vim nano | |
RUN apk update && apk add curl git gcc musl-dev | ||
RUN luarocks install luaossl OPENSSL_DIR=/usr/local/kong CRYPTO_DIR=/usr/local/kong | ||
RUN luarocks install --pin lua-resty-jwt | ||
RUN luarocks install kong-oidc | ||
# RUN luarocks install kong-oidc -- deprecated | ||
RUN luarocks install lunajson | ||
|
||
COPY ./luaplugins/oidc /plugins/oidc | ||
WORKDIR /plugins/oidc | ||
RUN luarocks make | ||
|
||
COPY ./luaplugins/query-checker /plugins/query-checker | ||
WORKDIR /plugins/query-checker | ||
RUN luarocks make | ||
|
@@ -27,4 +31,8 @@ COPY ./luaplugins/rbac /plugins/rbac | |
WORKDIR /plugins/rbac | ||
RUN luarocks make | ||
|
||
COPY ./luaplugins/scope-checker /plugins/scope-checker | ||
WORKDIR /plugins/scope-checker | ||
RUN luarocks make | ||
|
||
USER kong |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 32 additions & 18 deletions
50
luaplugins/multi-tenancy/plugins/multi-tenancy/filter.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,48 @@ | ||
local M = {} | ||
|
||
function split_token (inputstr, sep) | ||
function split_token(inputstr, sep) | ||
if sep == nil then | ||
sep = "%s" | ||
sep = "%s" | ||
end | ||
local t={} | ||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do | ||
table.insert(t, str) | ||
local t = {} | ||
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do | ||
table.insert(t, str) | ||
end | ||
return t | ||
end | ||
|
||
-- decoding | ||
function M.decode(token) | ||
b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' | ||
b = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" | ||
token_payload = split_token(token, ".") | ||
data = token_payload[2] | ||
data = string.gsub(data, '[^'..b..'=]', '') | ||
return (data:gsub('.', function(x) | ||
if (x == '=') then return '' end | ||
local r,f='',(b:find(x)-1) | ||
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end | ||
return r; | ||
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x) | ||
if (#x ~= 8) then return '' end | ||
local c=0 | ||
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end | ||
data = string.gsub(data, "[^" .. b .. "=]", "") | ||
return (data:gsub( | ||
".", | ||
function(x) | ||
if (x == "=") then | ||
return "" | ||
end | ||
local r, f = "", (b:find(x) - 1) | ||
for i = 6, 1, -1 do | ||
r = r .. (f % 2 ^ i - f % 2 ^ (i - 1) > 0 and "1" or "0") | ||
end | ||
return r | ||
end | ||
):gsub( | ||
"%d%d%d?%d?%d?%d?%d?%d?", | ||
function(x) | ||
if (#x ~= 8) then | ||
return "" | ||
end | ||
local c = 0 | ||
for i = 1, 8 do | ||
c = c + (x:sub(i, i) == "1" and 2 ^ (8 - i) or 0) | ||
end | ||
return string.char(c) | ||
end)) | ||
end | ||
)) | ||
end | ||
-- end of copy | ||
|
||
return M | ||
return M |
33 changes: 19 additions & 14 deletions
33
luaplugins/multi-tenancy/plugins/multi-tenancy/handler.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,53 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
-- Baseplugin deprecated in version 3.x.x | ||
-- local BasePlugin = require "kong.plugins.base_plugin" | ||
-- local MultiTenancyHandler = BasePlugin:extend() | ||
|
||
local MultiTenancyHandler = { | ||
VERSION = "0.0.2", | ||
PRIORITY = 10 | ||
} | ||
|
||
local filter = require("kong.plugins.multi-tenancy.filter") | ||
local kong = kong | ||
local lunajson = require "lunajson" | ||
|
||
local MultiTenancyHandler = BasePlugin:extend() | ||
|
||
function MultiTenancyHandler:new() | ||
MultiTenancyHandler.super.new(self, "multi-tenancy") | ||
end | ||
|
||
local function check_tenant(conf) | ||
local token = kong.request.get_header("Authorization") | ||
local tenant_name = conf.tenant_name | ||
kong.log.debug(" ##### Tenant name " , tenant_name) | ||
kong.log.debug(" ##### Tenant name ", tenant_name) | ||
local tenant_header = kong.request.get_header(tenant_name) | ||
if token == nil or tenant_header == nil then | ||
kong.log.err("Cannot process Headers: ", err) | ||
return nil, { status = 403, message = "Headers missing !!" } | ||
return nil, {status = 403, message = "Headers missing !!"} | ||
end | ||
local token_decoded = filter.decode(token) | ||
local jsonparse = lunajson.decode( token_decoded ) | ||
local jsonparse = lunajson.decode(token_decoded) | ||
if jsonparse[tenant_name] == nil then | ||
kong.log.err("fiware-service missing in token") | ||
return nil, { status = 403, message = "fiware-service missing in token" } | ||
else | ||
return nil, {status = 403, message = "fiware-service missing in token"} | ||
else | ||
arraylength = #jsonparse[tenant_name] | ||
for a = 1, arraylength do | ||
for a = 1, arraylength do | ||
if jsonparse[tenant_name][a] == tenant_header then | ||
return true | ||
end | ||
end | ||
end | ||
return false | ||
end | ||
|
||
end | ||
|
||
function MultiTenancyHandler:access(conf) | ||
MultiTenancyHandler.super.access(self) | ||
-- MultiTenancyHandler.super.access(self) | ||
local ok, err = check_tenant(conf) | ||
if not ok then | ||
return kong.response.error(403, "Permission Denied !") | ||
else | ||
else | ||
return | ||
end | ||
end | ||
|
||
return MultiTenancyHandler | ||
return MultiTenancyHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
local typedefs = require "kong.db.schema.typedefs" | ||
|
||
return { | ||
no_consumer = true, | ||
name = "multi-tenancy", | ||
fields = { | ||
tenant_name = { type = "string", required = true , default = "fiware-service" } | ||
{ | ||
config = { | ||
type = "record", | ||
fields = { | ||
{ | ||
tenant_name = {type = "string", required = true, default = "fiware-service"} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.