Skip to content

Commit

Permalink
Add --print-url. Fixes mozilla-iam#205.
Browse files Browse the repository at this point in the history
  • Loading branch information
april committed Apr 20, 2020
1 parent 5a2427b commit d6159e9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 7 additions & 2 deletions mozilla_aws_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def validate_cache(ctx, param, cache):
help="How to output the AWS API keys",
callback=validate_output
)
@click.option("--print-url", is_flag=True, help="Print the federation URL to stdout")
@click.option("--profile",
metavar="<profile>",
help="Override profile name used with `awscli` or `shared` "
Expand All @@ -174,7 +175,7 @@ def validate_cache(ctx, param, cache):
callback=validate_arn)
@click.option("-v", "--verbose", is_flag=True, help="Print debugging messages")
@click.option("-w", "--web-console", is_flag=True, help="Open AWS web console")
def main(batch, config, cache, output,
def main(batch, config, cache, output, print_url,
profile, role_arn, verbose, web_console):
"""Fetch AWS API Keys using SSO web login"""
if verbose:
Expand All @@ -196,6 +197,9 @@ def main(batch, config, cache, output,
if batch and role_arn is None:
raise click.exceptions.UsageError(
"You must pass a role_arn in batch mode")
if web_console and print_url:
raise click.exceptions.UsageError(
"Cannot print URL to output and redirect to web console")

logger.debug("Config : {}".format(config))

Expand All @@ -215,7 +219,8 @@ def main(batch, config, cache, output,
token_endpoint=config["openid-configuration"]["token_endpoint"],
web_console=web_console,
issuer_domain=config.get("issuer_domain", "aws.sso.mozilla.com"),
cache=cache
cache=cache,
print_url=print_url
)

login.login()
Expand Down
15 changes: 11 additions & 4 deletions mozilla_aws_cli/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ def __init__(
token_endpoint="https://auth.mozilla.auth0.com/oauth/token",
web_console=False,
issuer_domain=None,
cache=True
cache=True,
print_url=False,
):

# We use this for tracking various bits
Expand Down Expand Up @@ -113,7 +114,9 @@ def __init__(
self.last_state_check = None
self.max_sleep_no_state_check = 2 # seconds

# Whether we should open the AWS web console or not
# Whether we should open the AWS web console or not and whether we
# should print the URL to stdout
self.print_url = print_url
self.web_console = web_console

# If we're using the AWS CLI output, what profile should we use
Expand Down Expand Up @@ -476,7 +479,7 @@ def print_output(self):
if output_map and self.print_output_map:
print(output_set_env_vars(output_map, message))

if self.web_console:
if self.web_console or self.print_url:
self.aws_federate()
else:
self.state = "finished"
Expand Down Expand Up @@ -523,13 +526,17 @@ def aws_federate(self):

logger.debug("Web browser console URL: {}".format(url))

if self.opened_tab:
if self.print_url:
print(url)
self.state = "finished"
elif self.opened_tab:
self.state = "aws_federate"
self.web_state["awsFederationUrl"] = url
else:
self.opened_tab = True
webbrowser.open_new_tab(url)
self.state = "finished"

return url


Expand Down

0 comments on commit d6159e9

Please sign in to comment.