Skip to content

Commit 8a3555b

Browse files
add to tbx security pretty print for markdown
1 parent 2ac5e97 commit 8a3555b

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

.github/workflows/report.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,4 @@ jobs:
5151
poetry run coverage report -- --format markdown >> $GITHUB_STEP_SUMMARY
5252
echo -e "\n\n# Static Code Analysis\n" >> $GITHUB_STEP_SUMMARY
5353
cat .lint.txt >> $GITHUB_STEP_SUMMARY
54+
tbx security prettyprint markdown .security.json >> $GITHUB_STEP_SUMMARY

exasol/toolbox/tools/security.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Iterable,
1616
Tuple,
1717
)
18-
18+
from noxconfig import PROJECT_CONFIG
1919
import typer
2020

2121
stdout = print
@@ -100,6 +100,43 @@ def from_maven(report: str) -> Iterable[Issue]:
100100
)
101101

102102

103+
def from_json(report: str) -> Iterable[Issue]:
104+
report = json.loads(report)
105+
issues = report.get("results", {})
106+
for issue in issues:
107+
references = []
108+
if issue["more_info"]:
109+
references.append(issue["more_info"])
110+
if issue.get("issue_cve", {}).get("link", None):
111+
references.append(issue["issue_cve"]["link"])
112+
if issue.get("issue_cwe", {}).get("link", None):
113+
references.append(issue["issue_cwe"]["link"])
114+
yield Issue(
115+
cve=str(issue.get("issue_cve", {}).get("id", "")),
116+
cwe=str(issue.get("issue_cwe", {}).get("id", "")),
117+
description=issue["issue_text"],
118+
coordinates=issue["filename"].replace(
119+
str(PROJECT_CONFIG.root) + "/", ""
120+
) + f":{issue["line_number"]}:{issue["col_offset"]}:",
121+
references=tuple(references)
122+
)
123+
124+
125+
def issues_to_markdown(issues: Iterable[Issue]) -> str:
126+
markdown_str = ""
127+
markdown_str += "# Security\n\n"
128+
markdown_str += "|File|Cve|Cwe|Details|\n"
129+
markdown_str += "|---|:-:|:-:|---|\n"
130+
for issue in issues:
131+
row = "|" + issue.coordinates + "|"
132+
row += issue.cve + "|"
133+
row += issue.cwe + "|"
134+
for element in issue.references:
135+
row += element + " ,<br>"
136+
markdown_str += row[:-5] + "|\n"
137+
return markdown_str
138+
139+
103140
def security_issue_title(issue: Issue) -> str:
104141
return f"🔐 {issue.cve}: {issue.coordinates}"
105142

@@ -149,15 +186,16 @@ def create_security_issue(issue: Issue, project="") -> Tuple[str, str]:
149186
raise ex
150187

151188
std_err = result.stderr.decode("utf-8")
152-
std_out = result.stdout.decode("utf-8")
189+
std_out = result.stdout.decode("utf-8r")
153190

154191
return std_err, std_out
155192

156193

157194
CLI = typer.Typer()
158195
CVE_CLI = typer.Typer()
196+
PP_CLI = typer.Typer()
159197
CLI.add_typer(CVE_CLI, name="cve", help="Work with CVE's")
160-
198+
CLI.add_typer(PP_CLI, name="prettyprint", help="Prints pretty")
161199

162200
class Format(str, Enum):
163201
Maven = "maven"
@@ -256,6 +294,15 @@ def create(
256294
stdout(format_jsonl(issue_url, issue))
257295

258296

297+
@PP_CLI.command(name="markdown")
298+
def json_issue_to_markdown(
299+
json_file: str = typer.Argument(help="json file with issues to convert"),
300+
) -> None:
301+
with open(json_file, "r") as file:
302+
issues_ = from_json(file.read())
303+
print(issues_to_markdown(issues_))
304+
305+
259306
def format_jsonl(issue_url: str, issue: Issue) -> str:
260307
issue_json = asdict(issue)
261308
issue_json["issue_url"] = issue_url.strip()

0 commit comments

Comments
 (0)