-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
hotfix(cli) seed random number generator in CLI #1641
Conversation
If spawning multiple nodes at once (making use of the CLI from different host machines), we need to make sure none of them are using the same seed. To enforce this, we make use of the patched 'math.randomseed()' function, which should greatly reduce the probability of seed collision. To allow for this change, we need a special flag indicating our scripts if we are running inside of our CLI, so that out 'math.randomseed()' does not complain about being called in resty-cli's 'timer' context. * add `ngx.RESTY_CLI` flag in `bin/kong` * add an edge case in our patched `math.randomseed()` * apply `kong.core.globalpatches` to our CLI environment Fix #1592
Better to sync our scripts using resty-cli so that their environment does not differ.
Here is a question I'm asking myself: would it be safer to use OpenSSL's Are we safe with using OpenSSL's seed = ngx.time() + ngx.worker.pid() |
contains a test to check for resty-cli (based on global I'd prefer not to merge this, but to update it in the |
The reason for not relying on this is that I find it too fragile, being outside of our real of control. If resty-cli changes their implementation, it would break our check.
We definitely need this for |
Far fetched imo. Resty-cli will always have the
I can see that. But can you then change the implementation to at least functional equivalent of the dns branch? Meaning; stop throwing errors, simply skip reseeding. Dns will not be able to cope with errors while the resty-dns lib still reseeds. |
did I miss this in the code? |
regarding using OpenSSL for randomseeding; See openssl docs, seems that it will error if not properly seeded, hence should be safe to use??? |
So will every other interpreters. I just don't think it is resty-cli specific enough, and semantically does not have the same meaning as "I guarantee you we are running with resty-cli right now".
I think the best behavior is actually to print a log at the |
Actually hold on. The current behavior is better. It's throwing an error if not seeded and trying to seed in another context than If already seeded, we simply log at I'd rather merge this, and you can comment out the |
(especially since there will be a |
Testing for resty; before I had this, later switched to just testing for local i = 1
for k,v in pairs(arg or {}) do
if k<i then i = k end
end
local is_resty = ("/"..tostring((arg or {})[i]) or ""):match('/resty$') ~= nil |
better/simpler probably; local is_resty = ngx and (type(arg) == "table") |
other commenst valid. So for now, imo;
|
In the end, between: local is_resty = ngx and (type(arg) == "table") and: ngx.RESTY_CLI = true One is so much simpler and also more reliable (because other interpreters also have Let's not even talk about: local i = 1
for k,v in pairs(arg or {}) do
if k<i then i = k end
end
local is_resty = ("/"..tostring((arg or {})[i]) or ""):match('/resty$') ~= nil Which is, totally, completely overkill for the sole purposes of setting a flag. |
I would be fine with this in local i = 1
for k,v in pairs(arg or {}) do
if k<i then i = k end
end
ngx.IS_RESTY_CLI = ("/"..tostring((arg or {})[i]) or ""):match('/resty$') ~= nil We keep our dirty hacks in there (ew), we rely on a "smart" detection because we |
Merging as-is for now (0.9.2), we can change the resty check or the patched-too-early behavior later and especially in other branches. |
Summary
If spawning multiple nodes at once (making use of the CLI from different
host machines), we need to make sure none of them are using the same
seed. To enforce this, we make use of the patched
math.randomseed()
function, which should greatly reduce the probability of seed collision.
To allow for this change, we need a special flag indicating our scripts
if we are running inside of our CLI, so that out
math.randomseed()
does not complain about being called in resty-cli's 'timer' context.
Full changelog
ngx.RESTY_CLI
flag inbin/kong
math.randomseed()
kong.core.globalpatches
to our CLI environmentIssues resolved
Fix #1592