Skip to content

Commit

Permalink
[plugins/aws] Get account name from profile (#1778)
Browse files Browse the repository at this point in the history
* [plugins/aws] Get account name from profile

* [plugins/aws] More safeguards

* Use core_feedback
  • Loading branch information
lloesche authored Sep 20, 2023
1 parent cf5f015 commit d62a23e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 29 additions & 2 deletions plugins/aws/resoto_plugin_aws/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ def current_account_id_and_partition(profile: Optional[str] = None) -> Tuple[str
raise botocore.exceptions.NoCredentialsError()


def set_account_names(accounts: List[AwsAccount]) -> None:
def set_account_names(accounts: List[AwsAccount], core_feedback: CoreFeedback) -> None:
def set_account_name(account: AwsAccount) -> None:
def set_name_from_account_alias() -> bool:
try:
Expand Down Expand Up @@ -491,13 +491,40 @@ def set_name_from_org() -> bool:
pass
return False

def set_name_from_profile() -> bool:
if account.profile:
account.name = account.profile
log.debug(f"Set name for {account.kdname} from profile")
return True
return False

# if we prefer the profile name and we have a profile
# we set the name from the profile and return immediately
if Config.aws.prefer_profile_as_account_name:
if Config.aws.scrape_org:
core_feedback.error(
"Possible misconfiguration: setting prefer_profile_as_account_name"
" with scrape_org enabled is likely not what you want",
log,
)
if set_name_from_profile():
return

# otherwise we try to set the name from the account alias
# or the organization - depending on the configuration
# and what permissions we have
if Config.aws.prefer_account_alias_as_name:
if not set_name_from_account_alias():
set_name_from_org()
else:
if not set_name_from_org():
set_name_from_account_alias()

# if we still don't have a name, we try
# to set it from the profile if one is set
if account.name is None and not Config.aws.scrape_org:
set_name_from_profile()

if len(accounts) == 0:
return

Expand Down Expand Up @@ -591,7 +618,7 @@ def get_accounts(core_feedback: CoreFeedback) -> List[AwsAccount]:
except botocore.exceptions.BotoCoreError as e:
core_feedback.error(f"Unable to get accounts for profile {profile}: {e}", log)

set_account_names(accounts)
set_account_names(accounts, core_feedback)
return accounts


Expand Down
4 changes: 4 additions & 0 deletions plugins/aws/resoto_plugin_aws/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class AwsConfig:
" the role will be assumed when calling organizations:DescribeAccount."
},
)
prefer_profile_as_account_name: bool = field(
default=False,
metadata={"description": "Prefer the profile name as the account name, if a profile was used."},
)
fork_process: bool = field(
default=True,
metadata={
Expand Down

0 comments on commit d62a23e

Please sign in to comment.