|
| 1 | +# Copyright (C) 2021 Intel Corporation |
| 2 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +import os |
| 5 | +from typing import Dict, List, Union |
| 6 | + |
| 7 | + |
| 8 | +from ..merge import MergeReports |
| 9 | + |
| 10 | +from ..log import LOGGER |
| 11 | +from ..util import CVEData, ProductInfo |
| 12 | + |
| 13 | + |
| 14 | +def output_threats( |
| 15 | + all_cve_data: Dict[ProductInfo, CVEData], |
| 16 | + scanned_dir: str, |
| 17 | + filename: str, |
| 18 | + theme_dir: str, |
| 19 | + total_files: int, |
| 20 | + products_with_cve: int, |
| 21 | + products_without_cve: int, |
| 22 | + merge_report: Union[None, MergeReports], |
| 23 | + logger: LOGGER, |
| 24 | + outfile, |
| 25 | +): |
| 26 | + """Returns a THREATS.md report including depedencies found""" |
| 27 | + from pprint import pprint |
| 28 | + pprint(locals()) |
| 29 | + |
| 30 | + import textwrap |
| 31 | + outfile.write( |
| 32 | + textwrap.dedent( |
| 33 | + f""" |
| 34 | + # Threat Model |
| 35 | + """ |
| 36 | + ) |
| 37 | + ) |
| 38 | + |
| 39 | + # ------------------ BEGIN MERMAID OUTPUT ------------------ |
| 40 | + outfile.write( |
| 41 | + textwrap.dedent( |
| 42 | + """ |
| 43 | +
|
| 44 | + ```mermaid |
| 45 | + """ |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + # Write out the mermaid diagram |
| 50 | + import sys |
| 51 | + import asyncio |
| 52 | + import contextlib |
| 53 | + import dffml |
| 54 | + import dffml.cli.dataflow |
| 55 | + |
| 56 | + |
| 57 | + # TODO Check if dataflow extra is installed. Build dataflows from scan |
| 58 | + # results. Generate mermaid daigrams from flows. |
| 59 | + import cve_bin_tool.scanners.dataflow |
| 60 | + |
| 61 | + # The overlayed keyword arguements of fields within to be created |
| 62 | + field_modifications = { |
| 63 | + "dataflow": {"default_factory": lambda: cve_bin_tool.scanners.dataflow.COLLECTOR_DATAFLOW}, |
| 64 | + "simple": {"default": True}, |
| 65 | + "stages": {"default_factory": lambda: [dffml.Stage.PROCESSING.value]}, |
| 66 | + } |
| 67 | + # Create a derived class |
| 68 | + DiagramForMyDataFlow = dffml.cli.dataflow.Diagram.subclass( |
| 69 | + "DiagramForMyDataFlow", field_modifications, |
| 70 | + ) |
| 71 | + print(DiagramForMyDataFlow) |
| 72 | + # <class 'dffml.util.cli.cmd.DiagramForMyDataFlow'> |
| 73 | + print(DiagramForMyDataFlow.CONFIG) |
| 74 | + # <class 'types.DiagramForMyDataFlowConfig'> |
| 75 | + with contextlib.redirect_stdout(outfile): |
| 76 | + asyncio.run(DiagramForMyDataFlow._main()) |
| 77 | + |
| 78 | + |
| 79 | + outfile.write( |
| 80 | + textwrap.dedent( |
| 81 | + """ |
| 82 | + ``` |
| 83 | + """ |
| 84 | + ) |
| 85 | + ) |
| 86 | + # ------------------ END MERMAID OUTPUT ------------------ |
| 87 | + |
| 88 | + # ------------------ BEGIN OPEN ARCHITECTURE OUTPUT ------------------ |
| 89 | + outfile.write( |
| 90 | + textwrap.dedent( |
| 91 | + f""" |
| 92 | + ```json |
| 93 | + """ |
| 94 | + ) |
| 95 | + ) |
| 96 | + |
| 97 | + # Write out the mermaid diagram |
| 98 | + import sys |
| 99 | + import asyncio |
| 100 | + import contextlib |
| 101 | + import dffml |
| 102 | + import dffml.cli.dataflow |
| 103 | + import dffml.service.dev |
| 104 | + |
| 105 | + |
| 106 | + import dffml_config_yaml.configloader |
| 107 | + |
| 108 | + |
| 109 | + # TODO Check if dataflow extra is installed. Build dataflows from scan |
| 110 | + # results. Generate mermaid daigrams from flows. |
| 111 | + import cve_bin_tool.scanners.dataflow |
| 112 | + |
| 113 | + # The overlayed keyword arguements of fields within to be created |
| 114 | + field_modifications = { |
| 115 | + "export": {"default_factory": lambda: "cve_bin_tool.scanners.dataflow:COLLECTOR_DATAFLOW"}, |
| 116 | + # "configloader": {"default_factory": lambda: dffml_config_yaml.configloader.YamlConfigLoader}, |
| 117 | + "configloader": {"default_factory": lambda: dffml.JSONConfigLoader}, |
| 118 | + } |
| 119 | + |
| 120 | + # Create a derived class |
| 121 | + ExportForMyDataFlow = dffml.service.dev.Export.subclass( |
| 122 | + "ExportForMyDataFlow", field_modifications, |
| 123 | + ) |
| 124 | + print(ExportForMyDataFlow) |
| 125 | + # <class 'dffml.util.cli.cmd.ExportForMyDataFlow'> |
| 126 | + print(ExportForMyDataFlow.CONFIG) |
| 127 | + # <class 'types.ExportForMyDataFlowConfig'> |
| 128 | + import io |
| 129 | + a_out = io.StringIO() |
| 130 | + a_out.buffer = io.BytesIO() |
| 131 | + with contextlib.redirect_stdout(a_out): |
| 132 | + asyncio.run(ExportForMyDataFlow._main()) |
| 133 | + |
| 134 | + import json |
| 135 | + outfile.write(json.dumps(json.loads(a_out.buffer.getvalue().decode()), indent=4)) |
| 136 | + outfile.write( |
| 137 | + textwrap.dedent( |
| 138 | + """ |
| 139 | + ``` |
| 140 | + """ |
| 141 | + ) |
| 142 | + ) |
| 143 | + # ------------------ END OPEN ARCHITECTURE OUTPUT ------------------ |
0 commit comments