victorskl / yawsso

Yet Another AWS SSO - sync up AWS CLI v2 SSO login session to legacy CLI v1 credentials
MIT License
302 stars 26 forks source link

SSO "expiresAt" date format mismatch #42

Closed ninadpage closed 3 years ago

ninadpage commented 3 years ago

After a recent aws-cli update, yawsso breaks with the following exception:

Traceback (most recent call last):
  File "$HOME/.local/bin/yawsso", line 8, in <module>
    sys.exit(main())
  File "$HOME/.local/pipx/venvs/yawsso/lib/python3.8/site-packages/yawsso/cli.py", line 540, in main
    credentials = update_profile(profile_name, config)
  File "$HOME/.local/pipx/venvs/yawsso/lib/python3.8/site-packages/yawsso/cli.py", line 345, in update_profile
    credentials = fetch_credentials(profile_name, profile)
  File "$HOME/.local/pipx/venvs/yawsso/lib/python3.8/site-packages/yawsso/cli.py", line 224, in fetch_credentials
    cached_login = check_sso_cached_login_expires(profile_name, profile)
  File "$HOME/.local/pipx/venvs/yawsso/lib/python3.8/site-packages/yawsso/cli.py", line 215, in check_sso_cached_login_expires
    expires_utc = parse_sso_cached_login_expiry(cached_login)
  File "$HOME/.local/pipx/venvs/yawsso/lib/python3.8/site-packages/yawsso/cli.py", line 180, in parse_sso_cached_login_expiry
    expires_utc = datetime.strptime((cached_login["expiresAt"]), datetime_format_in_sso_cached_login)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '2020-12-28T20:47:20Z' does not match format '%Y-%m-%dT%H:%M:%SUTC'
# aws --version
aws-cli/2.1.14 Python/3.9.1 Darwin/19.6.0 source/x86_64 prompt/off

# yawsso --version
yawsso 0.6.0

Relevant issue in aws-java-sdk-v2: https://github.com/aws/aws-sdk-java-v2/issues/2190

I am not sure if this is breaking with an aws-cli update since I cannot find anything relevant in its changelog, so I suspect this might be following a server-side change which updates the expiresAt format to ISO 8601.

yawsso can handle this without breaking compatibility with the old UTC format by doing this:

def parse_sso_cached_login_expiry(cached_login):
    # older versions of aws-cli might use non-standard format with `UTC` instead of `Z`
    expires_at = cached_login["expiresAt"].replace('UTC', 'Z')
    datetime_format_in_sso_cached_login = "%Y-%m-%dT%H:%M:%SZ"
    expires_utc = datetime.strptime(expires_at, datetime_format_in_sso_cached_login)
    return expires_utc
victorskl commented 3 years ago

Thanks for the patch @ninadpage I will apply the fix and release 0.6.1 in a tick.

victorskl commented 3 years ago

Fixed #41 #42. Please upgrade to pip install -U yawsso==0.6.1