Skip to content

Commit

Permalink
Kiali version control
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Gutierrez committed Apr 17, 2024
1 parent 7b62efb commit cfc0e14
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 25 deletions.
52 changes: 46 additions & 6 deletions plugins/kiali-backend/src/clients/KialiAPIConnector.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
}
Expand All @@ -21,7 +30,26 @@ export class KialiApiImpl implements KialiApi {
this.kialiFetcher = new KialiFetcher(options.kiali, options.logger);
}

async proxy(endpoint: string, method: string): Promise<any> {
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<any> {
const authValid = await this.kialiFetcher.checkSession();
if (authValid.verify) {
this.logger.debug(
Expand All @@ -30,7 +58,7 @@ export class KialiApiImpl implements KialiApi {
}`,
);
return this.kialiFetcher
.newRequest<any>(endpoint, false, method)
.newRequest<any>(endpoint, false)
.then(resp => resp.data);
}
this.logger.debug(
Expand All @@ -45,9 +73,21 @@ export class KialiApiImpl implements KialiApi {
async status(): Promise<any> {
const authValid = await this.kialiFetcher.checkSession();
if (authValid.verify) {
return this.kialiFetcher
.newRequest<any>('api/status')
.then(resp => resp.data);
return this.kialiFetcher.newRequest<any>('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);
}
Expand Down
15 changes: 7 additions & 8 deletions plugins/kiali-backend/src/clients/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {

export type AuthValid = {
verify: boolean;
title?: string;
missingAttributes?: string[];
message?: string;
helper?: string;
Expand All @@ -32,13 +33,9 @@ export class KialiFetcher {
this.kialiAuth = new KialiAuthentication(KD);
}

newRequest = async <P>(
endpoint: string,
auth: boolean = false,
method?: string,
) => {
newRequest = async <P>(endpoint: string, auth: boolean = false) => {
this.logger.info(`Query to ${endpoint}`);
return axios.request<P>(this.getRequestInit(endpoint, auth, method));
return axios.request<P>(this.getRequestInit(endpoint, auth));
};

private async getAuthInfo(): Promise<AuthInfo> {
Expand All @@ -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'];
Expand All @@ -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;
}
Expand Down Expand Up @@ -107,6 +106,7 @@ export class KialiFetcher {
})
.catch(err => {
checkAuth.verify = false;
checkAuth.title = 'Authentication failed';
checkAuth.message = this.handleUnsuccessfulResponse(err);
});
}
Expand All @@ -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' };
Expand All @@ -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',
Expand Down
3 changes: 3 additions & 0 deletions plugins/kiali-backend/src/kiali_supported.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Kiali version": "v1.73"
}
4 changes: 1 addition & 3 deletions plugins/kiali-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
12 changes: 4 additions & 8 deletions plugins/kiali/src/pages/Kiali/KialiHelper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Page themeId="tool">
<Content>
<WarningPanel title={getTitle()}>{printAuthentication}</WarningPanel>
<WarningPanel title={props.check.title || 'Unexpected Check'}>
{props.check.authData ? printAuthentication : printVersionProblem}
</WarningPanel>
</Content>
</Page>
);
Expand Down
1 change: 1 addition & 0 deletions plugins/kiali/src/store/KialiProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { KialiContext } from './Context';
export type KialiChecker = {
verify: boolean;
missingAttributes?: string[];
title?: string;
message?: string;
helper?: string;
authData?: AuthInfo;
Expand Down

0 comments on commit cfc0e14

Please sign in to comment.