Skip to content

Commit

Permalink
feat(api): Do not allow user to save trigger with enormous TTL
Browse files Browse the repository at this point in the history
Closes #190
  • Loading branch information
beevee committed Mar 30, 2020
1 parent e28101c commit 68d7c97
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/dto/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ func (trigger *Trigger) Bind(request *http.Request) error {
return err
}

if err := checkTTLSanity(trigger, metricsSource); err != nil {
return api.ErrInvalidRequestContent{ValidationError: err}
}

if err := resolvePatterns(request, trigger, &triggerExpression, metricsSource); err != nil {
return err
}
Expand All @@ -146,6 +150,19 @@ func (trigger *Trigger) Bind(request *http.Request) error {
return nil
}

func checkTTLSanity(trigger *Trigger, metricsSource metricSource.MetricSource) error {
maximumAllowedTTL := metricsSource.GetMetricsTTLSeconds()

if trigger.TTL > maximumAllowedTTL {
triggerType := "local"
if trigger.IsRemote {
triggerType = "remote"
}
return fmt.Errorf("TTL for %s trigger can't be more than %d seconds", triggerType, maximumAllowedTTL)
}
return nil
}

func resolvePatterns(request *http.Request, trigger *Trigger, expressionValues *expression.TriggerExpression, metricsSource metricSource.MetricSource) error {
now := time.Now().Unix()
targetNum := 1
Expand Down
1 change: 1 addition & 0 deletions api/dto/triggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func TestExpressionModeMultipleTargetsWarnValue(t *testing.T) {
sourceProvider := metricSource.CreateMetricSourceProvider(localSource, remoteSource)

localSource.EXPECT().IsConfigured().Return(true, nil).AnyTimes()
localSource.EXPECT().GetMetricsTTLSeconds().Return(int64(3600)).AnyTimes()
localSource.EXPECT().Fetch(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(fetchResult, nil).AnyTimes()
fetchResult.EXPECT().GetPatterns().Return(make([]string, 0), nil).AnyTimes()
fetchResult.EXPECT().GetMetricsData().Return([]*metricSource.MetricData{metricSource.MakeMetricData("", []float64{}, 0, 0)}).AnyTimes()
Expand Down
5 changes: 5 additions & 0 deletions metric_source/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (local *Local) Fetch(target string, from int64, until int64, allowRealTimeA
return result, nil
}

// GetMetricsTTLSeconds returns metrics lifetime in Redis
func (local *Local) GetMetricsTTLSeconds() int64 {
return local.dataBase.GetMetricsTTLSeconds()
}

// IsConfigured always returns true. It easy to configure local source =)
func (local *Local) IsConfigured() (bool, error) {
return true, nil
Expand Down
5 changes: 5 additions & 0 deletions metric_source/remote/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ func (remote *Remote) Fetch(target string, from, until int64, allowRealTimeAlert
return &fetchResult, nil
}

// GetMetricsTTLSeconds returns maximum time interval that we are allowed to fetch from remote
func (remote *Remote) GetMetricsTTLSeconds() int64 {
return int64(remote.config.MetricsTTL.Seconds())
}

// IsConfigured returns false in cases that user does not properly configure remote settings like graphite URL
func (remote *Remote) IsConfigured() (bool, error) {
if remote.config.isEnabled() {
Expand Down
1 change: 1 addition & 0 deletions metric_source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package metricSource
// MetricSource implements graphite metrics source abstraction
type MetricSource interface {
Fetch(target string, from int64, until int64, allowRealTimeAlerting bool) (FetchResult, error)
GetMetricsTTLSeconds() int64
IsConfigured() (bool, error)
}

Expand Down
14 changes: 14 additions & 0 deletions mock/metric_source/source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 68d7c97

Please sign in to comment.