wazuh / wazuh-dashboard-plugins

Plugins for Wazuh Dashboard
https://wazuh.com/
GNU General Public License v2.0
437 stars 181 forks source link

Identify unused files #6748

Closed asteriscos closed 4 months ago

asteriscos commented 5 months ago

Description

We need to able to identify all unused files in the plugins.

Tasks

yenienserrano commented 5 months ago

Some of the tools I found and have been researching are:

The deadfile and knip work in a similar way, you have to pass them the most global file and from that file you will see which files are never imported. The files that it says are never used should be checked because it gives a strange answer.

Deadfile image

The no-unused-modules is to add it to linter and it tells you which files have an export and are not being imported anywhere else, it is interesting but actually we should fix the long standing linter problems before adding new problems.

no-unused-modules image
Desvelao commented 5 months ago

Update 2024/06/17

linter rule import/no-unused-modules

I was testing the import/no-unused-modules rule for the linter and provided by the eslint-plugin-import package.

  1. Install eslint-plugin-import as a dev dependency:
yarn add -D eslint-plugin-import
  1. Edit the configuration of linter: .eslintrc.json
{
...
"import/no-used-modules": [1, {"unusedExports:true"}]
}
  1. Run the linter filtering by mport/no-unused-modules rule:
yarn lint | grep 'import/no-unused-modules'

Test in 4.8.1 on the main plugin

