From d67293647ae2cde5b36f45e4596cb3f0461d8b72 Mon Sep 17 00:00:00 2001 From: Nicolas Chaulet Date: Mon, 8 Jan 2024 08:15:10 -0500 Subject: [PATCH] [Fleet] Accept socks5 proxy url (#174338) --- .../constants/fleet_server_policy_config.ts | 2 + .../edit_fleet_proxy_flyout/index.tsx | 2 +- .../use_fleet_proxy_form.test.tsx | 46 +++++++++++++++++++ ...roxy_form.tsx => use_fleet_proxy_form.tsx} | 5 +- .../server/types/rest_spec/fleet_proxies.ts | 14 +++++- 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.test.tsx rename x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/{user_fleet_proxy_form.tsx => use_fleet_proxy_form.tsx} (98%) diff --git a/x-pack/plugins/fleet/common/constants/fleet_server_policy_config.ts b/x-pack/plugins/fleet/common/constants/fleet_server_policy_config.ts index 5ee9ab858c9ab..db8cacb91d6d7 100644 --- a/x-pack/plugins/fleet/common/constants/fleet_server_policy_config.ts +++ b/x-pack/plugins/fleet/common/constants/fleet_server_policy_config.ts @@ -10,3 +10,5 @@ export const FLEET_SERVER_HOST_SAVED_OBJECT_TYPE = 'fleet-fleet-server-host'; export const DEFAULT_FLEET_SERVER_HOST_ID = 'fleet-default-fleet-server-host'; export const FLEET_PROXY_SAVED_OBJECT_TYPE = 'fleet-proxy'; + +export const PROXY_URL_REGEX = /^(http[s]?|socks5):\/\/[^\s$.?#].[^\s]*$/gm; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/index.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/index.tsx index 45869a07eba2a..fd615eecbe298 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/index.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/index.tsx @@ -25,7 +25,7 @@ import { FLYOUT_MAX_WIDTH } from '../../constants'; import type { FleetProxy } from '../../../../types'; import { TextInput, TextAreaInput } from '../form'; -import { useFleetProxyForm } from './user_fleet_proxy_form'; +import { useFleetProxyForm } from './use_fleet_proxy_form'; export interface FleetProxyFlyoutProps { onClose: () => void; diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.test.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.test.tsx new file mode 100644 index 0000000000000..ae30df1a91ff5 --- /dev/null +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.test.tsx @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { createFleetTestRendererMock } from '../../../../../../mock'; + +import { useFleetProxyForm } from './use_fleet_proxy_form'; + +describe('useFleetProxyForm', () => { + describe('validate url', () => { + it('should accept http url', async () => { + const testRenderer = createFleetTestRendererMock(); + const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {})); + result.current.inputs.urlInput.setValue('http://test.fr:8080'); + expect(result.current.inputs.urlInput.validate()).toBeTruthy(); + expect(result.current.inputs.urlInput.errors).toBeUndefined(); + }); + + it('should accept https url', async () => { + const testRenderer = createFleetTestRendererMock(); + const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {})); + result.current.inputs.urlInput.setValue('https://test.fr:8080'); + expect(result.current.inputs.urlInput.validate()).toBeTruthy(); + expect(result.current.inputs.urlInput.errors).toBeUndefined(); + }); + it('should accept socks5 url', async () => { + const testRenderer = createFleetTestRendererMock(); + const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {})); + result.current.inputs.urlInput.setValue('socks5://test.fr:8080'); + expect(result.current.inputs.urlInput.validate()).toBeTruthy(); + expect(result.current.inputs.urlInput.errors).toBeUndefined(); + }); + + it('should not accept invalid url', async () => { + const testRenderer = createFleetTestRendererMock(); + const { result } = testRenderer.renderHook(() => useFleetProxyForm(undefined, () => {})); + result.current.inputs.urlInput.setValue('iamnotavaliderror'); + expect(result.current.inputs.urlInput.validate()).toBeFalsy(); + + expect(result.current.inputs.urlInput.errors).toEqual(['Invalid URL']); + }); + }); +}); diff --git a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/user_fleet_proxy_form.tsx b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.tsx similarity index 98% rename from x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/user_fleet_proxy_form.tsx rename to x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.tsx index 102e4cf3343ce..94b845bb475fa 100644 --- a/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/user_fleet_proxy_form.tsx +++ b/x-pack/plugins/fleet/public/applications/fleet/sections/settings/components/edit_fleet_proxy_flyout/use_fleet_proxy_form.tsx @@ -20,8 +20,7 @@ import { import { useConfirmModal } from '../../hooks/use_confirm_modal'; import type { FleetProxy } from '../../../../types'; - -const URL_REGEX = /^(http)(s)?:\/\/[^\s$.?#].[^\s]*$/gm; +import { PROXY_URL_REGEX } from '../../../../../../../common/constants'; const ConfirmTitle = () => (