Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add profile case transformations to "aws-sso-util configure populate" #48

Merged
merged 9 commits into from
Jan 18, 2022
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)
26 changes: 26 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,26 @@ 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 transform == "capitalize":
benkehoe marked this conversation as resolved.
Show resolved Hide resolved
kwargs[field] = kwargs[field].capitalize()
elif transform == "casefold":
kwargs[field] = kwargs[field].casefold()
elif transform == "lower":
kwargs[field] = kwargs[field].lower()
elif transform == "upper":
kwargs[field] = kwargs[field].upper()
else:
benkehoe marked this conversation as resolved.
Show resolved Hide resolved
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 +189,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", "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", "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 +211,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 +268,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
17 changes: 17 additions & 0 deletions docs/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ 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)
* [`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