centreon-archived
centreon-archived copied to clipboard
Fix clapi delimiter escaped values (MON-14805)
Description
CLAPI (and also API Rest v1) use the semicolon as a delimiter:
- for values passed with the
-v
(variables) argument - in the line format for import/export
But it's impossible to manage value that can contains a semicolon, because no escape mechanism exists!
This PR add the possibility to escape the delimiter char with a backslash \;
in the values:
- first, escaped delimiters are ignored (by a substitution with another chars) when values are exploded in an array
- then on each values from the array, escaped delimiters are returned as the non-escaped char
- also on export, potential delimiter char inside each value is escaped with a backslash
This mechanism is applied:
- on input from the variables string (
-v
argument for CLAPI orvalues
property for API Rest v1) - on input from each imported lines (
-i
option with CLAPI) - on output from each exported lines (
-e
option with CLAPI)
Note: On output, the escapement is applied only for the export, but not (yet?) for others actions like SHOW or GETPARAM. It's a lot of work to deal with all this cases and it's not the more important (and maybe it's preferable to keep values unescaped for this displays). Only export are critical with this bug because it's necessary to be able to re-import them.
Fixes #11451, #6718
Type of change
- [x] Patch fixing an issue (non-breaking change)
- [ ] New functionality (non-breaking change)
- [ ] Breaking change (patch or feature) that might cause side effects breaking part of the Software
Target serie
- [ ] 21.04.x
- [ ] 21.10.x
- [x] 22.04.x
- [ ] 22.10.x (master)
How this pull request can be tested ?
Define a host macro (assume the host Centreon
exists) that contains semicolon in his value, export and re-import:
centreon -u admin -p '******' -o HOST -a SETMACRO -v "Centreon;USERAGENT;Mozilla/5.0 (X11\; Linux x86_64\; rv:12.0;0;Sample macro with a \; inside the value (and the comment)"
centreon -u admin -p '******' -e | grep "^HOST;setmacro;Centreon;useragent;" > "./clapi.tmp"
centreon -u admin -p '******' -i "./clapi.tmp"
centreon -u admin -p '******' -o HOST -a GETMACRO -v "Centreon"
With this PR, the SETMACRO is possible with semicolons inside values (they just need to be escaped with a backslash). The export command display a line with the escaped chars like this:
HOST;setmacro;Centreon-central-rennes-si;useragent;Mozilla/5.0 (X11\; Linux x86_64\; rv:12.0;0;'Sample macro with a \; inside the value (and the comment)'
And the import command works well with this line format (no data loss with bad truncated values as currently).
At least the GETMACRO command show the real internal value (without escapement like the export command). It's a proof that the escapement is handled correctly (values accepted with backslashes removed):
macro name;macro value;is_password;description;source
USERAGENT;Mozilla/6.0 (X11; Linux x86_64; rv:12.0;;'Sample macro with a ; inside the value (and the comment)';direct
Maybe we could also wish escapement for all this output commands (SHOW, GET*...), or not... Either way that would be a lot of extra work (not absolutely necessary for proper operation) for another PR...
Checklist
Community contributors & Centreon team
- [x] I have followed the coding style guidelines provided by Centreon
- [x] I have commented my code, especially new classes, functions or any legacy code modified. (docblock)
- [x] I have commented my code, especially hard-to-understand areas of the PR.
- [x] I have rebased my development branch on the base branch (master, maintenance).
SonarQube Quality Gate
0 Bugs
0 Vulnerabilities
0 Security Hotspots
9 Code Smells
No Coverage information
14.0% Duplication
@proxyconcept in order for the PR to be handled, you first have to create it for the develop branch. Make sure to synchronize it with the latest changes. Once created, make sure to add the label "22.04.x" for us to know it has to be backported, once reviewed and merged.
migrated to https://github.com/centreon/centreon-gha/pull/