node@osd:~/kbn/plugins/wazuh$ yarn lint | grep 'import/no-unused-modules'
warning Skipping preferred cache folder "/home/node/.cache/yarn" because it is not writable.                                                                                                 
warning Selected the next writable cache folder in the list, will be "/tmp/.yarn-cache-1000".                                                                                                
  34:1  warning  exported declaration 'WzSampleDataProvider' not used within other modules                    import/no-unused-modules
   14:1   warning  exported declaration 'default' not used within other modules  import/no-unused-modules
  1172:1   warning  exported declaration 'default' not used within other modules  import/no-unused-modules
  18:1   warning  exported declaration 'emptyFieldHandler' not used within other modules  import/no-unused-modules
   12:1   warning  exported declaration 'EventsSelectedFiles' not used within other modules  import/no-unused-modules
   5:1  warning  exported declaration 'ProgressChart' not used within other modules  import/no-unused-modules
  1:10  warning  exported declaration 'EuiSuggestInput' not used within other modules  import/no-unused-modules
  3:10  warning  exported declaration 'EuiSuggestItem' not used within other modules   import/no-unused-modules
  5:10  warning  exported declaration 'EuiSuggest' not used within other modules       import/no-unused-modules
  19:1   warning  exported declaration 'COLORS' not used within other modules                        import/no-unused-modules
  26:1   warning  exported declaration 'DISPLAYS' not used within other modules                      import/no-unused-modules
   14:1   warning  exported declaration 'apiRequestList' not used within other modules  import/no-unused-modules
   28:1   warning  exported declaration 'VisualizeTopMenu' not used within other modules  import/no-unused-modules
   58:1   warning  exported declaration 'WzMenu' not used within other modules           import/no-unused-modules
  28:1   warning  exported declaration 'TestConfiguration' not used within other modules  import/no-unused-modules
  26:1  warning  exported declaration 'ToolsWelcomeCards' not used within other modules  import/no-unused-modules
  12:1  warning  exported declaration 'colors' not used within other modules  import/no-unused-modules
   30:1   warning  exported declaration 'FilesInGroupTable' not used within other modules  import/no-unused-modules
  121:1   warning  exported declaration 'WzConfigurationActiveResponseActiveResponseAgent' not used within other modules  import/no-unused-modules
  68:1   warning  exported declaration 'default' not used within other modules           import/no-unused-modules
  70:1   warning  exported declaration 'WzSettingsViewer' not used within other modules  import/no-unused-modules
  78:1  warning  exported declaration 'WzConfigurationSettingsHeaderViewer' not used within other modules  import/no-unused-modules
  41:1   warning  exported declaration 'settingsBuilder' not used within other modules   import/no-unused-modules
  51:1   warning  exported declaration 'helpLinksBuilder' not used within other modules  import/no-unused-modules
   83:1   warning  exported declaration 'objectWithoutProperties' not used within other modules  import/no-unused-modules
  157:1   warning  exported declaration 'delay' not used within other modules                    import/no-unused-modules
   91:1   warning  exported declaration 'extractMessage' not used within other modules         import/no-unused-modules
  127:1   warning  exported declaration 'handleError' not used within other modules            import/no-unused-modules
  158:1   warning  exported declaration 'checkDaemons' not used within other modules           import/no-unused-modules
  195:1   warning  exported declaration 'makePing' not used within other modules               import/no-unused-modules
  287:1   warning  exported declaration 'restartManager' not used within other modules         import/no-unused-modules
  309:1   warning  exported declaration 'restartCluster' not used within other modules         import/no-unused-modules
  340:1   warning  exported declaration 'restartNode' not used within other modules            import/no-unused-modules
  365:1   warning  exported declaration 'saveConfiguration' not used within other modules      import/no-unused-modules
  385:1   warning  exported declaration 'saveNodeConfiguration' not used within other modules  import/no-unused-modules
  446:1   warning  exported declaration 'validateAfterSent' not used within other modules      import/no-unused-modules
  68:1   warning  exported declaration 'replaceXML' not used within other modules  import/no-unused-modules
   43:1   warning  exported declaration 'WzGroupsOverview' not used within other modules  import/no-unused-modules
    6:1   warning  exported declaration 'default' not used within other modules  import/no-unused-modules
  27:1  warning  exported declaration 'WzReportingOverview' not used within other modules  import/no-unused-modules
   52:1   warning  exported declaration 'WzStatisticsOverview' not used within other modules  import/no-unused-modules
  26:1   warning  exported declaration 'WzStatusAgentInfo' not used within other modules  import/no-unused-modules
  18:1   warning  exported declaration 'WzStatusDaemons' not used within other modules  import/no-unused-modules
  18:1   warning  exported declaration 'WzStatusNodeInfo' not used within other modules        import/no-unused-modules
   59:1   warning  exported declaration 'WzStatusOverview' not used within other modules                                         import/no-unused-modules
  20:1   warning  exported declaration 'WzStatusStats' not used within other modules  import/no-unused-modules
  147:1    warning  exported declaration 'default' not used within other modules        import/no-unused-modules
  12:1  warning  exported declaration 'WazuhConfig' not used within other modules  import/no-unused-modules
   18:1   warning  exported declaration 'ActionAgents' not used within other modules  import/no-unused-modules
  19:1   warning  exported declaration 'CheckDaemonsStatus' not used within other modules  import/no-unused-modules
  127:1  warning  exported declaration 'showFlyoutLogtest' not used within other modules                  import/no-unused-modules
  138:1  warning  exported declaration 'updateDockedLogtest' not used within other modules                import/no-unused-modules
  13:1  warning  exported declaration 'updateConfigurationSection' not used within other modules             import/no-unused-modules
   56:1  warning  exported declaration 'updateSortField' not used within other modules                 import/no-unused-modules
   77:1  warning  exported declaration 'updateSortDirection' not used within other modules             import/no-unused-modules
  140:1  warning  exported declaration 'updatePageIndex' not used within other modules                 import/no-unused-modules
  173:1  warning  exported declaration 'updateFilters' not used within other modules                   import/no-unused-modules
  180:1  warning  exported declaration 'cleanFilters' not used within other modules                    import/no-unused-modules
  186:1  warning  exported declaration 'cleanInfo' not used within other modules                       import/no-unused-modules
  50:1  warning  exported declaration 'cleanInfo' not used within other modules                          import/no-unused-modules
  13:1  warning  exported declaration 'updateMetric' not used within other modules                            import/no-unused-modules
  23:1   warning  exported declaration 'queryConfig' not used within other modules  import/no-unused-modules
  20:3  warning  exported declaration 'checkTimestamp' not used within other modules  import/no-unused-modules
  21:3  warning  exported declaration 'healthCheck' not used within other modules     import/no-unused-modules
   1:1   warning  exported declaration 'base64' not used within other modules  import/no-unused-modules
  13:1   warning  exported declaration 'stringToObj' not used within other modules  import/no-unused-modules
  1317:10   warning  exported declaration 'generateAlert' not used within other modules            import/no-unused-modules
error Command failed with exit code 1.

If you are using a Docker dev environment, this does not mount the linter configuration, so if you want to check the lint on a plugin, you could copy the linter configuration to the root of the plugin.

plugins
  main
    .eslintrc.json
...
.eslintrc.json

List unused files through knip

  1. Install knip as a dev dependency:
yarn add -D knip
  1. Create the configuration file for knip.
{
  "entry": [
    "{public,server,common}/index.{js,ts}",
    "{public,server,common}/**/*.test.{js,ts,tsx}"
  ],
  "project": ["**/*.{js,ts,jsx,tsx}", "!test/**/*", "!target/**/*"]
}

