forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache.go
140 lines (119 loc) · 3.63 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package service
import (
"context"
"fmt"
"time"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/datasources/guardian"
)
const (
DefaultCacheTTL = 5 * time.Second
)
func ProvideCacheService(cacheService *localcache.CacheService, sqlStore db.DB, dsGuardian guardian.DatasourceGuardianProvider) *CacheServiceImpl {
return &CacheServiceImpl{
logger: log.New("datasources"),
cacheTTL: DefaultCacheTTL,
CacheService: cacheService,
SQLStore: sqlStore,
dsGuardian: dsGuardian,
}
}
type CacheServiceImpl struct {
logger log.Logger
cacheTTL time.Duration
CacheService *localcache.CacheService
SQLStore db.DB
dsGuardian guardian.DatasourceGuardianProvider
}
func (dc *CacheServiceImpl) GetDatasource(
ctx context.Context,
datasourceID int64,
user identity.Requester,
skipCache bool,
) (*datasources.DataSource, error) {
cacheKey := idKey(datasourceID)
if !skipCache {
if cached, found := dc.CacheService.Get(cacheKey); found {
ds := cached.(*datasources.DataSource)
if ds.OrgID == user.GetOrgID() {
if err := dc.canQuery(user, ds); err != nil {
return nil, err
}
return ds, nil
}
}
}
dc.logger.FromContext(ctx).Debug("Querying for data source via SQL store", "id", datasourceID, "orgId", user.GetOrgID())
query := &datasources.GetDataSourceQuery{ID: datasourceID, OrgID: user.GetOrgID()}
ss := SqlStore{db: dc.SQLStore, logger: dc.logger}
ds, err := ss.GetDataSource(ctx, query)
if err != nil {
return nil, err
}
if ds.UID != "" {
dc.CacheService.Set(uidKey(ds.OrgID, ds.UID), ds, time.Second*5)
}
dc.CacheService.Set(cacheKey, ds, dc.cacheTTL)
if err = dc.canQuery(user, ds); err != nil {
return nil, err
}
return ds, nil
}
func (dc *CacheServiceImpl) GetDatasourceByUID(
ctx context.Context,
datasourceUID string,
user identity.Requester,
skipCache bool,
) (*datasources.DataSource, error) {
if datasourceUID == "" {
return nil, fmt.Errorf("can not get data source by uid, uid is empty")
}
if user.GetOrgID() == 0 {
return nil, fmt.Errorf("can not get data source by uid, orgId is missing")
}
uidCacheKey := uidKey(user.GetOrgID(), datasourceUID)
if !skipCache {
if cached, found := dc.CacheService.Get(uidCacheKey); found {
ds := cached.(*datasources.DataSource)
if ds.OrgID == user.GetOrgID() {
if err := dc.canQuery(user, ds); err != nil {
return nil, err
}
return ds, nil
}
}
}
dc.logger.FromContext(ctx).Debug("Querying for data source via SQL store", "uid", datasourceUID, "orgId", user.GetOrgID())
query := &datasources.GetDataSourceQuery{UID: datasourceUID, OrgID: user.GetOrgID()}
ss := SqlStore{db: dc.SQLStore, logger: dc.logger}
ds, err := ss.GetDataSource(ctx, query)
if err != nil {
return nil, err
}
dc.CacheService.Set(uidCacheKey, ds, dc.cacheTTL)
dc.CacheService.Set(idKey(ds.ID), ds, dc.cacheTTL)
if err = dc.canQuery(user, ds); err != nil {
return nil, err
}
return ds, nil
}
func idKey(id int64) string {
return fmt.Sprintf("ds-%d", id)
}
func uidKey(orgID int64, uid string) string {
return fmt.Sprintf("ds-orgid-uid-%d-%s", orgID, uid)
}
func (dc *CacheServiceImpl) canQuery(user identity.Requester, ds *datasources.DataSource) error {
guardian := dc.dsGuardian.New(user.GetOrgID(), user, *ds)
if canQuery, err := guardian.CanQuery(ds.ID); err != nil || !canQuery {
if err != nil {
return err
}
return datasources.ErrDataSourceAccessDenied
}
return nil
}