-
Notifications
You must be signed in to change notification settings - Fork 79
makes credentials expiration UTC/localtime aware #42
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
Conversation
according to https://docs.pytest.org/en/6.2.x/customize.html#setup-cfg it should be a path to where tests are. without it running `pytest test/unit` fails
2f1df39
to
f4870b2
Compare
Thank you @ivica-k ! We will run this PR through our internal test suite and follow up with any feedback next week :) |
I'm not entirely satisfied with the tests I wrote. |
utc_offset = time.localtime().tm_gmtoff // 60 // 60 | ||
now = datetime.datetime.now().astimezone() | ||
|
||
_logger.debug( | ||
"Credentials will expire at {} (UTC), local time is {}.\nDifference between local time and UTC time is {} hours.\n".format( | ||
self.expiration, now, utc_offset | ||
) | ||
) | ||
|
||
return (now - datetime.timedelta(hours=utc_offset)) > self.expiration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the datetime docs, here I think we could use datetime.now(timezone.utc)
instead of manually applying the utc offset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A much better solution! Implemented in 922d7b3
test/unit/test_credentials_holder.py
Outdated
"AccessKeyId": "something", | ||
"SecretAccessKey": "secret", | ||
"SessionToken": "fornow", | ||
"Expiration": expiration.astimezone(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Expiration we receive from boto3 is a datetime with UTC timezone. I think we could replace datetime.datetime.now()
with datetime.now(timezone.utc)
to achieve that. This would allow us to directly use expiration
when creating the credentials
dict
test/unit/test_credentials_holder.py
Outdated
@pytest.mark.parametrize( | ||
"expiration", | ||
[ | ||
datetime.datetime.now(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use add a minute or so onto this test case (i.e. datetime.datetime.now() + datetime.timedelta(minutes=1)
)? It's expired by the time the is_expired()
check occurs on my machine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I understand your point. Addressed in 922d7b3
f4870b2
to
922d7b3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM -- thank you!
Woot! That was super quick, thank you! |
Connecting to a Redshift cluster with Okta as the IdP was causing the
login_url
to be opened twice. The original issue is the fact that STS credentials expiration datetime was set in UTC and checked against local time (which is UTC+2 in my case). Local time was always an hour ahead (UTC+2 - STS session length of 1 hour) in my case and credentials were consideredexpired
as soon as they were created.Additionally fixes test collection by changing the
setup.cfg
- this could potentially be a different PR.Logs look like this
Description
Motivation and Context
Local time was always an hour ahead (UTC+2 - STS session length of 1 hour) in my case and credentials were considered
expired
as soon as they were created. Fixes #40Testing
Executed the following script in the CEST (
Europe/Amsterdam
) to verify thatlogin_url
was opened only onceIt would be great to have unit tests for this change but I am unsure how to
Screenshots (if appropriate)
Types of changes
Checklist
./build.sh
succeedspytest test/unit
and they are passing.