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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
202b508
Rewrite and test Github auth logic
paulmelnikow Oct 25, 2017
a105e98
Clean lint
paulmelnikow Oct 25, 2017
054314a
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 1, 2017
e045dbf
Clean diff
paulmelnikow Nov 1, 2017
8617f46
Rm unused
paulmelnikow Nov 1, 2017
815ec67
Adjust limits
paulmelnikow Nov 1, 2017
224266d
Readability improvements
paulmelnikow Nov 1, 2017
d138ce0
Simplify reserveFraction
paulmelnikow Nov 1, 2017
188d389
Validate tokens
paulmelnikow Nov 1, 2017
d99d095
Add an integration test
paulmelnikow Nov 1, 2017
71ccb19
Renames + docs
paulmelnikow Nov 1, 2017
ee5dba1
TokenPersistence: Tests and fixes
paulmelnikow Nov 2, 2017
06047cd
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 2, 2017
7cc3ea6
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 2, 2017
8f587bd
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 10, 2017
139b8ad
Apply fix from #1244
paulmelnikow Nov 10, 2017
c49f6d9
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 30, 2017
afe0da5
Update package-lock
paulmelnikow Nov 30, 2017
d67e875
Apply fix from https://github.com/badges/shields/commit/59c06628740bc…
paulmelnikow Jul 26, 2018
b03df7d
constEq -> crypto.timingSafeEqual
paulmelnikow Jul 26, 2018
e744f3c
Incorporate work from https://github.com/badges/shields/commit/127b46…
paulmelnikow Jul 26, 2018
31f7e8c
Incorporate work from https://github.com/badges/shields/commit/1d313b…
paulmelnikow Jul 26, 2018
a654fb1
Simplify initialization
paulmelnikow Jul 26, 2018
798b46e
Apply changes from https://github.com/badges/shields/commit/9882a44e5…
paulmelnikow Jul 26, 2018
5addd68
Clean lint
paulmelnikow Jul 26, 2018
1f86e85
Fix some tests
paulmelnikow Jul 26, 2018
b949ecc
Merge remote-tracking branch 'origin/master' into github-token-pool-2
paulmelnikow Jul 26, 2018
58fbda3
Update package-lock
paulmelnikow Jul 26, 2018
df6f303
Fix syntax
paulmelnikow Jul 26, 2018
5bcf33c
Clean lint
paulmelnikow Jul 26, 2018
c68bf49
Rm old github-auth
paulmelnikow Jul 26, 2018
b024e47
Fix most tests
paulmelnikow Jul 26, 2018
f67d335
Clean lint, fix up tests
paulmelnikow Jul 26, 2018
71e19ca
Merge branch 'master' into github-token-pool-2
paulmelnikow Jul 26, 2018
bdd72af
TokenPersistence.stop -> async
paulmelnikow Jul 26, 2018
5e46181
More fixes
paulmelnikow Jul 26, 2018
b01e92b
Reset some formatting changes
paulmelnikow Jul 26, 2018
a65c4b0
Reset some formatting changes
paulmelnikow Jul 26, 2018
52c4e42
Fix
paulmelnikow Jul 26, 2018
4134d27
Consolidate TokenPool classes
paulmelnikow Jul 26, 2018
2f8efda
Update TokenProvider for interface parity
paulmelnikow Jul 26, 2018
f3119f8
Go full dependency injection
paulmelnikow Jul 26, 2018
9b152f1
Restore integration test script, lost in merge
paulmelnikow Jul 27, 2018
c849a0e
github-auth -> services/github/auth
paulmelnikow Jul 27, 2018
3d625b7
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 2, 2018
1966a03
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 7, 2018
030296b
update package-lock
paulmelnikow Aug 7, 2018
89dfce2
Updates from merge
paulmelnikow Aug 7, 2018
c44ef30
Merge commit 'ab051b3' into github-token-pool-2
paulmelnikow Aug 9, 2018
d8b3802
Merge commit '7a664ca' into github-token-pool-2
paulmelnikow Aug 9, 2018
9654883
Run prettier
paulmelnikow Aug 9, 2018
2a4f2bc
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 12, 2018
9b3d5c6
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 17, 2018
b92de26
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 19, 2018
d854927
Roughly merge changes in github-constellation
paulmelnikow Aug 19, 2018
367f202
Add missing init code
paulmelnikow Aug 19, 2018
0e2b471
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 29, 2018
0ee7eed
Merge branch 'master' into github-token-pool-2
paulmelnikow Aug 29, 2018
8d74c63
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 15, 2018
e035788
Update from master
paulmelnikow Nov 15, 2018
0342509
Clean diff
paulmelnikow Nov 15, 2018
baf33ac
Clean diff
paulmelnikow Nov 15, 2018
2ffc965
Clean diff, try that again
paulmelnikow Nov 15, 2018
d050f48
Rm dup test
paulmelnikow Nov 15, 2018
e334cc8
Clean diff
paulmelnikow Nov 27, 2018
35b8e44
WIP
paulmelnikow Nov 27, 2018
8933591
Merge branch 'master' into github-token-pool-2
paulmelnikow Nov 27, 2018
ad4e5ee
Fixes
paulmelnikow Nov 27, 2018
585332c
Rm obsolete
paulmelnikow Nov 27, 2018
eb372a9
Add test coverage for change to TokenPool
paulmelnikow Nov 27, 2018
4bcc876
Temp fix to integration tests
paulmelnikow Nov 27, 2018
19f398c
Fix other integration tests
paulmelnikow Nov 27, 2018
375d609
Set credentials for integration tests
paulmelnikow Nov 27, 2018
7ea08cc
Merge branch 'master' into github-token-pool-2
paulmelnikow Dec 2, 2018
a84dc89
Merge branch 'master' into github-token-pool-2
paulmelnikow Jan 4, 2019
2248429
Make emitter private
paulmelnikow Jan 4, 2019
96e5e82
Rm unnecessary conditional
paulmelnikow Jan 4, 2019
e37d076
Clean up
paulmelnikow Jan 4, 2019
b4d8dda
Fix token persistence tests
paulmelnikow Jan 4, 2019
d6258e4
Work / cleanup
paulmelnikow Jan 4, 2019
5f68f3a
Updates
paulmelnikow Jan 4, 2019
d187d10
Rm emitter, no longer needed
paulmelnikow Jan 4, 2019
76c2e80
Fixes
paulmelnikow Jan 4, 2019
4d8dd16
Clean diff
paulmelnikow Jan 4, 2019
6fda89e
Fix integ tests
paulmelnikow Jan 4, 2019
c6576fa
Add debug output for a flaky test
paulmelnikow Jan 5, 2019
cebe1c8
Merge branch 'master' into github-token-pool-2
paulmelnikow Jan 8, 2019
023953d
Merge branch 'master' into github-token-pool-2
paulmelnikow Jan 9, 2019
78773e0
Remove obsolete secret.json setup
paulmelnikow Jan 9, 2019
7320221
Mock creds using sinon
paulmelnikow Jan 9, 2019
ac4619c
Reset circle config
paulmelnikow Jan 9, 2019
fd63acc
Add test for add-token endpoint
paulmelnikow Jan 9, 2019
f3b9191
Merge branch 'master' into github-token-pool-2
paulmelnikow Jan 10, 2019
4dd8cd4
Merge branch 'master' into github-token-pool-2
paulmelnikow Jan 11, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lib/fs-token-persistence.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'

