Skip to content

Commit f88a2c0

Browse files
committed
Fix possibility of Rails secret key not being set from config file.
Basically, after the changes in c65ea2f, this accidentally broke some other ordering issues when reading the config, so that if `cached_random_config_values.yml` contained a Rails secret token, and any other cached values were being generated, then the cached values would always take precedent. This fixes it by ensuring that any existing config should always take precedent over the cached config that's being generated. 18F/api.data.gov#437
1 parent d8e96be commit f88a2c0

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/api-umbrella/cli/read_config.lua

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local array_includes = require "api-umbrella.utils.array_includes"
22
local array_last = require "api-umbrella.utils.array_last"
3+
local deep_defaults = require "api-umbrella.utils.deep_defaults"
34
local deep_merge_overwrite_arrays = require "api-umbrella.utils.deep_merge_overwrite_arrays"
45
local dir = require "pl.dir"
56
local file = require "pl.file"
@@ -376,28 +377,31 @@ local function set_cached_random_tokens()
376377
local cached = {}
377378
if content then
378379
cached = lyaml.load(content)
379-
deep_merge_overwrite_arrays(config, cached)
380+
deep_defaults(config, cached)
380381
end
381382

382383
-- If the tokens haven't already been written to the cache, generate them.
383384
if not config["web"]["rails_secret_token"] or not config["static_site"]["api_key"] then
384385
if not config["web"]["rails_secret_token"] then
385-
cached["web"] = {
386-
rails_secret_token = random_token(128),
387-
}
386+
deep_defaults(cached, {
387+
web = {
388+
rails_secret_token = random_token(128),
389+
},
390+
})
388391
end
389392

390393
if not config["static_site"]["api_key"] then
391-
cached["static_site"] = {
392-
api_key = random_token(40),
393-
}
394+
deep_defaults(cached, {
395+
static_site = {
396+
api_key = random_token(40),
397+
},
398+
})
394399
end
395400

396401
-- Persist the cached tokens.
397402
dir.makepath(config["run_dir"])
398403
file.write(cached_path, lyaml.dump({ cached }))
399-
400-
deep_merge_overwrite_arrays(config, cached)
404+
deep_defaults(config, cached)
401405
end
402406
end
403407
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
local is_array = require "api-umbrella.utils.is_array"
2+
3+
-- Like deep_merge_overwrite_arrays, but only assigns values from the source to
4+
-- the destination if the destination is nil. So any existing values on the
5+
-- destination object will be retained.
6+
local function deep_defaults(dest, src)
7+
if not src then return dest end
8+
9+
for key, value in pairs(src) do
10+
if type(value) == "table" and type(dest[key]) == "table" then
11+
if is_array(value) or is_array(dest[key]) then
12+
if dest[key] == nil then
13+
dest[key] = value
14+
end
15+
else
16+
deep_defaults(dest[key], src[key])
17+
end
18+
else
19+
if dest[key] == nil then
20+
dest[key] = value
21+
end
22+
end
23+
end
24+
25+
return dest
26+
end
27+
28+
return deep_defaults

0 commit comments

Comments
 (0)