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

Remove the ablity to store the PDF reports into the filesystem #6487

Closed
4 of 6 tasks
Tracked by #6127
Desvelao opened this issue Mar 7, 2024 · 8 comments
Closed
4 of 6 tasks
Tracked by #6127

Remove the ablity to store the PDF reports into the filesystem #6487

Desvelao opened this issue Mar 7, 2024 · 8 comments
Assignees
Labels
level/task Task issue type/enhancement Enhancement issue

Comments

@Desvelao
Copy link
Member

Desvelao commented Mar 7, 2024

Description

As part of the issue that pretends to remove the stateful of the Wazuh dashboard plugins, we need to remove the ability to store the PDF reports files in the filesystem.

We want to move this feature to be managed in the same way that the Reporting plugin, but this could not be possible due to some PDF reports containing data of the Wazuh API.

An intermediate solution is keeping the reporting feature, but removing the feature to store the generated PDF reports to they are downloaded directly instead.

Tasks

  • Remove the ability to store PDF reports in the filesystem
    • Remove Reporting application
    • Remove related endpoints to manage the files (get all, get and delete)
    • Generate a PDF report should download the file
  • Adapt the taking of screenshots used in the PDF reports of the "modules"
  • Review and update documentation
@Desvelao Desvelao added type/enhancement Enhancement issue level/task Task issue labels Mar 7, 2024
@Desvelao Desvelao self-assigned this Mar 7, 2024
@Desvelao Desvelao changed the title Remove the store of PDF reports into the filesystem Remove ablity to store the PDF reports into the filesystem Mar 7, 2024
@Desvelao Desvelao changed the title Remove ablity to store the PDF reports into the filesystem Remove the ablity to store the PDF reports into the filesystem Mar 7, 2024
@Desvelao
Copy link
Member Author

Desvelao commented Mar 7, 2024

Research - Download file directly

The current approach to generating a PDF reports is as follows:

  1. Use a button to export to PDF.
    This does an API request, that generates the PDF report file and stores in the filesystem of the plugin host.
  2. To view the file, the user had to go to Dashboards management > Reporting application.

When I tried to change the approach to download the file directly, I found some problems.

Generating the file as a buffer

The current version of the library used to generate the PDF reports is pdfmake: 0.2.7 and the documentation only has examples storing the file into the filesystem. I was researching about how to save the file into a buffer instead and I got using a writable stream to replace the fs.createWritableStream

I found a recent version 0.3.0-beta7 that had added support to generate the file without requiring the filesystem, through a virtual filesystem. This could resolve the solution I had to apply with the current version. Due to the feature is not available in any released non-final version at the moment, I researched how to get a workaround.

Saving the file in the frontend side

Once I adapted the API endpoints that generate the PDF reports to return the file instead of a JSON response, I found a problem saving the response data in a file. I was trying an approach with the Content-Disposition header, but the file was not downloaded directly. So, following the approach to export to CSV, I saw this was using the FileSaver.saveAs service to store data in a file from the frontend side. This approach uses a blob with the content of the API response.

I tried multiples things but they doesn't work. I found that setting the responseType: "blob" of http client (axios) the plugin is using into the request options, modifes the response.data type to blob, so this could be used to create the blob object that uses the FileSaver.saveAs.

I created a service that wraps the logic to save the file, using the API response from axios http client. The API response should define the filename into the Content-Disposition header and the content type into the Content-Type header.

/**
 * Export response data to file
 */
export const exportResponseToFile = async (response: IExportResponseToFile) => {
  // Get the filename from the response headers
  const [_, filename] =
    response?.headers?.['content-disposition']?.match?.(/filename="([^"]+)"/) ||
    [];

  // Create blob
  const blob = new Blob([response.data], {
    type: response.headers['content-type'],
  });

  // Save file from frontend side
  FileSaver.saveAs(blob, filename);
};

@Desvelao
Copy link
Member Author

Desvelao commented Mar 7, 2024

Changes

  • The generation of PDF reports now downloads the file to the client instead of storing in the filesystem of the platform host
  • Remove the Reporting application

The previously generated reports will be inaccessible after upgrading to a version with this change.

@Desvelao
Copy link
Member Author

Desvelao commented Mar 8, 2024

Changes

  • Enhance error management related to the request to generate the PDF report
  • Remove the route decorator that ensured the report file path was valid
  • Refactor the service to take the visualization snapshots rendered by the new dashboard system
  • Add logic to communicate the dashboard with the Generate report button

@Desvelao
Copy link
Member Author

Changes

@Desvelao
Copy link
Member Author

Research

Some dashboards contain visualizations of type table. In the previous reporting, the data is sent from frontend side to the backend side through the tables property. This allowed to the backend side, add the data to tables instead of taking the screenshots and lose the data in other pages.

With the change in the render of the dashboard, we can't use the same mechanism to get this data.

I was researching this topic but I didn't get this works.

@Desvelao
Copy link
Member Author

I moved the issue to blocked until the work integration of the new method to render the dashboard is implemented.

@Desvelao
Copy link
Member Author

Changes

  • Update development branch with latest changes of base branch
  • Fix unit tests
  • Remove migration task of directory name of PDF reports

@Desvelao
Copy link
Member Author

We will close this issue due to this feature needs to be discussed and moved to a major version because it is a breaking change.

@Desvelao Desvelao closed this as not planned Won't fix, can't repro, duplicate, stale Mar 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
level/task Task issue type/enhancement Enhancement issue
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant