|
34 | 34 | SoSCollectorDirectory) |
35 | 35 | from sos.cleaner.archives.generic import DataDirArchive, TarballArchive |
36 | 36 | from sos.cleaner.archives.insights import InsightsArchive |
37 | | -from sos.utilities import (get_human_readable, import_module, ImporterHelper, |
38 | | - file_is_certificate, is_executable) |
| 37 | +from sos.utilities import (get_human_readable, import_module, ImporterHelper) |
39 | 38 |
|
40 | 39 |
|
41 | 40 | # an auxiliary method to kick off child processes over its instances |
42 | 41 | def obfuscate_arc_files(arc, flist): |
43 | 42 | return arc.obfuscate_arc_files(flist) |
44 | 43 |
|
45 | 44 |
|
46 | | - |
47 | 45 | class SoSCleaner(SoSComponent): |
48 | 46 | """ |
49 | 47 | This function is designed to obfuscate potentially sensitive information |
@@ -588,34 +586,9 @@ def obfuscate_report_paths(self): |
588 | 586 | "WARNING: binary files that potentially contain sensitive " |
589 | 587 | "information will NOT be removed from the final archive\n" |
590 | 588 | ) |
591 | | - if (self.opts.treat_certificates == "obfuscate" |
592 | | - and not is_executable("openssl")): |
593 | | - self.opts.treat_certificates = "remove" |
594 | | - self.ui_log.warning( |
595 | | - "WARNING: No `openssl` command available. Replacing " |
596 | | - "`--treat-certificates` from `obfuscate` to `remove`." |
597 | | - ) |
598 | | - if self.opts.treat_certificates == "obfuscate": |
599 | | - self.ui_log.warning( |
600 | | - "WARNING: certificate files that potentially contain " |
601 | | - "sensitive information will be CONVERTED to text and " |
602 | | - "OBFUSCATED in the final archive.\n" |
603 | | - ) |
604 | | - elif self.opts.treat_certificates == "keep": |
605 | | - self.ui_log.warning( |
606 | | - "WARNING: certificate files that potentially contain " |
607 | | - "sensitive information will be KEPT in the final " |
608 | | - "archive as is.\n" |
609 | | - ) |
610 | | - elif self.opts.treat_certificates == "remove": |
611 | | - self.ui_log.warning( |
612 | | - "WARNING: certificate files that potentially contain " |
613 | | - "sensitive information will be REMOVED in the final " |
614 | | - "archive.\n" |
615 | | - ) |
616 | | - for report_path in self.report_paths: |
617 | | - self.ui_log.info(f"Obfuscating {report_path.archive_path}") |
618 | | - self.obfuscate_report(report_path) |
| 589 | + pool = ThreadPoolExecutor(self.opts.jobs) |
| 590 | + pool.map(self.obfuscate_report, self.report_paths, chunksize=1) |
| 591 | + pool.shutdown(wait=True) |
619 | 592 | # finally, obfuscate the nested archive if one exists |
620 | 593 | if self.nested_archive: |
621 | 594 | self._replace_obfuscated_archives() |
@@ -732,63 +705,31 @@ def obfuscate_report(self, archive): # pylint: disable=too-many-branches |
732 | 705 | if not archive.is_extracted: |
733 | 706 | archive.extract() |
734 | 707 | archive.report_msg("Beginning obfuscation...") |
| 708 | + |
735 | 709 | for fname in archive.get_file_list(): |
736 | 710 | short_name = fname.split(archive.archive_name + '/')[1] |
737 | | - is_certificate = file_is_certificate(fname) |
738 | | - if is_certificate: |
739 | | - if is_certificate == "certificatekey": |
740 | | - # Always remove certificate Key files |
741 | | - archive.remove_file(short_name) |
742 | | - continue |
743 | | - if self.opts.treat_certificates == "keep": |
744 | | - continue |
745 | | - if self.opts.treat_certificates == "remove": |
746 | | - archive.remove_file(short_name) |
747 | | - continue |
748 | | - if self.opts.treat_certificates == "obfuscate": |
749 | | - archive.certificate_to_text(fname) |
750 | | - |
751 | | - file_list = list(archive.get_files()) |
752 | | - # we can't call simple |
753 | | - # executor.map(archive.obfuscate_arc_files,archive.get_files()) |
754 | | - # because a child process does not carry forward internal changes |
755 | | - # (e.g. mappings' datasets) from one call of obfuscate_arc_files |
756 | | - # method to another. Each obfuscate_arc_files method starts with |
757 | | - # vanilla parent archive, that is initialised *once* at its |
758 | | - # beginning via initializer=archive.load_parser_entries |
759 | | - # - but not afterwards.. |
760 | | - # |
761 | | - # So we must pass list of all files for each worker at the |
762 | | - # beginning. This means less granularity of the child processes |
763 | | - # work (one worker can finish much sooner than the other), but |
764 | | - # it is the best we can have (or have found) |
765 | | - # |
766 | | - # At least, the "file_list[i::self.opts.jobs]" means subsequent |
767 | | - # files (speculativelly of similar size and content) are |
768 | | - # distributed to different processes, which attempts to split the |
769 | | - # load evenly. Yet better approach might be reorderig file_list |
770 | | - # based on files' sizes. |
771 | | - |
772 | | - files_obfuscated_count = total_sub_count = removed_file_count = 0 |
773 | | - archive_list = [archive for i in range(self.opts.jobs)] |
774 | | - with ProcessPoolExecutor( |
775 | | - max_workers=self.opts.jobs, |
776 | | - initializer=archive.load_parser_entries) as executor: |
777 | | - futures = executor.map(obfuscate_arc_files, archive_list, |
778 | | - [file_list[i::self.opts.jobs] for i in |
779 | | - range(self.opts.jobs)]) |
780 | | - for (foc, tsc, rfc) in futures: |
781 | | - files_obfuscated_count += foc |
782 | | - total_sub_count += tsc |
783 | | - removed_file_count += rfc |
784 | | - |
785 | | - # As there is no easy way to get dataset dicts from child |
786 | | - # processes' mappings, we can reload our own parent-process |
787 | | - # archive from the disk files. The trick is that sequence of |
788 | | - # files/entries is the source of truth of *sequence* of calling |
789 | | - # *all* mapping.all(item) methods - so replaying this will |
790 | | - # generate the right datasets! |
791 | | - archive.load_parser_entries() |
| 711 | + if archive.should_skip_file(short_name): |
| 712 | + continue |
| 713 | + if (not self.opts.keep_binary_files and |
| 714 | + archive.should_remove_file(short_name)): |
| 715 | + # We reach this case if the option --keep-binary-files |
| 716 | + # was not used, and the file is in a list to be removed |
| 717 | + archive.remove_file(short_name) |
| 718 | + continue |
| 719 | + if (self.opts.keep_binary_files and |
| 720 | + (file_is_binary(fname) or |
| 721 | + archive.should_remove_file(short_name))): |
| 722 | + # We reach this case if the option --keep-binary-files |
| 723 | + # is used. In this case we want to make sure |
| 724 | + # the cleaner doesn't try to clean a binary file |
| 725 | + continue |
| 726 | + try: |
| 727 | + count = self.obfuscate_file(fname, short_name, |
| 728 | + archive.archive_name) |
| 729 | + if count: |
| 730 | + archive.update_sub_count(short_name, count) |
| 731 | + except Exception as err: |
| 732 | + self.log_debug(f"Unable to parse file {short_name}: {err}") |
792 | 733 |
|
793 | 734 | try: |
794 | 735 | self.obfuscate_directory_names(archive) |
|
0 commit comments