Skip to content

Commit c429ba5

Browse files
committed
CLI list with descryption error handle
Signed-off-by: shirady <[email protected]>
1 parent 445cfbf commit c429ba5

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

src/cmd/manage_nsfs.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ async function manage_bucket_operations(action, data, user_input) {
247247
} else if (action === ACTIONS.LIST) {
248248
const bucket_filters = _.pick(user_input, LIST_BUCKET_FILTERS);
249249
const wide = get_boolean_or_string_value(user_input.wide);
250-
const buckets = await list_config_files(TYPES.BUCKET, config_fs.buckets_dir_path, wide, undefined, bucket_filters);
250+
const buckets = await list_config_files(TYPES.BUCKET, wide, undefined, bucket_filters);
251251
write_stdout_response(ManageCLIResponse.BucketList, buckets);
252252
} else {
253253
// we should not get here (we check it before)
@@ -522,8 +522,7 @@ async function manage_account_operations(action, data, show_secrets, user_input)
522522
} else if (action === ACTIONS.LIST) {
523523
const account_filters = _.pick(user_input, LIST_ACCOUNT_FILTERS);
524524
const wide = get_boolean_or_string_value(user_input.wide);
525-
const accounts = await list_config_files(TYPES.ACCOUNT, config_fs.accounts_dir_path, wide,
526-
show_secrets, account_filters);
525+
const accounts = await list_config_files(TYPES.ACCOUNT, wide, show_secrets, account_filters);
527526
write_stdout_response(ManageCLIResponse.AccountList, accounts);
528527
} else {
529528
// we should not get here (we check it before)
@@ -583,12 +582,11 @@ function filter_bucket(bucket, filters) {
583582
}
584583
/**
585584
* list_config_files will list all the config files (json) in a given config directory
586-
* @param {string} config_path
587585
* @param {boolean} [wide]
588586
* @param {boolean} [show_secrets]
589587
* @param {object} [filters]
590588
*/
591-
async function list_config_files(type, config_path, wide, show_secrets, filters) {
589+
async function list_config_files(type, wide, show_secrets, filters) {
592590
const entries = type === TYPES.ACCOUNT ?
593591
await config_fs.list_root_accounts() :
594592
await config_fs.list_buckets();
@@ -598,8 +596,9 @@ async function list_config_files(type, config_path, wide, show_secrets, filters)
598596
// decrypt only if data has access_keys and show_secrets = true (no need to decrypt if show_secrets = false but should_filter = true)
599597
const options = {
600598
show_secrets: show_secrets || should_filter,
601-
should_decrypt: show_secrets,
602-
silent_if_missing: true
599+
decrypt_secret_key: show_secrets,
600+
silent_if_missing: true,
601+
add_master_key_warning: show_secrets,
603602
};
604603

605604
let config_files_list = await P.map_with_concurrency(10, entries, async entry => {

src/sdk/config_fs.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,19 +127,25 @@ class ConfigFS {
127127
* if silent_if_missing is true -
128128
* if the config file was deleted (encounter ENOENT error) - continue (returns undefined)
129129
* @param {string} config_file_path
130-
* @param {{show_secrets?: boolean, decrypt_secret_key?: boolean, silent_if_missing?: boolean}} [options]
130+
* @param {{show_secrets?: boolean, decrypt_secret_key?: boolean, silent_if_missing?: boolean, add_master_key_warning?: boolean}} [options]
131131
* @returns {Promise<Object>}
132132
*/
133133
async get_config_data(config_file_path, options = {}) {
134-
const { show_secrets = false, decrypt_secret_key = false, silent_if_missing = false } = options;
134+
const { show_secrets = false, decrypt_secret_key = false, silent_if_missing = false,
135+
add_master_key_warning = false } = options;
136+
let config_data;
135137
try {
136138
const { data } = await nb_native().fs.readFile(this.fs_context, config_file_path);
137-
const config_data = _.omit(JSON.parse(data.toString()), show_secrets ? [] : ['access_keys']);
139+
config_data = _.omit(JSON.parse(data.toString()), show_secrets ? [] : ['access_keys']);
138140
if (decrypt_secret_key) config_data.access_keys = await nc_mkm.decrypt_access_keys(config_data);
139141
return config_data;
140142
} catch (err) {
141143
dbg.warn('get_config_data: with config_file_path', config_file_path, 'got an error', err);
142144
if (silent_if_missing && err.code === 'ENOENT') return;
145+
if (add_master_key_warning && err.rpc_code === 'INVALID_MASTER_KEY') {
146+
config_data.master_key_warning = `Got an error on decryption account ${config_data._id}`;
147+
return config_data;
148+
}
143149
throw err;
144150
}
145151
}

src/test/unit_tests/jest_tests/test_nc_nsfs_account_cli.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,6 +1316,18 @@ describe('manage nsfs cli account flow', () => {
13161316
expect(JSON.parse(res.stdout).error.message).toBe(ManageCLIError.InvalidFlagsCombination.message);
13171317
});
13181318

1319+
it('cli list wide and show_secrets', async () => {
1320+
const account_options = { config_root, wide: true, show_secrets: true };
1321+
const action = ACTIONS.LIST;
1322+
const res = await exec_manage_cli(type, action, account_options);
1323+
expect(JSON.parse(res).response.reply.map(item => item.name))
1324+
.toEqual(expect.arrayContaining(['account3', 'account2', 'account1']));
1325+
const res_arr = JSON.parse(res).response.reply;
1326+
for (const item of res_arr) {
1327+
expect(item.access_keys[0].secret_key).toBeDefined();
1328+
}
1329+
});
1330+
13191331
});
13201332

13211333
describe('cli delete account', () => {
@@ -2038,6 +2050,23 @@ describe('cli without master key id', () => {
20382050
// rename the master_key.json
20392051
await master_key_file_rename(config_root, false);
20402052
});
2053+
2054+
it('cli list with wide and show_secrets flags (without master key id)', async () => {
2055+
// rename the master_key.json
2056+
await master_key_file_rename(config_root, true);
2057+
2058+
const action = ACTIONS.LIST;
2059+
const account_options = { config_root, wide: true, show_secrets: true };
2060+
const res = await exec_manage_cli(type, action, account_options);
2061+
const res_arr = JSON.parse(res).response.reply;
2062+
for (const item of res_arr) {
2063+
expect(item.access_keys[0].encrypted_secret_key).toBeDefined();
2064+
expect(item.master_key_warning).toBeDefined();
2065+
}
2066+
2067+
// rename the master_key.json
2068+
await master_key_file_rename(config_root, false);
2069+
});
20412070
});
20422071

20432072
/**

0 commit comments

Comments
 (0)