@@ -696,25 +696,31 @@ function validate_create_account_params(req) {
696696}
697697
698698function validate_and_return_requested_account ( params , action , requesting_account ) {
699- const on_itself = ! params . username ;
700- let requested_account ;
701- if ( on_itself ) {
702- // When accesskeyt API called without specific username, action on the same requesting account.
703- // So in that case requesting account and requested account is same.
704- requested_account = requesting_account ;
705- // we do not allow for AWS account root user to perform IAM action on itself
706- if ( requesting_account . owner === undefined ) {
707- throw new RpcError ( 'NOT_AUTHORIZED' , 'You do not have permission to perform this action.' ) ;
708- }
709- } else {
710- _check_if_requesting_account_is_root_account ( action , requesting_account , { username : params . username } ) ;
711- const account_email = get_account_email_from_username ( params . username , requesting_account . _id . toString ( ) ) ;
712- _check_if_account_exists ( action , account_email , params . username ) ;
713- requested_account = system_store . get_account_by_email ( account_email ) ;
714- _check_if_requested_account_is_root_account_or_IAM_user ( action , requesting_account , requested_account ) ;
715- _check_if_requested_is_owned_by_root_account ( action , requesting_account , requested_account ) ;
699+ const requester_username = requesting_account . name . unwrap ( ) ;
700+ // check if root account or IAM user is operating on themselves (with or without --user-name flag)
701+ const no_username_or_self_operation = ! params . username ; // can be root account or IAM user
702+ const is_iam_user_operating_on_itself = ! _check_root_account ( requesting_account ) &&
703+ requester_username . toLowerCase ( ) === params . username ?. toLowerCase ( ) ;
704+ const on_itself = no_username_or_self_operation || is_iam_user_operating_on_itself ;
705+
706+ let requested_account ;
707+ if ( on_itself ) {
708+ // When accesskey API called without specific username, action on the same requesting account.
709+ // So in that case requesting account and requested account is same.
710+ requested_account = requesting_account ;
711+ // we do not allow for AWS account root user to perform IAM action on itself
712+ if ( requesting_account . owner === undefined ) {
713+ throw new RpcError ( 'NOT_AUTHORIZED' , 'You do not have permission to perform this action.' ) ;
716714 }
717- return requested_account ;
715+ } else {
716+ _check_if_requesting_account_is_root_account ( action , requesting_account , { username : params . username } ) ;
717+ const account_email = get_account_email_from_username ( params . username , requesting_account . _id . toString ( ) ) ;
718+ _check_if_account_exists ( action , account_email , params . username ) ;
719+ requested_account = system_store . get_account_by_email ( account_email ) ;
720+ _check_if_requested_account_is_root_account_or_IAM_user ( action , requesting_account , requested_account ) ;
721+ _check_if_requested_is_owned_by_root_account ( action , requesting_account , requested_account ) ;
722+ }
723+ return requested_account ;
718724}
719725
720726function return_list_member ( iam_user , iam_path , iam_username ) {
0 commit comments