Skip to content

Commit

Permalink
feat(gui): added a new feature to support multiple domains for gui.
Browse files Browse the repository at this point in the history
  • Loading branch information
raoxiaoyan committed Sep 18, 2024
1 parent 71e7cb6 commit f69a166
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 4 additions & 0 deletions changelog/unreleased/kong/add_multiple_domain_for_gui.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
message: |
Added a new feature to support multiple domains for Gui.
type: feature
scope: "Core"
23 changes: 19 additions & 4 deletions kong/api/api_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,28 @@ function _M.before_filter(self)
end

function _M.cors_filter(self)
local origin = self.req.headers["Origin"]
local allowed_origins = kong.configuration.admin_gui_origin

if kong.configuration.admin_gui_origin then
origin = kong.configuration.admin_gui_origin
local function is_origin_allowed(req_origin)
for _, allowed_origin in ipairs(allowed_origins) do
if req_origin == allowed_origin then
return true
end
end
return false
end

local req_origin = self.req.headers["Origin"]

if allowed_origins and #allowed_origins > 0 then
if req_origin and not is_origin_allowed(req_origin) then
req_origin = allowed_origins[1]
end
else
req_origin = req_origin or "*"
end

ngx.header["Access-Control-Allow-Origin"] = origin or "*"
ngx.header["Access-Control-Allow-Origin"] = req_origin
ngx.header["Access-Control-Allow-Credentials"] = "true"

if ngx.req.get_method() == "OPTIONS" then
Expand Down
6 changes: 3 additions & 3 deletions kong/conf_loader/constants.lua
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,9 @@ local CONF_PARSERS = {
error_template_xml = { typ = "string" },
error_template_plain = { typ = "string" },

admin_gui_url = {typ = "string"},
admin_gui_path = {typ = "string"},
admin_gui_api_url = {typ = "string"},
admin_gui_url = { typ = "array" },
admin_gui_path = { typ = "string" },
admin_gui_api_url = { typ = "string" },

request_debug = { typ = "boolean" },
request_debug_token = { typ = "string" },
Expand Down
10 changes: 7 additions & 3 deletions kong/conf_loader/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -935,9 +935,13 @@ local function load(path, custom_conf, opts)
-- to make it suitable to be used as an origin in headers, we need to
-- parse and reconstruct the admin_gui_url to ensure it only contains
-- the scheme, host, and port
if conf.admin_gui_url then
local parsed_url = socket_url.parse(conf.admin_gui_url)
conf.admin_gui_origin = parsed_url.scheme .. "://" .. parsed_url.authority
if conf.admin_gui_url and #conf.admin_gui_url > 0 then
local admin_gui_origin = {}
for _, url in ipairs(conf.admin_gui_url) do
local parsed_url = socket_url.parse(url)
table.insert(admin_gui_origin, parsed_url.scheme .. "://" .. parsed_url.authority)
end
conf.admin_gui_origin = admin_gui_origin
end

-- hybrid mode HTTP tunneling (CONNECT) proxy inside HTTPS
Expand Down
15 changes: 12 additions & 3 deletions spec/01-unit/03-conf_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -323,23 +323,32 @@ describe("Configuration loader", function()
assert.is_nil(errors)
assert.is_not_nil(conf)
assert.is_not_nil(conf.admin_gui_origin)
assert.equal("http://localhost:8002", conf.admin_gui_origin)
assert.same({ "http://localhost:8002" }, conf.admin_gui_origin)

conf, _, errors = conf_loader(nil, {
admin_gui_url = "https://localhost:8002",
})
assert.is_nil(errors)
assert.is_not_nil(conf)
assert.is_not_nil(conf.admin_gui_origin)
assert.equal("https://localhost:8002", conf.admin_gui_origin)
assert.same({ "https://localhost:8002" }, conf.admin_gui_origin)

conf, _, errors = conf_loader(nil, {
admin_gui_url = "http://localhost:8002/manager",
})
assert.is_nil(errors)
assert.is_not_nil(conf)
assert.is_not_nil(conf.admin_gui_origin)
assert.equal("http://localhost:8002", conf.admin_gui_origin)
assert.same({ "http://localhost:8002" }, conf.admin_gui_origin)

conf, _, errors = conf_loader(nil, {
admin_gui_url = "http://localhost:8002/manager, https://localhost:8445/manager",
})
assert.is_nil(errors)
assert.is_not_nil(conf)
assert.is_not_nil(conf.admin_gui_origin)
assert.is_table(conf.admin_gui_origin)
assert.same({ "http://localhost:8002", "https://localhost:8445" }, conf.admin_gui_origin)
end)
it("strips comments ending settings", function()
local _os_getenv = os.getenv
Expand Down

0 comments on commit f69a166

Please sign in to comment.