forked from gane5hvarma/panther
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added new metrics * fixed interval issue for aggregate metric * unstaged panther_config changes * addressed initial PR feedback * addressed pr feedback * final pr feedback changes
- Loading branch information
1 parent
f0a44a4
commit 941701a
Showing
12 changed files
with
646 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package api | ||
|
||
/** | ||
* Panther is a Cloud-Native SIEM for the Modern Security Team. | ||
* Copyright (C) 2020 Panther Labs Inc | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import ( | ||
"math" | ||
"strconv" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/cloudwatch" | ||
"go.uber.org/zap" | ||
|
||
analysismodels "github.com/panther-labs/panther/api/gateway/analysis/models" | ||
"github.com/panther-labs/panther/api/lambda/metrics/models" | ||
) | ||
|
||
const alertsMetric = "AlertsCreated" | ||
|
||
// getAlertsBySeverity returns the count of log analysis alerts generated by severity | ||
// | ||
// This is a time series metric. | ||
func getAlertsBySeverity(input *models.GetMetricsInput, output *models.GetMetricsOutput) error { | ||
// Build the query based on the applicable metric dimensions | ||
analysisSeverities := []*string{ | ||
aws.String(string(analysismodels.SeverityCRITICAL)), | ||
aws.String(string(analysismodels.SeverityHIGH)), | ||
aws.String(string(analysismodels.SeverityMEDIUM)), | ||
aws.String(string(analysismodels.SeverityLOW)), | ||
aws.String(string(analysismodels.SeverityINFO)), | ||
} | ||
|
||
queries := make([]*cloudwatch.MetricDataQuery, 0, len(analysisSeverities)) | ||
for i, severity := range analysisSeverities { | ||
queries = append(queries, &cloudwatch.MetricDataQuery{ | ||
Id: aws.String("query" + strconv.Itoa(i)), | ||
Label: severity, | ||
MetricStat: &cloudwatch.MetricStat{ | ||
Metric: &cloudwatch.Metric{ | ||
Dimensions: []*cloudwatch.Dimension{ | ||
{ | ||
Name: aws.String("AnalysisType"), | ||
Value: aws.String("Rule"), | ||
}, | ||
{ | ||
Name: aws.String("Severity"), | ||
Value: severity, | ||
}, | ||
}, | ||
MetricName: aws.String(alertsMetric), | ||
Namespace: aws.String(input.Namespace), | ||
}, | ||
Period: aws.Int64(input.IntervalMinutes * 60), // number of seconds, must be multiple of 60 | ||
Stat: aws.String("Sum"), | ||
Unit: aws.String("Count"), | ||
}, | ||
}) | ||
} | ||
zap.L().Debug("prepared metric queries", zap.Any("queries", queries), zap.Any("toDate", input.ToDate), zap.Any("fromDate", input.FromDate)) | ||
|
||
metricData, err := getMetricData(input, queries) | ||
if err != nil { | ||
return err | ||
} | ||
values, timestamps := normalizeTimeStamps(input, metricData) | ||
|
||
output.AlertsBySeverity = &models.MetricResult{ | ||
SeriesData: models.TimeSeriesMetric{ | ||
Timestamps: timestamps, | ||
Series: values, | ||
}, | ||
} | ||
return nil | ||
} | ||
|
||
// getTotalAlertsDelta the total count of alerts in the given time period, and additionally the | ||
// total count of alerts in the previous time period of equal size to the requested time period. | ||
// | ||
// This is a single value metric. | ||
func getTotalAlertsDelta(input *models.GetMetricsInput, output *models.GetMetricsOutput) error { | ||
// Whatever time frame we're supposed to look at, actually look at double that time frame | ||
timeFrame := input.FromDate.Sub(input.ToDate) | ||
dStart := input.FromDate.Add(timeFrame) | ||
|
||
// Construct a new input model here because we want to overwrite the FromDate and IntervalHours | ||
// fields without potentially affecting any other metrics that need to be generated. | ||
dInput := &models.GetMetricsInput{ | ||
FromDate: dStart, | ||
ToDate: input.ToDate, | ||
IntervalMinutes: int64(math.Abs(timeFrame.Minutes())), | ||
} | ||
|
||
// Build the query based on the applicable metric dimensions | ||
queries := []*cloudwatch.MetricDataQuery{ | ||
{ | ||
Id: aws.String("query"), | ||
Label: aws.String("alerts"), | ||
MetricStat: &cloudwatch.MetricStat{ | ||
Metric: &cloudwatch.Metric{ | ||
Dimensions: []*cloudwatch.Dimension{ | ||
{ | ||
Name: aws.String("AnalysisType"), | ||
Value: aws.String("Rule"), | ||
}, | ||
}, | ||
MetricName: aws.String(alertsMetric), | ||
Namespace: aws.String(input.Namespace), | ||
}, | ||
Period: aws.Int64(dInput.IntervalMinutes * 60), // number of seconds, must be multiple of 60 | ||
Stat: aws.String("Sum"), | ||
Unit: aws.String("Count"), | ||
}, | ||
}, | ||
} | ||
|
||
zap.L().Debug("prepared metric queries", zap.Any("queries", queries), zap.Any("toDate", input.ToDate), zap.Any("fromDate", input.FromDate)) | ||
|
||
metricData, err := getMetricData(dInput, queries) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
values, _ := normalizeTimeStamps(dInput, metricData) | ||
output.TotalAlertsDelta = &models.MetricResult{ | ||
SingleValue: []models.SingleMetric{ | ||
{ | ||
Label: aws.String("Current Period"), | ||
Value: values[0].Values[0], | ||
}, | ||
{ | ||
Label: aws.String("Previous Period"), | ||
Value: values[0].Values[1], | ||
}, | ||
}, | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.