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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions packages/serverless-logic-web-tools/src/i18n/AppI18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ interface AppDictionary extends ReferenceDictionary {
validationError: string;
connectionError: string;
configExpiredWarning: string;
validDataIndexURLError: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking care of the translations.
This section is related to the OpenShift settings page.
I suggest creating a section for RuntimeToolsSettings page to store this property.
ie: RuntimeToolsSettings.configModal.validDataIndexURLError

};
confirmModal: {
title: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
* under the License.
*/

import React from "react";
import React, { useEffect } from "react";
import { Button, ButtonVariant } from "@patternfly/react-core/dist/js/components/Button";
import { EmptyState, EmptyStateBody, EmptyStateIcon } from "@patternfly/react-core/dist/js/components/EmptyState";
import { ActionGroup, Form, FormGroup } from "@patternfly/react-core/dist/js/components/Form";
import { ActionGroup, Form, FormAlert, FormGroup } from "@patternfly/react-core/dist/js/components/Form";
import { InputGroup, InputGroupText } from "@patternfly/react-core/dist/js/components/InputGroup";
import { Modal, ModalVariant } from "@patternfly/react-core/dist/js/components/Modal";
import { PageSection } from "@patternfly/react-core/dist/js/components/Page";
Expand All @@ -43,14 +43,28 @@ import {
saveConfigCookie,
} from "./RuntimeToolsConfig";
import { removeTrailingSlashFromUrl } from "../../url";
import { validDataIndexUrl } from "../../url";
import { Alert } from "@patternfly/react-core/dist/js";
import { useAppI18n } from "../../i18n";

const PAGE_TITLE = "Runtime Tools";

enum DataIndexValidation {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

XXS suggestion. On some pages we call this FormValiationOptions, we can make this name consistent with those pages:
packages/serverless-logic-web-tools/src/settings/openshift/OpenShiftSettingsSimpleConfig.tsx
packages/online-editor/src/accounts/kubernetes/ConnectToKubernetesSimple.tsx
packages/online-editor/src/accounts/openshift/ConnecToOpenShiftSimple.tsx

Suggested change
enum DataIndexValidation {
enum FormValiationOptions {

INITIAL = "INITIAL",
INVALID = "INVALID",
}

export function RuntimeToolsSettings(props: SettingsPageProps) {
const { i18n } = useAppI18n();
const settings = useSettings();
const settingsDispatch = useSettingsDispatch();
const [config, setConfig] = useState(settings.runtimeTools.config);
const [isModalOpen, setIsModalOpen] = useState(false);
const [isDataIndexUrlValidated, setDataIndexUrlValidated] = useState(DataIndexValidation.INVALID);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const [isDataIndexUrlValidated, setDataIndexUrlValidated] = useState(DataIndexValidation.INVALID);
const [isDataIndexUrlValidated, setDataIndexUrlValidated] = useState(DataIndexValidation.INITIAL);


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

const handleModalToggle = useCallback(() => {
setIsModalOpen((prevIsModalOpen) => !prevIsModalOpen);
Expand Down Expand Up @@ -80,6 +94,11 @@ export function RuntimeToolsSettings(props: SettingsPageProps) {
const newConfig: RuntimeToolsSettingsConfig = {
dataIndexUrl: removeTrailingSlashFromUrl(config.dataIndexUrl),
};
if (!validDataIndexUrl(config.dataIndexUrl)) {
setDataIndexUrlValidated(DataIndexValidation.INVALID);
return;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After checking if it's valid it would be great to really test the url answers to a simple query.
You can use:

export async function verifyDataIndex(dataIndexUrl?: string): Promise<boolean> {

Maybe we can move verifyDataIndex() inside the package runtime-tools-swf-gateway-api to avoid code duplication.

setConfig(newConfig);
settingsDispatch.runtimeTools.setConfig(newConfig);
saveConfigCookie(newConfig);
Expand Down Expand Up @@ -144,6 +163,17 @@ export function RuntimeToolsSettings(props: SettingsPageProps) {
appendTo={props.pageContainerRef.current || document.body}
>
<Form>
{isDataIndexUrlValidated === DataIndexValidation.INVALID && (
<FormAlert>
<Alert
variant="danger"
title={i18n.openshift.configModal.validDataIndexURLError}
aria-live="polite"
isInline
data-testid="alert-data-index-url-invalid"
/>
</FormAlert>
)}
<FormGroup
label={"Data Index URL"}
labelIcon={
Expand Down
9 changes: 9 additions & 0 deletions packages/serverless-logic-web-tools/src/url/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@
export function removeTrailingSlashFromUrl(url: string): string {
return url.replace(/\/$/, "");
}

export function validDataIndexUrl(url: string): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make this name consistent with the other kie-tools validation functions.

Suggested change
export function validDataIndexUrl(url: string): boolean {
export function isDataIndexUrlValid(url: string): boolean {

try {
new URL(url);
return true;
Comment on lines +32 to +33
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a bug here, we are accepting every protocol in this function.

@tiagobento it's out of scope, but do you think we should also reproduce this suggestion here?
https://github.com/kumaradityaraj/kie-tools/blob/fc27f22fa01303a39132ae27494b43a41bc7101f/packages/kubernetes-bridge/src/service/KubernetesConnection.ts#L51

Suggested change
new URL(url);
return true;
const parsedUrl = new URL(url);
return parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:";

} catch (_) {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

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

describe("removeTrailingSlash", () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover?

it.each([
["http://example.com/", true],
["https://example.com/", true],
Comment on lines +24 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's ensure we accept only http/https protocol.

Suggested change
["http://example.com/", true],
["https://example.com/", true],
["http://example.com", true],
["http://example.com/", true],
["https://example.com/", true],
["ftps://example.com/", false],

["loremIpsum", false],
["google.com", false],
])("should validate the data index URL", (inputUrl, isValidUrl) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's improve the log messages.

Suggested change
])("should validate the data index URL", (inputUrl, isValidUrl) => {
])("the data index URL %s validation should be %s", (inputUrl, isValidUrl) => {

expect(validDataIndexUrl(inputUrl)).toBe(isValidUrl);
});
});
Loading