From 36ca311970d6674855b4d72084a76f9180afe501 Mon Sep 17 00:00:00 2001 From: priv <140729444+scriptprivate@users.noreply.github.com> Date: Mon, 12 Aug 2024 03:30:09 -0300 Subject: [PATCH] feat: add debug logging and improve error handling - add debug statements for severity counts, limits and threshold checks - improve error handling for API request failures - refactor threshold checking logic - add more detailed output for threshold violations --- security-gate.pl | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/security-gate.pl b/security-gate.pl index ca8a580..d96bcab 100644 --- a/security-gate.pl +++ b/security-gate.pl @@ -57,12 +57,29 @@ sub main { print "\n"; + print "Debug: Severity counts: " . join(", ", map {"$_: $severity_counts{$_}"} @severities) . "\n"; + print "Debug: Severity limits: " . join(", ", map {"$_: $severity_limits{$_}"} @severities) . "\n"; + + my $threshold_exceeded = 0; foreach my $severity (@severities) { + print "Debug: Checking $severity - Count: $severity_counts{$severity}, Limit: $severity_limits{$severity}\n"; if ($severity_counts{$severity} > $severity_limits{$severity}) { - print "[+] More than $severity_limits{$severity} $severity security alerts found. Finalizing the process with error.\n"; - return 1; + print "[+] More than $severity_limits{$severity} $severity security alerts found.\n"; + $threshold_exceeded = 1; } } + + print "Debug: Threshold exceeded: $threshold_exceeded\n"; + + if ($threshold_exceeded) { + print "Finalizing the process with error.\n"; + return 1; + } + } + + else { + print "Error: Unable to fetch alerts. HTTP status code: " . $request->code() . "\n"; + return 1; } return 0; @@ -89,7 +106,8 @@ sub main { if ($ENV{TEST_MODE}) { main(); -} +} + else { exit main(); }