-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[configuration] allow passing full configuration by Data URL
Data URLs can be passed as the configuration url. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
- Loading branch information
Showing
7 changed files
with
133 additions
and
4 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
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,27 @@ | ||
local cjson = require('cjson') | ||
|
||
local configuration = cjson.encode(cjson.decode([[{ | ||
"services": [ | ||
{ | ||
"proxy": { | ||
"hosts": [ | ||
"localhost", | ||
"127.0.0.1" | ||
], | ||
"policy_chain": [ | ||
{ "name": "apicast.policy.echo" } | ||
] | ||
} | ||
} | ||
] | ||
} | ||
]])) | ||
|
||
return { | ||
worker_processes = '1', | ||
master_process = 'off', | ||
lua_code_cache = 'off', | ||
lua_code_cache = 'on', | ||
configuration_loader = 'lazy', | ||
configuration_cache = 0, | ||
configuration = string.format([[data:application/json,%s]],ngx.escape_uri(configuration)), | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
local MimeType = require('resty.mime') | ||
local cjson = require('cjson') | ||
|
||
local _M = {} | ||
|
||
local pattern = [[^data:(?<mediatype>[a-z]+\/[a-z0-9-+.]+(?<charset>;[a-z-]+=[a-z0-9-]+)?)?(?<base64>;base64)?,(?<data>[a-z0-9!$&',()*+;=\-._~:@\/?%\s]*?)$]] | ||
local re_match = ngx.re.match | ||
|
||
local function parse(url) | ||
local match, err = re_match(url, pattern, 'oji') | ||
|
||
if match then | ||
|
||
return { | ||
mime_type = MimeType.new(match.mediatype), | ||
data = match.data, | ||
base64 = not not match.base64, | ||
} | ||
else | ||
return nil, err or 'not valid data-url' | ||
end | ||
end | ||
|
||
local decoders = { | ||
['application/json'] = function(data) return cjson.encode(cjson.decode(data)) end, | ||
} | ||
|
||
local function decode(data_url) | ||
|
||
local data = data_url.data | ||
|
||
if data_url.base64 then | ||
data = ngx.decode_base64(data) | ||
else | ||
data = ngx.unescape_uri(data) | ||
end | ||
|
||
local media_type = data_url.mime_type.media_type | ||
|
||
local decoder = decoders[media_type] | ||
|
||
if decoder then | ||
return decoder(data) | ||
else | ||
return nil, 'unsupported mediatype' | ||
end | ||
end | ||
|
||
function _M.call(uri) | ||
local data_url, err = parse(uri) | ||
|
||
if not data_url then return nil, err end | ||
|
||
return decode(data_url) | ||
end | ||
|
||
|
||
return _M |
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local loader = require 'apicast.configuration_loader.data_url' | ||
local cjson = require('cjson') | ||
|
||
describe('Configuration Data URL loader', function() | ||
describe('.call', function() | ||
it('ignores empty url', function() | ||
assert.same({nil, 'not valid data-url'}, { loader.call() }) | ||
assert.same({nil, 'not valid data-url'}, { loader.call('') }) | ||
end) | ||
|
||
local config = cjson.encode{ | ||
services = { | ||
{ id = 21 }, | ||
{ id = 42 }, | ||
} | ||
} | ||
|
||
it('decodes urlencoded data url', function() | ||
local url = ([[data:application/json,%s]]):format(ngx.escape_uri(config)) | ||
assert.same(config, loader.call(url)) | ||
end) | ||
|
||
it('accepts bug ignores charset in the data url', function() | ||
local url = ([[data:application/json;charset=iso8601,%s]]):format(ngx.escape_uri(config)) | ||
assert.same(config, loader.call(url)) | ||
end) | ||
|
||
it('decodes base64 encoded data url', function() | ||
local url = ([[data:application/json;base64,%s]]):format(ngx.encode_base64(config)) | ||
assert.same(config, loader.call(url)) | ||
end) | ||
|
||
it('requires application/json media type', function() | ||
local url = ([[data:text/json,%s]]):format(ngx.escape_uri(config)) | ||
|
||
assert.same({nil, 'unsupported mediatype'}, { loader.call(url) }) | ||
end) | ||
|
||
end) | ||
end) |