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

Populate token expiry for GCE credentials #473

Merged
merged 1 commit into from
Jun 10, 2016

Conversation

bendemaree
Copy link
Contributor

Populates the token_expiry property for GCE App Assertion credentials. The token responses from the metadata service have a expires_in value in the response that can be leveraged to make things work better on the base OAuth2Credentials class, like access_token_expired.

I largely followed the implementation in the base client though less defensively; I don't see any reason why expires_in wouldn't be present though perhaps someone knows better. I did note that the existing tests actually assert this value is not set in the mocked token response so perhaps that provides evidence against my assumption.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.003%) to 97.569% when pulling c96c404 on bendemaree:gce-app-assertion-expiry into 3ca2ca7 on google:master.

@@ -27,6 +28,7 @@

from oauth2client._helpers import _from_bytes
from oauth2client import util
from oauth2client.client import _UTCNOW

This comment was marked as spam.

This comment was marked as spam.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.002%) to 97.569% when pulling bffa989 on bendemaree:gce-app-assertion-expiry into 3ca2ca7 on google:master.

@@ -73,6 +78,8 @@ def _refresh_success_helper(self, bytes_response=False):
self.assertEquals(None, credentials.access_token)
credentials.refresh(http)
self.assertEquals(access_token, credentials.access_token)
self.assertFalse(credentials.access_token_expired)
self.assertTrue(credentials.token_expiry > datetime.utcnow())

This comment was marked as spam.

This comment was marked as spam.

@theacodes
Copy link
Contributor

/cc @elibixby

@@ -135,6 +136,8 @@ def _refresh(self, http_request):
raise HttpAccessTokenRefreshError(str(e),
status=response.status)
self.access_token = token_content['access_token']
delta = datetime.timedelta(seconds=int(token_content['expires_in']))

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@elibixby
Copy link
Contributor

Hi bendemaree@ thanks for the contribution, but I'm actually working on a larger update to GCE credentials that includes this.

Still writing tests but I'll go ahead and submit a pull request so more work doesn't get duplicated =(

@dhermes
Copy link
Contributor

dhermes commented Mar 25, 2016

@elibixby smaller PRs are much easier to review FWIW.

@elibixby
Copy link
Contributor

See #476

I rewrote the wrapper function for the metadata server so that it can be used by lots of common calls that will need to be made, so I would like the first PR to contain that. However, if after that we want to rewrite the interface in a bunch of small PRs that's fine with me. Still, I think all these changes are quite related and should be reviewed together. Just my .02

(Also FYI, I AM splitting up the PR with the GCE credential updates, and the PR to add IAM blob signing, as IAM blob signing could potentially be used in other credential types)

@bendemaree
Copy link
Contributor Author

Ah, alright, thanks for the review anyway! Looking forward to the update; we're trying out some optimistic credential refresh logic since the OAuth dance takes a bit and could cause a request to hang while the token is implicitly refreshed.

Closing out; feel free to reopen if needed.

@bendemaree bendemaree closed this Mar 25, 2016
@bendemaree
Copy link
Contributor Author

@elibixby Given that #476 was closed out and there aren't followup PRs yet (I may have missed them though), would it be possible to put this back on the table for review?...

@elibixby
Copy link
Contributor

I'm going to split #476 into smaller PRs and submit those. It's on my plate for the next couple weeks.

I think the right thing to do here is write a metadata module that wraps the metadata server and caches requests. Which should make follow up PRs easy. If you want to take that on you are welcome to. I can't promise I will get to it this week.

@bendemaree
Copy link
Contributor Author

@elibixby @jonparrott Sorry to bring this back to your attention again, but is there any chance we can merge this in? I don't think it's more than a minor bugfix, and at this point it's been 2 months without the major refactor needed to get this behavioral fix. 😢 I don't have the familiarity or bandwidth to re-wrap the metadata server myself, unfortunately.

@bendemaree bendemaree reopened this Jun 3, 2016
@theacodes
Copy link
Contributor

@dhermes are you fine with this being merged?

@elibixby
Copy link
Contributor

elibixby commented Jun 6, 2016

@bendemaree Sorry about taking so long. I will have some time in the next week to work on my PRs, but if you'd like to go ahead and submit this for merge. That's understandable.

@dhermes
Copy link
Contributor

dhermes commented Jun 7, 2016

https://coveralls.io/builds/5540724

What happened to 100% coverage?

@dhermes
Copy link
Contributor

dhermes commented Jun 7, 2016

Ahhh I know. This needs to be rebased against HEAD in master

@googlebot
Copy link
Collaborator

We found a Contributor License Agreement for you (the sender of this pull request) and all commit authors, but as best as we can tell these commits were authored by someone else. If that's the case, please add them to this pull request and have them confirm that they're okay with these commits being contributed to Google. If we're mistaken and you did author these commits, just reply here to confirm.

@googlebot googlebot added cla: no and removed cla: yes labels Jun 7, 2016
Populates the token_expiry property for GCE App Assertion credentials
(thus enabling access_token_expired). This corrects assumptions like the
one in the access_token_expired property on GCE specifically: it's stated
there "If the token_expiry isn't set, we assume the token doesn't expire"
which seems to be incorrect for tokens retrieved from the GCE Metadata service.

Remove usage of _UTCNOW
@bendemaree bendemaree force-pushed the gce-app-assertion-expiry branch from 54613ba to b124d19 Compare June 7, 2016 12:06
@googlebot
Copy link
Collaborator

CLAs look good, thanks!

@googlebot googlebot added cla: yes and removed cla: no labels Jun 7, 2016
@bendemaree
Copy link
Contributor Author

@dhermes Squashed and rebased (eventually!).

@elibixby @jonparrott Thanks a lot for circling back on this; much obliged!

@@ -135,6 +136,8 @@ def _refresh(self, http_request):
raise HttpAccessTokenRefreshError(str(e),
status=response.status)
self.access_token = token_content['access_token']
delta = datetime.timedelta(seconds=int(token_content['expires_in']))
self.token_expiry = delta + datetime.datetime.utcnow()

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@bendemaree
Copy link
Contributor Author

Bump. 😁

@theacodes
Copy link
Contributor

This LGTM.

@theacodes
Copy link
Contributor

@dhermes if you have any post-merge objections, let me know and I'll fix myself.

@theacodes theacodes merged commit 54d7dce into googleapis:master Jun 10, 2016
@bendemaree
Copy link
Contributor Author

@jonparrott Is there an upcoming release planned that will include this?

@theacodes
Copy link
Contributor

Yes, it will be 3.0.0. I'm waiting for a few outstanding PRs to be resolved first.

@theacodes theacodes mentioned this pull request Jul 28, 2016
@bendemaree bendemaree deleted the gce-app-assertion-expiry branch September 7, 2016 04:17
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants