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

[Fleet] fix output host validator when host url is empty #174086

Merged
merged 1 commit into from
Jan 2, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ describe('Output form validation', () => {
expect(res).toEqual([{ message: 'URL is required' }]);
});

it('should not work with empty url', () => {
const res = validateESHosts(['']);

expect(res).toEqual([{ index: 0, message: 'URL is required' }]);
});

it('should work with valid url', () => {
const res = validateESHosts(['https://test.fr:9200']);

Expand Down Expand Up @@ -117,6 +123,11 @@ describe('Output form validation', () => {
{ index: 1, message: 'Duplicate URL' },
]);
});
it('should return an error when invalid protocol', () => {
const res = validateESHosts(['ftp://test.fr']);

expect(res).toEqual([{ index: 0, message: 'Invalid protocol' }]);
});
});

describe('validateLogstashHosts', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,29 @@ export function validateKafkaHosts(value: string[]) {
export function validateESHosts(value: string[]) {
const res: Array<{ message: string; index?: number }> = [];
const urlIndexes: { [key: string]: number[] } = {};
const urlRequiredMessage = i18n.translate(
'xpack.fleet.settings.outputForm.elasticUrlRequiredError',
{
defaultMessage: 'URL is required',
}
);
value.forEach((val, idx) => {
try {
if (!val) {
throw new Error('Host URL required');
}
const urlParsed = new URL(val);
if (!['http:', 'https:'].includes(urlParsed.protocol)) {
throw new Error('Invalid protocol');
res.push({
message: urlRequiredMessage,
index: idx,
});
} else {
const urlParsed = new URL(val);
if (!['http:', 'https:'].includes(urlParsed.protocol)) {
res.push({
message: i18n.translate('xpack.fleet.settings.outputForm.invalidProtocolError', {
defaultMessage: 'Invalid protocol',
}),
index: idx,
});
}
}
} catch (error) {
res.push({
Expand Down Expand Up @@ -125,9 +140,7 @@ export function validateESHosts(value: string[]) {

if (value.length === 0) {
res.push({
message: i18n.translate('xpack.fleet.settings.outputForm.elasticUrlRequiredError', {
defaultMessage: 'URL is required',
}),
message: urlRequiredMessage,
});
}

Expand Down
Loading