Skip to content

Commit

Permalink
Feat update token seen at (#2514)
Browse files Browse the repository at this point in the history
https://linear.app/unleash/issue/2-448/update-last-seen-column-for-api-tokens

Co-authored-by: Ivar Conradi Østhus <ivar@getunleash.ai>
  • Loading branch information
nunogois and ivarconr committed Nov 29, 2022
1 parent b32d3d0 commit 564c287
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/lib/db/api-token-store.ts
Expand Up @@ -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);
Expand Down
35 changes: 34 additions & 1 deletion src/lib/services/api-token-service.ts
Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -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),
Expand All @@ -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();
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -269,6 +300,8 @@ export class ApiTokenService {

destroy(): void {
clearInterval(this.timer);
clearTimeout(this.seenTimer);
this.timer = null;
this.seenTimer = null;
}
}

0 comments on commit 564c287

Please sign in to comment.