Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add export-csv option #62

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ hardeneks [OPTIONS]
* `--namespace TEXT`: Namespace to be checked (default is all namespaces)
* `--config TEXT`: Path to a hardeneks config file
* `--export-txt TEXT`: Export the report in txt format
* `--export-csv TEXT`: Export the report in csv format
* `--export-html TEXT`: Export the report in html format
* `--export-json TEXT`: Export the report in json format
* `--insecure-skip-tls-verify`: Skip TLS verification
29 changes: 29 additions & 0 deletions hardeneks/__init__.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
import yaml
import json
from collections import defaultdict
import csv

from botocore.exceptions import EndpointConnectionError
import boto3
@@ -101,6 +102,27 @@ def ndd():
with open(json_path, "w", encoding="utf-8") as f:
json.dump(json_blob, f, ensure_ascii=False, indent=4)

def _export_csv(rules: list, csv_path=str):
csv_data = []

for rule in rules:
csv_row = {
"Type": rule._type,
"Pillar": rule.pillar,
"Section": rule.section,
"Message": rule.message,
"Status": rule.result.status,
"Resources": ', '.join(rule.result.resources) if rule.result.resources else '',
"Resource Type": rule.result.resource_type,
"Namespace": rule.result.namespace,
"Resolution": rule.url,
}
csv_data.append(csv_row)

with open(csv_path, "w", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=csv_data[0].keys())
writer.writeheader()
writer.writerows(csv_data)

def print_consolidated_results(rules: list):

@@ -159,6 +181,10 @@ def run_hardeneks(
default=None,
help="Export the report in txt format",
),
export_csv: str = typer.Option(
default=None,
help="Export the report in csv format",
),
export_html: str = typer.Option(
default=None,
help="Export the report in html format",
@@ -188,6 +214,7 @@ def run_hardeneks(
namespace (str): Specific namespace to be checked
config (str): Path to hardeneks config file
export-txt (str): Export the report in txt format
export-csv (str): Export the report in csv format
export-html (str): Export the report in html format
export-json (str): Export the report in json format
insecure-skip-tls-verify (str): Skip tls verification
@@ -254,6 +281,8 @@ def run_hardeneks(

if export_txt:
console.save_text(export_txt)
if export_csv:
_export_csv(results, export_csv)
if export_html:
console.save_html(export_html)
if export_json: