-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(conf) add support to configure nginx directives via kong.conf #3530
Changes from 3 commits
4a42c8f
392e203
659bbb6
2317d42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
local pl_utils = require "pl.utils" | ||
local log = require "kong.cmd.utils.log" | ||
local fmt = string.format | ||
|
||
|
||
local cmd = [[ printenv ]] | ||
|
||
|
||
-- returns table and err | ||
local function read_all() | ||
log.debug("reading environment variables: %s", cmd) | ||
local vars = {} | ||
local success, ret_code, stdout, stderr = pl_utils.executeex(cmd) | ||
if not success or ret_code ~= 0 then | ||
return nil, fmt("could not read environment variables (exit code: %d): %s", | ||
ret_code, stderr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: alignment |
||
end | ||
|
||
for line in stdout:gmatch("[^\r\n]+") do | ||
local i = string.find(line, "=") -- match first = | ||
if i then | ||
local k = string.sub(line,1, i-1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: |
||
local v = string.sub(line, i+1) | ||
if k and v then | ||
vars[k] = v | ||
end | ||
end | ||
end | ||
return vars | ||
end | ||
|
||
|
||
return { | ||
read_all = read_all, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -107,6 +107,25 @@ function _M.start(kong_conf) | |
return true | ||
end | ||
|
||
function _M.check_conf(kong_conf) | ||
local nginx_bin, err = _M.find_nginx_bin() | ||
if not nginx_bin then | ||
return nil, err | ||
end | ||
|
||
local cmd = fmt("KONG_NGINX_CONF_CHECK=true %s -t -p %s -c %s", | ||
nginx_bin, kong_conf.prefix, "nginx.conf") | ||
log.debug("checking nginx conf: %s", cmd) | ||
|
||
local ok, retcode, _, stderr = pl_utils.executeex(cmd) | ||
if not ok then | ||
return false, ("failed to validate nginx configuration " .. | ||
"(exit code %d):\n%s"):format(retcode ,stderr) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: alignment and space after the comma |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's also add a debug log when the validation succeededs, otherwise the user just sees "checking nginx conf" and does not know what happened after that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. UNIX philosophy of no output is good? |
||
return true | ||
end | ||
|
||
function _M.stop(kong_conf) | ||
return send_signal(kong_conf, "TERM") | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ local pl_path = require "pl.path" | |
local tablex = require "pl.tablex" | ||
local utils = require "kong.tools.utils" | ||
local log = require "kong.cmd.utils.log" | ||
local env = require "kong.cmd.utils.env" | ||
local ip = require "kong.tools.ip" | ||
local ciphers = require "kong.tools.ciphers" | ||
|
||
|
@@ -28,11 +29,17 @@ local header_key_to_name = { | |
[string.lower(headers.UPSTREAM_STATUS)] = headers.UPSTREAM_STATUS, | ||
} | ||
|
||
local CONF_KEY_PREFIXES = { | ||
["nginx_http_directives"] = "nginx_http_", | ||
["nginx_proxy_directives"] = "nginx_proxy_", | ||
["nginx_admin_directives"] = "nginx_admin_", | ||
} | ||
|
||
local PREFIX_PATHS = { | ||
nginx_pid = {"pids", "nginx.pid"}, | ||
nginx_err_logs = {"logs", "error.log"}, | ||
nginx_acc_logs = {"logs", "access.log"}, | ||
nginx_admin_acc_logs = {"logs", "admin_access.log"}, | ||
admin_acc_logs = {"logs", "admin_access.log"}, | ||
nginx_conf = {"nginx.conf"}, | ||
nginx_kong_conf = {"nginx-kong.conf"} | ||
; | ||
|
@@ -448,6 +455,24 @@ local function parse_listeners(values) | |
return list | ||
end | ||
|
||
local function parse_nginx_directives(prefix, conf) | ||
local directives = {} | ||
|
||
if type(prefix) ~= "string" or type(conf) ~= "table" then | ||
return directives | ||
end | ||
|
||
for k, v in pairs(conf) do | ||
if type(k) == "string" then | ||
local _ , _ , directive= string.find(k, prefix .. "(.+)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: spacing ( |
||
if directive then | ||
directives[directive] = v | ||
end | ||
end | ||
end | ||
return directives | ||
end | ||
|
||
--- Load Kong configuration | ||
-- The loaded configuration will have all properties from the default config | ||
-- merged with the (optionally) specified config file, environment variables | ||
|
@@ -514,6 +539,48 @@ local function load(path, custom_conf) | |
-- Merging & validation | ||
----------------------- | ||
|
||
-- find dynamic keys that need to be loaded | ||
do | ||
local dynamic_keys = {} | ||
local function find_dynamic_keys(t, prefix) | ||
if not t then | ||
return | ||
end | ||
|
||
for k, v in pairs(t) do | ||
local _, _, directive = string.find(k, "(" .. prefix .. ".+)") | ||
if directive then | ||
dynamic_keys[directive] = true | ||
if tonumber(v) then | ||
t[k] = string.format("%q", v) | ||
end | ||
end | ||
end | ||
end | ||
|
||
local env_vars, err = env.read_all() | ||
if err then | ||
return nil, err | ||
end | ||
|
||
local kong_env_vars = {} | ||
for k, v in pairs(env_vars) do | ||
local clean_k = string.lower(k) | ||
local kong_var = string.match(clean_k, "^kong_(.+)") | ||
if kong_var then | ||
kong_env_vars[kong_var] = true | ||
end | ||
end | ||
|
||
for _, prefix in pairs(CONF_KEY_PREFIXES) do | ||
find_dynamic_keys(custom_conf, prefix) | ||
find_dynamic_keys(kong_env_vars, prefix) | ||
find_dynamic_keys(from_file_conf, prefix) | ||
end | ||
|
||
defaults = tablex.merge(defaults, dynamic_keys, true) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a comment that this is building the union? This way the intent is clearer (as discussed) to readers who see conf = tablex.merge(conf, defaults) -- intersection (remove extraneous properties) |
||
end | ||
|
||
-- merge default conf with file conf, ENV variables and arg conf (with precedence) | ||
local conf = tablex.pairmap(overrides, defaults, from_file_conf, custom_conf) | ||
|
||
|
@@ -525,6 +592,11 @@ local function load(path, custom_conf) | |
|
||
conf = tablex.merge(conf, defaults) -- intersection (remove extraneous properties) | ||
|
||
-- nginx directives from conf | ||
for block, prefix in pairs(CONF_KEY_PREFIXES) do | ||
conf[block] = parse_nginx_directives(prefix, conf) | ||
end | ||
|
||
-- print alphabetically-sorted values | ||
do | ||
local conf_arr = {} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,13 @@ require "resty.core" | |
local constants = require "kong.constants" | ||
|
||
do | ||
-- if we're running nginx -t then don't initialize | ||
if os.getenv("KONG_NGINX_CONF_CHECK") then | ||
return { | ||
init = function() | ||
end, | ||
} | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should go below the check for required shms, otherwise configs lacking the shm would error during the actual |
||
-- let's ensure the required shared dictionaries are | ||
-- declared via lua_shared_dict in the Nginx conf | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
local helpers = require "spec.helpers" | ||
|
||
|
||
describe("Custom NGINX directives", function() | ||
local proxy_client | ||
local bp | ||
|
||
local function start(config) | ||
return function() | ||
bp.routes:insert { | ||
hosts = { "headers-inspect.com" }, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, a super minor thing but which I think we should start paying attention in our tests (perhaps worth an addition to the style guide even?) — I suggest we stop using "valid-looking" URLs in tests, even if we're not hitting them (we've had in the past tests that used an innocent-looking URL which ended up pointing to a porn site!) — we can use instead There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for sharing pointing this out! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1
I've also seen cases of test/example code in public documentation using "domain.com", which at one point hosted malware; customers who blindly copy-pasted were in for a nasty surprise :p IANA has reserved Just my $0.02. |
||
} | ||
|
||
config = config or {} | ||
config.nginx_conf = "spec/fixtures/custom_nginx.template" | ||
|
||
assert(helpers.start_kong(config)) | ||
end | ||
end | ||
|
||
setup(function() | ||
bp = helpers.get_db_utils() | ||
end) | ||
|
||
before_each(function() | ||
proxy_client = helpers.proxy_client() | ||
end) | ||
|
||
after_each(function() | ||
if proxy_client then | ||
proxy_client:close() | ||
end | ||
end) | ||
|
||
describe("with config value 'nginx_proxy_add_header=foo-header bar-value'", function() | ||
|
||
setup(start{ | ||
["nginx_proxy_add_header"] = "foo-header bar-value" | ||
}) | ||
|
||
teardown(helpers.stop_kong) | ||
|
||
it("header 'foo-header' should be inserted", function() | ||
local res = assert(proxy_client:send { | ||
method = "GET", | ||
path = "/get", | ||
headers = { | ||
host = "headers-inspect.com", | ||
} | ||
}) | ||
|
||
assert.res_status(200, res) | ||
assert.equal("bar-value", res.headers["foo-header"]) | ||
end) | ||
|
||
end) | ||
end) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is no need for this extra variable anymore, it is used only once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used for the debug log as well.