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

Fix agent name validation #6105

Merged
merged 6 commits into from
Nov 8, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to the Wazuh app project will be documented in this file.
### Fixed

- Fixed problem when using non latin characters in the username [#6076](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6076)
- Fixed incorrect validation of the agent name on the Deploy new agent window [#6105](https://github.com/wazuh/wazuh-dashboard-plugins/pull/6105)

## Wazuh v4.7.0 - OpenSearch Dashboards 2.8.0 - Revision 01

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ describe('Validations', () => {
const invalidAgentName = '?';
const result = validateAgentName(invalidAgentName);
expect(result).toBe(
'The minimum length is 2 characters. The character is not valid. Allowed characters are A-Z, a-z, ".", "-", "_"',
'The minimum length is 2 characters. The character "?" is not valid. Allowed characters are A-Z, a-z, 0-9, ".", "-", "_"',
);
});

test('should return an error message for invalid format', () => {
test('should return an error message for invalid format of 1 character', () => {
const invalidAgentName = 'agent$name';
const result = validateAgentName(invalidAgentName);
expect(result).toBe(
'The character is not valid. Allowed characters are A-Z, a-z, ".", "-", "_"',
'The character "$" is not valid. Allowed characters are A-Z, a-z, 0-9, ".", "-", "_"',
);
});

test('should return an error message for invalid format of more than 1 character', () => {
const invalidAgentName = 'agent$?name';
const result = validateAgentName(invalidAgentName);
expect(result).toBe(
'The characters "$,?" are not valid. Allowed characters are A-Z, a-z, 0-9, ".", "-", "_"',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,26 @@ export const validateAgentName = (value: any) => {
if (value.length === 0) {
return undefined;
}
const regex = /^[A-Za-z.\-_,]+$/;
let invalidCharacters = validateCharacters(value);
if (value.length < 2) {
return `The minimum length is 2 characters.${
invalidCharacters && ` ${invalidCharacters}`
}`;
}
return `${invalidCharacters}`;
};

const isLengthValid = value.length >= 2 && value.length <= 63;
const isFormatValid = regex.test(value);
if (!isFormatValid && !isLengthValid) {
return 'The minimum length is 2 characters. The character is not valid. Allowed characters are A-Z, a-z, ".", "-", "_"';
} else if (!isLengthValid) {
return 'The minimum length is 2 characters.';
} else if (!isFormatValid) {
return 'The character is not valid. Allowed characters are A-Z, a-z, ".", "-", "_"';
const validateCharacters = (value: any) => {
const regex = /^[a-z0-9.\-_,]+$/i;
const invalidCharacters = [
...new Set(value.split('').filter(char => !regex.test(char))),
];
if (invalidCharacters.length > 1) {
return `The characters "${invalidCharacters.join(
',',
)}" are not valid. Allowed characters are A-Z, a-z, 0-9, ".", "-", "_"`;
} else if (invalidCharacters.length === 1) {
return `The character "${invalidCharacters[0]}" is not valid. Allowed characters are A-Z, a-z, 0-9, ".", "-", "_"`;
}
return '';
};
Loading