Skip to content

Commit

Permalink
Fixing K8s defect (#6)
Browse files Browse the repository at this point in the history
* Issue-5

* Issue-5
  • Loading branch information
fatihtokus authored Jul 24, 2023
1 parent ff225b4 commit 495b56c
Show file tree
Hide file tree
Showing 5 changed files with 1,040 additions and 99 deletions.
6 changes: 3 additions & 3 deletions plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name: "scan2html"
repository: github.com/fatihtokus/scan2html
version: "0.1.1"
version: "0.2.0"
usage: scan targets into a smart html file
description: |-
A Trivy plugin that scans and outputs the results to a html file.
trivy scan2html [-h,--help] command target filename
platforms:
- selector:
os: windows
uri: https://github.com/fatihtokus/scan2html/releases/download/v0.1.1/scan2html.tar.gz
uri: https://github.com/fatihtokus/scan2html/releases/download/v0.2.0/scan2html.tar.gz
bin: ./scan2html.sh
-
uri: https://github.com/fatihtokus/scan2html/releases/download/v0.1.1/scan2html.tar.gz
uri: https://github.com/fatihtokus/scan2html/releases/download/v0.2.0/scan2html.tar.gz
bin: ./scan2html
128 changes: 84 additions & 44 deletions report_template.html
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ <h1>Trivy Report <span class="_scanner"></span> <span class='_reportTime'></span
</div>
</footer>
</div>

<script>
var selectedReportTableSelector = '._vulnerabilityTable';

Expand All @@ -357,12 +358,12 @@ <h1>Trivy Report <span class="_scanner"></span> <span class='_reportTime'></span
return selectedReportTableSelector;
}

