Skip to content
This repository has been archived by the owner on Oct 15, 2019. It is now read-only.

refactor(auth): update auth classes #34

Merged
merged 3 commits into from
Jul 1, 2019

Conversation

gunjanswitchco
Copy link
Contributor

Updates to auth module based on gcloud-aio using requests.Session

@gunjanswitchco gunjanswitchco requested a review from a team as a code owner June 24, 2019 20:30
@ghost ghost requested a review from eddiedialpad June 24, 2019 20:30
.pre-commit-config.yaml Show resolved Hide resolved
with open(service, 'r') as f:
data = json.loads(f.read())
return data
except FileNotFoundError:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileNotFoundError isn't defined in Py2 which raises IOError on a file that wasn't found:

>>> open('foobar', 'r')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'foobar'

FileNotFoundError inherits from OSError, so I think you'll have to check for both IOError and OSError to make this portable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I realized that too after the lint/hook failures. Have a change in place to fix this in the next update to this PR.

SCOPES = ['https://www.googleapis.com/auth/iam']


class IamClient:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inherit from object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, a lint error caught this too. Thought I had fixed it, will be fixed in the next update to this PR.

class IamClient:
def __init__(self,
service_file=None, # type: Optional[str]
session=None, # type: requests.Session
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Optional to all of these

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so my understanding was that there won't be a need to add Optional to these based on gcloud-aio's Type annotations - https://github.com/talkiq/gcloud-aio/blob/master/auth/gcloud/aio/auth/token.py#L65.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these are incorrect in gcloud-aio as well; these would only be accepted if we have the --no-strict-optional flag set in mypy, which we should avoid since it tends to help mask missing null check issues. We should similarly make this change in -aio, since it'd be great to be more accurate about these things.

return self.token.service_data.get('client_email')

# https://cloud.google.com/iam/reference/rest/v1/projects.serviceAccounts.keys/get
# pylint: disable=too-many-arguments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just throw this into our .pre-commit-config.yaml

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do

# to throw this error at load time rather than lazily during normal operations,
# where plumbing this error through will require several changes to otherwise-
# good error handling.
import cryptography # pylint: disable=unused-import
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this (and the comment explaining why we're keeping it!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh! good point! will do!

gcloud/rest/auth/token.py Show resolved Hide resolved
return jwt.encode(payload, credentials['private_key'],
algorithm='RS256')

def acquire(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly, lets keep around the old public API aliased to the new version along with DeprecationWarnings, something like:

def acquire(self):
    warnings.warn('Token.acquire() is deprecated, please use Token.get()', DeprecationWarning)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, will do. Should acquire() then also call self.get() in the deprecation period?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, we want to match behavior as much as possible. Looks like acquire() is closest approximated by acquire_access_token(), ensure() is roughly equivalent to ensure_token(), etc. Basically, we want to make sure we have a few more releases where users aren't forced to change their API so they have time to properly migrate before we do a major version bump with breaking changes.

@@ -1,3 +1,4 @@
# pylint: disable=duplicate-code
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, I thought this was disabled globally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got flagged for this when trying to commit. Will take a look once I commit again

return self.session.post(self.token_uri, data=payload,
headers=REFRESH_HEADERS, timeout=timeout)

@backoff.on_exception(backoff.expo, Exception, max_tries=1) # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this to 1 instead of 5 tries? Not much point of having a backoff handler unless it has at least one retry

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, this is my bad. When running the integration tests, I was getting a '429 Client Error: Too Many Requests for url' error, which I pinged you about. So this was just me trying to see if the max_tries was causing this. Will revert this back to 5

gcloud/rest/auth/token.py Show resolved Hide resolved
Copy link
Member

@TheKevJames TheKevJames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified tests pass locally!

@TheKevJames TheKevJames merged commit 6feecd0 into talkiq:master Jul 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
4 participants