const fsos = require('fsos')
const githubAuth = require('./github-auth')
const TokenPersistence = require('./token-persistence')

class FsTokenPersistence extends TokenPersistence {
Expand All @@ -23,21 +22,28 @@ class FsTokenPersistence extends TokenPersistence {
}

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

Choose a reason for hiding this comment

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

Now the caller's responsibility.

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!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! I've been working on this for so long I almost forgot it was still coupled at this stage. 😝

I believe the pooling is also needed for #541.

this._tokens = new Set(tokens)
return tokens
}

async save() {
const tokens = githubAuth.getAllTokenIds()
const tokens = Array.from(this._tokens)
await fsos.set(this.path, JSON.stringify(tokens))
}

async onTokenAdded(token) {
if (!this._tokens) {
throw Error('initialize() has not been called')
}
this._tokens.add(token)
await this.save()
}

async onTokenRemoved(token) {
if (!this._tokens) {
throw Error('initialize() has not been called')
}
this._tokens.delete(token)
await this.save()
}
}
Expand Down
16 changes: 5 additions & 11 deletions lib/fs-token-persistence.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ const tmp = require('tmp')
const readFile = require('fs-readfile-promise')
const { expect } = require('chai')
const FsTokenPersistence = require('./fs-token-persistence')
const githubAuth = require('./github-auth')

