-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
feat: statistics for orphaned tokens #7568
Changes from all commits
8eba0be
4f5727c
17c8e7e
09f6f83
9016ce0
9ad2a12
e1f61d5
45a3899
8c9eb08
a2064c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,10 @@ beforeAll(async () => { | |
stores = db.stores; | ||
}); | ||
|
||
afterEach(async () => { | ||
await db.reset(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.destroy(); | ||
}); | ||
|
@@ -35,3 +39,146 @@ test('get token returns the token when exists', async () => { | |
expect(foundToken.tokenName).toBe(newToken.tokenName); | ||
expect(foundToken.type).toBe(newToken.type); | ||
}); | ||
|
||
describe('count deprecated tokens', () => { | ||
test('should return 0 if there is no legacy or orphaned tokens', async () => { | ||
await stores.projectStore.create({ | ||
id: 'test', | ||
name: 'test', | ||
}); | ||
await stores.apiTokenStore.insert({ | ||
secret: '*:*.be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. which of the fields in the token are important. can we emphasize the fields that matter and hide then one that don't? e.g. if only secret counts There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Almost all fields can count - for example tokens of ADMIN type can't be orphaned, because it doesn't have projects. At the same time it can be in v1 or v2 format. I'm providing a variety of possible correct tokens. |
||
environment: 'default', | ||
type: ApiTokenType.ADMIN, | ||
projects: [], | ||
tokenName: 'admin-token', | ||
}); | ||
await stores.apiTokenStore.insert({ | ||
secret: 'default:development.be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178', | ||
environment: 'default', | ||
type: ApiTokenType.CLIENT, | ||
projects: ['default'], | ||
tokenName: 'client-token', | ||
}); | ||
await stores.apiTokenStore.insert({ | ||
secret: '*:development.be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178', | ||
environment: 'default', | ||
type: ApiTokenType.CLIENT, | ||
projects: [], | ||
tokenName: 'client-wildcard-token', | ||
}); | ||
await stores.apiTokenStore.insert({ | ||
secret: '[]:production.3d6bdada42ddbd63a019d26955178be44368985f7fb3237c584ef86f', | ||
environment: 'default', | ||
type: ApiTokenType.FRONTEND, | ||
projects: ['default', 'test'], | ||
tokenName: 'frontend-token', | ||
}); | ||
|
||
const deprecatedTokens = | ||
await stores.apiTokenStore.countDeprecatedTokens(); | ||
|
||
expect(deprecatedTokens).toEqual({ | ||
activeLegacyTokens: 0, | ||
activeOrphanedTokens: 0, | ||
legacyTokens: 0, | ||
orphanedTokens: 0, | ||
}); | ||
}); | ||
|
||
test('should return 1 for legacy tokens', async () => { | ||
await stores.apiTokenStore.insert({ | ||
secret: 'be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178', | ||
environment: 'default', | ||
type: ApiTokenType.ADMIN, | ||
projects: [], | ||
tokenName: 'admin-test-token', | ||
}); | ||
|
||
const deprecatedTokens = | ||
await stores.apiTokenStore.countDeprecatedTokens(); | ||
|
||
expect(deprecatedTokens).toEqual({ | ||
activeLegacyTokens: 0, | ||
activeOrphanedTokens: 0, | ||
legacyTokens: 1, | ||
orphanedTokens: 0, | ||
}); | ||
}); | ||
|
||
test('should return 1 for orphaned tokens', async () => { | ||
await stores.apiTokenStore.insert({ | ||
secret: 'deleted-project:development.be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178', | ||
environment: 'default', | ||
type: ApiTokenType.CLIENT, | ||
projects: [], | ||
tokenName: 'admin-test-token', | ||
}); | ||
|
||
const deprecatedTokens = | ||
await stores.apiTokenStore.countDeprecatedTokens(); | ||
|
||
expect(deprecatedTokens).toEqual({ | ||
activeLegacyTokens: 0, | ||
activeOrphanedTokens: 0, | ||
legacyTokens: 0, | ||
orphanedTokens: 1, | ||
}); | ||
}); | ||
|
||
test('should not count wildcard tokens as orphaned', async () => { | ||
await stores.apiTokenStore.insert({ | ||
secret: '*:*.be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178', | ||
environment: 'default', | ||
type: ApiTokenType.CLIENT, | ||
projects: [], | ||
tokenName: 'client-test-token', | ||
}); | ||
|
||
const deprecatedTokens = | ||
await stores.apiTokenStore.countDeprecatedTokens(); | ||
|
||
expect(deprecatedTokens).toEqual({ | ||
activeLegacyTokens: 0, | ||
activeOrphanedTokens: 0, | ||
legacyTokens: 0, | ||
orphanedTokens: 0, | ||
}); | ||
}); | ||
|
||
test('should count active tokens based on seen_at', async () => { | ||
const legacyTokenSecret = | ||
'be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178'; | ||
const orphanedTokenSecret = | ||
'[]:production.be44368985f7fb3237c584ef86f3d6bdada42ddbd63a019d26955178'; | ||
await stores.apiTokenStore.insert({ | ||
secret: legacyTokenSecret, | ||
environment: 'default', | ||
type: ApiTokenType.ADMIN, | ||
projects: [], | ||
tokenName: 'admin-test-token', | ||
}); | ||
await stores.apiTokenStore.insert({ | ||
secret: orphanedTokenSecret, | ||
environment: 'default', | ||
type: ApiTokenType.FRONTEND, | ||
projects: [], | ||
tokenName: 'frontend-test-token', | ||
}); | ||
|
||
await stores.apiTokenStore.markSeenAt([ | ||
legacyTokenSecret, | ||
orphanedTokenSecret, | ||
]); | ||
|
||
const deprecatedTokens = | ||
await stores.apiTokenStore.countDeprecatedTokens(); | ||
|
||
expect(deprecatedTokens).toEqual({ | ||
activeLegacyTokens: 1, | ||
activeOrphanedTokens: 1, | ||
legacyTokens: 1, | ||
orphanedTokens: 1, | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fun. Killswitch being disabled by default was masking that tests related to other flag don't pass.