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

Rewrite and test Github auth logic, separating standard and search quota #1205

Merged
merged 94 commits into from
Jan 11, 2019
Merged

Rewrite and test Github auth logic, separating standard and search quota #1205

merged 94 commits into from
Jan 11, 2019

Conversation

paulmelnikow
Copy link
Member

@paulmelnikow paulmelnikow commented Oct 25, 2017

Summary:

  • Create new priority queue-based token cycling logic which correctly handles simultaneous requests using the same token
  • Correctly support search API rate limits, which are separate from core API rate limits (Github search has custom rate limits #1119)
  • When tokens are exhausted, return an error instead of attempting fallback to our API key
  • Use a static token in development, when one is configured (replacing When a global gh_token is configured, always use it #1118)
  • Split up Github auth logic into separate modules which can be tested in isolation

Todo:

  • Test persistence
  • Integration test?
  • Incorporate recent changes to lib/github-auth.js
  • Catch persistence errors and send them to sentry
  • Rework TokenProvider / TokenPool abstraction which may not be so helpful. Github has two pools so things feel a little bit off.
  • Update token persistence for these changes to the api provider
  • Wait for merge of TokenPool: Add debug logging helper + return correct error #2400

To handle in follow on work:

  • Log / emit to sentry when tokens are exhausted
  • (maybe) Consolidate reserve logic, probably in Token

@paulmelnikow paulmelnikow added bug Bugs in badges and the frontend core Server, BaseService, GitHub auth, Shared helpers labels Oct 25, 2017
@paulmelnikow paulmelnikow changed the title Rewrite and test Github auth logic [WIP] Rewrite and test Github auth logic Nov 2, 2017
# Conflicts:
#	lib/github-auth.js
# Conflicts:
#	lib/github-auth.js
#	package-lock.json
#	package.json
#	server.js
@paulmelnikow
Copy link
Member Author

I'm eager to get this shipped, though I'm hesitant to do it until I get access to logs…

}));
}

function constEq(a, b) {
Copy link

Choose a reason for hiding this comment

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

I'm new to node in general but why not use crypto.timingSafeEqual(a, b) instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't write this, just moved it. Yes, happy to switch that over. It seems like that solves the same problem.

paulmelnikow added a commit that referenced this pull request Jul 12, 2018
This was designed as part of a rewrite of the Github auth code in #1205 which is stalled because I don't want to deploy it without access to server logs. The need for token rotation came up recently in #541, so I picked up this code, removed the github-specific bits, and pulled it in. Ordinarily I wouldn't merge helper code without a use, though sadly it seems like the best way to move forward this rewrite.
@paulmelnikow
Copy link
Member Author

Since we have access to errors through Sentry, I think I will revive this and see if we can't nudge it forward.

@paulmelnikow paulmelnikow removed the on-hold Deferred in favor of another approach, blocked on preceding efforts, stale, or abandoned label Jan 4, 2019
Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

I'm a little over half way through, but figured I'd go ahead and share my current thoughts/questions

.circleci/config.yml Outdated Show resolved Hide resolved
.circleci/config.yml Outdated Show resolved Hide resolved
@@ -23,21 +22,28 @@ class FsTokenPersistence extends TokenPersistence {
}

const tokens = JSON.parse(contents)
tokens.forEach(tokenString => {
githubAuth.addGithubToken(tokenString)
})
Copy link
Member

Choose a reason for hiding this comment

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

My first time looking at this part of the code, but it looks like this change here decouples this persistence engine from github specifics, which creates the possibility of this being reusable for other services?

I've no idea if there's a similar need in other services, but 👍 either way!

lib/fs-token-persistence.spec.js Show resolved Hide resolved
expected.push(newToken)

await persistence.initialize()
githubAuth.addGithubToken(newToken)
await persistence.noteTokenAdded(newToken)
Copy link
Member

Choose a reason for hiding this comment

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

i know that the function name noteTokenAdded here wasn't something modified in this PR, but it felt a little strange to me. seeing this for the first time I wasn't clear what exactly the function does from the name

Copy link
Member Author

Choose a reason for hiding this comment

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

Should we change it to add()?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I'd vote for add() or addToken() (and definitely add() if we can/will add things other than tokens)

services/github/auth/acceptor.spec.js Show resolved Hide resolved
services/github/auth/acceptor.spec.js Outdated Show resolved Hide resolved
@calebcartwright
Copy link
Member

I've made it through my first pass and added some inline comments. Although beware, there are more questions for my own understanding than there is useful feedback for you 😁

@paulmelnikow
Copy link
Member Author

Although beware, there are more questions for my own understanding than

Avoiding knowledge bottlenecks is fully half of why we do this :)

@calebcartwright
Copy link
Member

I think this is good to go, will do another pass here in a bit and approve

body: { shieldsSecret: fakeShieldsSecret, token: fakeAccessToken },
})

expect(onTokenAccepted).to.have.been.calledWith(fakeAccessToken)
Copy link
Member

Choose a reason for hiding this comment

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

👍

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

This was a herculean effort! ✨ 🎉 👏

It looks good to me as is, only pending comment was the potential rename of noteTokenAdded function to add, but that's not a blocker

@paulmelnikow
Copy link
Member Author

Thanks so much! Will be glad to close this out… and see if it really works. Let’s one of us tackle the renames in a follow-on.

@paulmelnikow
Copy link
Member Author

I'll merge and deploy this in the afternoon when we get past peak usage time, in case anything breaks.

@paulmelnikow paulmelnikow merged commit c4efdc8 into badges:master Jan 11, 2019
@shields-deployment
Copy link

This pull request was merged to master branch. This change is now waiting for deployment, which will usually happen within a few days. Stay tuned by joining our #ops channel on Discord!

After deployment, changes are copied to gh-pages branch:

@paulmelnikow paulmelnikow deleted the github-token-pool-2 branch January 11, 2019 02:30
paulmelnikow added a commit that referenced this pull request Jan 22, 2019
The TokenProvider abstraction was refactored away during #1205 and is now obsolete. Other users of token pooling should use TokenPool and TokenPersistence directly.
paulmelnikow added a commit that referenced this pull request Jan 23, 2019
The TokenProvider abstraction was refactored away during #1205 and is now obsolete. Other users of token pooling should use TokenPool and TokenPersistence directly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocker PRs and epics which block other work bug Bugs in badges and the frontend core Server, BaseService, GitHub auth, Shared helpers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants