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 the ability to override the oath key name #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ $ pip install --user awscli-plugin-yubikeytotp
To enable the plugin, add this to your `~/.aws/config`:
```
[plugins]
# The next line is required if you are using CLI v2
# Use the path that `pip` installed the package to - this one is in user mode
cli_legacy_plugin_path = /home/myhomefolder/.local/lib/python3.8/site-packages/

yubikeytotp = awscli_plugin_yubikeytotp
```
Also make sure to have your MFA ARN configured for your profile:
Expand All @@ -37,6 +41,8 @@ Also make sure to have your MFA ARN configured for your profile:
role_arn = arn:aws:iam::...
mfa_serial = arn:aws:iam::...
source_profile = default
# You can also override the key name (useful if you used a "friendly" key name for when you're using the console)
mfa_alias = shinykey
```


Expand Down
12 changes: 8 additions & 4 deletions awscli_plugin_yubikeytotp/prompter.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ def _unix_console_print(s):


class YubikeyTotpPrompter(object):
def __init__(self, mfa_serial, original_prompter=None):
def __init__(self, mfa_serial, mfa_alias, original_prompter=None):
self.mfa_serial = mfa_serial
self.mfa_alias = mfa_alias
self._original_prompter = original_prompter

def __call__(self, prompt):
Expand All @@ -36,13 +37,13 @@ def __call__(self, prompt):
["ykman", "oath", "list"], capture_output=True, check=True
)
available_keys = available_keys_result.stdout.decode("utf-8").split()
available_keys.index(self.mfa_serial)
available_keys.index(self.mfa_alias)

console_print(
"Generating OATH code on YubiKey. You may have to touch your YubiKey to proceed..."
)
ykman_result = subprocess.run(
["ykman", "oath", "code", "-s", self.mfa_serial], capture_output=True
["ykman", "oath", "code", "-s", self.mfa_alias], capture_output=True
)
console_print("Successfully created OATH code.")
token = ykman_result.stdout.decode("utf-8").strip()
Expand All @@ -69,12 +70,15 @@ def inject_yubikey_totp_prompter(session, **kwargs):

config = session.get_scoped_config()
mfa_serial = config.get("mfa_serial")
mfa_alias = config.get("mfa_alias")
if mfa_serial is None:
# no MFA, so don't interfere with regular flow
return

if mfa_alias is None:
mfa_alias = mfa_serial
assume_role_provider = providers.get_provider("assume-role")
original_prompter = assume_role_provider._prompter
assume_role_provider._prompter = YubikeyTotpPrompter(
mfa_serial, original_prompter=original_prompter
mfa_serial, mfa_alias, original_prompter=original_prompter
)