Skip to content

Commit ac7ca70

Browse files
authored
Merge pull request #8832 from jackyalbo/jacky-fix
Moving root_secret load to P.map and loading the latest keys first
2 parents 813a268 + 46bcb9a commit ac7ca70

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/server/system_services/master_key_manager.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ const crypto = require('crypto');
77
const config = require('../../../config');
88
const db_client = require('../../util/db_client').instance();
99
const dbg = require('../../util/debug_module')(__filename);
10+
const js_utils = require('../../util/js_utils');
1011
const SensitiveString = require('../../util/sensitive_string');
1112
const LRUCache = require('../../util/lru_cache');
1213
const fs = require('fs');
1314
const path = require('path');
15+
const P = require('../../util/promise');
1416

1517
// dummy object id of root key
1618
const ROOT_KEY = '00000000aaaabbbbccccdddd';
@@ -89,15 +91,17 @@ class MasterKeysManager {
8991
this.active_root_key = active_root_key_id;
9092
dbg.log0(`load_root_keys_from_mount: Root keys was updated at: ${this.last_load_time}. ` +
9193
`active root key is: ${this.active_root_key}`);
92-
for (const key_id of root_keys) {
93-
// skipping file named active_root_key - as we already handled it
94-
// also skipping some garbage files k8s adding to the mount
95-
if (key_id === 'active_root_key' || key_id.startsWith('..')) continue;
94+
// we won't load the active_root_key and not keys starting with '..'
95+
const filtered_root_keys = root_keys.filter(key_id => key_id !== 'active_root_key' && !key_id.startsWith('..'));
96+
// we will load newer keys first - active key will be first(sorting by epoch)
97+
const sorted_keys = filtered_root_keys.sort(js_utils.sort_compare_by(key_id => Number(key_id.split('-')[1]), -1));
98+
await P.map_with_concurrency(20, sorted_keys, async key_id => {
9699
const current_key_path = path.join(config.ROOT_KEY_MOUNT, key_id);
97100
const key_cipher = await fs.promises.readFile(current_key_path, 'utf8');
98101
const r_key = this._add_to_resolved_keys(key_id, key_cipher, key_id !== active_root_key_id);
99102
this.root_keys_by_id[key_id] = r_key;
100-
}
103+
});
104+
dbg.log0(`load_root_keys_from_mount: done loading all root_keys from mount: ${sorted_keys.length} keys.`);
101105
this.is_initialized = true;
102106
}
103107

@@ -162,7 +166,7 @@ class MasterKeysManager {
162166
if (this.is_root_key(_id)) return this.get_root_key();
163167
const mkey = this.master_keys_by_id[_id.toString()];
164168
const rkey = this.root_keys_by_id[_id.toString()];
165-
if (!mkey && !rkey) throw new Error('NO_SUCH_KEY');
169+
if (!mkey && !rkey) throw new Error('NO_SUCH_KEY: ' + _id.toString());
166170
return this.resolved_master_keys_by_id[_id.toString()] ||
167171
(mkey && this._resolve_master_key(mkey));
168172
}

0 commit comments

Comments
 (0)