-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http-log) set headers with single value, not array (#6992)
- Loading branch information
Showing
8 changed files
with
213 additions
and
26 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
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,119 @@ | ||
-- Helper module for 280_to_300 migration operations. | ||
-- | ||
-- Operations are versioned and specific to a migration so they remain | ||
-- fixed in time and are not modified for use in future migrations. | ||
-- | ||
-- If you want to reuse these operations in a future migration, | ||
-- copy the functions over to a new versioned module. | ||
|
||
|
||
local function render(template, keys) | ||
return (template:gsub("$%(([A-Z_]+)%)", keys)) | ||
end | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
-- Postgres operations for Workspace migration | ||
-------------------------------------------------------------------------------- | ||
|
||
|
||
local postgres = { | ||
|
||
up = {}, | ||
|
||
teardown = { | ||
|
||
------------------------------------------------------------------------------ | ||
-- General function to fixup a plugin configuration | ||
fixup_plugin_config = function(_, connector, plugin_name, fixup_fn) | ||
local pgmoon_json = require("pgmoon.json") | ||
for plugin, err in connector:iterate("SELECT id, name, config FROM plugins") do | ||
if err then | ||
return nil, err | ||
end | ||
|
||
if plugin.name == plugin_name then | ||
local fix = fixup_fn(plugin.config) | ||
|
||
if fix then | ||
local sql = render( | ||
"UPDATE plugins SET config = $(NEW_CONFIG)::jsonb WHERE id = '$(ID)'", { | ||
NEW_CONFIG = pgmoon_json.encode_json(plugin.config), | ||
ID = plugin.id, | ||
}) | ||
|
||
local _, err = connector:query(sql) | ||
if err then | ||
return nil, err | ||
end | ||
end | ||
end | ||
end | ||
|
||
return true | ||
end, | ||
}, | ||
|
||
} | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
-- Cassandra operations for Workspace migration | ||
-------------------------------------------------------------------------------- | ||
|
||
|
||
local cassandra = { | ||
|
||
up = {}, | ||
|
||
teardown = { | ||
|
||
------------------------------------------------------------------------------ | ||
-- General function to fixup a plugin configuration | ||
fixup_plugin_config = function(_, connector, plugin_name, fixup_fn) | ||
local coordinator = assert(connector:get_stored_connection()) | ||
local cassandra = require("cassandra") | ||
local cjson = require("cjson") | ||
|
||
for rows, err in coordinator:iterate("SELECT id, name, config FROM plugins") do | ||
if err then | ||
return nil, err | ||
end | ||
|
||
for i = 1, #rows do | ||
local plugin = rows[i] | ||
if plugin.name == plugin_name then | ||
if type(plugin.config) ~= "string" then | ||
return nil, "plugin config is not a string" | ||
end | ||
local config = cjson.decode(plugin.config) | ||
local fix = fixup_fn(config) | ||
|
||
if fix then | ||
local _, err = coordinator:execute("UPDATE plugins SET config = ? WHERE id = ?", { | ||
cassandra.text(cjson.encode(config)), | ||
cassandra.uuid(plugin.id) | ||
}) | ||
if err then | ||
return nil, err | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
return true | ||
end, | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
-------------------------------------------------------------------------------- | ||
|
||
|
||
return { | ||
postgres = postgres, | ||
cassandra = cassandra, | ||
} |
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,36 @@ | ||
local operations = require "kong.db.migrations.operations.280_to_300" | ||
|
||
|
||
local function ws_migration_teardown(ops) | ||
return function(connector) | ||
return ops:fixup_plugin_config(connector, "http-log", function(config) | ||
local updated = false | ||
if type(config) == "table" then -- not required, but let's be defensive here | ||
local headers = config.headers | ||
if type(headers) == "table" then | ||
for header_name, value_array in pairs(headers) do | ||
if type(value_array) == "table" then | ||
-- only update if it's still a table, so it is reentrant | ||
headers[header_name] = value_array[1] or "empty header value" | ||
updated = true | ||
end | ||
end | ||
end | ||
end | ||
return updated | ||
end) | ||
end | ||
end | ||
|
||
|
||
return { | ||
postgres = { | ||
up = "", | ||
teardown = ws_migration_teardown(operations.postgres.teardown), | ||
}, | ||
|
||
cassandra = { | ||
up = "", | ||
teardown = ws_migration_teardown(operations.cassandra.teardown), | ||
}, | ||
} |
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,3 @@ | ||
return { | ||
"001_280_to_300", | ||
} |
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