remove redis token persistence backend (#9065)

* remove redis token persistence backend

* error and exit if redis_url is set
This commit is contained in:
chris48s
2023-04-15 20:17:35 +01:00
committed by GitHub
parent ef8498bfbd
commit b96192eec6
11 changed files with 6 additions and 427 deletions

View File

@@ -1,94 +0,0 @@
import RedisServer from 'redis-server'
import Redis from 'ioredis'
import { expect } from 'chai'
import RedisTokenPersistence from './redis-token-persistence.js'
describe('Redis token persistence', function () {
let server
// In CI, expect redis already to be running.
if (!process.env.CI) {
beforeEach(async function () {
server = new RedisServer({ config: { host: 'localhost' } })
await server.open()
})
}
const key = 'tokenPersistenceIntegrationTest'
let redis
beforeEach(async function () {
redis = new Redis()
await redis.del(key)
})
afterEach(async function () {
if (redis) {
await redis.quit()
redis = undefined
}
})
if (!process.env.CI) {
afterEach(async function () {
await server.close()
server = undefined
})
}
let persistence
beforeEach(function () {
persistence = new RedisTokenPersistence({ key })
})
afterEach(async function () {
if (persistence) {
await persistence.stop()
persistence = undefined
}
})
context('when the key does not exist', function () {
it('does nothing', async function () {
const tokens = await persistence.initialize()
expect(tokens).to.deep.equal([])
})
})
context('when the key exists', function () {
const initialTokens = ['a', 'b', 'c'].map(char => char.repeat(40))
beforeEach(async function () {
await redis.sadd(key, initialTokens)
})
it('loads the contents', async function () {
const tokens = await persistence.initialize()
expect(tokens.sort()).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()
expected.push(newToken)
await persistence.initialize()
await persistence.noteTokenAdded(newToken)
const savedTokens = await redis.smembers(key)
expect(savedTokens.sort()).to.deep.equal(expected)
})
})
context('when tokens are removed', function () {
it('saves the change', async function () {
const expected = Array.from(initialTokens)
const toRemove = expected.pop()
await persistence.initialize()
await persistence.noteTokenRemoved(toRemove)
const savedTokens = await redis.smembers(key)
expect(savedTokens.sort()).to.deep.equal(expected)
})
})
})
})

View File

@@ -1,57 +0,0 @@
import { URL } from 'url'
import Redis from 'ioredis'
import log from '../server/log.js'
export default class RedisTokenPersistence {
constructor({ url, key }) {
this.url = url
this.key = key
this.noteTokenAdded = this.noteTokenAdded.bind(this)
this.noteTokenRemoved = this.noteTokenRemoved.bind(this)
}
async initialize() {
const options =
this.url && this.url.startsWith('rediss:')
? {
// https://www.compose.com/articles/ssl-connections-arrive-for-redis-on-compose/
tls: { servername: new URL(this.url).hostname },
}
: undefined
this.redis = new Redis(this.url, options)
this.redis.on('error', e => {
log.error(e)
})
const tokens = await this.redis.smembers(this.key)
return tokens
}
async stop() {
await this.redis.quit()
}
async onTokenAdded(token) {
await this.redis.sadd(this.key, token)
}
async onTokenRemoved(token) {
await this.redis.srem(this.key, token)
}
async noteTokenAdded(token) {
try {
await this.onTokenAdded(token)
} catch (e) {
log.error(e)
}
}
async noteTokenRemoved(token) {
try {
await this.onTokenRemoved(token)
} catch (e) {
log.error(e)
}
}
}