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

refactor(tools/uuid): update UUID reference #12952

Merged
merged 16 commits into from
May 27, 2024
11 changes: 6 additions & 5 deletions kong/api/endpoints.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local Errors = require "kong.db.errors"
local utils = require "kong.tools.utils"
local uuid = require "kong.tools.uuid"
local arguments = require "kong.api.arguments"
local workspaces = require "kong.workspaces"
local app_helpers = require "lapis.application"
Expand All @@ -17,7 +17,8 @@ local type = type
local fmt = string.format
local concat = table.concat
local re_match = ngx.re.match
local split = utils.split
local split = require("kong.tools.string").split
local get_default_exit_body = require("kong.tools.http").get_default_exit_body


-- error codes http status codes
Expand Down Expand Up @@ -132,7 +133,7 @@ local function handle_error(err_t)
return kong.response.exit(status, err_t)
end

return kong.response.exit(status, utils.get_default_exit_body(status, err_t))
return kong.response.exit(status, get_default_exit_body(status, err_t))
end


Expand Down Expand Up @@ -241,7 +242,7 @@ local function query_entity(context, self, db, schema, method)
end
end

if key.id and not utils.is_valid_uuid(key.id) then
if key.id and not uuid.is_valid_uuid(key.id) then
local endpoint_key = schema.endpoint_key
if endpoint_key then
local field = schema.fields[endpoint_key]
Expand Down Expand Up @@ -526,7 +527,7 @@ local function put_entity_endpoint(schema, foreign_schema, foreign_field_name, m
self.params[foreign_schema.name] = pk
else
associate = true
self.params[foreign_schema.name] = utils.uuid()
self.params[foreign_schema.name] = uuid.uuid()
end

else
Expand Down
6 changes: 3 additions & 3 deletions kong/api/routes/certificates.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local endpoints = require "kong.api.endpoints"
local arguments = require "kong.api.arguments"
local utils = require "kong.tools.utils"
local uuid = require "kong.tools.uuid"


local ngx = ngx
Expand All @@ -15,7 +15,7 @@ local function prepare_params(self)
local id = unescape_uri(self.params.certificates)
local method = self.req.method
local name
if not utils.is_valid_uuid(id) then
if not uuid.is_valid_uuid(id) then
name = arguments.infer_value(id, kong.db.snis.schema.fields.name)

local sni, _, err_t = kong.db.snis:select_by_name(name)
Expand All @@ -31,7 +31,7 @@ local function prepare_params(self)
return kong.response.exit(404, { message = "SNI not found" })
end

id = utils.uuid()
id = uuid.uuid()
end
end

Expand Down
6 changes: 3 additions & 3 deletions kong/api/routes/upstreams.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local endpoints = require "kong.api.endpoints"
local utils = require "kong.tools.utils"
local uuid = require "kong.tools.uuid"


local kong = kong
Expand All @@ -21,7 +21,7 @@ local function set_target_health(self, db, is_healthy)
end

local target
if utils.is_valid_uuid(unescape_uri(self.params.targets)) then
if uuid.is_valid_uuid(unescape_uri(self.params.targets)) then
target, _, err_t = endpoints.select_entity(self, db, db.targets.schema)

else
Expand Down Expand Up @@ -90,7 +90,7 @@ local function target_endpoint(self, db, callback)
end

local target
if utils.is_valid_uuid(unescape_uri(self.params.targets)) then
if uuid.is_valid_uuid(unescape_uri(self.params.targets)) then
target, _, err_t = endpoints.select_entity(self, db, db.targets.schema)

else
Expand Down
4 changes: 2 additions & 2 deletions kong/cluster_events/strategies/postgres.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local utils = require "kong.tools.utils"
local uuid = require "kong.tools.uuid"
local new_tab = require "table.new"


Expand Down Expand Up @@ -97,7 +97,7 @@ function _M:insert(node_id, channel, at, data, delay)
nbf = "NULL"
end

local pg_id = self.connector:escape_literal(utils.uuid())
local pg_id = self.connector:escape_literal(uuid.uuid())
local pg_node_id = self.connector:escape_literal(node_id)
local pg_channel = self.connector:escape_literal(channel)
local pg_data = self.connector:escape_literal(data)
Expand Down
4 changes: 2 additions & 2 deletions kong/db/dao/init.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local cjson = require "cjson"
local iteration = require "kong.db.iteration"
local utils = require "kong.tools.utils"
local kong_table = require "kong.tools.table"
local defaults = require "kong.db.strategies.connector".defaults
local hooks = require "kong.hooks"
local workspaces = require "kong.workspaces"
local new_tab = require "table.new"
local DAO_MAX_TTL = require("kong.constants").DATABASE.DAO_MAX_TTL
local is_valid_uuid = require("kong.tools.uuid").is_valid_uuid

local setmetatable = setmetatable
local tostring = tostring
Expand Down Expand Up @@ -174,7 +174,7 @@ local function validate_options_value(self, options)

if options.workspace then
if type(options.workspace) == "string" then
if not utils.is_valid_uuid(options.workspace) then
if not is_valid_uuid(options.workspace) then
local ws = kong.db.workspaces:select_by_name(options.workspace)
if ws then
options.workspace = ws.id
Expand Down
4 changes: 2 additions & 2 deletions kong/db/errors.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local pl_pretty = require("pl.pretty").write
local pl_keys = require("pl.tablex").keys
local nkeys = require("table.nkeys")
local table_isarray = require("table.isarray")
local utils = require("kong.tools.utils")
local uuid = require("kong.tools.uuid")


local type = type
Expand Down Expand Up @@ -757,7 +757,7 @@ do
---@return string|nil
local function validate_id(id)
return (type(id) == "string"
and utils.is_valid_uuid(id)
and uuid.is_valid_uuid(id)
and id)
or nil
end
Expand Down
4 changes: 2 additions & 2 deletions kong/db/migrations/core/013_220_to_230.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local utils = require("kong.tools.utils")
local uuid = require("kong.tools.uuid")


local CLUSTER_ID = utils.uuid()
local CLUSTER_ID = uuid.uuid()


return {
Expand Down
4 changes: 2 additions & 2 deletions kong/db/schema/others/declarative_config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local uuid = require("resty.jit-uuid")
local utils = require("kong.tools.utils")
local kong_table = require("kong.tools.table")
local Errors = require("kong.db.errors")
local Entity = require("kong.db.schema.entity")
Expand All @@ -8,6 +7,7 @@ local constants = require("kong.constants")
local plugin_loader = require("kong.db.schema.plugin_loader")
local vault_loader = require("kong.db.schema.vault_loader")
local schema_topological_sort = require("kong.db.schema.topological_sort")
local utils_uuid = require("kong.tools.uuid").uuid


local null = ngx.null
Expand Down Expand Up @@ -649,7 +649,7 @@ local function populate_ids_for_validation(input, known_entities, parent_entity,
if key then
item[pk_name] = generate_uuid(schema.name, key)
else
item[pk_name] = utils.uuid()
item[pk_name] = utils_uuid()
end
end

Expand Down
3 changes: 2 additions & 1 deletion kong/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ local wasm = require "kong.runloop.wasm"
local reports = require "kong.reports"
local pl_file = require "pl.file"
local req_dyn_hook = require "kong.dynamic_hook"
local uuid = require("kong.tools.uuid").uuid


local kong = kong
Expand Down Expand Up @@ -767,7 +768,7 @@ function Kong.init()
end

if config.request_debug and config.role ~= "control_plane" and is_http_module then
local token = config.request_debug_token or utils.uuid()
local token = config.request_debug_token or uuid()

local request_debug_token_file = pl_path.join(config.prefix,
constants.REQUEST_DEBUG_TOKEN_FILE)
Expand Down
9 changes: 5 additions & 4 deletions kong/pdk/client.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
-- @module kong.client


local utils = require "kong.tools.utils"
local phase_checker = require "kong.pdk.private.phases"
local is_valid_uuid = require("kong.tools.uuid").is_valid_uuid
local check_https = require("kong.tools.http").check_https


local ngx = ngx
Expand Down Expand Up @@ -187,11 +188,11 @@ local function new(self)
error("consumer_id must be a string", 2)
end

if not utils.is_valid_uuid(consumer_id) and not search_by_username then
if not is_valid_uuid(consumer_id) and not search_by_username then
error("cannot load a consumer with an id that is not a uuid", 2)
end

if utils.is_valid_uuid(consumer_id) then
if is_valid_uuid(consumer_id) then
local result, err = kong.db.consumers:select({ id = consumer_id })

if result then
Expand Down Expand Up @@ -289,7 +290,7 @@ local function new(self)

if ngx.config.subsystem == "http" then
local is_trusted = self.ip.is_trusted(self.client.get_ip())
local is_https, err = utils.check_https(is_trusted, allow_terminated)
local is_https, err = check_https(is_trusted, allow_terminated)
if err then
return nil, err
end
Expand Down
9 changes: 5 additions & 4 deletions kong/pdk/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
--
-- @module kong.node

local utils = require "kong.tools.utils"
local ffi = require "ffi"
local private_node = require "kong.pdk.private.node"
local uuid = require("kong.tools.uuid").uuid
local bytes_to_str = require("kong.tools.string").bytes_to_str


local floor = math.floor
Expand Down Expand Up @@ -46,7 +47,7 @@ local function convert_bytes(bytes, unit, scale)
return floor(bytes)
end

return utils.bytes_to_str(bytes, unit, scale)
return bytes_to_str(bytes, unit, scale)
end


Expand All @@ -73,7 +74,7 @@ local function new(self)

local shm = ngx.shared.kong

local ok, err = shm:safe_add(NODE_ID_KEY, utils.uuid())
local ok, err = shm:safe_add(NODE_ID_KEY, uuid())
if not ok and err ~= "exists" then
error("failed to set 'node_id' in shm: " .. err)
end
Expand Down Expand Up @@ -160,7 +161,7 @@ local function new(self)
unit = unit or "b"
scale = scale or 2

local pok, perr = pcall(utils.bytes_to_str, 0, unit, scale)
local pok, perr = pcall(bytes_to_str, 0, unit, scale)
if not pok then
error(perr, 2)
end
Expand Down
8 changes: 4 additions & 4 deletions kong/pdk/private/node.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local log = require "kong.cmd.utils.log"
local utils = require "kong.tools.utils"
local uuid = require "kong.tools.uuid"
local pl_file = require "pl.file"
local pl_path = require "pl.path"
local pl_dir = require "pl.dir"
Expand Down Expand Up @@ -31,15 +31,15 @@ local function initialize_node_id(prefix)
return nil, fmt("failed to access file %s: %s", filename, err)
end

if not utils.is_valid_uuid(id) then
if not uuid.is_valid_uuid(id) then
log.debug("file %s contains invalid uuid: %s", filename, id)
-- set false to override it when it contains an invalid uuid.
file_exists = false
end
end

if not file_exists then
local id = utils.uuid()
local id = uuid.uuid()
log.debug("persisting node_id (%s) to %s", id, filename)

local ok, write_err = pl_file.write(filename, id)
Expand Down Expand Up @@ -89,7 +89,7 @@ local function load_node_id(prefix)
return nil, fmt("failed to access file %s: %s", filename, read_err)
end

if not utils.is_valid_uuid(id) then
if not uuid.is_valid_uuid(id) then
return nil, fmt("file %s contains invalid uuid: %q", filename, id)
end

Expand Down
8 changes: 4 additions & 4 deletions kong/plugins/acl/api.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local endpoints = require "kong.api.endpoints"
local utils = require "kong.tools.utils"
local uuid = require "kong.tools.uuid"


local ngx = ngx
Expand All @@ -13,10 +13,10 @@ return {
schema = kong.db.acls.schema,
before = function(self, db, helpers)
local group = unescape_uri(self.params.acls)
if not utils.is_valid_uuid(group) then
if not uuid.is_valid_uuid(group) then
local consumer_id = unescape_uri(self.params.consumers)

if not utils.is_valid_uuid(consumer_id) then
if not uuid.is_valid_uuid(consumer_id) then
local consumer, _, err_t = endpoints.select_entity(self, db, db.consumers.schema)
if err_t then
return endpoints.handle_error(err_t)
Expand All @@ -42,7 +42,7 @@ return {
return kong.response.error(404)
end

self.params.acls = utils.uuid()
self.params.acls = uuid.uuid()
end

self.params.group = group
Expand Down
2 changes: 1 addition & 1 deletion kong/plugins/correlation-id/handler.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Copyright (C) Kong Inc.
local uuid = require "kong.tools.utils".uuid
local uuid = require "kong.tools.uuid".uuid
local kong_meta = require "kong.meta"


Expand Down
3 changes: 2 additions & 1 deletion kong/runloop/balancer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local pl_tablex = require "pl.tablex"
local utils = require "kong.tools.utils"
local hooks = require "kong.hooks"
local recreate_request = require("ngx.balancer").recreate_request
local uuid = require("kong.tools.uuid").uuid

local healthcheckers = require "kong.runloop.balancer.healthcheckers"
local balancers = require "kong.runloop.balancer.balancers"
Expand Down Expand Up @@ -145,7 +146,7 @@ local function get_value_to_hash(upstream, ctx)
ctx = ngx.ctx
end

identifier = utils.uuid()
identifier = uuid()

ctx.balancer_data.hash_cookie = {
key = upstream.hash_on_cookie,
Expand Down
Loading
Loading