feat: Add an instance id. This gets set during onboarding and can be used to uniquely identify that instance.
parent
c3c608b117
commit
6a712df61e
|
@ -2,6 +2,7 @@ package authorizer_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/golang/mock/gomock"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -12,6 +13,7 @@ import (
|
|||
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
|
||||
"github.com/influxdata/influxdb/v2/http"
|
||||
"github.com/influxdata/influxdb/v2/inmem"
|
||||
iMock "github.com/influxdata/influxdb/v2/instance/mock"
|
||||
"github.com/influxdata/influxdb/v2/kit/platform"
|
||||
"github.com/influxdata/influxdb/v2/kv"
|
||||
"github.com/influxdata/influxdb/v2/kv/migration/all"
|
||||
|
@ -23,11 +25,13 @@ import (
|
|||
)
|
||||
|
||||
func TestOnboardingValidation(t *testing.T) {
|
||||
_, onboard := setup(t)
|
||||
_, onboard, instanceSvc := setup(t)
|
||||
|
||||
ts := authorizer.NewTaskService(zaptest.NewLogger(t), mockTaskService(3, 2, 1))
|
||||
|
||||
r, err := onboard.OnboardInitialUser(context.Background(), &influxdb.OnboardingRequest{
|
||||
ctx := context.Background()
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil).Times(1)
|
||||
r, err := onboard.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "Setec Astronomy",
|
||||
Password: "too many secrets",
|
||||
Org: "thing",
|
||||
|
@ -38,7 +42,7 @@ func TestOnboardingValidation(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ctx := pctx.SetAuthorizer(context.Background(), r.Auth)
|
||||
ctx = pctx.SetAuthorizer(context.Background(), r.Auth)
|
||||
|
||||
_, err = ts.CreateTask(ctx, taskmodel.TaskCreate{
|
||||
OrganizationID: r.Org.ID,
|
||||
|
@ -125,9 +129,11 @@ func TestValidations(t *testing.T) {
|
|||
otherOrg = &influxdb.Organization{Name: "other_org"}
|
||||
)
|
||||
|
||||
svc, onboard := setup(t)
|
||||
svc, onboard, instanceSvc := setup(t)
|
||||
|
||||
r, err := onboard.OnboardInitialUser(context.Background(), &influxdb.OnboardingRequest{
|
||||
ctx := context.Background()
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil).Times(1)
|
||||
r, err := onboard.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "Setec Astronomy",
|
||||
Password: "too many secrets",
|
||||
Org: "thing",
|
||||
|
@ -577,7 +583,7 @@ from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`
|
|||
}
|
||||
}
|
||||
|
||||
func setup(t *testing.T) (*tenant.Service, influxdb.OnboardingService) {
|
||||
func setup(t *testing.T) (*tenant.Service, influxdb.OnboardingService, *iMock.MockInstanceService) {
|
||||
t.Helper()
|
||||
|
||||
store := newStore(t)
|
||||
|
@ -590,10 +596,11 @@ func setup(t *testing.T) (*tenant.Service, influxdb.OnboardingService) {
|
|||
}
|
||||
|
||||
authSvc := authorization.NewService(authStore, svc)
|
||||
instanceSvc := iMock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
onboard := tenant.NewOnboardService(svc, authSvc)
|
||||
onboard := tenant.NewOnboardService(svc, authSvc, instanceSvc)
|
||||
|
||||
return svc, onboard
|
||||
return svc, onboard, instanceSvc
|
||||
}
|
||||
|
||||
func newStore(t *testing.T) kv.Store {
|
||||
|
|
|
@ -13,6 +13,8 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/influxdb/v2/instance"
|
||||
|
||||
"github.com/influxdata/flux"
|
||||
"github.com/influxdata/flux/dependencies/testing"
|
||||
"github.com/influxdata/flux/dependencies/url"
|
||||
|
@ -388,6 +390,8 @@ func (m *Launcher) run(ctx context.Context, opts *InfluxdOpts) (err error) {
|
|||
|
||||
pointsWriter = replicationSvc
|
||||
|
||||
instanceSvc := instance.NewService(m.sqlStore)
|
||||
|
||||
// When --hardening-enabled, use an HTTP IP validator that restricts
|
||||
// flux and pkger HTTP requests to private addressess.
|
||||
var urlValidator url.Validator
|
||||
|
@ -622,7 +626,7 @@ func (m *Launcher) run(ctx context.Context, opts *InfluxdOpts) (err error) {
|
|||
onboardOpts = append(onboardOpts, tenant.WithAlwaysAllowInitialUser())
|
||||
}
|
||||
|
||||
onboardSvc := tenant.NewOnboardService(ts, authSvc, onboardOpts...) // basic service
|
||||
onboardSvc := tenant.NewOnboardService(ts, authSvc, instanceSvc, onboardOpts...) // basic service
|
||||
onboardSvc = tenant.NewAuthedOnboardSvc(onboardSvc) // with auth
|
||||
onboardSvc = tenant.NewOnboardingMetrics(m.reg, onboardSvc, metric.WithSuffix("new")) // with metrics
|
||||
onboardSvc = tenant.NewOnboardingLogger(onboardingLogger, onboardSvc) // with logging
|
||||
|
|
|
@ -9,10 +9,13 @@ import (
|
|||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/influxdata/influxdb/v2"
|
||||
"github.com/influxdata/influxdb/v2/authorization"
|
||||
"github.com/influxdata/influxdb/v2/inmem"
|
||||
"github.com/influxdata/influxdb/v2/instance/mock"
|
||||
"github.com/influxdata/influxdb/v2/kit/platform"
|
||||
"github.com/influxdata/influxdb/v2/kv/migration"
|
||||
"github.com/influxdata/influxdb/v2/kv/migration/all"
|
||||
|
@ -175,11 +178,15 @@ func TestUpgradeSecurity(t *testing.T) {
|
|||
authStoreV2, err := authorization.NewStore(kvStore)
|
||||
require.NoError(t, err)
|
||||
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil).Times(1)
|
||||
|
||||
v2 := &influxDBv2{
|
||||
authSvc: authv1.NewService(authStoreV1, tenantSvc),
|
||||
onboardSvc: tenant.NewOnboardService(
|
||||
tenantSvc,
|
||||
authorization.NewService(authStoreV2, tenantSvc),
|
||||
instanceSvc,
|
||||
),
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,10 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/influxdata/influxdb/v2/instance"
|
||||
"github.com/influxdata/influxdb/v2/sqlite"
|
||||
sqliteMigrations "github.com/influxdata/influxdb/v2/sqlite/migrations"
|
||||
|
||||
"github.com/influxdata/influx-cli/v2/clients"
|
||||
"github.com/influxdata/influx-cli/v2/pkg/stdio"
|
||||
"github.com/influxdata/influxdb/v2"
|
||||
|
@ -89,6 +93,7 @@ func (o *optionsV1) populateDirs() {
|
|||
|
||||
type optionsV2 struct {
|
||||
boltPath string
|
||||
sqliteDBPath string
|
||||
cliConfigsPath string
|
||||
enginePath string
|
||||
cqPath string
|
||||
|
@ -180,6 +185,13 @@ func NewCommand(ctx context.Context, v *viper.Viper) (*cobra.Command, error) {
|
|||
Desc: "path for boltdb database",
|
||||
Short: 'm',
|
||||
},
|
||||
{
|
||||
DestP: &options.target.sqliteDBPath,
|
||||
Flag: "sqlitedb-path",
|
||||
Default: filepath.Join(v2dir, sqlite.DefaultFilename),
|
||||
Desc: "path for sqlite database",
|
||||
Short: 's',
|
||||
},
|
||||
{
|
||||
DestP: &options.target.cliConfigsPath,
|
||||
Flag: "influx-configs-path",
|
||||
|
@ -530,6 +542,12 @@ func (o *optionsV2) validatePaths() error {
|
|||
return fmt.Errorf("error checking for existing file at %q: %w", o.boltPath, err)
|
||||
}
|
||||
|
||||
if _, err := os.Stat(o.sqliteDBPath); err == nil {
|
||||
return fmt.Errorf("file present at target path for upgraded 2.x sqlite DB: %q", o.sqliteDBPath)
|
||||
} else if !os.IsNotExist(err) {
|
||||
return fmt.Errorf("error checking for existing file at %q: %w", o.sqliteDBPath, err)
|
||||
}
|
||||
|
||||
if fi, err := os.Stat(o.enginePath); err == nil {
|
||||
if !fi.IsDir() {
|
||||
return fmt.Errorf("upgraded 2.x engine path %q is not a directory", o.enginePath)
|
||||
|
@ -632,6 +650,17 @@ func newInfluxDBv2(ctx context.Context, opts *optionsV2, log *zap.Logger) (svc *
|
|||
return nil, err
|
||||
}
|
||||
|
||||
sqlStore, err := sqlite.NewSqlStore(opts.sqliteDBPath, log.With(zap.String("service", "sqlite")))
|
||||
if err != nil {
|
||||
log.Error("Failed to open SQL store", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
sqlMigrator := sqlite.NewMigrator(sqlStore, log.With(zap.String("service", "SQL migrations")))
|
||||
if err := sqlMigrator.Up(ctx, sqliteMigrations.AllUp); err != nil {
|
||||
log.Error("Failed to apply SQL migrations", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Create Tenant service (orgs, buckets, )
|
||||
svc.tenantStore = tenant.NewStore(svc.kvStore)
|
||||
svc.ts = tenant.NewSystem(svc.tenantStore, log.With(zap.String("store", "new")), reg, metric.WithSuffix("new"))
|
||||
|
@ -660,8 +689,9 @@ func newInfluxDBv2(ctx context.Context, opts *optionsV2, log *zap.Logger) (svc *
|
|||
|
||||
svc.authSvcV2 = authorization.NewService(authStoreV2, svc.ts)
|
||||
|
||||
is := instance.NewService(sqlStore)
|
||||
// on-boarding service (influx setup)
|
||||
svc.onboardSvc = tenant.NewOnboardService(svc.ts, svc.authSvcV2)
|
||||
svc.onboardSvc = tenant.NewOnboardService(svc.ts, svc.authSvcV2, is)
|
||||
|
||||
// v1 auth service
|
||||
authStoreV1, err := authv1.NewStore(svc.kvStore)
|
||||
|
|
|
@ -11,6 +11,8 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/influxdb/v2/sqlite"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
|
@ -211,6 +213,7 @@ func TestUpgradeRealDB(t *testing.T) {
|
|||
|
||||
tl := launcher.NewTestLauncherServer()
|
||||
boltPath := filepath.Join(tl.Path, bolt.DefaultFilename)
|
||||
sqlitePath := filepath.Join(tl.Path, sqlite.DefaultFilename)
|
||||
enginePath := filepath.Join(tl.Path, "engine")
|
||||
cqPath := filepath.Join(tl.Path, "cq.txt")
|
||||
cliConfigPath := filepath.Join(tl.Path, "influx-configs")
|
||||
|
@ -219,6 +222,7 @@ func TestUpgradeRealDB(t *testing.T) {
|
|||
v1opts := &optionsV1{configFile: v1ConfigPath}
|
||||
v2opts := &optionsV2{
|
||||
boltPath: boltPath,
|
||||
sqliteDBPath: sqlitePath,
|
||||
enginePath: enginePath,
|
||||
cqPath: cqPath,
|
||||
cliConfigsPath: cliConfigPath,
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
package influxdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/influxdata/influxdb/v2/kit/platform"
|
||||
)
|
||||
|
||||
type Instance struct {
|
||||
ID platform.ID `json:"id" db:"id"`
|
||||
}
|
||||
|
||||
type InstanceService interface {
|
||||
CreateInstance(ctx context.Context) (*Instance, error)
|
||||
|
||||
GetInstance(ctx context.Context) (*Instance, error)
|
||||
}
|
|
@ -0,0 +1,66 @@
|
|||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/influxdata/influxdb/v2 (interfaces: InstanceService)
|
||||
|
||||
// Package mock is a generated GoMock package.
|
||||
package mock
|
||||
|
||||
import (
|
||||
context "context"
|
||||
reflect "reflect"
|
||||
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
influxdb "github.com/influxdata/influxdb/v2"
|
||||
)
|
||||
|
||||
// MockInstanceService is a mock of InstanceService interface.
|
||||
type MockInstanceService struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockInstanceServiceMockRecorder
|
||||
}
|
||||
|
||||
// MockInstanceServiceMockRecorder is the mock recorder for MockInstanceService.
|
||||
type MockInstanceServiceMockRecorder struct {
|
||||
mock *MockInstanceService
|
||||
}
|
||||
|
||||
// NewMockInstanceService creates a new mock instance.
|
||||
func NewMockInstanceService(ctrl *gomock.Controller) *MockInstanceService {
|
||||
mock := &MockInstanceService{ctrl: ctrl}
|
||||
mock.recorder = &MockInstanceServiceMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockInstanceService) EXPECT() *MockInstanceServiceMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// CreateInstance mocks base method.
|
||||
func (m *MockInstanceService) CreateInstance(arg0 context.Context) (*influxdb.Instance, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "CreateInstance", arg0)
|
||||
ret0, _ := ret[0].(*influxdb.Instance)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// CreateInstance indicates an expected call of CreateInstance.
|
||||
func (mr *MockInstanceServiceMockRecorder) CreateInstance(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateInstance", reflect.TypeOf((*MockInstanceService)(nil).CreateInstance), arg0)
|
||||
}
|
||||
|
||||
// GetInstance mocks base method.
|
||||
func (m *MockInstanceService) GetInstance(arg0 context.Context) (*influxdb.Instance, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetInstance", arg0)
|
||||
ret0, _ := ret[0].(*influxdb.Instance)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetInstance indicates an expected call of GetInstance.
|
||||
func (mr *MockInstanceServiceMockRecorder) GetInstance(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetInstance", reflect.TypeOf((*MockInstanceService)(nil).GetInstance), arg0)
|
||||
}
|
|
@ -0,0 +1,76 @@
|
|||
package instance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/influxdata/influxdb/v2"
|
||||
"github.com/influxdata/influxdb/v2/kit/platform"
|
||||
ierrors "github.com/influxdata/influxdb/v2/kit/platform/errors"
|
||||
"github.com/influxdata/influxdb/v2/snowflake"
|
||||
"github.com/influxdata/influxdb/v2/sqlite"
|
||||
)
|
||||
|
||||
var (
|
||||
errInstanceNotFound = &ierrors.Error{
|
||||
Code: ierrors.ENotFound,
|
||||
Msg: "instance not found",
|
||||
}
|
||||
)
|
||||
|
||||
func NewService(store *sqlite.SqlStore) *service {
|
||||
return &service{
|
||||
store: store,
|
||||
idGenerator: snowflake.NewIDGenerator(),
|
||||
}
|
||||
}
|
||||
|
||||
type service struct {
|
||||
store *sqlite.SqlStore
|
||||
idGenerator platform.IDGenerator
|
||||
}
|
||||
|
||||
func (s service) CreateInstance(ctx context.Context) (*influxdb.Instance, error) {
|
||||
s.store.Mu.Lock()
|
||||
defer s.store.Mu.Unlock()
|
||||
|
||||
q := sq.Insert("instance").
|
||||
SetMap(sq.Eq{
|
||||
"id": s.idGenerator.ID(),
|
||||
"created_at": "datetime('now')",
|
||||
"updated_at": "datetime('now')",
|
||||
}).
|
||||
Suffix("RETURNING id")
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var is influxdb.Instance
|
||||
if err := s.store.DB.GetContext(ctx, &is, query, args...); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &is, nil
|
||||
}
|
||||
|
||||
func (s service) GetInstance(ctx context.Context) (*influxdb.Instance, error) {
|
||||
q := sq.Select("id").
|
||||
From("instance")
|
||||
|
||||
query, args, err := q.ToSql()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var is influxdb.Instance
|
||||
if err := s.store.DB.GetContext(ctx, &is, query, args...); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, errInstanceNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return &is, nil
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
package instance
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/influxdb/v2"
|
||||
"github.com/influxdata/influxdb/v2/kit/platform"
|
||||
"github.com/influxdata/influxdb/v2/mock"
|
||||
"github.com/influxdata/influxdb/v2/sqlite"
|
||||
"github.com/influxdata/influxdb/v2/sqlite/migrations"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
var (
|
||||
ctx = context.Background()
|
||||
initID = platform.ID(1)
|
||||
connection = influxdb.Instance{
|
||||
ID: initID,
|
||||
}
|
||||
)
|
||||
|
||||
func TestCreateAndGetConnection(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
svc, clean := newTestService(t)
|
||||
defer clean(t)
|
||||
|
||||
// Getting an invalid ID should return an error.
|
||||
got, err := svc.GetInstance(ctx)
|
||||
require.Equal(t, errInstanceNotFound, err)
|
||||
require.Nil(t, got)
|
||||
|
||||
// Create an instance, check the results.
|
||||
created, err := svc.CreateInstance(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, connection, *created)
|
||||
|
||||
// Read the created connection and assert it matches the creation response.
|
||||
got, err = svc.GetInstance(ctx)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, connection, *got)
|
||||
}
|
||||
|
||||
func newTestService(t *testing.T) (*service, func(t *testing.T)) {
|
||||
store, clean := sqlite.NewTestStore(t)
|
||||
logger := zaptest.NewLogger(t)
|
||||
sqliteMigrator := sqlite.NewMigrator(store, logger)
|
||||
require.NoError(t, sqliteMigrator.Up(ctx, migrations.AllUp))
|
||||
|
||||
svc := service{
|
||||
store: store,
|
||||
idGenerator: mock.NewIncrementingIDGenerator(initID),
|
||||
}
|
||||
|
||||
return &svc, clean
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
DROP TABLE instance;
|
|
@ -0,0 +1,6 @@
|
|||
CREATE TABLE instance
|
||||
(
|
||||
id VARCHAR(16) NOT NULL PRIMARY KEY,
|
||||
created_at TIMESTAMP NOT NULL,
|
||||
updated_at TIMESTAMP NOT NULL
|
||||
);
|
|
@ -2,6 +2,7 @@ package tenant_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/golang/mock/gomock"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
|
@ -9,13 +10,14 @@ import (
|
|||
"github.com/influxdata/influxdb/v2"
|
||||
"github.com/influxdata/influxdb/v2/authorization"
|
||||
ihttp "github.com/influxdata/influxdb/v2/http"
|
||||
"github.com/influxdata/influxdb/v2/instance/mock"
|
||||
"github.com/influxdata/influxdb/v2/tenant"
|
||||
itesting "github.com/influxdata/influxdb/v2/testing"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/zap/zaptest"
|
||||
)
|
||||
|
||||
func initOnboardHttpService(f itesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, func()) {
|
||||
func initOnboardHttpService(f itesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, *mock.MockInstanceService, func()) {
|
||||
t.Helper()
|
||||
|
||||
s := itesting.NewTestInmemStore(t)
|
||||
|
@ -27,7 +29,9 @@ func initOnboardHttpService(f itesting.OnboardingFields, t *testing.T) (influxdb
|
|||
require.NoError(t, err)
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := context.Background()
|
||||
if !f.IsOnboarding {
|
||||
|
@ -51,7 +55,7 @@ func initOnboardHttpService(f itesting.OnboardingFields, t *testing.T) (influxdb
|
|||
Client: httpClient,
|
||||
}
|
||||
|
||||
return &client, server.Close
|
||||
return &client, instanceSvc, server.Close
|
||||
}
|
||||
|
||||
func TestOnboardService(t *testing.T) {
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
type OnboardService struct {
|
||||
service *Service
|
||||
authSvc influxdb.AuthorizationService
|
||||
instanceSvc influxdb.InstanceService
|
||||
alwaysAllow bool
|
||||
log *zap.Logger
|
||||
}
|
||||
|
@ -37,11 +38,12 @@ func WithOnboardingLogger(logger *zap.Logger) OnboardServiceOptionFn {
|
|||
}
|
||||
}
|
||||
|
||||
func NewOnboardService(svc *Service, as influxdb.AuthorizationService, opts ...OnboardServiceOptionFn) influxdb.OnboardingService {
|
||||
func NewOnboardService(svc *Service, as influxdb.AuthorizationService, is influxdb.InstanceService, opts ...OnboardServiceOptionFn) influxdb.OnboardingService {
|
||||
s := &OnboardService{
|
||||
service: svc,
|
||||
authSvc: as,
|
||||
log: zap.NewNop(),
|
||||
service: svc,
|
||||
authSvc: as,
|
||||
instanceSvc: is,
|
||||
log: zap.NewNop(),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
|
@ -112,6 +114,11 @@ func (s *OnboardService) onboardUser(ctx context.Context, req *influxdb.Onboardi
|
|||
|
||||
result := &influxdb.OnboardingResults{}
|
||||
|
||||
instance, err := s.instanceSvc.CreateInstance(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// create a user
|
||||
user := &influxdb.User{
|
||||
Name: req.User,
|
||||
|
@ -157,7 +164,7 @@ func (s *OnboardService) onboardUser(ctx context.Context, req *influxdb.Onboardi
|
|||
UserType: influxdb.Owner,
|
||||
MappingType: influxdb.UserMappingType,
|
||||
ResourceType: influxdb.InstanceResourceType,
|
||||
ResourceID: platform.ID(1), // The instance doesn't have a resourceid
|
||||
ResourceID: instance.ID,
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package tenant_test
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/golang/mock/gomock"
|
||||
"github.com/influxdata/influxdb/v2/kit/platform"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
@ -9,6 +11,7 @@ import (
|
|||
"github.com/influxdata/influxdb/v2"
|
||||
"github.com/influxdata/influxdb/v2/authorization"
|
||||
icontext "github.com/influxdata/influxdb/v2/context"
|
||||
"github.com/influxdata/influxdb/v2/instance/mock"
|
||||
"github.com/influxdata/influxdb/v2/kv"
|
||||
"github.com/influxdata/influxdb/v2/pkg/testing/assert"
|
||||
"github.com/influxdata/influxdb/v2/tenant"
|
||||
|
@ -20,13 +23,13 @@ func TestBoltOnboardingService(t *testing.T) {
|
|||
influxdbtesting.OnboardInitialUser(initBoltOnboardingService, t)
|
||||
}
|
||||
|
||||
func initBoltOnboardingService(f influxdbtesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, func()) {
|
||||
func initBoltOnboardingService(f influxdbtesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, *mock.MockInstanceService, func()) {
|
||||
s := influxdbtesting.NewTestInmemStore(t)
|
||||
svc := initOnboardingService(s, f, t)
|
||||
return svc, func() {}
|
||||
svc, isvc := initOnboardingService(s, f, t)
|
||||
return svc, isvc, func() {}
|
||||
}
|
||||
|
||||
func initOnboardingService(s kv.Store, f influxdbtesting.OnboardingFields, t *testing.T) influxdb.OnboardingService {
|
||||
func initOnboardingService(s kv.Store, f influxdbtesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, *mock.MockInstanceService) {
|
||||
storage := tenant.NewStore(s)
|
||||
ten := tenant.NewService(storage)
|
||||
|
||||
|
@ -34,8 +37,10 @@ func initOnboardingService(s kv.Store, f influxdbtesting.OnboardingFields, t *te
|
|||
require.NoError(t, err)
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
// we will need an auth service as well
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
|
@ -48,7 +53,7 @@ func initOnboardingService(s kv.Store, f influxdbtesting.OnboardingFields, t *te
|
|||
}
|
||||
}
|
||||
|
||||
return svc
|
||||
return svc, instanceSvc
|
||||
}
|
||||
|
||||
func TestOnboardURM(t *testing.T) {
|
||||
|
@ -59,13 +64,16 @@ func TestOnboardURM(t *testing.T) {
|
|||
authStore, err := authorization.NewStore(s)
|
||||
require.NoError(t, err)
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
||||
UserID: 123,
|
||||
})
|
||||
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil)
|
||||
|
||||
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "name",
|
||||
Org: "name",
|
||||
|
@ -97,13 +105,16 @@ func TestOnboardAuth(t *testing.T) {
|
|||
authStore, err := authorization.NewStore(s)
|
||||
require.NoError(t, err)
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
||||
UserID: 123,
|
||||
})
|
||||
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil)
|
||||
|
||||
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "name",
|
||||
Org: "name",
|
||||
|
@ -176,14 +187,17 @@ func TestOnboardService_RetentionPolicy(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
// we will need an auth service as well
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
||||
UserID: 123,
|
||||
})
|
||||
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil)
|
||||
|
||||
var retention int64 = 72 * 3600 // 72h
|
||||
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "name",
|
||||
|
@ -208,14 +222,17 @@ func TestOnboardService_RetentionPolicyDeprecated(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
|
||||
// we will need an auth service as well
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
||||
UserID: 123,
|
||||
})
|
||||
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil)
|
||||
|
||||
retention := 72 * time.Hour
|
||||
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "name",
|
||||
|
@ -240,12 +257,15 @@ func TestOnboardService_WeakPassword(t *testing.T) {
|
|||
require.NoError(t, err)
|
||||
|
||||
authSvc := authorization.NewService(authStore, ten)
|
||||
svc := tenant.NewOnboardService(ten, authSvc)
|
||||
instanceSvc := mock.NewMockInstanceService(gomock.NewController(t))
|
||||
svc := tenant.NewOnboardService(ten, authSvc, instanceSvc)
|
||||
|
||||
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
||||
UserID: 123,
|
||||
})
|
||||
|
||||
instanceSvc.EXPECT().CreateInstance(ctx).Return(&influxdb.Instance{ID: platform.ID(1)}, nil)
|
||||
|
||||
_, err = svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
||||
User: "name",
|
||||
Password: "short",
|
||||
|
|
|
@ -5,8 +5,11 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/mock/gomock"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
platform "github.com/influxdata/influxdb/v2"
|
||||
imock "github.com/influxdata/influxdb/v2/instance/mock"
|
||||
platform2 "github.com/influxdata/influxdb/v2/kit/platform"
|
||||
"github.com/influxdata/influxdb/v2/kit/platform/errors"
|
||||
"github.com/influxdata/influxdb/v2/mock"
|
||||
|
@ -39,7 +42,7 @@ type OnboardingFields struct {
|
|||
|
||||
// OnboardInitialUser testing
|
||||
func OnboardInitialUser(
|
||||
init func(OnboardingFields, *testing.T) (platform.OnboardingService, func()),
|
||||
init func(OnboardingFields, *testing.T) (platform.OnboardingService, *imock.MockInstanceService, func()),
|
||||
t *testing.T,
|
||||
) {
|
||||
type args struct {
|
||||
|
@ -188,9 +191,12 @@ func OnboardInitialUser(
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s, done := init(tt.fields, t)
|
||||
s, isvc, done := init(tt.fields, t)
|
||||
defer done()
|
||||
ctx := context.Background()
|
||||
if tt.wants.errCode == "" {
|
||||
isvc.EXPECT().CreateInstance(gomock.Any()).Return(&platform.Instance{ID: platform2.ID(1)}, nil).Times(1)
|
||||
}
|
||||
results, err := s.OnboardInitialUser(ctx, tt.args.request)
|
||||
if (err != nil) != (tt.wants.errCode != "") {
|
||||
t.Logf("Error: %v", err)
|
||||
|
|
Loading…
Reference in New Issue