From a1707523a6e8a805b774593c3aceeeab0cf2156c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20=C3=81brah=C3=A1m?= Date: Wed, 31 Jan 2024 11:26:16 +0100 Subject: [PATCH] handle unexpected errors around uninstall tokens --- x-pack/plugins/fleet/server/plugin.ts | 45 ++++++++++++++++----------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/x-pack/plugins/fleet/server/plugin.ts b/x-pack/plugins/fleet/server/plugin.ts index 618ebf4c89685..cb0709507a250 100644 --- a/x-pack/plugins/fleet/server/plugin.ts +++ b/x-pack/plugins/fleet/server/plugin.ts @@ -703,8 +703,21 @@ export class FleetPlugin } private async initializeUninstallTokens() { - await this.generateUninstallTokens(); - this.validateUninstallTokens(); + try { + await this.generateUninstallTokens(); + } catch (error) { + appContextService + .getLogger() + .error('Error happened during uninstall token generation.', { error: { message: error } }); + } + + try { + await this.validateUninstallTokens(); + } catch (error) { + appContextService + .getLogger() + .error('Error happened during uninstall token validation.', { error: { message: error } }); + } } private async generateUninstallTokens() { @@ -724,24 +737,18 @@ export class FleetPlugin } } - private validateUninstallTokens() { + private async validateUninstallTokens() { const logger = appContextService.getLogger(); - const uninstallTokenService = appContextService.getUninstallTokenService(); - logger.debug('Validating uninstall tokens'); - uninstallTokenService - ?.checkTokenValidityForAllPolicies() - .catch((error) => { - logger.error('Error happened during uninstall token validation.', { - error: { message: error }, - }); - }) - .then((unintallTokenValidationError) => { - if (unintallTokenValidationError) { - logger.warn(unintallTokenValidationError.error); - } else { - logger.debug('Uninstall tokens validation successful.'); - } - }); + + const unintallTokenValidationError = await appContextService + .getUninstallTokenService() + ?.checkTokenValidityForAllPolicies(); + + if (unintallTokenValidationError) { + logger.warn(unintallTokenValidationError.error); + } else { + logger.debug('Uninstall tokens validation successful.'); + } } }