Skip to content
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

fix: iam/cp4d token refresh logic #73

Merged
merged 1 commit into from
Sep 14, 2020
Merged

Conversation

jorge-ibm
Copy link
Contributor

@jorge-ibm jorge-ibm commented Sep 10, 2020

Fixes a very unique scenario described in https://github.ibm.com/arf/planning-sdk-squad/issues/2149. In the scenario where a client has been idle passed the refresh time, but before expiration time, the previous logic would result in multiple goroutines being kicked off attempting to request a new token until a new token was fetched or until the refresh time caught up to the current time. I've updated the new refreshTime calculation to be 1 minute ahead of the current time vs 1 minute ahead of the previous refreshTime value.

Java also probably has this same issue so I will update that core as well.

ref: https://github.ibm.com/arf/planning-sdk-squad/issues/2149

assert.Equal(t, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImhlbGxvIiwicm9sZSI6InVzZXIiLCJwZXJtaXNzaW9ucyI6WyJhZG1pbmlzdHJhdG9yIiwiZGVwbG95bWVudF9hZG1pbiJdLCJzdWIiOiJoZWxsbyIsImlzcyI6IkpvaG4iLCJhdWQiOiJEU1giLCJ1aWQiOiI5OTkiLCJpYXQiOjE1NjAyNzcwNTEsImV4cCI6MTU2MDI4MTgxOSwianRpIjoiMDRkMjBiMjUtZWUyZC00MDBmLTg2MjMtOGNkODA3MGI1NDY4In0.cIodB4I6CCcX8vfIImz7Cytux3GpWyObt9Gkur5g1QI",
token)
assert.NotNil(t, authenticator.tokenData)
assert.Equal(t, newRefreshTime, authenticator.tokenData.RefreshTime)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This line is the main case I wanted to test. Before, calling getToken would result in the next thread realizing it still needs to refresh since the previous goroutine may have not finished requesting a new token, which would mean the refresh time would be updated, and another goroutine would kick off. If the refresh time isn't updated, then we know it didn't attempt to request a new token.

Copy link
Member

@padamstx padamstx left a comment

Choose a reason for hiding this comment

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

LGTM; I left a couple minor comments for changes to the test cases.

v4/core/cp4d_authenticator_test.go Outdated Show resolved Hide resolved
token)
assert.NotNil(t, authenticator.tokenData)
// RefreshTime should have advanced by 1 minute from the current time
newRefreshTime := GetCurrentTime() + 60
Copy link
Member

Choose a reason for hiding this comment

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

I guess this validation step should occur less than a second after getToken() updates the refresh time to make sure that that assert.Equal() succeeds :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah the use of GetCurrentTime in the test is a bit worrisome but it seems to work..

v4/core/iam_authenticator_test.go Outdated Show resolved Hide resolved
@jorge-ibm jorge-ibm force-pushed the fix/token-refresh-logic branch from 81ca287 to abe8410 Compare September 10, 2020 22:02
@jorge-ibm jorge-ibm merged commit 8d4685e into master Sep 14, 2020
ibm-devx-automation pushed a commit that referenced this pull request Sep 14, 2020
## [4.4.1](v4.4.0...v4.4.1) (2020-09-14)

### Bug Fixes

* iam/cp4d token refresh logic ([#73](#73)) ([8d4685e](8d4685e))
@ibm-devx-automation
Copy link

🎉 This PR is included in version 4.4.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

@padamstx padamstx deleted the fix/token-refresh-logic branch October 14, 2020 22:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants