From 634e188f7351b211f120ac40889dbbfc33b92676 Mon Sep 17 00:00:00 2001 From: olav Date: Mon, 22 Aug 2022 15:02:39 +0200 Subject: [PATCH] fix: clean up proxy clients on API key deletion (#1950) --- src/lib/routes/admin-api/api-token.ts | 11 ++++++++++- src/lib/services/proxy-service.ts | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/lib/routes/admin-api/api-token.ts b/src/lib/routes/admin-api/api-token.ts index 26fccf6f66..aeba07c675 100644 --- a/src/lib/routes/admin-api/api-token.ts +++ b/src/lib/routes/admin-api/api-token.ts @@ -31,6 +31,7 @@ import { } from '../../openapi/spec/api-token-schema'; import { UpdateApiTokenSchema } from '../../openapi/spec/update-api-token-schema'; import { emptyResponse } from '../../openapi/util/standard-responses'; +import { ProxyService } from '../../services/proxy-service'; interface TokenParam { token: string; @@ -40,6 +41,8 @@ export class ApiTokenController extends Controller { private accessService: AccessService; + private proxyService: ProxyService; + private openApiService: OpenApiService; private logger: Logger; @@ -49,15 +52,20 @@ export class ApiTokenController extends Controller { { apiTokenService, accessService, + proxyService, openApiService, }: Pick< IUnleashServices, - 'apiTokenService' | 'accessService' | 'openApiService' + | 'apiTokenService' + | 'accessService' + | 'proxyService' + | 'openApiService' >, ) { super(config); this.apiTokenService = apiTokenService; this.accessService = accessService; + this.proxyService = proxyService; this.openApiService = openApiService; this.logger = config.getLogger('api-token-controller.js'); @@ -180,6 +188,7 @@ export class ApiTokenController extends Controller { const { token } = req.params; await this.apiTokenService.delete(token); + this.proxyService.deleteClientForProxyToken(token); res.status(200).end(); } diff --git a/src/lib/services/proxy-service.ts b/src/lib/services/proxy-service.ts index 1172c663f5..91ff127efb 100644 --- a/src/lib/services/proxy-service.ts +++ b/src/lib/services/proxy-service.ts @@ -114,6 +114,10 @@ export class ProxyService { return client; } + deleteClientForProxyToken(secret: string): void { + this.clients.delete(secret); + } + private static assertExpectedTokenType({ type }: ApiUser) { assert(type === ApiTokenType.FRONTEND || type === ApiTokenType.ADMIN); }