trek10inc / awsume

A utility for easily assuming AWS IAM roles from the command line.
https://awsu.me
MIT License
494 stars 90 forks source link

ValueError: astimezone() cannot be applied to a naive datetime #62

Closed kevitan closed 5 years ago

kevitan commented 5 years ago

Freshly installed awsume on MacOS Mojave.

Python version: 3.5.7 AWSume version: 4.0.4 (incidentally, I don't see this release on under the Releases)

I get an error when I try to assume a role - It seems to succeed, but in the info message that prints out the session expiration, the script errors out:

$ awsume my-profile
Enter MFA token: xxxxxxxxx
Traceback (most recent call last):
  File "/usr/local/opt/pyenv/versions/3.5.7/bin/awsumepy", line 12, in <module>
    sys.exit(main())
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/main.py", line 36, in main
    run_awsume(sys.argv)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/main.py", line 25, in run_awsume
    awsume.run(argument_list)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/app.py", line 231, in run
    credentials = self.get_credentials(args, profiles)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/app.py", line 187, in get_credentials
    credentials = self.plugin_manager.hook.get_credentials(config=self.config, arguments=args, profiles=profiles)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/pluggy/hooks.py", line 289, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/pluggy/manager.py", line 87, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/pluggy/manager.py", line 81, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/default_plugins.py", line 389, in get_credentials
    ignore_cache=arguments.force_refresh,
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/lib/aws.py", line 90, in get_session_token
    safe_print('Session token will expire at {}'.format(parse_time(user_session['Expiration'])), colorama.Fore.GREEN)
  File "/usr/local/opt/pyenv/versions/3.5.7/lib/python3.5/site-packages/awsume/awsumepy/lib/aws.py", line 15, in parse_time
    return date_time.astimezone(dateutil.tz.tzlocal()).strftime('%Y-%m-%d %H:%M:%S')
ValueError: astimezone() cannot be applied to a naive datetime

I will note that if I remove the offending call to astimezone() from parse_time():

def parse_time(date_time: datetime):
  return date_time.strftime('%Y-%m-%d %H:%M:%S')

it works as expected. But we do note that there are several places in the code that already call this, and when I try to remove those calls but leave it in parse_time(), the error remains.

I'm not super familiar with the dateutil and datetime libraries, but it seems that because the astimezone() is already called in the client code, there is not a need to call it again inparse_time().

mbarneyjr commented 5 years ago

I have successfully replicated the issue. We'll release a patch to fix this, but in the mean time I can confirm it works in later versions of Python. Thanks for the bug report!

mbarneyjr commented 5 years ago

This has been resolved in 4.0.5 If you're still having problems even after updating, feel free to reopen this issue or open a new one

Thanks!