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(clapi/APIv1): Output fixes #4156

Merged
merged 13 commits into from
May 28, 2024
Merged

fix(clapi/APIv1): Output fixes #4156

merged 13 commits into from
May 28, 2024

Conversation

dmyios
Copy link
Contributor

@dmyios dmyios commented May 21, 2024

Description

These patches affect clapi and REST v1. They fix issues regarding 1/ delimiters not escaped and 2/ multiline values.

Fixes # MON-53495

Type of change

  • 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

  • 22.04.x
  • 22.10.x
  • 23.04.x
  • 23.10.x
  • 24.04.x (master)

How this pull request can be tested ?

The cases below show REST v1 output for commands equivalent to short clapi params in first row of tables. The output with patches is the correct result.

Semi colon case

-o HTPL -a GETPARAM -v "htp-test;alias|notes|check_period|active_checks_enabled"
original outputoutput with patches
{
    "result": [
        {
            "alias": "htp-test",
            "notes": "note 1",
            "check_period": " note 2",
            "active_checks_enabled": "24x7;2"
        }
    ]
}
{
    "result": [
        {
            "alias": "htp-test",
            "notes": "note 1; note 2",
            "check_period": "24x7",
            "active_checks_enabled": "2"
        }
    ]
}

Multiline case

-o HTPL -a GETPARAM -v "htp-test;alias|comment|check_period"
original outputoutput with patches
Error HTTP 500
{
    "result": [
        {
            "alias": "htp-test",
            "comment": "line 1\r\nline 2|\r\nline 3",
            "check_period": "24x7"
        }
    ]
}

If we change the parameters order, it's broken in a different way.

-o HTPL -a GETPARAM -v "htp-test;comment|alias|check_period"
original outputoutput with patches
{
    "result": [
        "line 1",
        "line 2|",
        {
            "comment": "line 3",
            "alias": "htp-test",
            "check_period": "24x7"
        }
    ]
}
{
    "result": [
        {
            "comment": "line 1\r\nline 2|\r\nline 3",
            "alias": "htp-test",
            "check_period": "24x7"
        }
   ]
}

Checklist

Community contributors & Centreon team

  • I have followed the coding style guidelines provided by Centreon
  • I have commented my code, especially new classes, functions or any legacy code modified. (docblock)
  • I have commented my code, especially hard-to-understand areas of the PR.
  • I have rebased my development branch on the base branch (master, maintenance).

Copy link
Contributor

coderabbitai bot commented May 21, 2024

Walkthrough

The recent changes focus on improving the handling and parsing of CSV data across various Centreon classes. This includes refactoring the postAction method in centreon_clapi.class.php to use new CSV functions, and consistently applying csvEscape for better CSV output sanitization. Additionally, new utility functions for CSV parsing have been added to common-Func.php and csvFunctions.php.

Changes

File Path Change Summary
centreon/www/api/class/centreon_clapi.class.php Refactored CSV parsing logic in postAction method for improved readability and maintainability. Added require_once for new CSV functions.
centreon/www/class/centreon-clapi/centreonCommand.class.php Updated getparam method to use csvEscape for output sanitization.
centreon/www/class/centreon-clapi/centreonHost.class.php Modified getparam and getmacro methods to use csvEscape for better CSV output handling.
centreon/www/class/centreon-clapi/centreonHostGroup.class.php Updated getparam method to use csvEscape for output sanitization.
centreon/www/class/centreon-clapi/centreonObject.class.php Added csvEscape method and updated getparam method to use it.
centreon/www/include/common/common-Func.php Added new functions csv_to_associative_array and parse_csv for efficient CSV data manipulation.
centreon/www/class/centreon-clapi/centreonHostGroupService.class.php Updated getmacro method to use csvEscape for output sanitization.
centreon/www/class/centreon-clapi/centreonService.class.php Updated getparam and getmacro methods to use csvEscape for better CSV output handling.
centreon/www/class/centreon-clapi/centreonServiceGroup.class.php Updated getparam method to use csvEscape for output sanitization.
centreon/www/class/centreon-clapi/centreonServiceTemplate.class.php Updated getparam and getmacro methods to use csvEscape for better CSV output handling.
centreon/www/include/common/csvFunctions.php Added new functions csvToAssociativeArray and parseCsv for handling CSV data.

