-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·183 lines (155 loc) · 6.18 KB
/
cli.py
File metadata and controls
executable file
·183 lines (155 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Defines the main command-line interface (CLI) for CodeSecTools.
This script sets up the main entry point for the application using `typer`.
It dynamically discovers and adds CLI commands from all available SAST tools.
"""
import importlib.metadata
import os
from typing import Optional
import typer
import typer.core
from click import Choice
from rich import print
from rich.table import Table
from typing_extensions import Annotated
from codesectools.datasets import DATASETS_ALL
from codesectools.datasets.core.dataset import Dataset
from codesectools.sasts import SASTS_ALL
from codesectools.sasts.all.cli import build_cli as build_all_sast_cli
from codesectools.sasts.core.sast.requirements import DownloadableRequirement
cli = typer.Typer(name="cstools", no_args_is_help=True)
def version_callback(value: bool) -> None:
"""Print the application version and exit."""
if value:
print(importlib.metadata.version("codesectools"))
raise typer.Exit()
@cli.callback()
def main(
debug: Annotated[
bool,
typer.Option(
"-d",
"--debug",
help="Show debugging messages and disable pretty exceptions.",
),
] = False,
version: Annotated[
Optional[bool],
typer.Option(
"-v",
"--version",
help="Show the tool's version.",
callback=version_callback,
),
] = None,
) -> None:
"""CodeSecTools: A framework for code security that provides abstractions for static analysis tools and datasets to support their integration, testing, and evaluation."""
if debug:
os.environ["DEBUG"] = "1"
os.environ["_TYPER_STANDARD_TRACEBACK"] = "1"
@cli.command()
def status(
sasts: Annotated[bool, typer.Option("--sasts", help="Show sasts only")] = False,
datasets: Annotated[
bool, typer.Option("--datasets", help="Show datasets only")
] = False,
) -> None:
"""Display the availability of SAST tools and datasets."""
if sasts or (not sasts and not datasets):
table = Table(show_lines=True)
table.add_column("SAST", justify="center", no_wrap=True)
table.add_column("Type", justify="center", no_wrap=True)
table.add_column("Status", justify="center", no_wrap=True)
table.add_column("Note", justify="center")
for sast_name, sast_data in SASTS_ALL.items():
if sast_data["status"] == "full":
table.add_row(
sast_name,
sast_data["sast"].__bases__[0].__name__,
"Full ✅",
"[b]Analysis[/b] and [b]result parsing[/b] are available",
)
elif sast_data["status"] == "partial":
table.add_row(
sast_name,
sast_data["sast"].__bases__[0].__name__,
"Partial ⚠️",
f"Only [b]result parsing[/b] is available\nMissing: [red]{sast_data['missing']}[/red]",
)
else:
table.add_row(
sast_name,
sast_data["sast"].__bases__[0].__name__,
"None ❌",
f"[b]Nothing[/b] is available\nMissing: [red]{sast_data['missing']}[/red]",
)
print(table)
if datasets or (not sasts and not datasets):
table = Table(show_lines=True)
table.add_column("Dataset", justify="center", no_wrap=True)
table.add_column("Type", justify="center", no_wrap=True)
table.add_column("Cached", justify="center", no_wrap=True)
table.add_column("Note", justify="center")
for dataset_name, dataset in DATASETS_ALL.items():
if dataset.is_cached():
table.add_row(
dataset_name,
dataset.__bases__[0].__name__,
"✅",
f"Supported languages: [b]{''.join(dataset.supported_languages)}[/b]",
)
else:
table.add_row(
dataset_name,
dataset.__bases__[0].__name__,
"❌",
f"Download with: [i red]cstools download {dataset_name}[/i red]",
)
print(table)
def get_downloadable() -> dict[str, DownloadableRequirement | Dataset]:
"""Identify and collect all missing downloadable resources.
Collects unfulfilled `DownloadableRequirement` instances from all SAST tools
and un-cached `Dataset` instances.
Returns:
A dictionary mapping the resource name to its downloadable object.
"""
downloadable = {}
for _, sast_data in SASTS_ALL.items():
sast = sast_data["sast"]
for req in sast.requirements.all:
if isinstance(req, DownloadableRequirement):
if not req.is_fulfilled() and req.dependencies_fulfilled():
downloadable[req.name] = req
for dataset_name, dataset in DATASETS_ALL.items():
dataset_instance = dataset()
if not dataset.is_cached():
downloadable[dataset_name] = dataset_instance
return downloadable
if DOWNLOADABLE := get_downloadable():
download_hidden = False
download_arg_type = str
download_arg_value = typer.Argument(
click_type=Choice(["all"] + list(DOWNLOADABLE)),
metavar="NAME",
)
else:
download_hidden = True
download_arg_type = Optional[str]
download_arg_value = None
@cli.command(hidden=download_hidden)
def download(name: download_arg_type = download_arg_value, test: bool = False) -> None:
"""Download and install any missing resources that are available for download."""
if name is None:
print("All downloadable resources have been retrieved.")
else:
if name == "all":
targets = DOWNLOADABLE.values()
else:
targets = [DOWNLOADABLE[name]]
for downloadable in targets:
if isinstance(downloadable, DownloadableRequirement):
downloadable.download()
else:
downloadable.download_dataset(test=test)
cli.add_typer(build_all_sast_cli())
for _, sast_data in SASTS_ALL.items():
cli.add_typer(sast_data["cli_factory"].build_cli())