diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 00000000..f54dd66a --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# Reformat with Black +8cccf72646d604a342991af64762b0026c673ec2 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4bb4d937..aaf9df9e 100755 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,5 @@ pyOpenSSL>=18.0.0 cryptography==3.3.2; python_version == '2.7' beautifulsoup4>=4.6.0 lxml>=4.3.0 +pytz==2021.1 +tzlocal==2.1 \ No newline at end of file diff --git a/tests/unit_test.py b/tests/unit_test.py index 842b8172..a10276c1 100644 --- a/tests/unit_test.py +++ b/tests/unit_test.py @@ -25,6 +25,7 @@ super, zip, ) +from datetime import datetime from os import path import sys @@ -244,6 +245,19 @@ def test_collect_integer(monkeypatch, value, expected): assert helpers.collect_integer(10) == expected +def test_utc_to_local(): + """Check if passed utc datestamp becomes local one.""" + import pytz + from tokendito import helpers + from tzlocal import get_localzone + + utc = datetime.now(pytz.utc) + local_time = utc.replace(tzinfo=pytz.utc).astimezone(tz=get_localzone()) + local_time = local_time.strftime("%Y-%m-%d %H:%M:%S %Z") + + assert helpers.utc_to_local(utc) == local_time + + def test_prepare_payload(): """Check if values passed return in a dictionary.""" from tokendito import helpers diff --git a/tokendito/__version__.py b/tokendito/__version__.py index e0398afd..22ad4b18 100644 --- a/tokendito/__version__.py +++ b/tokendito/__version__.py @@ -1,7 +1,7 @@ # vim: set filetype=python ts=4 sw=4 # -*- coding: utf-8 -*- """tokendito version.""" -__version__ = "1.1.0" +__version__ = "1.1.1" __title__ = "tokendito" __description__ = "Get AWS STS tokens from Okta SSO" __long_description_content_type__ = "text/x-rst" diff --git a/tokendito/helpers.py b/tokendito/helpers.py index 3af731d3..5587479b 100644 --- a/tokendito/helpers.py +++ b/tokendito/helpers.py @@ -41,10 +41,13 @@ from bs4 import __version__ as __bs4_version__ from bs4 import BeautifulSoup from future import standard_library +import pytz import requests from requests import __version__ as __requests_version__ from tokendito import settings from tokendito.__version__ import __version__ +from tzlocal import get_localzone + standard_library.install_aliases() @@ -147,6 +150,18 @@ def setup(args): return parsed_args +def utc_to_local(utc_dt): + """Convert UTC time into local time. + + :param:utc_str:datetime + :return:local_time:string + """ + local_time = utc_dt.replace(tzinfo=pytz.utc).astimezone(tz=get_localzone()) + local_time = local_time.strftime("%Y-%m-%d %H:%M:%S %Z") + + return local_time + + def to_unicode(bytestring): """Convert a string into a Unicode compliant object. @@ -342,19 +357,21 @@ def print_selected_role(profile_name, expiration_time): :return: """ + expiration_time_local = utc_to_local(expiration_time) msg = ( "\nGenerated profile '{}' in {}.\n" "\nUse profile to authenticate to AWS:\n\t" "aws --profile '{}' sts get-caller-identity" "\nOR\n\t" "export AWS_PROFILE='{}'\n\n" - "Credentials are valid until {}." + "Credentials are valid until {} ({})." ).format( profile_name, settings.aws_shared_credentials_file, profile_name, profile_name, expiration_time, + expiration_time_local, ) return print(msg)