22'use strict' ;
33
44const _ = require ( 'lodash' ) ;
5- const SensitiveString = require ( '../util/sensitive_string' ) ;
65const account_util = require ( '../util/account_util' ) ;
76const iam_utils = require ( '../endpoint/iam/iam_utils' ) ;
87const dbg = require ( '../util/debug_module' ) ( __filename ) ;
@@ -48,7 +47,7 @@ class AccountSpaceNB {
4847 { username : params . username , path : params . iam_path } ) ;
4948 account_util . _check_username_already_exists ( action , params , requesting_account ) ;
5049 const iam_arn = iam_utils . create_arn_for_user ( requesting_account . _id . toString ( ) , params . username , params . iam_path ) ;
51- const account_name = account_util . get_account_name_from_username ( params . username , requesting_account . name . unwrap ( ) ) ;
50+ const account_name = account_util . get_account_name_from_username ( params . username , requesting_account . _id ) ;
5251 const req = {
5352 rpc_params : {
5453 name : account_name ,
@@ -60,16 +59,14 @@ class AccountSpaceNB {
6059 is_iam : true ,
6160 iam_arn : iam_arn ,
6261 iam_path : params . iam_path ,
63- role : 'iam_user ' ,
62+ role : 'admin ' ,
6463
6564 // TODO: default_resource remove
6665 default_resource : 'noobaa-default-backing-store' ,
6766 } ,
6867 account : requesting_account ,
6968 } ;
70- // CORE CHANGES PENDING - START
7169 const iam_account = await account_util . create_account ( req ) ;
72- // CORE CHANGES PENDING - END
7370
7471 // TODO : Clean account cache
7572 // TODO : Send Event
@@ -87,19 +84,19 @@ class AccountSpaceNB {
8784 async get_user ( params , account_sdk ) {
8885 const action = IAM_ACTIONS . GET_USER ;
8986 const requesting_account = system_store . get_account_by_email ( account_sdk . requesting_account . email ) ;
90- const account_name = account_util . get_account_name_from_username ( params . username , requesting_account . name . unwrap ( ) ) ;
87+ const account_name = account_util . get_account_name_from_username ( params . username , requesting_account . _id ) ;
9188 const requested_account = system_store . get_account_by_email ( account_name ) ;
9289 account_util . _check_if_requesting_account_is_root_account ( action , requesting_account ,
9390 { username : params . username , iam_path : params . iam_path } ) ;
9491 account_util . _check_if_account_exists ( action , account_name ) ;
9592 account_util . _check_if_requested_account_is_root_account_or_IAM_user ( action , requesting_account , requested_account ) ;
9693 account_util . _check_if_requested_is_owned_by_root_account ( action , requesting_account , requested_account ) ;
94+ const iam_arn = iam_utils . create_arn_for_user ( requesting_account . _id . toString ( ) , params . username , requested_account . iam_path ) ;
9795 const reply = {
9896 user_id : requested_account . _id . toString ( ) ,
99- // TODO : IAM PATH
10097 iam_path : requested_account . iam_path || IAM_DEFAULT_PATH ,
10198 username : account_util . get_iam_username ( requested_account . name . unwrap ( ) ) ,
102- arn : requested_account . iam_arn ,
99+ arn : iam_arn ,
103100 // TODO: GAP Need to save created date
104101 create_date : Date . now ( ) ,
105102 // TODO: Dates missing : GAP
@@ -111,7 +108,7 @@ class AccountSpaceNB {
111108 async update_user ( params , account_sdk ) {
112109 const action = IAM_ACTIONS . UPDATE_USER ;
113110 const requesting_account = system_store . get_account_by_email ( account_sdk . requesting_account . email ) ;
114- const username = account_util . get_account_name_from_username ( params . username , requesting_account . name . unwrap ( ) ) ;
111+ const username = account_util . get_account_name_from_username ( params . username , requesting_account . _id ) ;
115112 account_util . _check_if_requesting_account_is_root_account ( action , requesting_account ,
116113 { username : params . username , iam_path : params . iam_path } ) ;
117114 account_util . _check_if_account_exists ( action , username ) ;
@@ -124,14 +121,12 @@ class AccountSpaceNB {
124121 if ( params . new_iam_path !== undefined ) iam_path = params . new_iam_path ;
125122 if ( params . new_username !== undefined ) user_name = params . new_username ;
126123 const iam_arn = iam_utils . create_arn_for_user ( requested_account . _id . toString ( ) , user_name , iam_path ) ;
127- const new_account_name = new SensitiveString ( ` ${ params . new_username } : ${ requesting_account . name . unwrap ( ) } ` ) ;
124+ const new_account_name = account_util . get_account_name_from_username ( params . new_username , requesting_account . _id ) ;
128125 const updates = {
129126 name : new_account_name ,
130127 email : new_account_name ,
131- iam_arn : iam_arn ,
132128 iam_path : iam_path ,
133129 } ;
134- // CORE CHANGES PENDING - START
135130 await system_store . make_changes ( {
136131 update : {
137132 accounts : [ {
@@ -140,11 +135,9 @@ class AccountSpaceNB {
140135 } ]
141136 }
142137 } ) ;
143- // CORE CHANGES PENDING - END
144138 // TODO : Clean account cache
145139 // TODO : Send Event
146140 return {
147- // TODO: IAM path needs to be saved
148141 iam_path : iam_path || IAM_DEFAULT_PATH ,
149142 username : user_name ,
150143 user_id : requested_account . _id . toString ( ) ,
@@ -156,14 +149,15 @@ class AccountSpaceNB {
156149 async delete_user ( params , account_sdk ) {
157150 const action = IAM_ACTIONS . DELETE_USER ;
158151 const requesting_account = system_store . get_account_by_email ( account_sdk . requesting_account . email ) ;
159- const username = account_util . get_account_name_from_username ( params . username , requesting_account . name . unwrap ( ) ) ;
152+ const username = account_util . get_account_name_from_username ( params . username , requesting_account . _id ) ;
160153 account_util . _check_if_requesting_account_is_root_account ( action , requesting_account , { username : params . username } ) ;
161154 account_util . _check_if_account_exists ( action , username ) ;
162155 const requested_account = system_store . get_account_by_email ( username ) ;
163156 account_util . _check_if_requested_account_is_root_account_or_IAM_user ( action , requesting_account , requested_account ) ;
164157 account_util . _check_if_requested_is_owned_by_root_account ( action , requesting_account , requested_account ) ;
158+ // Need to delete all accesskeys before deleting user
159+ account_util . _check_if_user_does_not_have_access_keys_before_deletion ( action , requested_account ) ;
165160 // TODO: DELETE INLINE POLICY : Manually
166- // TODO: DELETE ACCESS KEY : manually
167161 const req = {
168162 system : system_store . data . systems [ 0 ] ,
169163 account : requested_account ,
@@ -181,28 +175,28 @@ class AccountSpaceNB {
181175 account_util . _check_if_requesting_account_is_root_account ( action , requesting_account , { } ) ;
182176 const is_truncated = false ; // GAP - no pagination at this point
183177
184- const root_name = requesting_account . name . unwrap ( ) ;
185- // CORE CHANGES PENDING - START
178+
186179 const requesting_account_iam_users = _ . filter ( system_store . data . accounts , function ( acc ) {
187180 if ( ! acc . name . unwrap ( ) . includes ( IAM_SPLIT_CHARACTERS ) ) {
188181 return false ;
189182 }
190- return acc . name . unwrap ( ) . split ( IAM_SPLIT_CHARACTERS ) [ 1 ] === root_name ;
183+ return acc . name . unwrap ( ) . split ( IAM_SPLIT_CHARACTERS ) [ 1 ] === requesting_account . _id . toString ( ) ;
191184 } ) ;
192185 let members = _ . map ( requesting_account_iam_users , function ( iam_user ) {
186+ const iam_username = account_util . get_iam_username ( iam_user . name . unwrap ( ) ) ;
187+ const iam_path = iam_user . iam_path || IAM_DEFAULT_PATH ;
193188 const member = {
194189 user_id : iam_user . _id . toString ( ) ,
195- iam_path : iam_user . iam_path || IAM_DEFAULT_PATH ,
196- username : iam_user . name . unwrap ( ) . split ( IAM_SPLIT_CHARACTERS ) [ 0 ] ,
197- arn : iam_user . iam_arn ,
190+ iam_path : iam_path ,
191+ username : iam_username ,
192+ arn : iam_utils . create_arn_for_user ( iam_user . _id . toString ( ) , iam_username , iam_path ) ,
198193 // TODO: GAP Need to save created date
199194 create_date : Date . now ( ) ,
200195 // TODO: GAP missing password_last_used
201196 password_last_used : Date . now ( ) , // GAP
202197 } ;
203198 return member ;
204199 } ) ;
205- // CORE CHANGES PENDING - END
206200 members = members . sort ( ( a , b ) => a . username . localeCompare ( b . username ) ) ;
207201 return { members, is_truncated } ;
208202 }
@@ -215,7 +209,7 @@ class AccountSpaceNB {
215209 const action = IAM_ACTIONS . CREATE_ACCESS_KEY ;
216210 const requesting_account = system_store . get_account_by_email ( account_sdk . requesting_account . email ) ;
217211 const requested_account = validate_and_return_requested_account ( params , action , requesting_account , account_sdk ) ;
218- const account_email = params . username ? new SensitiveString ( ` ${ params . username } : ${ requesting_account . name . unwrap ( ) } ` ) :
212+ const account_email = params . username ? account_util . get_account_name_from_username ( params . username , requesting_account . _id ) :
219213 account_sdk . requesting_account . email ;
220214 account_util . _check_number_of_access_key_array ( action , requested_account ) ;
221215 const req = {
@@ -225,7 +219,6 @@ class AccountSpaceNB {
225219 } ,
226220 account : requesting_account ,
227221 } ;
228- // CORE CHANGES PENDING - START
229222 let iam_access_key ;
230223 try {
231224 iam_access_key = await account_util . generate_account_keys ( req ) ;
@@ -236,8 +229,6 @@ class AccountSpaceNB {
236229 throw new IamError ( { code, message : message_with_details , http_code, type } ) ;
237230 }
238231
239- // CORE CHANGES PENDING - STOP
240-
241232 return {
242233 username : params . username ,
243234 access_key : iam_access_key . access_key . unwrap ( ) ,
@@ -362,7 +353,7 @@ function validate_and_return_requested_account(params, action, requesting_accoun
362353 // So in that case requesting account and requested account is same.
363354 requested_account = requesting_account ;
364355 } else {
365- const account_email = account_util . get_account_name_from_username ( params . username , requesting_account . name . unwrap ( ) ) ;
356+ const account_email = account_util . get_account_name_from_username ( params . username , requesting_account . _id ) ;
366357 account_util . _check_if_account_exists ( action , account_email ) ;
367358 requested_account = system_store . get_account_by_email ( account_email ) ;
368359 account_util . _check_if_requesting_account_is_root_account ( action , requesting_account , { username : params . username } ) ;
0 commit comments