Skip to content

Commit

Permalink
Fix agent name validation (#6105)
Browse files Browse the repository at this point in the history
* Fix agent name validation

* Update changelog

* Remove unnecessary validation

* update tests

* Remove trailing space if characters are valid

* Avoid duplicate call to function
  • Loading branch information
Tostti authored Nov 8, 2023
1 parent c90a6ae commit 2737506
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
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 '';
};

0 comments on commit 2737506

Please sign in to comment.