Skip to content

Commit

Permalink
NC | avoid concurrency master key manager initialization
Browse files Browse the repository at this point in the history
Signed-off-by: Romy <35330373+romayalon@users.noreply.github.com>
  • Loading branch information
romayalon committed May 30, 2024
1 parent a97af1d commit 6f1d2a7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/cmd/manage_nsfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,10 @@ async function account_management(action, user_input) {
user_input.name = config.ANONYMOUS_ACCOUNT_NAME;
user_input.email = config.ANONYMOUS_ACCOUNT_NAME;
}
// init nc_mkm here to avoid concurrent initializations
// init if actions is add/update (require encryption) or show_secrets = true (require decryption)
if ([ACTIONS.ADD, ACTIONS.UPDATE].includes(action) || show_secrets) await nc_mkm.init();

const data = await fetch_account_data(action, user_input);
await manage_account_operations(action, data, show_secrets, user_input);
}
Expand Down Expand Up @@ -627,7 +631,9 @@ async function list_config_files(type, config_path, wide, show_secrets, filters)
if (wide || should_filter) {
const full_path = path.join(config_path, entry.name);
const data = await get_config_data(config_root_backend, full_path, show_secrets || should_filter);
if (data.access_keys) data.access_keys = await nc_mkm.decrypt_access_keys(data);
// decryption causing mkm initalization
// decrypt only if data has access_keys and show_secrets = true (no need to decrypt if show_secrets = false but should_filter = true)
if (data.access_keys && show_secrets) data.access_keys = await nc_mkm.decrypt_access_keys(data);
if (should_filter && !filter_list_item(type, data, filters)) return undefined;
// remove secrets on !show_secrets && should filter
return wide ? _.omit(data, show_secrets ? [] : ['access_keys']) : { name: entry.name.slice(0, entry.name.indexOf('.json')) };
Expand Down
10 changes: 7 additions & 3 deletions src/manage_nsfs/nc_master_key_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';

const _ = require('lodash');
const util = require('util');
const path = require('path');
const crypto = require('crypto');
const P = require('../util/promise');
Expand Down Expand Up @@ -93,7 +92,7 @@ class NCMasterKeysManager {
if (!this.active_master_key) {
throw new RpcError('INVALID_MASTER_KEYS_FILE', 'Invalid master_keys.json file, couldn\'t find active master key in master_keys_by_id');
}
dbg.log1(`_set_keys: master_key_manager updated successfully! active master key is: ${util.inspect(this.active_master_key)}`);
dbg.log1('_set_keys: master_key_manager updated successfully!');
return this.active_master_key;
}

Expand Down Expand Up @@ -234,13 +233,18 @@ class NCMasterKeysManager {
for (let retries = 0; retries < config.MASTER_KEYS_EXEC_MAX_RETRIES;) {
try {
if (this.last_init_time &&
(new Date()).getTime() - this.last_init_time > config.NC_MASTER_KEYS_MANAGER_REFRESH_THRESHOLD) return;
(new Date()).getTime() - this.last_init_time > config.NC_MASTER_KEYS_MANAGER_REFRESH_THRESHOLD) {
dbg.log1('_init_from_exec: cache is updated nothing to do, skipping...');
return;
}
dbg.log1('_init_from_exec: calling config.NC_MASTER_KEYS_GET_EXECUTABLE script');
const get_master_keys_res = await os_util.exec(command, { return_stdout: true });
const { status, version, data } = JSON.parse(get_master_keys_res);
if (status === EXEC_STATUS_OK) {
dbg.log0(`init_from_exec: get master keys response status=${status}, version=${version}`);
this._set_keys(data);
this.last_init_time = (new Date()).getTime();
dbg.log1('_init_from_exec: updating this.last_init_time', this.last_init_time);
return;
} else if (status === EXEC_STATUS_NOT_FOUND) {
dbg.warn(`init_from_exec: get master keys failed with status=${status}, creating a new master key`);
Expand Down

0 comments on commit 6f1d2a7

Please sign in to comment.