example configuration for the main plugin

  1. Run knip listing the unused files
npx knip --files
> Test in 4.8.1 on the `main` plugin ```console node@osd:~/kbn/plugins/wazuh$ npx knip --files common/semver.ts public/icon-style.js scripts/generate-api-data.js common/wazu-menu/wz-menu-management.cy.ts common/wazu-menu/wz-menu-overview.cy.ts common/wazu-menu/wz-menu-security.cy.ts common/wazu-menu/wz-menu-settings.cy.ts common/wazu-menu/wz-menu-tools.cy.ts server/integration-files/known-fields.ts server/integration-files/monitoring-known-fields.ts server/lib/remove-key.ts public/factories/wazuh-config.js public/services/query-config.js scripts/release/bump.js public/utils/base64.js public/utils/cdblist-to-object.js public/components/add-modules-data/add-modules-data-main.tsx public/components/add-modules-data/module-guide.js public/components/agents/prompt-agent-no-support-module.tsx public/components/d3/progress.js public/components/visualize/visualize-top-menu.js public/components/wz-date-picker/index.ts public/components/wz-search-bar/wz-search-badges.tsx public/controllers/dev-tools/index.js public/controllers/management/colors.js public/controllers/register-agent/index.tsx server/integration-files/visualizations/overview/overview-sca.ts public/components/add-modules-data/guides/audit.js public/components/add-modules-data/guides/aws.js public/components/add-modules-data/guides/cis-cat.js public/components/add-modules-data/guides/docker.js public/components/add-modules-data/guides/fim.js public/components/add-modules-data/guides/index.js public/components/add-modules-data/guides/office.js public/components/add-modules-data/guides/open-scap.js public/components/add-modules-data/guides/osquery.js public/components/add-modules-data/guides/virustotal.js public/components/add-modules-data/guides/vulnerabilities.js public/components/agents/vuls/index.ts public/components/agents/vuls/inventory.tsx public/components/agents/vuls/main.tsx public/components/common/modules/index.ts public/components/overview/metrics/index.ts public/components/settings/configuration/index.ts public/components/tools/devtools/api-requests-list.js public/components/tools/devtools/devToolsHistory.tsx public/components/tools/devtools/devtools.tsx public/components/tools/devtools/main-dev-tools.tsx public/components/wz-search-bar/lib/q-tags-handler.ts public/controllers/agent/components/checkUpgrade.tsx public/controllers/dev-tools/components/test-configuration.js public/controllers/dev-tools/components/tools-welcome-cards.js public/controllers/management/components/files-group-table.js public/controllers/overview/components/alerts-stats.js public/components/agents/vuls/inventory/detail.tsx public/components/agents/vuls/inventory/flyout.tsx public/components/agents/vuls/inventory/index.ts public/components/agents/vuls/inventory/table.tsx public/components/overview/compliance-table/components/index.ts public/components/overview/vulnerabilities/search_bar/index.ts public/kibana-integrations/discover/application/helpers/breadcrumbs.ts public/kibana-integrations/discover/application/helpers/get_index_pattern_id.ts public/kibana-integrations/discover/application/helpers/get_switch_index_pattern_app_state.ts public/kibana-integrations/discover/application/helpers/popularize_field.ts public/components/overview/compliance-table/components/requirement-flyout/index.ts public/components/overview/vulnerabilities/common/hooks/useCheckIndexFields.tsx public/kibana-integrations/discover/application/components/doc/doc.tsx public/kibana-integrations/discover/application/components/doc/use_es_doc_search.ts public/kibana-integrations/discover/application/components/help_menu/help_menu_util.js public/kibana-integrations/discover/application/components/sidebar/discover_index_pattern_title.tsx public/kibana-integrations/discover/application/components/top_nav/open_search_panel.js public/kibana-integrations/discover/application/components/top_nav/show_open_search_panel.js public/controllers/management/components/management/configuration/util-components/code-viewer.js public/controllers/management/components/management/groups/utils/columns-main.js public/kibana-integrations/discover/application/angular/doc_table/lib/pager/index.js ``` In the test `knip` configuration, there are some ignored paths, so the unused files count could be higher.

Conclusion

The linter import/no-unused-modules could ensure the source code does not include unused modules. This could reduce the files on the repository but it requires we use the linter in our development and checks workflow.

For another hand, knip gets unused files (and more features) that helps to remove unused code. If we decide to add this tool, we could create a script (in the package.json) to run it.