describe('File system token persistence', function() {
beforeEach(githubAuth.removeAllTokens)
afterEach(githubAuth.removeAllTokens)

let path, persistence
beforeEach(function() {
path = tmp.tmpNameSync()
Expand All @@ -19,8 +15,8 @@ describe('File system token persistence', function() {

context('when the file does not exist', function() {
it('does nothing', async function() {
await persistence.initialize()
expect(githubAuth.getAllTokenIds()).to.deep.equal([])
const tokens = await persistence.initialize()
expect(tokens).to.deep.equal([])
})

it('saving creates an empty file', async function() {
Expand All @@ -41,18 +37,17 @@ describe('File system token persistence', function() {
})
calebcartwright marked this conversation as resolved.
Show resolved Hide resolved

it('loads the contents', async function() {
await persistence.initialize()
expect(githubAuth.getAllTokenIds()).to.deep.equal(initialTokens)
const tokens = await persistence.initialize()
expect(tokens).to.deep.equal(initialTokens)
})

context('when tokens are added', function() {
it('saves the change', async function() {
const newToken = 'e'.repeat(40)
const expected = initialTokens.slice()
const expected = Array.from(initialTokens)
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)


const savedTokens = JSON.parse(await readFile(path))
Expand All @@ -67,7 +62,6 @@ describe('File system token persistence', function() {

await persistence.initialize()

githubAuth.rmGithubToken(toRemove)
await persistence.noteTokenRemoved(toRemove)

const savedTokens = JSON.parse(await readFile(path))
Expand Down
207 changes: 0 additions & 207 deletions lib/github-auth.js

This file was deleted.

14 changes: 4 additions & 10 deletions lib/redis-token-persistence.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ const RedisServer = require('redis-server')
const redis = require('redis')
const { expect } = require('chai')
const RedisTokenPersistence = require('./redis-token-persistence')
const githubAuth = require('./github-auth')

describe('Redis token persistence', function() {
beforeEach(githubAuth.removeAllTokens)
afterEach(githubAuth.removeAllTokens)

let server
// In CI, expect redis already to be running.
if (!process.env.CI) {
Expand Down Expand Up @@ -56,8 +52,8 @@ describe('Redis token persistence', function() {

context('when the key does not exist', function() {
it('does nothing', async function() {
await persistence.initialize()
expect(githubAuth.getAllTokenIds()).to.deep.equal([])
const tokens = await persistence.initialize()
expect(tokens).to.deep.equal([])
})
})

Expand All @@ -75,8 +71,8 @@ describe('Redis token persistence', function() {
})

it('loads the contents', async function() {
await persistence.initialize()
expect(githubAuth.getAllTokenIds()).to.deep.equal(initialTokens)
const tokens = await persistence.initialize()
expect(tokens).to.deep.equal(initialTokens)
})

context('when tokens are added', function() {
Expand All @@ -86,7 +82,6 @@ describe('Redis token persistence', function() {
expected.push(newToken)

await persistence.initialize()
githubAuth.addGithubToken(newToken)
await persistence.noteTokenAdded(newToken)

const savedTokens = await lrange(key, 0, -1)
Expand All @@ -101,7 +96,6 @@ describe('Redis token persistence', function() {

await persistence.initialize()

githubAuth.rmGithubToken(toRemove)
await persistence.noteTokenRemoved(toRemove)

const savedTokens = await lrange(key, 0, -1)
Expand Down
13 changes: 2 additions & 11 deletions lib/redis-token-persistence.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const redis = require('redis')
const { promisify } = require('util')
const log = require('./log')
const githubAuth = require('./github-auth')
const TokenPersistence = require('./token-persistence')

class RedisTokenPersistence extends TokenPersistence {
Expand All @@ -21,16 +20,8 @@ class RedisTokenPersistence extends TokenPersistence {

const lrange = promisify(this.client.lrange).bind(this.client)

let tokens
try {
tokens = await lrange(this.key, 0, -1)
} catch (e) {
log.error(e)
}

tokens.forEach(tokenString => {
githubAuth.addGithubToken(tokenString)
})
Copy link
Member Author

Choose a reason for hiding this comment

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

Now the caller's responsibility.

const tokens = await lrange(this.key, 0, -1)
return tokens
}

async stop() {
Expand Down
Loading