function isVulnerabilityTableSeleted() {
function isVulnerabilityTableSelected() {
return selectedReportTable() == '._vulnerabilityTable';
}

function noSelectedReportTable() {
return isVulnerabilityTableSeleted() ? '._misconfigTable' : '._vulnerabilityTable';
return isVulnerabilityTableSelected() ? '._misconfigTable' : '._vulnerabilityTable';
}


Expand All @@ -377,7 +378,7 @@ <h1>Trivy Report <span class="_scanner"></span> <span class='_reportTime'></span
function init() {
initializeReportTitle();
createTableRows();
if(isVulnerabilityTableSeleted()){
if(isVulnerabilityTableSelected()){
initializeVulnerabilityTableFilters();
} else {
initializeMisconfigurationTableFilters();
Expand Down Expand Up @@ -442,56 +443,95 @@ <h1>Trivy Report <span class="_scanner"></span> <span class='_reportTime'></span
}

function vulnerabilitiesOrMisconfigurations() {
return isVulnerabilityTableSeleted() ? vulnerabilities() : misconfigurations();
return isVulnerabilityTableSelected() ? vulnerabilities() : misconfigurations();
}

function vulnerabilities() {
var resultJson1 = [];
resultJson.Results.forEach((result) => {
var target = result.Target;
if(result.Vulnerabilities){
result.Vulnerabilities.forEach((vulnerability) => {
resultJson1.push(
{
"Target": target,
"Library": vulnerability.PkgName,
"Vulnerability": vulnerability.VulnerabilityID,
"Severity": vulnerability.Severity,
"InstalledVersion": vulnerability.InstalledVersion,
"FixedVersion": vulnerability.FixedVersion,
"Title": vulnerability.Title
}
);
});
}
if(resultJson.Results) {
return mapVulnerabilityResults(resultJson.Results);
}

// k8s format
return vulnerabilitiesForK8s();
}

function vulnerabilitiesForK8s() {
var formattedResultJson = [];
resultJson.Vulnerabilities.forEach((topVulnerability) => {
formattedResultJson = formattedResultJson.concat(mapVulnerabilityResults(topVulnerability.Results));
});

return resultJson1;
return formattedResultJson;

}

function mapVulnerabilityResults(results) {
var formattedResultJson = [];
results.forEach((result) => {
var target = result.Target;
if(result.Vulnerabilities){
result.Vulnerabilities.forEach((vulnerability) => {
formattedResultJson.push(
{
"Target": target,
"Library": vulnerability.PkgName,
"Vulnerability": vulnerability.VulnerabilityID,
"Severity": vulnerability.Severity,
"InstalledVersion": vulnerability.InstalledVersion,
"FixedVersion": vulnerability.FixedVersion,
"Title": vulnerability.Title
}
);
});
}
});

return formattedResultJson;

}

function misconfigurations() {
var resultJson1 = [];

resultJson.Results.forEach((result) => {
var target = result.Target;
if(result.Misconfigurations){
result.Misconfigurations.forEach((misconfiguration) => {
resultJson1.push(
{
"Target": target,
"ID": misconfiguration.ID,
"Title": misconfiguration.Title,
"Severity": misconfiguration.Severity,
"Type": misconfiguration.Type,
"Message": misconfiguration.Message
}
);
});
}
if(resultJson.Results) {
return mapMisconfigurationResults(resultJson.Results);
}

// k8s format
return misconfigurationsForK8s();

}

function misconfigurationsForK8s() {
var formattedResultJson = [];
resultJson.Misconfigurations.forEach((topMisconfiguration) => {
formattedResultJson = formattedResultJson.concat(mapMisconfigurationResults(topMisconfiguration.Results));
});

return resultJson1;
return formattedResultJson;
}

function mapMisconfigurationResults(results) {
var formattedResultJson = [];
if(results){
results.forEach((result) => {
var target = result.Target;
if(result.Misconfigurations){
result.Misconfigurations.forEach((misconfiguration) => {
formattedResultJson.push(
{
"Target": target,
"ID": misconfiguration.ID,
"Title": misconfiguration.Title,
"Severity": misconfiguration.Severity,
"Type": misconfiguration.Type,
"Message": misconfiguration.Message
}
);
});
}
});
}

return formattedResultJson;

}

Expand Down Expand Up @@ -605,7 +645,7 @@ <h1>Trivy Report <span class="_scanner"></span> <span class='_reportTime'></span
}

function selectedOptions() {
return isVulnerabilityTableSeleted() ? selectedOptionsForVulnerabilities() : selectedOptionsForMisconfigurations();
return isVulnerabilityTableSelected() ? selectedOptionsForVulnerabilities() : selectedOptionsForMisconfigurations();
}

function filterResults() {
Expand Down Expand Up @@ -647,4 +687,4 @@ <h1>Trivy Report <span class="_scanner"></span> <span class='_reportTime'></span
});
});
}
</script>
</script>
30 changes: 22 additions & 8 deletions scan2html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# help
function help() {
cat << EOS >&2
scan2html v0.0.1
scan2html v0.2.0
Usage: trivy scan2html [-h,--help] command target filename
A Trivy plugin that scans and output the results to a html file.
Expand All @@ -25,15 +25,28 @@ tmp_result="result.json"
function scan {
BASEDIR=$(dirname "$0")
args=("${@:2:$#-2}")
last_arg="${!#}"
reportName="${!#}"
echo "reportName: $reportName"
scanner="$1"
set -- "$1" "${args[@]}" "$last_arg"
echo "$BASEDIR"
echo "$args"
echo "$last_arg"
echo "scanner: scanner"
set -- "$1" "${args[@]}" "$reportName"
echo "BASEDIR: $BASEDIR"
echo "all_params: $@"
echo "args: $args"
echo "reportName: $reportName"
trivy "$scanner" --format json -o "$BASEDIR"/$tmp_result "${args[@]}"
cat "$BASEDIR"/report_template.html >>$last_arg
if [ -f $reportName ]; then
timestamp=$(date +%s)
#timeUserFriendly=`date +"%Y-%m-%d %T"`
timeUserFriendly=$(date +%Y%m%d%H%M%S)
newReportName=${reportName/.html/($timeUserFriendly)}
newReportName=$newReportName".html"
echo "$reportName already exists, creating $newReportName instead!"
reportName=$newReportName
fi
cat "$BASEDIR"/report_template.html >>"$reportName"
{
echo ""
echo "<script>"
echo "const resultJson = "
cat "$BASEDIR"/$tmp_result
Expand All @@ -45,8 +58,9 @@ function scan {
echo "</script>"
echo "</body>"
echo "</html>"
} >>$last_arg
} >>"$reportName"

echo "$reportName has been created!"
trap 'rm -f $tmp_result' EXIT
}

Expand Down
Loading

0 comments on commit 495b56c

Please sign in to comment.