mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Alerting: Validate that tags are 100 characters or less (#62335)
Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
This commit is contained in:
parent
c3b476e1dc
commit
0dacb11a12
@ -106,7 +106,7 @@ func UAEnabled(ctx context.Context) bool {
|
|||||||
return enabled
|
return enabled
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *DashAlertExtractorService) getAlertFromPanels(ctx context.Context, jsonWithPanels *simplejson.Json, validateAlertFunc func(*models.Alert) bool, logTranslationFailures bool, dashAlertInfo DashAlertInfo) ([]*models.Alert, error) {
|
func (e *DashAlertExtractorService) getAlertFromPanels(ctx context.Context, jsonWithPanels *simplejson.Json, validateAlertFunc func(*models.Alert) error, logTranslationFailures bool, dashAlertInfo DashAlertInfo) ([]*models.Alert, error) {
|
||||||
ret := make([]*models.Alert, 0)
|
ret := make([]*models.Alert, 0)
|
||||||
|
|
||||||
for _, panelObj := range jsonWithPanels.Get("panels").MustArray() {
|
for _, panelObj := range jsonWithPanels.Get("panels").MustArray() {
|
||||||
@ -238,8 +238,8 @@ func (e *DashAlertExtractorService) getAlertFromPanels(ctx context.Context, json
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !validateAlertFunc(alert) {
|
if err := validateAlertFunc(alert); err != nil {
|
||||||
return nil, ValidationError{Reason: fmt.Sprintf("Panel id is not correct, alertName=%v, panelId=%v", alert.Name, alert.PanelId)}
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = append(ret, alert)
|
ret = append(ret, alert)
|
||||||
@ -248,8 +248,14 @@ func (e *DashAlertExtractorService) getAlertFromPanels(ctx context.Context, json
|
|||||||
return ret, nil
|
return ret, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateAlertRule(alert *models.Alert) bool {
|
func validateAlertRule(alert *models.Alert) error {
|
||||||
return alert.ValidToSave()
|
if !alert.ValidDashboardPanel() {
|
||||||
|
return ValidationError{Reason: fmt.Sprintf("Panel id is not correct, alertName=%v, panelId=%v", alert.Name, alert.PanelId)}
|
||||||
|
}
|
||||||
|
if !alert.ValidTags() {
|
||||||
|
return ValidationError{Reason: "Invalid tags, must be less than 100 characters"}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAlerts extracts alerts from the dashboard json and does full validation on the alert json data.
|
// GetAlerts extracts alerts from the dashboard json and does full validation on the alert json data.
|
||||||
@ -257,7 +263,7 @@ func (e *DashAlertExtractorService) GetAlerts(ctx context.Context, dashAlertInfo
|
|||||||
return e.extractAlerts(ctx, validateAlertRule, true, dashAlertInfo)
|
return e.extractAlerts(ctx, validateAlertRule, true, dashAlertInfo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *DashAlertExtractorService) extractAlerts(ctx context.Context, validateFunc func(alert *models.Alert) bool, logTranslationFailures bool, dashAlertInfo DashAlertInfo) ([]*models.Alert, error) {
|
func (e *DashAlertExtractorService) extractAlerts(ctx context.Context, validateFunc func(alert *models.Alert) error, logTranslationFailures bool, dashAlertInfo DashAlertInfo) ([]*models.Alert, error) {
|
||||||
dashboardJSON, err := copyJSON(dashAlertInfo.Dash.Data)
|
dashboardJSON, err := copyJSON(dashAlertInfo.Dash.Data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -294,8 +300,11 @@ func (e *DashAlertExtractorService) extractAlerts(ctx context.Context, validateF
|
|||||||
// ValidateAlerts validates alerts in the dashboard json but does not require a valid dashboard id
|
// ValidateAlerts validates alerts in the dashboard json but does not require a valid dashboard id
|
||||||
// in the first validation pass.
|
// in the first validation pass.
|
||||||
func (e *DashAlertExtractorService) ValidateAlerts(ctx context.Context, dashAlertInfo DashAlertInfo) error {
|
func (e *DashAlertExtractorService) ValidateAlerts(ctx context.Context, dashAlertInfo DashAlertInfo) error {
|
||||||
_, err := e.extractAlerts(ctx, func(alert *models.Alert) bool {
|
_, err := e.extractAlerts(ctx, func(alert *models.Alert) error {
|
||||||
return alert.OrgId != 0 && alert.PanelId != 0
|
if alert.OrgId == 0 || alert.PanelId == 0 {
|
||||||
|
return errors.New("missing OrgId, PanelId or both")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}, false, dashAlertInfo)
|
}, false, dashAlertInfo)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -92,8 +92,17 @@ type Alert struct {
|
|||||||
Settings *simplejson.Json
|
Settings *simplejson.Json
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Alert) ValidToSave() bool {
|
func (a *Alert) ValidDashboardPanel() bool {
|
||||||
return a.DashboardId != 0 && a.OrgId != 0 && a.PanelId != 0
|
return a.OrgId != 0 && a.DashboardId != 0 && a.PanelId != 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *Alert) ValidTags() bool {
|
||||||
|
for _, tag := range a.GetTagsFromSettings() {
|
||||||
|
if len(tag.Key) > 100 || len(tag.Value) > 100 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Alert) ContainsUpdates(other *Alert) bool {
|
func (a *Alert) ContainsUpdates(other *Alert) bool {
|
||||||
|
@ -304,7 +304,7 @@ export class BackendSrv implements BackendService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
showErrorAlert<T>(config: BackendSrvRequest, err: FetchError) {
|
showErrorAlert(config: BackendSrvRequest, err: FetchError) {
|
||||||
if (config.showErrorAlert === false) {
|
if (config.showErrorAlert === false) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -324,6 +324,7 @@ export class BackendSrv implements BackendService {
|
|||||||
|
|
||||||
// Validation
|
// Validation
|
||||||
if (err.status === 422) {
|
if (err.status === 422) {
|
||||||
|
description = err.data.message;
|
||||||
message = 'Validation failed';
|
message = 'Validation failed';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user