From cfc0e14addebd6484e25e5ee9eaa4ad69b87b5bb Mon Sep 17 00:00:00 2001 From: Alberto Gutierrez Date: Wed, 17 Apr 2024 11:24:56 +0200 Subject: [PATCH] Kiali version control --- .../src/clients/KialiAPIConnector.ts | 52 ++++++++++++++++--- plugins/kiali-backend/src/clients/fetch.ts | 15 +++--- .../kiali-backend/src/kiali_supported.json | 3 ++ plugins/kiali-backend/src/service/router.ts | 4 +- plugins/kiali/src/pages/Kiali/KialiHelper.tsx | 12 ++--- plugins/kiali/src/store/KialiProvider.tsx | 1 + 6 files changed, 62 insertions(+), 25 deletions(-) create mode 100644 plugins/kiali-backend/src/kiali_supported.json diff --git a/plugins/kiali-backend/src/clients/KialiAPIConnector.ts b/plugins/kiali-backend/src/clients/KialiAPIConnector.ts index 3430a406600..f7bd22ab773 100644 --- a/plugins/kiali-backend/src/clients/KialiAPIConnector.ts +++ b/plugins/kiali-backend/src/clients/KialiAPIConnector.ts @@ -1,13 +1,22 @@ import { Logger } from 'winston'; +import supported from '../kiali_supported.json'; import { KialiDetails } from '../service/config'; -import { KialiFetcher } from './fetch'; +import { AuthValid, KialiFetcher } from './fetch'; export type Options = { logger: Logger; kiali: KialiDetails; }; +const KIALI_CORE_VERSION = 'Kiali version'; + +type Status = { [K: string]: string }; + +interface StatusState { + status: Status; +} + export interface KialiApi { proxy(endpoint: string, method?: string): Promise; } @@ -21,7 +30,26 @@ export class KialiApiImpl implements KialiApi { this.kialiFetcher = new KialiFetcher(options.kiali, options.logger); } - async proxy(endpoint: string, method: string): Promise { + supportedVersion = (version: string): string | undefined => { + this.logger.info('Validating kiali version'); + const versionSupported = supported[KIALI_CORE_VERSION].replace( + /^./, + '', + ).split('.'); + const versionClean = version.replace(/^./, '').split('.'); + this.logger.info( + `Kiali Version supported ${supported[KIALI_CORE_VERSION]}`, + ); + if ( + versionSupported[0] === versionClean[0] && + versionSupported[1] === versionClean[1] + ) { + return undefined; + } + return `kiali version supported is ${supported[KIALI_CORE_VERSION]}, we found version ${version}`; + }; + + async proxy(endpoint: string): Promise { const authValid = await this.kialiFetcher.checkSession(); if (authValid.verify) { this.logger.debug( @@ -30,7 +58,7 @@ export class KialiApiImpl implements KialiApi { }`, ); return this.kialiFetcher - .newRequest(endpoint, false, method) + .newRequest(endpoint, false) .then(resp => resp.data); } this.logger.debug( @@ -45,9 +73,21 @@ export class KialiApiImpl implements KialiApi { async status(): Promise { const authValid = await this.kialiFetcher.checkSession(); if (authValid.verify) { - return this.kialiFetcher - .newRequest('api/status') - .then(resp => resp.data); + return this.kialiFetcher.newRequest('api/status').then(resp => { + const st: StatusState = resp.data; + const versionControl = this.supportedVersion( + st.status[KIALI_CORE_VERSION], + ); + if (versionControl) { + const response: AuthValid = { + verify: false, + title: 'kiali version not supported', + message: versionControl, + }; + return Promise.resolve(response); + } + return Promise.resolve(resp.data); + }); } return Promise.resolve(authValid); } diff --git a/plugins/kiali-backend/src/clients/fetch.ts b/plugins/kiali-backend/src/clients/fetch.ts index afdcf33d78e..b84f096546b 100644 --- a/plugins/kiali-backend/src/clients/fetch.ts +++ b/plugins/kiali-backend/src/clients/fetch.ts @@ -14,6 +14,7 @@ import { export type AuthValid = { verify: boolean; + title?: string; missingAttributes?: string[]; message?: string; helper?: string; @@ -32,13 +33,9 @@ export class KialiFetcher { this.kialiAuth = new KialiAuthentication(KD); } - newRequest = async

( - endpoint: string, - auth: boolean = false, - method?: string, - ) => { + newRequest = async

(endpoint: string, auth: boolean = false) => { this.logger.info(`Query to ${endpoint}`); - return axios.request

(this.getRequestInit(endpoint, auth, method)); + return axios.request

(this.getRequestInit(endpoint, auth)); }; private async getAuthInfo(): Promise { @@ -63,6 +60,7 @@ export class KialiFetcher { this.KialiDetails.serviceAccountToken === '' ) { result.verify = false; + result.title = 'Authentication failed'; result.message = `Attribute 'serviceAccountToken' is not in the backstage configuration`; result.helper = `For more information follow the steps in https://janus-idp.io/plugins/kiali`; result.missingAttributes = ['serviceAccountToken']; @@ -71,6 +69,7 @@ export class KialiFetcher { } default: result.verify = false; + result.title = 'Authentication failed'; result.message = `Strategy ${auth.strategy} is not supported in Kiali backstage plugin yet`; break; } @@ -107,6 +106,7 @@ export class KialiFetcher { }) .catch(err => { checkAuth.verify = false; + checkAuth.title = 'Authentication failed'; checkAuth.message = this.handleUnsuccessfulResponse(err); }); } @@ -126,7 +126,6 @@ export class KialiFetcher { private getRequestInit = ( endpoint: string, auth: boolean = false, - method?: string, ): AxiosRequestConfig => { const requestInit: AxiosRequestConfig = { timeout: TIMEOUT_FETCH }; const headers = { 'X-Auth-Type-Kiali-UI': '1' }; @@ -141,7 +140,7 @@ export class KialiFetcher { requestInit.data = params; requestInit.method = 'post'; } else { - requestInit.method = method ? method : 'get'; + requestInit.method = 'get'; requestInit.headers = { ...headers, Accept: 'application/json', diff --git a/plugins/kiali-backend/src/kiali_supported.json b/plugins/kiali-backend/src/kiali_supported.json new file mode 100644 index 00000000000..e0fec6aba6e --- /dev/null +++ b/plugins/kiali-backend/src/kiali_supported.json @@ -0,0 +1,3 @@ +{ + "Kiali version": "v1.73" +} diff --git a/plugins/kiali-backend/src/service/router.ts b/plugins/kiali-backend/src/service/router.ts index 13d2e7e3dea..550040aeb1b 100644 --- a/plugins/kiali-backend/src/service/router.ts +++ b/plugins/kiali-backend/src/service/router.ts @@ -22,10 +22,8 @@ export const makeRouter = ( // curl -H "Content-type: application/json" -H "Accept: application/json" -X GET localhost:7007/api/kiali/proxy --data '{"endpoint": "api/namespaces"}' router.post('/proxy', async (req, res) => { const endpoint = req.body.endpoint; - const method = req.body.method; - logger.info(`Call to Kiali ${endpoint}`); - res.json(await kialiAPI.proxy(endpoint, method)); + res.json(await kialiAPI.proxy(endpoint)); }); router.post('/status', async (_, res) => { diff --git a/plugins/kiali/src/pages/Kiali/KialiHelper.tsx b/plugins/kiali/src/pages/Kiali/KialiHelper.tsx index 2818ea52b09..3a074fea195 100644 --- a/plugins/kiali/src/pages/Kiali/KialiHelper.tsx +++ b/plugins/kiali/src/pages/Kiali/KialiHelper.tsx @@ -57,17 +57,13 @@ export const KialiHelper = (props: { check: KialiChecker }) => { ); - const getTitle = () => { - if (!props.check.verify) { - return 'Authentication failed.'; - } - - return 'Unexpected Check'; - }; + const printVersionProblem = <>{props.check.message}; return ( - {printAuthentication} + + {props.check.authData ? printAuthentication : printVersionProblem} + ); diff --git a/plugins/kiali/src/store/KialiProvider.tsx b/plugins/kiali/src/store/KialiProvider.tsx index 8d087c0e88a..8fe1e815c1f 100644 --- a/plugins/kiali/src/store/KialiProvider.tsx +++ b/plugins/kiali/src/store/KialiProvider.tsx @@ -39,6 +39,7 @@ import { KialiContext } from './Context'; export type KialiChecker = { verify: boolean; missingAttributes?: string[]; + title?: string; message?: string; helper?: string; authData?: AuthInfo;