1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

Feat update token seen at (#2514)

https://linear.app/unleash/issue/2-448/update-last-seen-column-for-api-tokens

Co-authored-by: Ivar Conradi Østhus <ivar@getunleash.ai>
This commit is contained in:
Nuno Góis 2022-11-29 19:46:40 +00:00 committed by GitHub
parent b32d3d0fee
commit 564c287025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 2 deletions

View File

@ -203,7 +203,7 @@ export class ApiTokenStore implements IApiTokenStore {
const now = new Date();
try {
await this.db(TABLE)
.whereIn('secrets', secrets)
.whereIn('secret', secrets)
.update({ seen_at: now });
} catch (err) {
this.logger.error('Could not update lastSeen, error: ', err);

View File

@ -26,6 +26,7 @@ import {
ApiTokenUpdatedEvent,
} from '../types';
import { omitKeys } from '../util';
import { IFlagResolver } from 'lib/types/experimental';
const resolveTokenPermissions = (tokenType: string) => {
if (tokenType === ApiTokenType.ADMIN) {
@ -52,10 +53,16 @@ export class ApiTokenService {
private timer: NodeJS.Timeout;
private seenTimer: NodeJS.Timeout;
private activeTokens: IApiToken[] = [];
private eventStore: IEventStore;
private lastSeenSecrets: Set<string> = new Set<string>();
private flagResolver: IFlagResolver;
constructor(
{
apiTokenStore,
@ -65,8 +72,12 @@ export class ApiTokenService {
IUnleashStores,
'apiTokenStore' | 'environmentStore' | 'eventStore'
>,
config: Pick<IUnleashConfig, 'getLogger' | 'authentication'>,
config: Pick<
IUnleashConfig,
'getLogger' | 'authentication' | 'flagResolver'
>,
) {
this.flagResolver = config.flagResolver;
this.store = apiTokenStore;
this.eventStore = eventStore;
this.environmentStore = environmentStore;
@ -76,6 +87,9 @@ export class ApiTokenService {
() => this.fetchActiveTokens(),
minutesToMilliseconds(1),
).unref();
if (this.flagResolver.isEnabled('tokensLastSeen')) {
this.updateLastSeen();
}
if (config.authentication.initApiTokens.length > 0) {
process.nextTick(async () =>
this.initApiTokens(config.authentication.initApiTokens),
@ -92,6 +106,19 @@ export class ApiTokenService {
}
}
async updateLastSeen(): Promise<void> {
if (this.lastSeenSecrets.size > 0) {
const toStore = [...this.lastSeenSecrets];
this.lastSeenSecrets = new Set<string>();
await this.store.markSeenAt(toStore);
}
this.seenTimer = setTimeout(
async () => this.updateLastSeen(),
minutesToMilliseconds(3),
).unref();
}
public async getAllTokens(): Promise<IApiToken[]> {
return this.store.getAll();
}
@ -137,6 +164,10 @@ export class ApiTokenService {
}
if (token) {
if (this.flagResolver.isEnabled('tokensLastSeen')) {
this.lastSeenSecrets.add(token.secret);
}
return new ApiUser({
username: token.username,
permissions: resolveTokenPermissions(token.type),
@ -269,6 +300,8 @@ export class ApiTokenService {
destroy(): void {
clearInterval(this.timer);
clearTimeout(this.seenTimer);
this.timer = null;
this.seenTimer = null;
}
}