Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more updates from feedback on PR 48
Browse files Browse the repository at this point in the history
see #48
oshaughnessy committed Jan 14, 2022
1 parent 7392b32 commit 386a33d
Showing 3 changed files with 18 additions and 89 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -5,3 +5,4 @@
* [Eoin Shanaghy](https://github.com/eoinsha)
* [Geordam](https://github.com/Geordam)
* [Imran](https://github.com/fai555)
* [O'Shaughnessy Evans](https://github.com/oshaughnessy)
34 changes: 17 additions & 17 deletions cli/src/aws_sso_util/populate_profiles.py
Original file line number Diff line number Diff line change
@@ -149,20 +149,22 @@ def trim_formatter(i, n, **kwargs):
return formatter(i, n, **kwargs)
return trim_formatter

def get_field_name_case_formatter(field, transform, formatter):
def get_name_case_formatter(account_name_transform, role_name_transform, formatter):
field_transform_map = {
"account_name": account_name_transform,
"role_name": role_name_transform,
}
def case_formatter(i, n, **kwargs):
LOGGER.debug("evaluating case transform(%s)", transform)
LOGGER.debug("* in = %s", kwargs[field])
if transform == 'capitalize':
transformation = lambda s: s.capitalize()
elif transform == 'casefold':
transformation = lambda s: s.casefold()
elif transform == 'lower':
transformation = lambda s: s.lower()
elif transform == 'upper':
transformation = lambda s: s.upper()
kwargs[field] = (transformation)(kwargs[field])
LOGGER.debug("* out = %s", kwargs[field])
for field, transform in field_transform_map:
if transform == "capitalize":
transformation = lambda s: s.capitalize()
elif transform == "casefold":
transformation = lambda s: s.casefold()
elif transform == "lower":
transformation = lambda s: s.lower()
elif transform == "upper":
transformation = lambda s: s.upper()
kwargs[field] = (transformation)(kwargs[field])
return formatter(i, n, **kwargs)
return case_formatter

@@ -265,10 +267,8 @@ def populate_profiles(
profile_name_formatter = get_formatter(profile_name_include_region, region_format, no_region_format)
if profile_name_trim_account_name_patterns or profile_name_trim_role_name_patterns:
profile_name_formatter = get_trim_formatter(profile_name_trim_account_name_patterns, profile_name_trim_role_name_patterns, profile_name_formatter)
if profile_name_account_name_case_transform:
profile_name_formatter = get_field_name_case_formatter("account_name", profile_name_account_name_case_transform, profile_name_formatter)
if profile_name_role_name_case_transform:
profile_name_formatter = get_field_name_case_formatter("role_name", profile_name_role_name_case_transform, profile_name_formatter)
if profile_name_account_name_case_transform or profile_name_role_name_case_transform or:
profile_name_formatter = get_name_case_formatter(profile_name_account_name_case_transform, profile_name_role_name_case_transform, profile_name_formatter)

try:
profile_name_formatter(0, 1, account_name="foo", account_id="bar", role_name="baz", region="us-east-1")
72 changes: 0 additions & 72 deletions docs/configure.md
Original file line number Diff line number Diff line change
@@ -157,74 +157,6 @@ aws s3 ls --profile my-sso-profile
`aws-sso-util configure populate` allows you to configure profiles for all the access you have through AWS SSO.
You specify one or more regions, and a profile is created for every account, role, and region you have access to through AWS SSO (note that if access to a region is prohibited by an IAM policy, this does not suppress creation of the profile).

## Options

Usage: aws-sso-util configure populate [OPTIONS]

Configure profiles for all accounts and roles.

Writes a profile to your AWS config file (~/.aws/config) for every account
and role you have access to, for the regions you specify.

Options:
-u, --sso-start-url URL Your AWS SSO start URL
--sso-region TEXT The AWS region your AWS SSO instance is
deployed in

-r, --region REGION AWS region for the profiles, can provide
multiple times

--dry-run Print the config to stdout instead of
writing to your config file

-c, --config-default KEY=VALUE Additional config field to set, can provide
multiple times

--existing-config-action [keep|overwrite|discard]
Action when config defaults conflict with
existing settings

--components VALUE,VALUE,... Profile name components to join (comma-
separated). Possible values are:
account_name account_id account_number
role_name region short_region

--separator, --sep SEP Separator for profile name components,
default is '.'

--include-region [default|always]
By default, the first region is left off the
profile name

--region-style [short|long] Default is five character region
abbreviations

--trim-account-name TEXT Regex to remove from account names, can
provide multiple times

--trim-role-name TEXT Regex to remove from role names, can provide
multiple times

--account-name-case [capitalize|casefold|lower|upper]
Method to change the case of the account
name

--role-name-case [capitalize|casefold|lower|upper]
Method to change the case of the role name
--profile-name-process TEXT
--safe-account-names / --raw-account-names
In profiles, replace any character sequences
in account names not in A-Za-z0-9-._ with a
single -

--credential-process / --no-credential-process
Force enable/disable setting the credential
process SDK helper

--force-refresh Re-login
-v, --verbose
--help Show this message and exit.

You can provide regions through the `--region`/`-r` flag (multiple regions like `-r REGION1 -r REGION2`), or by setting the `AWS_CONFIGURE_DEFAULT_REGION` environment variable (this is ignored if any regions are specified on the command line).

You can view the profiles without writing them using the `--dry-run` flag.
@@ -291,10 +223,6 @@ For example, to lowercase both the account name and role name, add these options

--account-name-case lower --role-name-case lower

If you want to see what it does, try a dry run:

aws-sso-util configure populate --region $AWS_REGION --dry-run --account-name-case lower --role-name-case lower

### Profile name process

Finally, if you want total control over the generated profile names, you can provide a shell command with `--profile-name-process` and it will be executed with the following positional arguments:

0 comments on commit 386a33d

Please sign in to comment.