🐰
In the land of code where bunnies hop,
We’ve made the CSV parsing pop!
With csvEscape, our data's neat,
Bugs and errors faced defeat.
Functions new, and refactors bright,
Centreon’s future, shining light! 🌟
🐇

Tip

New Features and Improvements

Review Settings

Introduced new personality profiles for code reviews. Users can now select between "Chill" and "Assertive" review tones to tailor feedback styles according to their preferences. The "Assertive" profile posts more comments and nitpicks the code more aggressively, while the "Chill" profile is more relaxed and posts fewer comments.

AST-based Instructions

CodeRabbit offers customizing reviews based on the Abstract Syntax Tree (AST) pattern matching. Read more about AST-based instructions in the documentation.

Community-driven AST-based Rules

We are kicking off a community-driven initiative to create and share AST-based rules. Users can now contribute their AST-based rules to detect security vulnerabilities, code smells, and anti-patterns. Please see the ast-grep-essentials repository for more information.

New Static Analysis Tools

We are continually expanding our support for static analysis tools. We have added support for biome, hadolint, and ast-grep. Update the settings in your .coderabbit.yaml file or head over to the settings page to enable or disable the tools you want to use.

Tone Settings

Users can now customize CodeRabbit to review code in the style of their favorite characters or personalities. Here are some of our favorite examples:

  • Mr. T: "You must talk like Mr. T in all your code reviews. I pity the fool who doesn't!"
  • Pirate: "Arr, matey! Ye must talk like a pirate in all yer code reviews. Yarrr!"
  • Snarky: "You must be snarky in all your code reviews. Snark, snark, snark!"

Revamped Settings Page

We have redesigned the settings page for a more intuitive layout, enabling users to find and adjust settings quickly. This change was long overdue; it not only improves the user experience but also allows our development team to add more settings in the future with ease. Going forward, the changes to .coderabbit.yaml will be reflected in the settings page, and vice versa.

Miscellaneous

  • Turn off free summarization: You can switch off free summarization of PRs opened by users not on a paid plan using the enable_free_tier setting.
  • Knowledge-base scope: You can now set the scope of the knowledge base to either the repository (local) or the organization (global) level using the knowledge_base setting. In addition, you can specify Jira project keys and Linear team keys to limit the knowledge base scope for those integrations.
  • High-level summary placement: You can now customize the location of the high-level summary in the PR description using the high_level_summary_placeholder setting (default @coderabbitai summary).
  • Revamped request changes workflow: You can now configure CodeRabbit to auto-approve or request changes on PRs based on the review feedback using the request_changes_workflow setting.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dmyios dmyios changed the title WIP Mon 53495 fix(clapi/APIv1): Output fixes May 23, 2024
@dmyios dmyios marked this pull request as ready for review May 23, 2024 13:00
@dmyios dmyios requested a review from a team as a code owner May 23, 2024 13:00
@dmyios dmyios requested review from adr-mo and a-launois May 23, 2024 13:00
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 4

Outside diff range and nitpick comments (1)
centreon/www/class/centreon-clapi/centreonHost.class.php (1)

917-920: Review the use of delim for consistency.

It appears that the delimiter delim is used inconsistently in the getmacro method. Consider defining and using a consistent delimiter throughout the method to avoid potential issues with CSV formatting.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between ad0fd66 and 0a0705c.
Files selected for processing (10)
  • centreon/www/api/class/centreon_clapi.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonCommand.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonHost.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonHostGroup.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonHostGroupService.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonObject.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonService.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonServiceGroup.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonServiceTemplate.class.php (2 hunks)
  • centreon/www/include/common/common-Func.php (1 hunks)
Files not summarized due to errors (1)
  • centreon/www/class/centreon-clapi/centreonObject.class.php: Error: Server error. Please try again later.
Additional comments not posted (9)
centreon/www/class/centreon-clapi/centreonHostGroup.class.php (1)

