Skip to content

Commit 0dbdccc

Browse files
committed
IAM | CreateUser, UpdateUser - UserName, NewUserName Check
Signed-off-by: shirady <57721533+shirady@users.noreply.github.com>
1 parent 7362470 commit 0dbdccc

File tree

6 files changed

+972
-785
lines changed

6 files changed

+972
-785
lines changed

src/api/account_api.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,9 @@ module.exports = {
676676
iam_path: {
677677
type: 'string',
678678
},
679+
username: {
680+
type: 'string',
681+
},
679682
}
680683
},
681684
reply: {

src/sdk/accountspace_fs.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -794,15 +794,34 @@ class AccountSpaceFS {
794794
}
795795

796796
async _check_username_already_exists(action, params, requesting_account) {
797-
const owner_account_id = this._get_owner_account_argument(requesting_account, params);
797+
const owner_account_id = this._get_owner_account_argument(requesting_account);
798798
const username = params.username;
799-
const name_exists = await this.config_fs.is_account_exists_by_name(username, owner_account_id);
800-
if (name_exists) {
801-
dbg.error(`AccountSpaceFS.${action} username already exists`, username);
802-
const message_with_details = `User with name ${username} already exists.`;
803-
const { code, http_code, type } = IamError.EntityAlreadyExists;
804-
throw new IamError({ code, message: message_with_details, http_code, type });
799+
const file_name_exists = await this.config_fs.is_account_exists_by_name(username, owner_account_id);
800+
if (file_name_exists) {
801+
this._throw_error_if_account_already_exists(action, username);
802+
}
803+
const is_username_lowercase_exists_under_owner = await this._check_if_account_exists_under_the_owner(
804+
requesting_account, username);
805+
if (is_username_lowercase_exists_under_owner) {
806+
this._throw_error_if_account_already_exists(action, username);
807+
}
808+
}
809+
810+
_throw_error_if_account_already_exists(action, username) {
811+
dbg.error(`AccountSpaceFS.${action} username already exists`, username);
812+
const message_with_details = `User with name ${username} already exists.`;
813+
const { code, http_code, type } = IamError.EntityAlreadyExists;
814+
throw new IamError({ code, message: message_with_details, http_code, type });
815+
}
816+
817+
async _check_if_account_exists_under_the_owner(requesting_account, username) {
818+
const members = await this._list_config_files_for_users(requesting_account, undefined);
819+
for (const member of members) {
820+
if (member.username.toLowerCase() === username.toLowerCase()) {
821+
return true;
822+
}
805823
}
824+
return false;
806825
}
807826

808827
async _copy_data_from_requesting_account_to_account_config(action, requesting_account, params) {

src/sdk/accountspace_nb.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class AccountSpaceNB {
4545
s3_access: true,
4646
allow_bucket_creation: true,
4747
owner: requesting_account._id.toString(),
48+
username: params.username,
4849
iam_path: params.iam_path,
4950
roles: ['admin'],
5051
// TODO: default_resource remove

src/server/system_services/account_server.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ const check_new_azure_connection_timeout = 20 * 1000;
3737
*
3838
*/
3939
async function create_account(req) {
40-
const action = IAM_ACTIONS.CREATE_USER;
4140
let iam_arn;
4241
if (req.rpc_params.owner) {
43-
const user_name = account_util.get_iam_username(req.rpc_params.email.unwrap());
42+
const action = IAM_ACTIONS.CREATE_USER;
4443
account_util._check_if_requesting_account_is_root_account(action, req.account,
45-
{ username: user_name, path: req.rpc_params.iam_path });
46-
account_util._check_username_already_exists(action, req.rpc_params.email, user_name);
47-
iam_arn = iam_utils.create_arn_for_user(req.account._id.toString(), user_name,
44+
{ username: req.rpc_params.username, path: req.rpc_params.iam_path });
45+
account_util._check_username_already_exists(action, req.rpc_params.email,
46+
req.rpc_params.username);
47+
iam_arn = iam_utils.create_arn_for_user(req.account._id.toString(), req.rpc_params.username,
4848
req.rpc_params.iam_path || IAM_DEFAULT_PATH);
4949
} else {
5050
account_util.validate_create_account_permissions(req);
@@ -1234,8 +1234,14 @@ async function update_user(req) {
12341234
let iam_path = requested_account.iam_path;
12351235
let user_name = account_util.get_iam_username(requested_account.name.unwrap());
12361236
// Change to complete user name
1237-
const new_username = account_util.get_account_name_from_username(req.rpc_params.new_username, requesting_account._id.toString());
1238-
account_util._check_username_already_exists(action, new_username, req.rpc_params.new_username);
1237+
const is_username_update = req.rpc_params.new_username !== undefined &&
1238+
req.rpc_params.new_username !== req.rpc_params.username;
1239+
if (is_username_update) {
1240+
const email_new_username_wrapped = account_util.get_account_name_from_username(
1241+
req.rpc_params.new_username,
1242+
requesting_account._id.toString());
1243+
account_util._check_username_already_exists(action, email_new_username_wrapped, req.rpc_params.new_username);
1244+
}
12391245
account_util._check_if_requested_account_is_root_account_or_IAM_user(action, requesting_account, requested_account);
12401246
account_util._check_if_requested_is_owned_by_root_account(action, requesting_account, requested_account);
12411247
if (req.rpc_params.new_iam_path) iam_path = req.rpc_params.new_iam_path;

0 commit comments

Comments
 (0)