Add seed collection admin endpoint
This commit is contained in:
1 parent
ce00bc84b5
commit
56f10f7c29
3 files changed
+198
-1
No files matched your search
@@ -8,6 +8,7 @@ import {
|
||||
Post,
|
||||
Query,
|
||||
Req,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import type {
|
||||
AnalysisJobStatusDto,
|
||||
@@ -20,6 +21,8 @@ import {
|
||||
toRegionalRoute,
|
||||
type RiotPlatformRoute,
|
||||
} from '../riot/riot.types';
|
||||
import { SecretService } from '../global/secret/secret.service';
|
||||
import { SEED_SOURCES, type SeedSource } from '../global/database/schemas';
|
||||
import { AnalysisQueueService } from './analysis-queue.service';
|
||||
import {
|
||||
DEFAULT_ANALYSIS_MATCH_COUNT,
|
||||
@@ -28,8 +31,14 @@ import {
|
||||
} from './analysis.constants';
|
||||
import { AnalysisReadService } from './analysis-read.service';
|
||||
import { AnalysisRequestThrottleService } from './analysis-request-throttle.service';
|
||||
import type { UserAnalysisJobData } from './analysis.types';
|
||||
import type {
|
||||
SeedCollectionJobData,
|
||||
UserAnalysisJobData,
|
||||
} from './analysis.types';
|
||||
import type { Request } from 'express';
|
||||
import { createHash, timingSafeEqual } from 'node:crypto';
|
||||
|
||||
const MAX_ADMIN_SEED_COLLECTION_SOURCE_LIMIT = 200;
|
||||
|
||||
@Controller()
|
||||
export class AnalysisController {
|
||||
@@ -37,6 +46,7 @@ export class AnalysisController {
|
||||
private readonly analysisQueueService: AnalysisQueueService,
|
||||
private readonly analysisReadService: AnalysisReadService,
|
||||
private readonly analysisRequestThrottleService: AnalysisRequestThrottleService,
|
||||
private readonly secretService: SecretService,
|
||||
) {}
|
||||
|
||||
@Post('analysis/request')
|
||||
@@ -52,6 +62,18 @@ export class AnalysisController {
|
||||
return this.analysisQueueService.enqueueUserAnalysis(data);
|
||||
}
|
||||
|
||||
@Post('admin/seed-collection/request')
|
||||
@HttpCode(202)
|
||||
public async requestSeedCollection(
|
||||
@Body() body: unknown,
|
||||
@Req() request: Request,
|
||||
): Promise<AnalysisRequestAcceptedDto> {
|
||||
await this.assertAdminRequest(request);
|
||||
return this.analysisQueueService.enqueueSeedCollection(
|
||||
this.parseSeedCollectionRequest(body),
|
||||
);
|
||||
}
|
||||
|
||||
@Get('analysis/jobs/:jobId')
|
||||
public async getJobStatus(
|
||||
@Param('jobId') jobId: string,
|
||||
@@ -105,6 +127,19 @@ export class AnalysisController {
|
||||
};
|
||||
}
|
||||
|
||||
private parseSeedCollectionRequest(body: unknown): SeedCollectionJobData {
|
||||
const record = body === undefined ? {} : this.requireRecord(body);
|
||||
const platformRoute = this.parsePlatformRoute(record.platform ?? 'kr');
|
||||
const queueId = this.parseSeedCollectionQueueId(record.queueId);
|
||||
const sourceLimits = this.parseSeedSourceLimits(record.sourceLimits);
|
||||
|
||||
return {
|
||||
platformRoute,
|
||||
queueId,
|
||||
...(sourceLimits ? { sourceLimits } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
private requireRecord(value: unknown): Record<string, unknown> {
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||
throw new BadRequestException('Request body must be an object.');
|
||||
@@ -165,6 +200,18 @@ export class AnalysisController {
|
||||
return queueId;
|
||||
}
|
||||
|
||||
private parseSeedCollectionQueueId(value: unknown): number {
|
||||
if (value === undefined) return 420;
|
||||
if (
|
||||
typeof value !== 'number' ||
|
||||
!Number.isInteger(value) ||
|
||||
value !== 420
|
||||
) {
|
||||
throw new BadRequestException('queueId is unsupported.');
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private parseSimilarPlayersLimit(value?: string): number {
|
||||
if (value === undefined) return 10;
|
||||
const limit = Number(value);
|
||||
@@ -174,7 +221,73 @@ export class AnalysisController {
|
||||
return Math.min(limit, 10);
|
||||
}
|
||||
|
||||
private parseSeedSourceLimits(
|
||||
value: unknown,
|
||||
): Partial<Record<SeedSource, number>> | undefined {
|
||||
if (value === undefined) {
|
||||
return undefined;
|
||||
}
|
||||
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
||||
throw new BadRequestException('sourceLimits must be an object.');
|
||||
}
|
||||
|
||||
const sourceLimits: Partial<Record<SeedSource, number>> = {};
|
||||
for (const [source, limit] of Object.entries(value)) {
|
||||
if (!SEED_SOURCES.includes(source as SeedSource)) {
|
||||
throw new BadRequestException(`sourceLimits.${source} is unsupported.`);
|
||||
}
|
||||
if (
|
||||
typeof limit !== 'number' ||
|
||||
!Number.isInteger(limit) ||
|
||||
limit < 0 ||
|
||||
limit > MAX_ADMIN_SEED_COLLECTION_SOURCE_LIMIT
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
`sourceLimits.${source} must be an integer between 0 and ${MAX_ADMIN_SEED_COLLECTION_SOURCE_LIMIT}.`,
|
||||
);
|
||||
}
|
||||
sourceLimits[source as SeedSource] = limit;
|
||||
}
|
||||
|
||||
return sourceLimits;
|
||||
}
|
||||
|
||||
private resolveClientKey(request: Request): string {
|
||||
return request.ip || request.socket.remoteAddress || 'unknown-client';
|
||||
}
|
||||
|
||||
private async assertAdminRequest(request: Request): Promise<void> {
|
||||
const actualToken = this.extractBearerToken(request);
|
||||
if (!actualToken) {
|
||||
throw new UnauthorizedException('Admin token is required.');
|
||||
}
|
||||
|
||||
let expectedToken: string;
|
||||
try {
|
||||
expectedToken = await this.secretService.get<string>('ADMIN_API_TOKEN');
|
||||
} catch {
|
||||
throw new UnauthorizedException('Admin token is not configured.');
|
||||
}
|
||||
|
||||
if (!this.secureTokenEquals(actualToken, expectedToken)) {
|
||||
throw new UnauthorizedException('Admin token is invalid.');
|
||||
}
|
||||
}
|
||||
|
||||
private extractBearerToken(request: Request): string | null {
|
||||
const authorization = request.headers.authorization;
|
||||
if (!authorization) return null;
|
||||
const match = authorization.match(/^Bearer\s+(.+)$/i);
|
||||
return match ? match[1]?.trim() || null : null;
|
||||
}
|
||||
|
||||
private secureTokenEquals(
|
||||
actualToken: string,
|
||||
expectedToken: string,
|
||||
): boolean {
|
||||
if (expectedToken.length === 0) return false;
|
||||
const actualHash = createHash('sha256').update(actualToken).digest();
|
||||
const expectedHash = createHash('sha256').update(expectedToken).digest();
|
||||
return timingSafeEqual(actualHash, expectedHash);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user