Skip to content

Commit

Permalink
fixup for bad profile
Browse files Browse the repository at this point in the history
  • Loading branch information
pcmxgti committed Oct 18, 2022
1 parent d85cca1 commit fab0f96
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 8 deletions.
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ def pytest_addoption(parser):
default="/dev/null",
help="Sets an optional config file to read from",
)
parser.addoption("--aws-profile", default="pytest", help="Sets the AWS profile name")
7 changes: 5 additions & 2 deletions tests/functional_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def custom_args(request):
"--okta-mfa-response",
"--aws-role-arn",
"--config-file",
"--aws-profile",
]
arg_list = []
# pytest does not have a method for listing options, so we have look them up.
Expand Down Expand Up @@ -211,6 +212,8 @@ def test_generate_credentials(custom_args):
args = [
"--aws-role-arn",
f"{config.aws['role_arn']}",
"--aws-profile",
f"{config.aws['profile']}",
"--okta-app-url",
f"{config.okta['app_url']}",
"--okta-mfa-method",
Expand Down Expand Up @@ -247,8 +250,8 @@ def test_aws_credentials(custom_args):

if not config.aws["role_arn"]:
pytest.skip("No AWS profile defined, test will be skipped.")
profile = config.aws["role_arn"].split("/")[-1]
runnable = ["aws", "--profile", profile, "sts", "get-caller-identity"]

runnable = ["aws", "--profile", config.aws["profile"], "sts", "get-caller-identity"]
proc = run_process(runnable)
assert not proc["stderr"]
assert proc["exit_status"] == 0
21 changes: 21 additions & 0 deletions tests/unit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,27 @@ def test_process_interactive_input(mocker):
assert user.process_interactive_input({"pytest": "pytest"}) == error


@pytest.mark.parametrize(
"value,submit,expected",
[
("pytest", None, "pytest"),
("pytest", "deadbeef", "pytest"),
("pytest", 0xDEADBEEF, "pytest"),
(None, None, "default"),
(None, "", "default"),
(None, 0xDEADBEEF, str(0xDEADBEEF)),
],
)
def test_set_role_name(value, submit, expected):
"""Test setting the AWS Role (profile) name."""
from tokendito import user, Config

pytest_config = Config(aws=dict(profile=value))

ret = user.set_role_name(pytest_config, submit)
assert ret.aws["profile"] == expected


@pytest.mark.parametrize(
"config,expected",
[
Expand Down
8 changes: 4 additions & 4 deletions tokendito/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def assert_credentials(role_response={}):
aws_access_key = role_response["Credentials"]["AccessKeyId"]
aws_secret_key = role_response["Credentials"]["SecretAccessKey"]
aws_session_token = role_response["Credentials"]["SessionToken"]
except KeyError:
except (KeyError, TypeError):
logger.error("SAML Response did not contain credentials")
sys.exit(1)

Expand All @@ -194,8 +194,8 @@ def assert_credentials(role_response={}):
def select_assumeable_role(apps):
"""Select the role to perform the AssumeRoleWithSaml.
# :param apps: apps metadata, list of tuples
# :return: AWS AssumeRoleWithSaml response, role name, tuple
:param apps: apps metadata, list of tuples
:return: tuple with AWS AssumeRoleWithSaml response and role name
"""
authenticated_aps = {}
for url, saml_response, saml, label in apps:
Expand All @@ -217,4 +217,4 @@ def select_assumeable_role(apps):
authenticated_aps[_id]["saml"],
)

return assume_role_response, role_name
return (assume_role_response, role_name)
6 changes: 4 additions & 2 deletions tokendito/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ def cli(args):
)
sys.exit(1)

user.set_role_name(config, role_name)

user.set_local_credentials(
response=role_response,
role=role_name,
role=config.aws["profile"],
region=config.aws["region"],
output=config.aws["output"],
)

user.display_selected_role(profile_name=role_name, role_response=role_response)
user.display_selected_role(profile_name=config.aws["profile"], role_response=role_response)
15 changes: 15 additions & 0 deletions tokendito/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,21 @@ def get_password():
return res


def set_role_name(config_obj, name):
"""Set AWS Role alias name based on user preferences.
:param config: Config object.
:param name: Role name. Defaults to the string "default"
:return: Config object.
"""
if name is None or name == "":
name = "default"
if config_obj.aws["profile"] is None:
config_obj.aws["profile"] = str(name)

return config_obj


def update_configuration(ini_file, profile):
"""Update configuration file on local system.
Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ envlist = lint, py{36,37,38,39,310}, auth, coverage
[testenv]
deps = -r requirements-dev.txt
commands =
coverage erase
pytest --cov=tokendito --cov-append -v -ra -k 'unit' -s tests/ --
pytest --cov=tokendito --cov-append -v -ra -k 'functional and not credentials' -s tests/ --

Expand Down

0 comments on commit fab0f96

Please sign in to comment.