292-292: Ensure that the csv_escape function correctly handles all edge cases for CSV escaping.

Verification successful

The csv_escape function in centreonObject.class.php appears to handle common edge cases for CSV escaping, such as text containing quotes, delimiters, or newlines. The function checks if escaping is required and then appropriately escapes the text.

  • The function checks if the text starts with a quote, contains the delimiter, or contains a newline.
  • If any of these conditions are met, it escapes the text by wrapping it in quotes and doubling any existing quotes within the text.

This implementation seems to cover the necessary edge cases for CSV escaping.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correctness of the csv_escape function by checking its implementation.

# Test: Search for the function implementation. Expect: Function handles all edge cases.
ast-grep --lang php --pattern $'function csv_escape($_, $_) { $$$ }'

Length of output: 176



Script:

#!/bin/bash
# Description: Verify the correctness of the csv_escape function by checking its implementation.

# Test: Search for the function implementation. Expect: Function handles all edge cases.
rg 'function csv_escape' --php

Length of output: 440



Script:

#!/bin/bash
# Description: Verify the correctness of the csv_escape function by checking its implementation.

# Test: Search for the function implementation. Expect: Function handles all edge cases.
rg 'function csv_escape'

Length of output: 126



Script:

#!/bin/bash
# Description: Extract the implementation of the csv_escape function from centreonObject.class.php.

# Test: Extract the function implementation. Expect: Function handles all edge cases.
rg -A 10 'function csv_escape' centreon/www/class/centreon-clapi/centreonObject.class.php

Length of output: 392

centreon/www/class/centreon-clapi/centreonCommand.class.php (1)

254-254: Ensure that the csv_escape function correctly handles all edge cases for CSV escaping.

Verification successful

The csv_escape function in centreonObject.class.php appears to handle common edge cases for CSV escaping, such as text containing quotes, delimiters, or newlines. The function correctly escapes these cases by enclosing the text in quotes and doubling any existing quotes within the text.

  • The function checks if the text starts with a quote, contains the delimiter, or contains a newline.
  • If any of these conditions are met, it encloses the text in quotes and replaces any existing quotes with double quotes.

This implementation seems robust for typical CSV escaping needs.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the correctness of the csv_escape function by checking its implementation.

# Test: Search for the function implementation. Expect: Function handles all edge cases.
ast-grep --lang php --pattern $'function csv_escape($_, $_) { $$$ }'

Length of output: 176



Script:

#!/bin/bash
# Description: Verify the correctness of the csv_escape function by checking its implementation.

# Test: Search for the function implementation. Expect: Function handles all edge cases.
rg 'function csv_escape' -tphp

Length of output: 132



Script:

#!/bin/bash
# Description: Extract the implementation of the csv_escape function.

# Test: Extract the function implementation. Expect: Full function code.
rg -A 10 'protected function csv_escape' centreon/www/class/centreon-clapi/centreonObject.class.php

Length of output: 402

centreon/www/class/centreon-clapi/centreonObject.class.php (2)

352-352: Proper use of csv_escape in getparam method enhances CSV output handling.


667-680: Addition of csv_escape method correctly implements CSV escaping logic.

centreon/www/class/centreon-clapi/centreonServiceGroup.class.php (1)

177-177: Correct implementation of csv_escape in getparam method to ensure proper CSV formatting.

centreon/www/class/centreon-clapi/centreonHostGroupService.class.php (1)

534-535: Proper use of CSV escaping in macro output.

Verification successful

The csv_escape function is implemented correctly to handle CSV escaping by enclosing text in quotes if it contains special characters and doubling any existing quotes within the text. This ensures that the CSV output is properly formatted.

  • centreon/www/class/centreon-clapi/centreonObject.class.php:
    protected function csv_escape($text)
    {
        $escape_required = $text[0] == '"' || strpos($text, $this->delim) !== false || strpos($text, "\n") !== false;
        if ($escape_required) {
            $text = '"' . str_replace('"', '""', $text) . '"';
        }
        return $text;
    }

The usage of csv_escape in centreonHostGroupService.class.php is consistent with its intended purpose.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify integration of csv_escape function across the system.

