Skip to content
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) allow user-specified OpenResty install path #8412

Merged
merged 5 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions kong.conf.default
Original file line number Diff line number Diff line change
Expand Up @@ -1544,3 +1544,14 @@
# **Warning**: Certain variables, when made
# available, may create opportunities to
# escape the sandbox.

#openresty_path = # Path to the OpenResty installation that Kong
# will use. When this is empty (the default),
# Kong determines the OpenResty installation
# by searching for a system-installed OpenResty
# and falling back to searching $PATH for the
# nginx binary.
#
# Setting this attribute disables the search
# behavior and explicitly instructs Kong which
# OpenResty installation to use.
19 changes: 14 additions & 5 deletions kong/cmd/utils/nginx_signals.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local nginx_search_paths = {
"/opt/openresty/nginx/sbin",
""
}

local nginx_version_pattern = "^nginx.-openresty.-([%d%.]+)"
local nginx_compatible = version.set(unpack(meta._DEPENDENCIES.nginx))

Expand Down Expand Up @@ -50,11 +51,19 @@ end

local _M = {}

function _M.find_nginx_bin()
function _M.find_nginx_bin(kong_conf)
log.debug("searching for OpenResty 'nginx' executable")

local search_paths = nginx_search_paths
if kong_conf and kong_conf.openresty_path then
log.debug("using custom OpenResty path: %s", kong_conf.openresty_path)
search_paths = {
pl_path.join(kong_conf.openresty_path, "nginx", "sbin"),
}
end

local found
for _, path in ipairs(nginx_search_paths) do
for _, path in ipairs(search_paths) do
local path_to_check = pl_path.join(path, nginx_bin_name)
if is_openresty(path_to_check) then
if path_to_check == "nginx" then
Expand Down Expand Up @@ -83,7 +92,7 @@ function _M.find_nginx_bin()
end

function _M.start(kong_conf)
local nginx_bin, err = _M.find_nginx_bin()
local nginx_bin, err = _M.find_nginx_bin(kong_conf)
if not nginx_bin then
return nil, err
end
Expand Down Expand Up @@ -120,7 +129,7 @@ function _M.start(kong_conf)
end

function _M.check_conf(kong_conf)
local nginx_bin, err = _M.find_nginx_bin()
local nginx_bin, err = _M.find_nginx_bin(kong_conf)
if not nginx_bin then
return nil, err
end
Expand Down Expand Up @@ -152,7 +161,7 @@ function _M.reload(kong_conf)
return nil, "nginx not running in prefix: " .. kong_conf.prefix
end

local nginx_bin, err = _M.find_nginx_bin()
local nginx_bin, err = _M.find_nginx_bin(kong_conf)
if not nginx_bin then
return nil, err
end
Expand Down
2 changes: 2 additions & 0 deletions kong/templates/kong_defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,6 @@ pluginserver_names = NONE
untrusted_lua = sandbox
untrusted_lua_sandbox_requires =
untrusted_lua_sandbox_environment =

openresty_path =
]]
72 changes: 72 additions & 0 deletions spec/02-integration/02-cmd/15-utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
describe("kong cli utils", function()

describe("nginx_signals", function()
local signals = require "kong.cmd.utils.nginx_signals"
local pl_path = require "pl.path"
local pl_file = require "pl.file"
local pl_dir = require "pl.dir"
flrgh marked this conversation as resolved.
Show resolved Hide resolved

describe("find_nginx_bin()", function()
local tmpdir
before_each(function()
tmpdir = pl_path.tmpname()
assert(os.remove(tmpdir))
end)

after_each(function()
pcall(pl_dir.rmtree, tmpdir)
end)

local function fake_nginx_binary(version)
local bin_dir = pl_path.join(tmpdir, "nginx/sbin")
pl_dir.makepath(bin_dir)

local nginx = pl_path.join(bin_dir, "nginx")
pl_file.write(nginx, string.format(
[[#!/bin/sh
echo 'nginx version: openresty/%s' >&2]], version
))

assert(os.execute("chmod +x " .. nginx))

return nginx
end


it("works with empty/unset input", function()
local bin, err = signals.find_nginx_bin()
assert.is_nil(err)
assert.matches("sbin/nginx", bin)
assert.truthy(pl_path.exists(bin))
end)

it("works when openresty_path is unset", function()
local bin, err = signals.find_nginx_bin({})
assert.is_nil(err)
assert.matches("sbin/nginx", bin)
assert.truthy(pl_path.exists(bin))
end)

it("prefers `openresty_path` when supplied", function()
local meta = require "kong.meta"
local version = meta._DEPENDENCIES.nginx[1]

local nginx = fake_nginx_binary(version)

local bin, err = signals.find_nginx_bin({ openresty_path = tmpdir })

assert.is_nil(err)
assert.equals(nginx, bin)
end)

it("returns nil+error if a compatible nginx bin is not found in `openresty_path`", function()
fake_nginx_binary("1.0.1")
local bin, err = signals.find_nginx_bin({ openresty_path = tmpdir })
assert.is_nil(bin)
assert.not_nil(err)
assert.matches("could not find OpenResty", err)
end)

end)
end)
end)