Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: cache query misses to protect against DDoS (#6771)
## About the changes This PR establishes a simple yet effective mechanism to avoid DDoS against our DB while also protecting against memory leaks. This will enable us to release the flag `queryMissingTokens` to make our token validation consistent across different nodes --------- Co-authored-by: Nuno Góis <github@nunogois.com>
- Loading branch information
1 parent
d466f60
commit d7ab886
Showing
4 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import dbInit, { type ITestDb } from '../helpers/database-init'; | ||
import getLogger from '../../fixtures/no-logger'; | ||
import type { IUnleashStores } from '../../../lib/types'; | ||
import { ApiTokenType } from '../../../lib/types/models/api-token'; | ||
|
||
let stores: IUnleashStores; | ||
let db: ITestDb; | ||
|
||
beforeAll(async () => { | ||
db = await dbInit('api_token_store_serial', getLogger); | ||
stores = db.stores; | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
||
test('get token is undefined when not exist', async () => { | ||
const token = await stores.apiTokenStore.get('abcde123'); | ||
expect(token).toBeUndefined(); | ||
}); | ||
|
||
test('get token returns the token when exists', async () => { | ||
const newToken = await stores.apiTokenStore.insert({ | ||
secret: 'abcde321', | ||
environment: 'default', | ||
type: ApiTokenType.ADMIN, | ||
projects: [], | ||
tokenName: 'admin-test-token', | ||
}); | ||
const foundToken = await stores.apiTokenStore.get('abcde321'); | ||
expect(foundToken).toBeDefined(); | ||
expect(foundToken.secret).toBe(newToken.secret); | ||
expect(foundToken.environment).toBe(newToken.environment); | ||
expect(foundToken.tokenName).toBe(newToken.tokenName); | ||
expect(foundToken.type).toBe(newToken.type); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters