mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Correlations: Only return correlation for which both source and target datasources exist (#55454)
* Correlation: only return correlation for which both source and targe ds exist * add test
This commit is contained in:
parent
018733dd24
commit
d07abdd23c
@ -147,14 +147,10 @@ func (s CorrelationsService) getCorrelation(ctx context.Context, cmd GetCorrelat
|
||||
return ErrSourceDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
found, err := session.Where("uid = ? AND source_uid = ?", correlation.UID, correlation.SourceUID).Get(&correlation)
|
||||
found, err := session.Select("correlation.*").Join("", "data_source AS dss", "correlation.source_uid = dss.uid and dss.org_id = ?", cmd.OrgId).Join("", "data_source AS dst", "correlation.target_uid = dst.uid and dst.org_id = ?", cmd.OrgId).Where("correlation.uid = ? AND correlation.source_uid = ?", correlation.UID, correlation.SourceUID).Get(&correlation)
|
||||
if !found {
|
||||
return ErrCorrelationNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
})
|
||||
|
||||
@ -166,9 +162,6 @@ func (s CorrelationsService) getCorrelation(ctx context.Context, cmd GetCorrelat
|
||||
}
|
||||
|
||||
func (s CorrelationsService) getCorrelationsBySourceUID(ctx context.Context, cmd GetCorrelationsBySourceUIDQuery) ([]Correlation, error) {
|
||||
correlationsCondiBean := Correlation{
|
||||
SourceUID: cmd.SourceUID,
|
||||
}
|
||||
correlations := make([]Correlation, 0)
|
||||
|
||||
err := s.SQLStore.WithTransactionalDbSession(ctx, func(session *sqlstore.DBSession) error {
|
||||
@ -180,12 +173,7 @@ func (s CorrelationsService) getCorrelationsBySourceUID(ctx context.Context, cmd
|
||||
return ErrSourceDataSourceDoesNotExists
|
||||
}
|
||||
|
||||
err := session.Find(&correlations, correlationsCondiBean)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
return session.Select("correlation.*").Join("", "data_source AS dss", "correlation.source_uid = dss.uid and dss.org_id = ?", cmd.OrgId).Join("", "data_source AS dst", "correlation.target_uid = dst.uid and dst.org_id = ?", cmd.OrgId).Where("correlation.source_uid = ?", cmd.SourceUID).Find(&correlations)
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
@ -199,7 +187,7 @@ func (s CorrelationsService) getCorrelations(ctx context.Context, cmd GetCorrela
|
||||
correlations := make([]Correlation, 0)
|
||||
|
||||
err := s.SQLStore.WithDbSession(ctx, func(session *sqlstore.DBSession) error {
|
||||
return session.Select("correlation.*").Join("", "data_source", "correlation.source_uid = data_source.uid").Where("data_source.org_id = ?", cmd.OrgId).Find(&correlations)
|
||||
return session.Select("correlation.*").Join("", "data_source AS dss", "correlation.source_uid = dss.uid and dss.org_id = ?", cmd.OrgId).Join("", "data_source AS dst", "correlation.target_uid = dst.uid and dst.org_id = ?", cmd.OrgId).Find(&correlations)
|
||||
})
|
||||
if err != nil {
|
||||
return []Correlation{}, err
|
||||
|
@ -1,6 +1,7 @@
|
||||
package correlations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -10,6 +11,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/correlations"
|
||||
"github.com/grafana/grafana/pkg/services/datasources"
|
||||
"github.com/grafana/grafana/pkg/services/org"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@ -83,6 +85,27 @@ func TestIntegrationReadCorrelation(t *testing.T) {
|
||||
ctx.createDs(createDsCommand)
|
||||
dsWithoutCorrelations := createDsCommand.Result
|
||||
|
||||
// This creates 2 records in the correlation table that should never be returned by the API.
|
||||
// Given all tests in this file work on the assumption that only a single correlation exists,
|
||||
// this covers the case where bad data exists in the database.
|
||||
err := ctx.env.SQLStore.WithDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
created, err := sess.InsertMulti(&[]correlations.Correlation{
|
||||
{
|
||||
UID: "uid-1",
|
||||
SourceUID: dsWithoutCorrelations.Uid,
|
||||
TargetUID: "THIS-DOES-NOT_EXIST",
|
||||
},
|
||||
{
|
||||
UID: "uid-2",
|
||||
SourceUID: "THIS-DOES-NOT_EXIST",
|
||||
TargetUID: dsWithoutCorrelations.Uid,
|
||||
},
|
||||
})
|
||||
require.Equal(t, int64(2), created)
|
||||
return err
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("Get all correlations", func(t *testing.T) {
|
||||
t.Run("Unauthenticated users shouldn't be able to read correlations", func(t *testing.T) {
|
||||
res := ctx.Get(GetParams{
|
||||
|
Loading…
Reference in New Issue
Block a user