From 1b8179992e5fcec5cad612b8211055f737962e39 Mon Sep 17 00:00:00 2001 From: acsbendi Date: Sat, 13 Jul 2019 15:58:34 +0200 Subject: [PATCH] GetUser returns the IAM user who owns the access key in the request. --- moto/iam/models.py | 8 ++++++++ moto/iam/responses.py | 12 +++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/moto/iam/models.py b/moto/iam/models.py index f92568df4..bb19b8cad 100644 --- a/moto/iam/models.py +++ b/moto/iam/models.py @@ -1248,5 +1248,13 @@ class IAMBackend(BaseBackend): return saml_provider raise IAMNotFoundException("SamlProvider {0} not found".format(saml_provider_arn)) + def get_user_from_access_key_id(self, access_key_id): + for user_name, user in self.users.items(): + access_keys = self.get_all_access_keys(user_name) + for access_key in access_keys: + if access_key.access_key_id == access_key_id: + return user + return None + iam_backend = IAMBackend() diff --git a/moto/iam/responses.py b/moto/iam/responses.py index 7671e8cb8..7ec6242f6 100644 --- a/moto/iam/responses.py +++ b/moto/iam/responses.py @@ -1,9 +1,8 @@ from __future__ import unicode_literals from moto.core.responses import BaseResponse -from .models import iam_backend -AVATAO_USER_NAME = "avatao-user" +from .models import iam_backend, User class IamResponse(BaseResponse): @@ -428,9 +427,12 @@ class IamResponse(BaseResponse): def get_user(self): user_name = self._get_param('UserName') if not user_name: - user_name = AVATAO_USER_NAME - # If no user is specified, IAM returns the current user - user = iam_backend.get_user(user_name) + access_key_id = self.get_current_user() + user = iam_backend.get_user_from_access_key_id(access_key_id) + if user is None: + user = User("default_user") + else: + user = iam_backend.get_user(user_name) template = self.response_template(USER_TEMPLATE) return template.render(action='Get', user=user)