2019-02-19 23:47:19 +00:00
|
|
|
package kv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-05-16 15:30:36 +00:00
|
|
|
"time"
|
2019-02-19 23:47:19 +00:00
|
|
|
|
2020-01-13 14:22:52 +00:00
|
|
|
"github.com/influxdata/influxdb/resource/noop"
|
|
|
|
|
2019-12-06 03:55:26 +00:00
|
|
|
"github.com/benbjohnson/clock"
|
2019-02-19 23:47:19 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/rand"
|
2020-01-13 14:22:52 +00:00
|
|
|
"github.com/influxdata/influxdb/resource"
|
2019-02-19 23:47:19 +00:00
|
|
|
"github.com/influxdata/influxdb/snowflake"
|
2019-12-06 03:55:26 +00:00
|
|
|
"go.uber.org/zap"
|
2019-02-19 23:47:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ influxdb.UserService = (*Service)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
// OpPrefix is the prefix for kv errors.
|
|
|
|
const OpPrefix = "kv/"
|
|
|
|
|
|
|
|
// Service is the struct that influxdb services are implemented on.
|
|
|
|
type Service struct {
|
2020-02-07 16:01:37 +00:00
|
|
|
kv Store
|
|
|
|
log *zap.Logger
|
|
|
|
clock clock.Clock
|
|
|
|
Config ServiceConfig
|
|
|
|
audit resource.Logger
|
2019-09-07 23:00:26 +00:00
|
|
|
IDGenerator influxdb.IDGenerator
|
|
|
|
|
|
|
|
// special ID generator that never returns bytes with backslash,
|
|
|
|
// comma, or space. Used to support very specific encoding of org &
|
|
|
|
// bucket into the old measurement in storage.
|
|
|
|
OrgBucketIDs influxdb.IDGenerator
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
TokenGenerator influxdb.TokenGenerator
|
2019-11-07 14:46:30 +00:00
|
|
|
// TODO(desa:ariel): this should not be embedded
|
2019-04-19 19:46:58 +00:00
|
|
|
influxdb.TimeGenerator
|
|
|
|
Hash Crypt
|
2019-12-26 04:55:04 +00:00
|
|
|
|
2019-12-31 03:11:53 +00:00
|
|
|
checkStore *IndexStore
|
2019-12-27 02:15:14 +00:00
|
|
|
endpointStore *IndexStore
|
|
|
|
variableStore *IndexStore
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns an instance of a Service.
|
2019-12-04 23:10:23 +00:00
|
|
|
func NewService(log *zap.Logger, kv Store, configs ...ServiceConfig) *Service {
|
2019-05-15 17:16:47 +00:00
|
|
|
s := &Service{
|
2019-12-04 23:10:23 +00:00
|
|
|
log: log,
|
2019-09-07 23:00:26 +00:00
|
|
|
IDGenerator: snowflake.NewIDGenerator(),
|
|
|
|
// Seed the random number generator with the current time
|
|
|
|
OrgBucketIDs: rand.NewOrgBucketID(time.Now().UnixNano()),
|
2019-02-19 23:47:19 +00:00
|
|
|
TokenGenerator: rand.NewTokenGenerator(64),
|
|
|
|
Hash: &Bcrypt{},
|
|
|
|
kv: kv,
|
2020-01-13 14:22:52 +00:00
|
|
|
audit: noop.ResourceLogger{},
|
2019-04-19 19:46:58 +00:00
|
|
|
TimeGenerator: influxdb.RealTimeGenerator{},
|
2019-12-31 03:11:53 +00:00
|
|
|
checkStore: newCheckStore(),
|
2019-12-27 02:15:14 +00:00
|
|
|
endpointStore: newEndpointStore(),
|
2019-12-31 03:11:53 +00:00
|
|
|
variableStore: newVariableStore(),
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
2019-05-15 17:16:47 +00:00
|
|
|
|
|
|
|
if len(configs) > 0 {
|
|
|
|
s.Config = configs[0]
|
|
|
|
} else {
|
|
|
|
s.Config.SessionLength = influxdb.DefaultSessionLength
|
|
|
|
}
|
|
|
|
|
2019-12-06 03:55:26 +00:00
|
|
|
s.clock = s.Config.Clock
|
|
|
|
if s.clock == nil {
|
|
|
|
s.clock = clock.New()
|
|
|
|
}
|
|
|
|
|
2019-05-15 17:16:47 +00:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceConfig allows us to configure Services
|
|
|
|
type ServiceConfig struct {
|
|
|
|
SessionLength time.Duration
|
2019-12-06 03:55:26 +00:00
|
|
|
Clock clock.Clock
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize creates Buckets needed.
|
|
|
|
func (s *Service) Initialize(ctx context.Context) error {
|
2019-03-05 00:38:10 +00:00
|
|
|
return s.kv.Update(ctx, func(tx Tx) error {
|
2019-02-19 23:47:19 +00:00
|
|
|
if err := s.initializeAuths(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-03-04 17:41:24 +00:00
|
|
|
if err := s.initializeDocuments(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
if err := s.initializeBuckets(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeDashboards(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeKVLog(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeLabels(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeOnboarding(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeOrgs(ctx, tx); err != nil {
|
|
|
|
return err
|
2019-04-09 22:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeTasks(ctx, tx); err != nil {
|
|
|
|
return err
|
2019-02-19 23:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializePasswords(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeScraperTargets(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeSecrets(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeSessions(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeSources(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeTelegraf(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.initializeURMs(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-27 02:15:14 +00:00
|
|
|
if err := s.variableStore.Init(ctx, tx); err != nil {
|
2019-02-19 23:47:19 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-31 18:28:26 +00:00
|
|
|
if err := s.initializeVariablesOrgIndex(tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-31 03:11:53 +00:00
|
|
|
if err := s.checkStore.Init(ctx, tx); err != nil {
|
2019-07-19 17:43:29 +00:00
|
|
|
return err
|
2019-12-31 03:11:53 +00:00
|
|
|
|
2019-07-19 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 02:46:42 +00:00
|
|
|
if err := s.initializeNotificationRule(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-27 02:15:14 +00:00
|
|
|
if err := s.endpointStore.Init(ctx, tx); err != nil {
|
2019-08-09 15:25:07 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
return s.initializeUsers(ctx, tx)
|
|
|
|
})
|
2020-02-07 16:01:37 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-13 14:22:52 +00:00
|
|
|
// WithResourceLogger sets the resource audit logger for the service.
|
|
|
|
func (s *Service) WithResourceLogger(audit resource.Logger) {
|
|
|
|
s.audit = audit
|
|
|
|
}
|
|
|
|
|
2019-02-19 23:47:19 +00:00
|
|
|
// WithStore sets kv store for the service.
|
|
|
|
// Should only be used in tests for mocking.
|
|
|
|
func (s *Service) WithStore(store Store) {
|
|
|
|
s.kv = store
|
|
|
|
}
|
2019-09-07 23:00:26 +00:00
|
|
|
|
|
|
|
// WithSpecialOrgBucketIDs sets the generator for the org
|
|
|
|
// and bucket ids.
|
|
|
|
//
|
|
|
|
// Should only be used in tests for mocking.
|
|
|
|
func (s *Service) WithSpecialOrgBucketIDs(gen influxdb.IDGenerator) {
|
|
|
|
s.OrgBucketIDs = gen
|
|
|
|
}
|