2024-04-19 15:40:15 +02:00
|
|
|
import type { IFlagResolver, IUnleashConfig } from '../types';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type { Logger } from '../logger';
|
|
|
|
import type { EdgeTokenSchema } from '../openapi/spec/edge-token-schema';
|
2022-09-01 15:26:26 +02:00
|
|
|
import { constantTimeCompare } from '../util/constantTimeCompare';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type { ValidatedEdgeTokensSchema } from '../openapi/spec/validated-edge-tokens-schema';
|
|
|
|
import type { ApiTokenService } from './api-token-service';
|
2024-04-19 15:40:15 +02:00
|
|
|
import metricsHelper from '../util/metrics-helper';
|
|
|
|
import { FUNCTION_TIME } from '../metric-events';
|
2022-09-01 15:26:26 +02:00
|
|
|
|
|
|
|
export default class EdgeService {
|
|
|
|
private logger: Logger;
|
|
|
|
|
2024-02-06 15:14:08 +01:00
|
|
|
private apiTokenService: ApiTokenService;
|
2022-09-01 15:26:26 +02:00
|
|
|
|
2024-04-19 15:40:15 +02:00
|
|
|
private flagResolver: IFlagResolver;
|
|
|
|
|
|
|
|
private timer: Function;
|
|
|
|
|
2022-09-01 15:26:26 +02:00
|
|
|
constructor(
|
2024-02-06 15:14:08 +01:00
|
|
|
{ apiTokenService }: { apiTokenService: ApiTokenService },
|
2024-04-19 15:40:15 +02:00
|
|
|
{
|
|
|
|
getLogger,
|
|
|
|
flagResolver,
|
|
|
|
eventBus,
|
|
|
|
}: Pick<IUnleashConfig, 'getLogger' | 'flagResolver' | 'eventBus'>,
|
2022-09-01 15:26:26 +02:00
|
|
|
) {
|
|
|
|
this.logger = getLogger('lib/services/edge-service.ts');
|
2024-02-06 15:14:08 +01:00
|
|
|
this.apiTokenService = apiTokenService;
|
2024-04-19 15:40:15 +02:00
|
|
|
this.flagResolver = flagResolver;
|
|
|
|
this.timer = (functionName: string) =>
|
|
|
|
metricsHelper.wrapTimer(eventBus, FUNCTION_TIME, {
|
|
|
|
className: 'EdgeService',
|
|
|
|
functionName,
|
|
|
|
});
|
2022-09-01 15:26:26 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 15:39:39 +02:00
|
|
|
async getValidTokens(tokens: string[]): Promise<ValidatedEdgeTokensSchema> {
|
2024-04-19 15:40:15 +02:00
|
|
|
if (this.flagResolver.isEnabled('checkEdgeValidTokensFromCache')) {
|
|
|
|
const stopTimer = this.timer('validateTokensWithCache');
|
|
|
|
// new behavior: use cached tokens when possible
|
|
|
|
// use the db to fetch the missing ones
|
|
|
|
// cache stores both missing and active so we don't hammer the db
|
|
|
|
const validatedTokens: EdgeTokenSchema[] = [];
|
|
|
|
for (const token of tokens) {
|
|
|
|
const found =
|
|
|
|
await this.apiTokenService.getTokenWithCache(token);
|
|
|
|
if (found) {
|
|
|
|
validatedTokens.push({
|
|
|
|
token: token,
|
|
|
|
type: found.type,
|
|
|
|
projects: found.projects,
|
|
|
|
});
|
|
|
|
}
|
2022-09-01 15:26:26 +02:00
|
|
|
}
|
2024-04-19 15:40:15 +02:00
|
|
|
stopTimer();
|
|
|
|
return { tokens: validatedTokens };
|
|
|
|
} else {
|
|
|
|
// old behavior: go to the db to fetch all tokens and then filter in memory
|
|
|
|
const stopTimer = this.timer('validateTokensWithoutCache');
|
|
|
|
const activeTokens =
|
|
|
|
await this.apiTokenService.getAllActiveTokens();
|
|
|
|
const edgeTokens = tokens.reduce(
|
|
|
|
(result: EdgeTokenSchema[], token) => {
|
|
|
|
const dbToken = activeTokens.find((activeToken) =>
|
|
|
|
constantTimeCompare(activeToken.secret, token),
|
|
|
|
);
|
|
|
|
if (dbToken) {
|
|
|
|
result.push({
|
|
|
|
token: token,
|
|
|
|
type: dbToken.type,
|
|
|
|
projects: dbToken.projects,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
[],
|
|
|
|
);
|
|
|
|
stopTimer();
|
|
|
|
return { tokens: edgeTokens };
|
|
|
|
}
|
2022-09-01 15:26:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EdgeService;
|