Added rule icons

This commit is contained in:
Michael Chen 2022-12-13 17:51:01 +01:00
parent b4f5e3ebd4
commit 62c96fcbf6
Signed by: cnml
GPG Key ID: 5845BF3F82D5F629

View File

@ -4,7 +4,7 @@ import json
import itertools
import yaml
import jsonschema
from typing import Any, List, NotRequired, Optional, TypedDict
from typing import Any, Dict, List, Literal, NotRequired, Optional, TypedDict
import requests
try:
from yachalk import chalk
@ -220,8 +220,10 @@ class Artifact(TypedDict):
lines: NotRequired[list[int]]
repository: NotRequired[str]
RuleStatus = Literal["disregarded", "observed", "not applicable", "unknown"]
class SecurityRule(TypedDict):
status: str
status: RuleStatus
argument: str | list[str]
artifacts: NotRequired[list[Artifact]]
@ -322,7 +324,7 @@ def rule_to_string(info: ModelInformation, id: int, rule: SecurityRule | None):
return ""
argument = rule['argument']
argument = argument if isinstance(argument, str) else "".join(f"\n1. {arg}" for arg in argument)
text = f"""#### Rule {id}: {rule_names[id]}
text = f"""#### Rule {id}: {rule_names[id]} {{#rule{id:02}}}
This rule is {rule['status']}: {argument}"""
artifacts = rule.get("artifacts", [])
@ -334,8 +336,18 @@ Artifacts:
return text
def write_security_rules(info: ModelInformation, security_rules: dict[int, SecurityRule]):
icons: Dict[RuleStatus | str, str] = {
'disregarded': '<i class="fa fa-exclamation-circle" style="color: #d72b28;"></i>',
'observed': '<i class="fa fa-check-square-o" style="color: #6be16d;"></i>',
'not applicable': '<i class="fa fa-info-circle" style="color: #31708;"></i>',
'unknown': '<i class="fa fa-warning" style="color: #bfc600;"></i>',
}
return f"""## Security Rules
{" | ".join(f"R{i}" for i in range(1, 19))}
{" | ".join("--" for _ in range(1, 19))}
{" | ".join(f'<a href="#rule{i:02}">{icons[security_rules.get(i, {"status": "unknown"})["status"]]}</a>' for i in range(1, 19))}
### Authentication / Authorization
{(chr(10)*2).join(rule_to_string(info, i, security_rules.get(i)) for i in range(1, 7))}