Skip to content

Commit

Permalink
add profile case transformations to "aws-sso-util configure populate" (
Browse files Browse the repository at this point in the history
…#48)

This provides a way to modify the profile name components for a common use case without having to resort to --profile-name-process
  • Loading branch information
oshaughnessy authored Jan 18, 2022
1 parent 4c56532 commit 702d48d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
30 changes: 30 additions & 0 deletions cli/src/aws_sso_util/populate_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ def trim_formatter(i, n, **kwargs):
return formatter(i, n, **kwargs)
return trim_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):
for field, transform in field_transform_map.items():
if not transform:
continue
if transform == "capitalize":
kwargs[field] = kwargs[field].capitalize()
elif transform == "casefold":
kwargs[field] = kwargs[field].casefold()
elif transform == "lower":
kwargs[field] = kwargs[field].lower()
elif transform == "title":
kwargs[field] = kwargs[field].title()
elif transform == "upper":
kwargs[field] = kwargs[field].upper()
else:
raise ValueError("Unknown {} transform value {}".format(field, transform))
return formatter(i, n, **kwargs)
return case_formatter

def get_safe_account_name(name):
return re.sub(r"[\s\[\]]+", "-", name).strip("-")

Expand All @@ -169,6 +193,8 @@ def get_safe_account_name(name):
@click.option("--region-style", "profile_name_region_style", type=click.Choice(["short", "long"]), default="short", help="Default is five character region abbreviations")
@click.option("--trim-account-name", "profile_name_trim_account_name_patterns", multiple=True, default=[], help="Regex to remove from account names, can provide multiple times")
@click.option("--trim-role-name", "profile_name_trim_role_name_patterns", multiple=True, default=[], help="Regex to remove from role names, can provide multiple times")
@click.option("--account-name-case", "profile_name_account_name_case_transform", type=click.Choice(["capitalize", "casefold", "lower", "title", "upper"]), help="Method to change the case of the account name")
@click.option("--role-name-case", "profile_name_role_name_case_transform", type=click.Choice(["capitalize", "casefold", "lower", "title", "upper"]), help="Method to change the case of the role name")
@click.option("--profile-name-process")
@click.option("--safe-account-names/--raw-account-names", default=True, help="In profiles, replace any character sequences in account names not in A-Za-z0-9-._ with a single -")

Expand All @@ -189,6 +215,8 @@ def populate_profiles(
profile_name_region_style,
profile_name_trim_account_name_patterns,
profile_name_trim_role_name_patterns,
profile_name_account_name_case_transform,
profile_name_role_name_case_transform,
profile_name_process,
safe_account_names,
credential_process,
Expand Down Expand Up @@ -244,6 +272,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 or profile_name_role_name_case_transform:
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")
Expand Down
18 changes: 18 additions & 0 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ A useful piece of syntax for this is lookahead/lookbehind assertions.
* `(?<!My)Role` would turn `"AdminRole"` into `"Admin"` and `"UserRole"` into `"User"` but leave `"MyRole"` as is.
* `RoleFor(?=Admin)` and `RoleFor(?!My)` work similarly for suffixes.

### Change the case of account and role names

You can alter the case of your profile names with `--account-name-case` and
`--role-name-case`. Use these to change the generated profile names after
assembly and trimming.

The values for these options are as follows, and correspond to the Python string methods of the same names:

* [`capitalize`](https://docs.python.org/3/library/stdtypes.html#str.capitalize)
* [`casefold`](https://docs.python.org/3/library/stdtypes.html#str.casefold)
* [`lower`](https://docs.python.org/3/library/stdtypes.html#str.lower)
* [`title`](https://docs.python.org/3/library/stdtypes.html#str.title)
* [`upper`](https://docs.python.org/3/library/stdtypes.html#str.upper)

For example, to lowercase both the account name and role name, add these options:

--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:
Expand Down

0 comments on commit 702d48d

Please sign in to comment.