Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kie-issues#2466: Serverless Logic Web Tools: Runtime Tools settings doesn't validate the data index url. #2608

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions packages/runtime-tools-swf-gateway-api/src/gatewayApi/apis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -729,3 +729,22 @@ export const saveFormContent = (formName: string, content: FormContent): Promise
.catch((error) => reject(error));
});
};

export async function verifyDataIndex(dataIndexUrl?: string): Promise<boolean> {
if (!dataIndexUrl) {
return false;
}

try {
const response = await fetch(dataIndexUrl, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: '{"query":""}',
});
return response.status === 200;
} catch (e) {
return false;
}
}
7 changes: 6 additions & 1 deletion packages/serverless-logic-web-tools/src/i18n/AppI18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ interface AppDictionary extends ReferenceDictionary {
validationError: string;
connectionError: string;
configExpiredWarning: string;
validDataIndexURLError: string;
};
confirmModal: {
title: string;
Expand Down Expand Up @@ -106,6 +105,12 @@ interface AppDictionary extends ReferenceDictionary {
dependencyWarningTooltip: string;
};
};
RuntimeToolsSettings: {
configModal: {
validDataIndexURLError: string;
dataIndexConnectionError: string;
};
};
}

export interface AppI18n extends AppDictionary, CommonI18n {}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export const de: AppI18n = {
validationError: "Sie müssen alle erforderlichen Felder ausfüllen, bevor Sie fortfahren können.",
connectionError: "Verbindung abgelehnt. Bitte überprüfen Sie die angegebenen Informationen.",
configExpiredWarning: "Token oder Konto ist abgelaufen. Bitte aktualisieren Sie Ihre Konfiguration.",
validDataIndexURLError: "Bitte geben Sie eine gültige Datenindex-URL ein.",
},
confirmModal: {
title: "Bereitstellen",
Expand Down Expand Up @@ -113,4 +112,10 @@ export const de: AppI18n = {
"Modelle in diesem Arbeitsbereich können von Bereitstellungen aus anderen Arbeitsbereichen abhängen.",
},
},
RuntimeToolsSettings: {
configModal: {
validDataIndexURLError: "Bitte geben Sie eine gültige Datenindex-URL ein.",
dataIndexConnectionError: "Verbindung abgelehnt. Bitte überprüfen Sie die bereitgestellten Informationen.",
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export const en: AppI18n = {
validationError: "You must fill out all required fields before you can proceed.",
connectionError: "Connection refused. Please check the information provided.",
configExpiredWarning: "Token or account expired. Please update your configuration.",
validDataIndexURLError: "Please enter a valid Data Index URL.",
},
confirmModal: {
title: "Deploy",
Expand Down Expand Up @@ -109,4 +108,10 @@ export const en: AppI18n = {
dependencyWarningTooltip: "Models in this workspace may depend on deployments from other workspaces.",
},
},
RuntimeToolsSettings: {
configModal: {
validDataIndexURLError: "Please enter a valid Data Index URL.",
dataIndexConnectionError: "Connection refused. Please check the information provided.",
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ import {
saveConfigCookie,
} from "./RuntimeToolsConfig";
import { removeTrailingSlashFromUrl } from "../../url";
import { validDataIndexUrl } from "../../url";
import { isDataIndexUrlValid } from "../../url";
import { Alert } from "@patternfly/react-core/dist/js";
import { useAppI18n } from "../../i18n";
import { verifyDataIndex } from "@kie-tools/runtime-tools-swf-gateway-api/src/gatewayApi/apis";

const PAGE_TITLE = "Runtime Tools";

enum DataIndexValidation {
enum FormValiationOptions {
INITIAL = "INITIAL",
INVALID = "INVALID",
CONNECTION_ERROR = "CONNECTION_ERROR",
}

export function RuntimeToolsSettings(props: SettingsPageProps) {
Expand All @@ -60,10 +62,12 @@ export function RuntimeToolsSettings(props: SettingsPageProps) {
const settingsDispatch = useSettingsDispatch();
const [config, setConfig] = useState(settings.runtimeTools.config);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isDataIndexUrlValidated, setDataIndexUrlValidated] = useState(DataIndexValidation.INVALID);
const [isDataIndexUrlValidated, setDataIndexUrlValidated] = useState(FormValiationOptions.INITIAL);
const [isDataIndexUrlVerified, setisDataIndexUrlVerified] = useState(FormValiationOptions.INITIAL);
kumaradityaraj marked this conversation as resolved.
Show resolved Hide resolved
const [dataIndexUrlAvailable, setDataIndexUrlAvailable] = useState<boolean>(false);

useEffect(() => {
setDataIndexUrlValidated(DataIndexValidation.INITIAL);
setDataIndexUrlValidated(FormValiationOptions.INITIAL);
}, [config]);

const handleModalToggle = useCallback(() => {
Expand All @@ -90,13 +94,21 @@ export function RuntimeToolsSettings(props: SettingsPageProps) {
resetConfigCookie();
}, [settingsDispatch.runtimeTools]);

const onApply = useCallback(() => {
const onApply = useCallback(async () => {
const newConfig: RuntimeToolsSettingsConfig = {
dataIndexUrl: removeTrailingSlashFromUrl(config.dataIndexUrl),
};
if (!validDataIndexUrl(config.dataIndexUrl)) {
setDataIndexUrlValidated(DataIndexValidation.INVALID);
const isDataIndexUrlVerified = await verifyDataIndex(config.dataIndexUrl);
if (!isDataIndexUrlValid(config.dataIndexUrl)) {
setDataIndexUrlValidated(FormValiationOptions.INVALID);
return;
} else {
if (isDataIndexUrlVerified == true) {
setDataIndexUrlAvailable(true);
} else {
setisDataIndexUrlVerified(FormValiationOptions.CONNECTION_ERROR);
return;
}
}

kumaradityaraj marked this conversation as resolved.
Show resolved Hide resolved
setConfig(newConfig);
Expand Down Expand Up @@ -163,17 +175,28 @@ export function RuntimeToolsSettings(props: SettingsPageProps) {
appendTo={props.pageContainerRef.current || document.body}
>
<Form>
{isDataIndexUrlValidated === DataIndexValidation.INVALID && (
{isDataIndexUrlValidated === FormValiationOptions.INVALID && (
<FormAlert>
<Alert
variant="danger"
title={i18n.openshift.configModal.validDataIndexURLError}
title={i18n.RuntimeToolsSettings.configModal.validDataIndexURLError}
aria-live="polite"
isInline
data-testid="alert-data-index-url-invalid"
/>
</FormAlert>
)}
{isDataIndexUrlVerified === FormValiationOptions.CONNECTION_ERROR && (
<FormAlert>
<Alert
variant="danger"
title={i18n.RuntimeToolsSettings.configModal.dataIndexConnectionError}
aria-live="polite"
isInline
data-testid="alert-data-index-url-connection-error"
/>
</FormAlert>
)}
<FormGroup
label={"Data Index URL"}
labelIcon={
Expand Down
6 changes: 3 additions & 3 deletions packages/serverless-logic-web-tools/src/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export function removeTrailingSlashFromUrl(url: string): string {
return url.replace(/\/$/, "");
}

export function validDataIndexUrl(url: string): boolean {
export function isDataIndexUrlValid(url: string): boolean {
try {
new URL(url);
return true;
const parsedUrl = new URL(url);
return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";
} catch (_) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
* under the License.
*/

import { validDataIndexUrl } from "../../src/url";
import { isDataIndexUrlValid } from "../../src/url";

describe("removeTrailingSlash", () => {
describe("isDataIndexUrlValid", () => {
it.each([
["http://example.com", true],
["http://example.com/", true],
["https://example.com/", true],
["loremIpsum", false],
["google.com", false],
])("should validate the data index URL", (inputUrl, isValidUrl) => {
expect(validDataIndexUrl(inputUrl)).toBe(isValidUrl);
["ftps://example.com/", false],
])("the data index URL %s validation should be %s", (inputUrl, isValidUrl) => {
expect(isDataIndexUrlValid(inputUrl)).toBe(isValidUrl);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

import React, { PropsWithChildren, useEffect, useMemo, useState } from "react";
import { DEFAULT_APPDATA_VALUES } from "../AppConstants";
import { AppData, verifyDataIndex } from "../data";
import { AppData } from "../data";
import { useAppDataPromise } from "../hooks/useAppDataPromise";
import { AppContext } from "./AppContext";
import { verifyDataIndex } from "@kie-tools/runtime-tools-swf-gateway-api/src/gatewayApi/apis";

export function AppContextProvider(props: PropsWithChildren<{}>) {
const appDataPromise = useAppDataPromise();
Expand Down
19 changes: 0 additions & 19 deletions packages/sonataflow-deployment-webapp/src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,3 @@ export async function fetchAppData(): Promise<AppData> {
const response = await fetch(routes.dataJson.path({}));
return (await response.json()) as AppData;
}

export async function verifyDataIndex(dataIndexUrl?: string): Promise<boolean> {
if (!dataIndexUrl) {
return false;
}

try {
const response = await fetch(dataIndexUrl, {
headers: {
"Content-Type": "application/json",
},
method: "POST",
body: '{"query":""}',
});
return response.status === 200;
} catch (e) {
return false;
}
}
Loading