Skip to content

Commit

Permalink
refactor(tools): update all references to tools.rand (#12932)
Browse files Browse the repository at this point in the history
* fix get_rand_bytes reference in tests

* fix get_rand_bytes reference in codebase

* fix random_string reference in tests

* fix random_string reference in codebase

* move rand out of utils

* fix 20-wasm/09-filter-meta_spec.lua

* restore utils.rand.*
  • Loading branch information
chronolaw authored Apr 26, 2024
1 parent 600b0a4 commit 5dc3b7b
Show file tree
Hide file tree
Showing 28 changed files with 91 additions and 88 deletions.
4 changes: 2 additions & 2 deletions kong/globalpatches.lua
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ return function(options)
--
-- This patched method will create a unique seed per worker process,
-- using a combination of both time and the worker's pid.
local util = require "kong.tools.utils"
local get_rand_bytes = require("kong.tools.rand").get_rand_bytes
local seeded = {}
local randomseed = math.randomseed

Expand Down Expand Up @@ -436,7 +436,7 @@ return function(options)
end

local seed
local bytes, err = util.get_rand_bytes(8)
local bytes, err = get_rand_bytes(8)
if bytes then
ngx.log(ngx.DEBUG, "seeding PRNG from OpenSSL RAND_bytes()")

Expand Down
5 changes: 2 additions & 3 deletions kong/pdk/tracing.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ local require = require
local ffi = require "ffi"
local tablepool = require "tablepool"
local new_tab = require "table.new"
local utils = require "kong.tools.utils"
local phase_checker = require "kong.pdk.private.phases"
local tracing_context = require "kong.tracing.tracing_context"

Expand All @@ -20,12 +19,12 @@ local ipairs = ipairs
local tostring = tostring
local setmetatable = setmetatable
local getmetatable = getmetatable
local rand_bytes = utils.get_rand_bytes
local rand_bytes = require("kong.tools.rand").get_rand_bytes
local check_phase = phase_checker.check
local PHASES = phase_checker.phases
local ffi_cast = ffi.cast
local ffi_str = ffi.string
local ffi_time_unix_nano = utils.time_ns
local ffi_time_unix_nano = require("kong.tools.time").time_ns
local tablepool_fetch = tablepool.fetch
local tablepool_release = tablepool.release
local ngx_log = ngx.log
Expand Down
13 changes: 6 additions & 7 deletions kong/plugins/oauth2/access.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local url = require "socket.url"
local utils = require "kong.tools.utils"
local constants = require "kong.constants"
local timestamp = require "kong.tools.timestamp"
local secret = require "kong.plugins.oauth2.secret"
Expand All @@ -13,15 +12,15 @@ local type = type
local next = next
local table = table
local error = error
local split = utils.split
local strip = utils.strip
local split = require("kong.tools.string").split
local strip = require("kong.tools.string").strip
local string_find = string.find
local string_gsub = string.gsub
local string_byte = string.byte
local check_https = utils.check_https
local encode_args = utils.encode_args
local random_string = utils.random_string
local table_contains = utils.table_contains
local check_https = require("kong.tools.http").check_https
local encode_args = require("kong.tools.http").encode_args
local random_string = require("kong.tools.rand").random_string
local table_contains = require("kong.tools.table").table_contains


local ngx_decode_args = ngx.decode_args
Expand Down
7 changes: 4 additions & 3 deletions kong/plugins/oauth2/secret.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ local assert = assert
local tonumber = tonumber
local encode_base64 = ngx.encode_base64
local decode_base64 = ngx.decode_base64
local get_rand_bytes = require("kong.tools.rand").get_rand_bytes


local ENABLED_ALGORITHMS = {
Expand Down Expand Up @@ -131,7 +132,7 @@ if ENABLED_ALGORITHMS.ARGON2 then
m_cost = ARGON2_M_COST,
}
do
local hash = argon2.hash_encoded("", utils.get_rand_bytes(ARGON2_SALT_LEN), ARGON2_OPTIONS)
local hash = argon2.hash_encoded("", get_rand_bytes(ARGON2_SALT_LEN), ARGON2_OPTIONS)
local parts = utils.split(hash, "$")
remove(parts)
remove(parts)
Expand All @@ -141,7 +142,7 @@ if ENABLED_ALGORITHMS.ARGON2 then
local crypt = {}

function crypt.hash(secret)
return argon2.hash_encoded(secret, utils.get_rand_bytes(ARGON2_SALT_LEN), ARGON2_OPTIONS)
return argon2.hash_encoded(secret, get_rand_bytes(ARGON2_SALT_LEN), ARGON2_OPTIONS)
end

function crypt.verify(secret, hash)
Expand Down Expand Up @@ -230,7 +231,7 @@ if ENABLED_ALGORITHMS.PBKDF2 then
end
end

local salt = opts.salt or utils.get_rand_bytes(PBKDF2_SALT_LEN)
local salt = opts.salt or get_rand_bytes(PBKDF2_SALT_LEN)
local hash, err = kdf:derive(opts.outlen or PBKDF2_HASH_LEN, {
pass = secret,
salt = salt,
Expand Down
6 changes: 3 additions & 3 deletions kong/plugins/session/schema.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local typedefs = require "kong.db.schema.typedefs"
local Schema = require "kong.db.schema"
local utils = require "kong.tools.utils"
local get_rand_bytes = require("kong.tools.rand").get_rand_bytes


local char = string.char
Expand Down Expand Up @@ -50,10 +50,10 @@ local logout_methods = Schema.define({
})


--- kong.utils.random_string with 32 bytes instead
--- kong.rand.random_string with 32 bytes instead
-- @returns random string of length 44
local function random_string()
return encode_base64(utils.get_rand_bytes(32, true))
return encode_base64(get_rand_bytes(32, true))
:gsub("/", char(rand(48, 57))) -- 0 - 10
:gsub("+", char(rand(65, 90))) -- A - Z
:gsub("=", char(rand(97, 122))) -- a - z
Expand Down
3 changes: 1 addition & 2 deletions kong/plugins/zipkin/handler.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local new_zipkin_reporter = require "kong.plugins.zipkin.reporter".new
local new_span = require "kong.plugins.zipkin.span".new
local utils = require "kong.tools.utils"
local propagation = require "kong.tracing.propagation"
local request_tags = require "kong.plugins.zipkin.request_tags"
local kong_meta = require "kong.meta"
Expand All @@ -12,7 +11,7 @@ local ngx_var = ngx.var
local split = ngx_re.split
local subsystem = ngx.config.subsystem
local fmt = string.format
local rand_bytes = utils.get_rand_bytes
local rand_bytes = require("kong.tools.rand").get_rand_bytes
local to_hex = require "resty.string".to_hex

local ZipkinLogHandler = {
Expand Down
3 changes: 1 addition & 2 deletions kong/plugins/zipkin/span.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ You can find it documented in this OpenAPI spec:
https://github.com/openzipkin/zipkin-api/blob/7e33e977/zipkin2-api.yaml#L280
]]

local utils = require "kong.tools.utils"
local rand_bytes = utils.get_rand_bytes
local rand_bytes = require("kong.tools.rand").get_rand_bytes

local floor = math.floor

Expand Down
2 changes: 1 addition & 1 deletion kong/tools/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ do
"kong.tools.table",
"kong.tools.string",
"kong.tools.uuid",
"kong.tools.rand",
"kong.tools.rand", -- keep it here for compatibility
"kong.tools.time",
"kong.tools.ip",
"kong.tools.http",
Expand Down
1 change: 1 addition & 0 deletions spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ describe("Utils", function()
assert.False(utils.validate_utf8(string.char(237, 160, 128))) -- Single UTF-16 surrogate
end)
describe("random_string()", function()
local utils = require "kong.tools.rand"
it("should return a random string", function()
local first = utils.random_string()
assert.is_string(first)
Expand Down
3 changes: 1 addition & 2 deletions spec/01-unit/26-tracing/01-tracer_pdk_spec.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
require "spec.helpers" -- initializes 'kong' global for tracer
local match = require("luassert.match")

local utils = require "kong.tools.utils"
local SAMPLING_BYTE = 8
local rand_bytes = utils.get_rand_bytes
local rand_bytes = require("kong.tools.rand").get_rand_bytes
local TEST_COUNT = 10000
-- we can only ensure a sampling precision of 0.02
local SAMPLING_PRECISION = 0.02
Expand Down
3 changes: 1 addition & 2 deletions spec/02-integration/14-tracing/02-propagation_spec.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
local helpers = require "spec.helpers"
local cjson = require "cjson"
local utils = require "kong.tools.utils"
local to_hex = require("resty.string").to_hex
local from_hex = require 'kong.tracing.propagation.utils'.from_hex

local rand_bytes = utils.get_rand_bytes
local rand_bytes = require("kong.tools.rand").get_rand_bytes

local function gen_id(len)
return to_hex(rand_bytes(len))
Expand Down
13 changes: 7 additions & 6 deletions spec/02-integration/20-wasm/06-clustering_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local helpers = require "spec.helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson.safe"
local STATUS = require("kong.constants").CLUSTERING_SYNC_STATUS
local admin = require "spec.fixtures.admin_api"
Expand All @@ -10,6 +9,8 @@ local FILTER_SRC = "spec/fixtures/proxy_wasm_filters/build/response_transformer.

local json = cjson.encode
local file = helpers.file
local random_string = require("kong.tools.rand").random_string
local uuid = require("kong.tools.uuid").uuid


local function expect_status(id, exp)
Expand Down Expand Up @@ -135,7 +136,7 @@ describe("#wasm - hybrid mode #postgres", function()

lazy_setup(function()
dp_filter_path = new_wasm_filter_directory()
node_id = utils.uuid()
node_id = uuid()

assert(helpers.start_kong({
role = "data_plane",
Expand Down Expand Up @@ -172,7 +173,7 @@ describe("#wasm - hybrid mode #postgres", function()

it("syncs wasm filter chains to the data plane", function()
local service = admin.services:insert({})
local host = "wasm-" .. utils.random_string() .. ".test"
local host = "wasm-" .. random_string() .. ".test"

admin.routes:insert({
service = service,
Expand All @@ -196,7 +197,7 @@ describe("#wasm - hybrid mode #postgres", function()
end)
.is_truthy("service/route are ready on the data plane")

local value = utils.random_string()
local value = random_string()

local filter = admin.filter_chains:insert({
service = { id = service.id },
Expand Down Expand Up @@ -293,7 +294,7 @@ describe("#wasm - hybrid mode #postgres", function()

lazy_setup(function()
helpers.clean_logfile(cp_errlog)
node_id = utils.uuid()
node_id = uuid()

assert(helpers.start_kong({
role = "data_plane",
Expand Down Expand Up @@ -330,7 +331,7 @@ describe("#wasm - hybrid mode #postgres", function()
lazy_setup(function()
helpers.clean_logfile(cp_errlog)
tmp_dir = helpers.make_temp_dir()
node_id = utils.uuid()
node_id = uuid()

assert(helpers.start_kong({
role = "data_plane",
Expand Down
4 changes: 2 additions & 2 deletions spec/02-integration/20-wasm/09-filter-meta_spec.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local helpers = require "spec.helpers"
local utils = require "kong.tools.utils"
local cjson = require "cjson"
local random_string = require("kong.tools.rand").random_string

local file = helpers.file

Expand All @@ -24,7 +24,7 @@ local function post_config(client, config)
end

local function random_name()
return "test-" .. utils.random_string()
return "test-" .. random_string()
end


Expand Down
29 changes: 15 additions & 14 deletions spec/03-plugins/04-file-log/01-log_spec.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
local cjson = require "cjson"
local utils = require "kong.tools.utils"
local helpers = require "spec.helpers"
local pl_file = require "pl.file"
local pl_stringx = require "pl.stringx"
local pl_path = require "pl.path"
local fmt = string.format
local random_string = require("kong.tools.rand").random_string
local uuid = require("kong.tools.uuid").uuid


local FILE_LOG_PATH = os.tmpname()
Expand Down Expand Up @@ -272,7 +273,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("logs to file", function()
local uuid = utils.random_string()
local uuid = random_string()

-- Making the request
local res = assert(proxy_client:send({
Expand All @@ -294,7 +295,7 @@ for _, strategy in helpers.each_strategy() do

describe("custom log values by lua", function()
it("logs custom values to file", function()
local uuid = utils.random_string()
local uuid = random_string()

-- Making the request
local res = assert(proxy_client:send({
Expand All @@ -316,7 +317,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("unsets existing log values", function()
local uuid = utils.random_string()
local uuid = random_string()

-- Making the request
local res = assert(proxy_client:send({
Expand All @@ -339,7 +340,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("logs to file #grpc", function()
local uuid = utils.random_string()
local uuid = random_string()

-- Making the request
local ok, resp = proxy_client_grpc({
Expand All @@ -361,7 +362,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("logs to file #grpcs", function()
local uuid = utils.random_string()
local uuid = random_string()

-- Making the request
local ok, resp = proxy_client_grpcs({
Expand All @@ -383,7 +384,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("reopens file on each request", function()
local uuid1 = utils.uuid()
local uuid1 = uuid()

-- Making the request
local res = assert(proxy_client:send({
Expand All @@ -402,7 +403,7 @@ for _, strategy in helpers.each_strategy() do
os.remove(FILE_LOG_PATH)

-- Making the next request
local uuid2 = utils.uuid()
local uuid2 = uuid()
res = assert(proxy_client:send({
method = "GET",
path = "/status/200",
Expand All @@ -413,7 +414,7 @@ for _, strategy in helpers.each_strategy() do
}))
assert.res_status(200, res)

local uuid3 = utils.uuid()
local uuid3 = uuid()
res = assert(proxy_client:send({
method = "GET",
path = "/status/200",
Expand All @@ -432,7 +433,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("does not create log file if directory doesn't exist", function()
local uuid = utils.random_string()
local uuid = random_string()

helpers.clean_logfile()

Expand All @@ -452,7 +453,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("the given path is not a file but a directory", function()
local uuid = utils.random_string()
local uuid = random_string()

helpers.clean_logfile()

Expand All @@ -472,7 +473,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("logs are lost if reopen = false and file doesn't exist", function()
local uuid1 = utils.uuid()
local uuid1 = uuid()

os.remove(FILE_LOG_PATH)

Expand All @@ -491,7 +492,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("does not log if Kong has no write permissions to the file", function()
local uuid = utils.random_string()
local uuid = random_string()

helpers.clean_logfile()

Expand All @@ -511,7 +512,7 @@ for _, strategy in helpers.each_strategy() do
end)

it("the given path is a character device file", function()
local uuid = utils.random_string()
local uuid = random_string()

helpers.clean_logfile()

Expand Down
Loading

1 comment on commit 5dc3b7b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:5dc3b7ba3509a28b57f2421fdef7c24391585f9b
Artifacts available https://github.com/Kong/kong/actions/runs/8845016202

Please sign in to comment.