Skip to content

Commit 6b285c7

Browse files
committed
output engine: threats: Generate THREATS.md file
Signed-off-by: John Andersen <[email protected]>
1 parent 3eaa2ab commit 6b285c7

File tree

4 files changed

+189
-15
lines changed

4 files changed

+189
-15
lines changed

cve_bin_tool/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ def main(argv=None):
186186
"-f",
187187
"--format",
188188
action="store",
189-
choices=["csv", "json", "console", "html", "pdf"],
189+
choices=["csv", "json", "console", "html", "pdf", "md"],
190190
help="update output format (default: console)",
191191
default="console",
192192
)

cve_bin_tool/output_engine/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from ..version import VERSION
1818
from .console import output_console
1919
from .html import output_html
20+
from .threats import output_threats
2021
from .util import (
2122
add_extension_if_not,
2223
format_output,
@@ -390,6 +391,22 @@ def output_cves(self, outfile, output_type="console"):
390391
self.logger,
391392
outfile,
392393
)
394+
elif output_type == "md":
395+
if self.filename.endswith("THREATS.md"):
396+
output_threats(
397+
self.all_cve_data,
398+
self.scanned_dir,
399+
self.filename,
400+
self.themes_dir,
401+
self.total_files,
402+
self.products_with_cve,
403+
self.products_without_cve,
404+
self.merge_report,
405+
self.logger,
406+
outfile,
407+
)
408+
else:
409+
raise NotImplementedError("Only generating THREATS.md currently supported. Attempted output to {self.filename!r}.")
393410
else: # console, or anything else that is unrecognised
394411
output_console(
395412
self.all_cve_data,

cve_bin_tool/output_engine/threats.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 ------------------

doc/DATA_FLOW_SCANNER.rst

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,53 @@ shouldi were appropriate.
77

88
References:
99

10-
- https://intel.github.io/dffml/shouldi.html
11-
- https://intel.github.io/dffml/examples/shouldi.html
12-
- https://intel.github.io/dffml/examples/dataflows.html
10+
- shouldi
11+
12+
- https://intel.github.io/dffml/shouldi.html
13+
- https://intel.github.io/dffml/examples/shouldi.html
14+
- https://intel.github.io/dffml/examples/dataflows.html
15+
16+
- Alice/Open Architecture
17+
18+
- https://github.com/intel/dffml/discussions/1369
1319

1420
.. note::
1521

1622
Tested against development version of DFFML
1723
9ddcdfd6f8de743f87d41b74d53fde2c182861c7
1824

19-
2025
Install
2126
*******
2227

2328
Install with extra
2429

2530
.. code-block:: console
2631
27-
$ python -m pip install cve-bin-tool[dataflow]
32+
$ python -m pip install cve-bin-tool[dataflow]
33+
34+
Scan with Data Flow Orchestrator
35+
********************************
36+
37+
Scan files as usual (alice is a shortname for the Open Architecture format).
38+
39+
.. code-block:: console
40+
41+
$ cve-bin-tool --engine dataflow .
2842
29-
Examples
30-
********
43+
Output To Open Architecture Format
44+
**********************************
3145

32-
Scan files as usual
46+
When running the scan we can output to the Open Architecture format (aka Alice).
3347

3448
.. code-block:: console
35-
:test:
3649
37-
$ python -m cve_bin_tool.scanners.dataflow .
50+
$ cve-bin-tool --engine dataflow --format alice --output-file scan.alice .
51+
52+
Supplement Threat Model with Scan Data
53+
**************************************
3854

39-
Scan a git repo. Currently runs ``shoudli`` scanning. (In future we can add
40-
overlays to run the build then scan).
55+
When running the scan we can output to the Open Architecture format (aka Alice).
4156

4257
.. code-block:: console
43-
:test:
4458
45-
$ python -m cve_bin_tool.scanners.dataflow https://github.com/intel/cve-bin-tool
59+
$ cve-bin-tool --input-file scan.alice --format md --output-file THREATS.md

0 commit comments

Comments
 (0)