# Test: Search for usage of csv_escape in other relevant parts of the system. Expect: Consistent usage across similar functionalities.
rg --type php 'csv_escape'

Length of output: 2638



Script:

#!/bin/bash
# Description: Extract and review the implementation of the csv_escape function.

# Test: Locate and display the implementation of the csv_escape function in centreonObject.class.php.
ast-grep --lang php --pattern 'protected function csv_escape($$_) { $$$ }' centreon/www/class/centreon-clapi/centreonObject.class.php

Length of output: 242



Script:

#!/bin/bash
# Description: Extract and review the implementation of the csv_escape function.

# Test: Locate and display the implementation of the csv_escape function in centreonObject.class.php.
rg 'protected function csv_escape' centreon/www/class/centreon-clapi/centreonObject.class.php -A 10

Length of output: 402

centreon/www/class/centreon-clapi/centreonServiceTemplate.class.php (2)

341-341: Ensure proper CSV escaping to prevent issues with special characters in output.


668-670: Properly escape CSV output to handle special characters and multiline values effectively.

centreon/www/class/centreon-clapi/centreonHost.class.php (1)

670-670: Ensure consistent CSV escaping in getparam method.

The changes correctly apply CSV escaping to the output of the getparam method, which aligns with the PR's goal to handle special characters in CSV outputs effectively.

Also applies to: 918-920

centreon/www/api/class/centreon_clapi.class.php Outdated Show resolved Hide resolved
centreon/www/include/common/common-Func.php Outdated Show resolved Hide resolved
centreon/www/api/class/centreon_clapi.class.php Outdated Show resolved Hide resolved
centreon/www/include/common/common-Func.php Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 0a0705c and 8b075c0.
Files selected for processing (2)
  • centreon/www/api/class/centreon_clapi.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonObject.class.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • centreon/www/api/class/centreon_clapi.class.php
Additional comments not posted (1)
centreon/www/class/centreon-clapi/centreonObject.class.php (1)

352-352: Integration of csv_escape in getparam method enhances CSV output handling.

centreon/www/class/centreon-clapi/centreonObject.class.php Outdated Show resolved Hide resolved
@dmyios dmyios requested review from a-launois May 24, 2024 12:12
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 8b075c0 and 001271c.
Files selected for processing (10)
  • centreon/www/api/class/centreon_clapi.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonCommand.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonHost.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonHostGroup.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonHostGroupService.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonObject.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonService.class.php (2 hunks)
  • centreon/www/class/centreon-clapi/centreonServiceGroup.class.php (1 hunks)
  • centreon/www/class/centreon-clapi/centreonServiceTemplate.class.php (2 hunks)
  • centreon/www/include/common/csvFunctions.php (1 hunks)
Files skipped from review as they are similar to previous changes (9)
  • centreon/www/api/class/centreon_clapi.class.php
  • centreon/www/class/centreon-clapi/centreonCommand.class.php
  • centreon/www/class/centreon-clapi/centreonHost.class.php
  • centreon/www/class/centreon-clapi/centreonHostGroup.class.php
  • centreon/www/class/centreon-clapi/centreonHostGroupService.class.php
  • centreon/www/class/centreon-clapi/centreonObject.class.php
  • centreon/www/class/centreon-clapi/centreonService.class.php
  • centreon/www/class/centreon-clapi/centreonServiceGroup.class.php
  • centreon/www/class/centreon-clapi/centreonServiceTemplate.class.php

centreon/www/include/common/csvFunctions.php Show resolved Hide resolved
centreon/www/include/common/csvFunctions.php Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 001271c and 1e9b51f.
Files selected for processing (1)
  • centreon/www/api/class/centreon_clapi.class.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • centreon/www/api/class/centreon_clapi.class.php

@dmyios dmyios merged commit 8f9c866 into develop May 28, 2024
233 checks passed
@dmyios dmyios deleted the MON-53495 branch May 28, 2024 06:56
tuntoja pushed a commit that referenced this pull request Jun 12, 2024
Co-authored-by: TdlQ <>
Co-authored-by: alaunois <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants