@@ -11,20 +11,30 @@ const { exec_manage_cli, set_path_permissions_and_owner, TMP_PATH, set_nc_config
11
11
const { TYPES , ACTIONS } = require ( '../../../manage_nsfs/manage_nsfs_constants' ) ;
12
12
const ManageCLIError = require ( '../../../manage_nsfs/manage_nsfs_cli_errors' ) . ManageCLIError ;
13
13
const ManageCLIResponse = require ( '../../../manage_nsfs/manage_nsfs_cli_responses' ) . ManageCLIResponse ;
14
+ const { get_process_fs_context } = require ( '../../../util/native_fs_utils' ) ;
15
+ const nb_native = require ( '../../../util/nb_native' ) ;
14
16
15
17
const tmp_fs_path = path . join ( TMP_PATH , 'test_nc_invalid_mkm_integration' ) ;
16
18
const config_root = path . join ( tmp_fs_path , 'config_root_account_mkm_integration' ) ;
17
19
const root_path = path . join ( tmp_fs_path , 'root_path_account_mkm_integration/' ) ;
18
- const defaults = {
19
- _id : 'account1' ,
20
+ const defaults_account1 = {
20
21
type : TYPES . ACCOUNT ,
21
22
name : 'account1' ,
22
- new_buckets_path : `${ root_path } new_buckets_path_mkm_integration /` ,
23
- uid : 999 ,
24
- gid : 999 ,
23
+ new_buckets_path : `${ root_path } new_buckets_path_mkm_integration_account1 /` ,
24
+ uid : 1001 ,
25
+ gid : 1001 ,
25
26
access_key : 'GIGiFAnjaaE7OKD5N7hA' ,
26
27
secret_key : 'U2AYaMpU3zRDcRFWmvzgQr9MoHIAsD+3oEXAMPLE' ,
27
28
} ;
29
+ const defaults_account2 = {
30
+ type : TYPES . ACCOUNT ,
31
+ name : 'account2' ,
32
+ new_buckets_path : `${ root_path } new_buckets_path_mkm_integration_account2/` ,
33
+ uid : 1002 ,
34
+ gid : 1002 ,
35
+ access_key : 'HIHiFAnjaaE7OKD5N7hA' ,
36
+ secret_key : 'U3BYaMpU3zRDcRFWmvzgQr9MoHIAsD+3oEXAMPLE' ,
37
+ } ;
28
38
29
39
describe ( 'manage nsfs cli account flow + fauly master key flow' , ( ) => {
30
40
describe ( 'cli account ops - master key is missing' , ( ) => {
@@ -49,13 +59,13 @@ describe('manage nsfs cli account flow + fauly master key flow', () => {
49
59
} ) ;
50
60
51
61
it ( 'cli account list' , async ( ) => {
52
- const { name } = defaults ;
62
+ const { name } = defaults_account1 ;
53
63
const list_res = await list_account_flow ( ) ;
54
64
expect ( list_res . response . reply [ 0 ] . name ) . toBe ( name ) ;
55
65
} ) ;
56
66
57
67
it ( 'cli account status' , async ( ) => {
58
- const { name, uid, gid, new_buckets_path } = defaults ;
68
+ const { name, uid, gid, new_buckets_path } = defaults_account1 ;
59
69
const status_res = await status_account ( ) ;
60
70
expect ( status_res . response . reply . name ) . toBe ( name ) ;
61
71
expect ( status_res . response . reply . email ) . toBe ( name ) ;
@@ -99,7 +109,7 @@ describe('manage nsfs cli account flow + fauly master key flow', () => {
99
109
100
110
it ( 'should fail | cli create account' , async ( ) => {
101
111
try {
102
- await create_account ( { ...defaults , name : 'account_corrupted_mk' } ) ;
112
+ await create_account ( { ...defaults_account1 , name : 'account_corrupted_mk' } ) ;
103
113
fail ( 'should have failed with InvalidMasterKey' ) ;
104
114
} catch ( err ) {
105
115
expect ( JSON . parse ( err . stdout ) . error . code ) . toBe ( ManageCLIError . InvalidMasterKey . code ) ;
@@ -116,13 +126,13 @@ describe('manage nsfs cli account flow + fauly master key flow', () => {
116
126
} ) ;
117
127
118
128
it ( 'cli account list' , async ( ) => {
119
- const { name } = defaults ;
129
+ const { name } = defaults_account1 ;
120
130
const list_res = await list_account_flow ( ) ;
121
131
expect ( list_res . response . reply [ 0 ] . name ) . toBe ( name ) ;
122
132
} ) ;
123
133
124
134
it ( 'cli account status' , async ( ) => {
125
- const { name, uid, gid, new_buckets_path } = defaults ;
135
+ const { name, uid, gid, new_buckets_path } = defaults_account1 ;
126
136
const status_res = await status_account ( ) ;
127
137
expect ( status_res . response . reply . name ) . toBe ( name ) ;
128
138
expect ( status_res . response . reply . email ) . toBe ( name ) ;
@@ -165,7 +175,7 @@ describe('manage nsfs cli account flow + fauly master key flow', () => {
165
175
166
176
it ( 'should fail | cli create account' , async ( ) => {
167
177
try {
168
- await create_account ( { ...defaults , name : 'account_corrupted_mk' } ) ;
178
+ await create_account ( { ...defaults_account1 , name : 'account_corrupted_mk' } ) ;
169
179
fail ( 'should have failed with InvalidMasterKey' ) ;
170
180
} catch ( err ) {
171
181
expect ( JSON . parse ( err . stdout ) . error . code ) . toBe ( ManageCLIError . InvalidMasterKey . code ) ;
@@ -182,13 +192,13 @@ describe('manage nsfs cli account flow + fauly master key flow', () => {
182
192
} ) ;
183
193
184
194
it ( 'cli account list' , async ( ) => {
185
- const { name } = defaults ;
195
+ const { name } = defaults_account1 ;
186
196
const list_res = await list_account_flow ( ) ;
187
197
expect ( list_res . response . reply [ 0 ] . name ) . toBe ( name ) ;
188
198
} ) ;
189
199
190
200
it ( 'cli account status' , async ( ) => {
191
- const { name, uid, gid, new_buckets_path } = defaults ;
201
+ const { name, uid, gid, new_buckets_path } = defaults_account1 ;
192
202
const status_res = await status_account ( ) ;
193
203
expect ( status_res . response . reply . name ) . toBe ( name ) ;
194
204
expect ( status_res . response . reply . email ) . toBe ( name ) ;
@@ -211,6 +221,34 @@ describe('manage nsfs cli account flow + fauly master key flow', () => {
211
221
expect ( delete_res . response . code ) . toBe ( ManageCLIResponse . AccountDeleted . code ) ;
212
222
} ) ;
213
223
} ) ;
224
+
225
+ describe ( 'cli with renamed master key file (invalid)' , ( ) => {
226
+ const type = TYPES . ACCOUNT ;
227
+
228
+ beforeEach ( async ( ) => {
229
+ await setup_nc_system_and_first_account ( ) ;
230
+ await setup_account ( defaults_account2 ) ;
231
+ await master_key_file_rename ( true ) ;
232
+ } ) ;
233
+
234
+ afterEach ( async ( ) => {
235
+ await master_key_file_rename ( false ) ;
236
+ await fs_utils . folder_delete ( `${ config_root } ` ) ;
237
+ await fs_utils . folder_delete ( `${ root_path } ` ) ;
238
+ } ) ;
239
+
240
+ it ( 'cli list with wide and show_secrets flags (will show encrypted_secret_key and warning property)' , async ( ) => {
241
+ const action = ACTIONS . LIST ;
242
+ const account_options = { config_root, wide : true , show_secrets : true } ;
243
+ const res = await exec_manage_cli ( type , action , account_options ) ;
244
+ const account_array_res = JSON . parse ( res ) . response . reply ;
245
+ for ( const account_res of account_array_res ) {
246
+ expect ( account_res . access_keys [ 0 ] . encrypted_secret_key ) . toBeUndefined ( ) ;
247
+ expect ( account_res . access_keys [ 0 ] . secret_key ) . toBeUndefined ( ) ;
248
+ expect ( account_res . decryption_err ) . toBeDefined ( ) ;
249
+ }
250
+ } ) ;
251
+ } ) ;
214
252
} ) ;
215
253
216
254
async function create_account ( account_options ) {
@@ -224,7 +262,7 @@ async function create_account(account_options) {
224
262
225
263
async function update_account ( ) {
226
264
const action = ACTIONS . UPDATE ;
227
- const { type, name, new_buckets_path } = defaults ;
265
+ const { type, name, new_buckets_path } = defaults_account1 ;
228
266
const new_uid = '1111' ;
229
267
const account_options = { config_root, name, new_buckets_path, uid : new_uid } ;
230
268
const res = await exec_manage_cli ( type , action , account_options ) ;
@@ -234,7 +272,7 @@ async function update_account() {
234
272
235
273
async function status_account ( show_secrets ) {
236
274
const action = ACTIONS . STATUS ;
237
- const { type, name } = defaults ;
275
+ const { type, name } = defaults_account1 ;
238
276
const account_options = { config_root, name, show_secrets } ;
239
277
const res = await exec_manage_cli ( type , action , account_options ) ;
240
278
const parsed_res = JSON . parse ( res ) ;
@@ -243,7 +281,7 @@ async function status_account(show_secrets) {
243
281
244
282
async function list_account_flow ( ) {
245
283
const action = ACTIONS . LIST ;
246
- const { type } = defaults ;
284
+ const { type } = defaults_account1 ;
247
285
const account_options = { config_root } ;
248
286
const res = await exec_manage_cli ( type , action , account_options ) ;
249
287
const parsed_res = JSON . parse ( res ) ;
@@ -252,7 +290,7 @@ async function list_account_flow() {
252
290
253
291
async function delete_account_flow ( ) {
254
292
const action = ACTIONS . DELETE ;
255
- const { type, name } = defaults ;
293
+ const { type, name } = defaults_account1 ;
256
294
const account_options = { config_root, name } ;
257
295
const res = await exec_manage_cli ( type , action , account_options ) ;
258
296
const parsed_res = JSON . parse ( res ) ;
@@ -269,11 +307,34 @@ function fail(reason) {
269
307
async function setup_nc_system_and_first_account ( ) {
270
308
await fs_utils . create_fresh_path ( root_path ) ;
271
309
set_nc_config_dir_in_config ( config_root ) ;
310
+ await setup_account ( defaults_account1 ) ;
311
+ }
312
+
313
+ async function setup_account ( account_defaults ) {
272
314
const action = ACTIONS . ADD ;
273
- const { type, name, new_buckets_path, uid, gid } = defaults ;
315
+ const { type, name, new_buckets_path, uid, gid } = account_defaults ;
274
316
const account_options = { config_root, name, new_buckets_path, uid, gid } ;
275
317
await fs_utils . create_fresh_path ( new_buckets_path ) ;
276
318
await fs_utils . file_must_exist ( new_buckets_path ) ;
277
319
await set_path_permissions_and_owner ( new_buckets_path , account_options , 0o700 ) ;
278
320
await exec_manage_cli ( type , action , account_options ) ;
279
321
}
322
+
323
+
324
+ /**
325
+ * master_key_file_rename will rename the master_keys.json file
326
+ * to mock a situation where master_key_id points to a missing master key
327
+ * use the to_rename_temp false to rename it back (after the test)
328
+ * @param {boolean } to_rename_temp
329
+ */
330
+ async function master_key_file_rename ( to_rename_temp ) {
331
+ const default_fs_config = get_process_fs_context ( ) ;
332
+ const source_path = path . join ( config_root , 'master_keys.json' ) ;
333
+ const dest_path = path . join ( config_root , 'temp_master_keys.json' ) ;
334
+ // eliminate the master key file by renaming it
335
+ if ( to_rename_temp ) {
336
+ await nb_native ( ) . fs . rename ( default_fs_config , source_path , dest_path ) ;
337
+ } else {
338
+ await nb_native ( ) . fs . rename ( default_fs_config , dest_path , source_path ) ;
339
+ }
340
+ }
0 commit comments