2020-04-06 21:58:15 +00:00
|
|
|
package tenant_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2020-11-02 22:38:43 +00:00
|
|
|
"time"
|
2020-04-06 21:58:15 +00:00
|
|
|
|
2020-09-10 17:59:11 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-03-01 14:55:39 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/authorization"
|
2020-09-02 16:15:57 +00:00
|
|
|
icontext "github.com/influxdata/influxdb/v2/context"
|
2020-04-06 21:58:15 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
2021-09-13 19:12:35 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/pkg/testing/assert"
|
2020-04-06 21:58:15 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/tenant"
|
|
|
|
influxdbtesting "github.com/influxdata/influxdb/v2/testing"
|
2021-09-13 19:12:35 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-04-06 21:58:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBoltOnboardingService(t *testing.T) {
|
|
|
|
influxdbtesting.OnboardInitialUser(initBoltOnboardingService, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBoltOnboardingService(f influxdbtesting.OnboardingFields, t *testing.T) (influxdb.OnboardingService, func()) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-04-06 21:58:15 +00:00
|
|
|
svc := initOnboardingService(s, f, t)
|
2021-08-31 20:43:45 +00:00
|
|
|
return svc, func() {}
|
2020-04-06 21:58:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initOnboardingService(s kv.Store, f influxdbtesting.OnboardingFields, t *testing.T) influxdb.OnboardingService {
|
2020-07-01 11:08:20 +00:00
|
|
|
storage := tenant.NewStore(s)
|
2020-04-06 21:58:15 +00:00
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
authStore, err := authorization.NewStore(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
authSvc := authorization.NewService(authStore, ten)
|
|
|
|
|
2020-04-06 21:58:15 +00:00
|
|
|
// we will need an auth service as well
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
svc := tenant.NewOnboardService(ten, authSvc)
|
2020-04-06 21:58:15 +00:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
t.Logf("Onboarding: %v", f.IsOnboarding)
|
|
|
|
if !f.IsOnboarding {
|
|
|
|
// create a dummy so so we can no longer onboard
|
|
|
|
err := ten.CreateUser(ctx, &influxdb.User{Name: "dummy", Status: influxdb.Active})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc
|
|
|
|
}
|
2020-09-02 16:15:57 +00:00
|
|
|
|
|
|
|
func TestOnboardURM(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-09-02 16:15:57 +00:00
|
|
|
storage := tenant.NewStore(s)
|
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
authStore, err := authorization.NewStore(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
authSvc := authorization.NewService(authStore, ten)
|
|
|
|
|
|
|
|
svc := tenant.NewOnboardService(ten, authSvc)
|
2020-09-02 16:15:57 +00:00
|
|
|
|
|
|
|
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
|
|
|
UserID: 123,
|
|
|
|
})
|
|
|
|
|
2021-06-22 13:09:52 +00:00
|
|
|
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
2020-09-02 16:15:57 +00:00
|
|
|
User: "name",
|
|
|
|
Org: "name",
|
|
|
|
Bucket: "name",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
urms, _, err := ten.FindUserResourceMappings(ctx, influxdb.UserResourceMappingFilter{ResourceID: onboard.Org.ID})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(urms) > 1 {
|
|
|
|
t.Fatal("additional URMs created")
|
|
|
|
}
|
|
|
|
if urms[0].UserID != onboard.User.ID {
|
|
|
|
t.Fatal("org assigned to the wrong user")
|
|
|
|
}
|
|
|
|
}
|
2020-09-10 17:59:11 +00:00
|
|
|
|
|
|
|
func TestOnboardAuth(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-09-10 17:59:11 +00:00
|
|
|
storage := tenant.NewStore(s)
|
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
authStore, err := authorization.NewStore(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
authSvc := authorization.NewService(authStore, ten)
|
|
|
|
|
|
|
|
svc := tenant.NewOnboardService(ten, authSvc)
|
2020-09-10 17:59:11 +00:00
|
|
|
|
|
|
|
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
|
|
|
UserID: 123,
|
|
|
|
})
|
|
|
|
|
2021-06-22 13:09:52 +00:00
|
|
|
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
2020-09-10 17:59:11 +00:00
|
|
|
User: "name",
|
|
|
|
Org: "name",
|
|
|
|
Bucket: "name",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
auth := onboard.Auth
|
|
|
|
expectedPerm := []influxdb.Permission{
|
2021-06-22 13:09:52 +00:00
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.AuthorizationsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.AuthorizationsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.BucketsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.BucketsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.DashboardsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.DashboardsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.OrgsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.OrgsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.SourcesResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.SourcesResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.TelegrafsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.TelegrafsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.UsersResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.UsersResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.VariablesResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.VariablesResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.ScraperResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.ScraperResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.SecretsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.SecretsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.LabelsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.LabelsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.ViewsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.ViewsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.DocumentsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.DocumentsResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.NotificationRuleResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.NotificationRuleResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.NotificationEndpointResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.NotificationEndpointResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.ChecksResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.ChecksResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.DBRPResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.DBRPResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.NotebooksResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.NotebooksResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.AnnotationsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.AnnotationsResourceType}},
|
2021-10-26 15:32:35 +00:00
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.RemotesResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.RemotesResourceType}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.ReplicationsResourceType}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.ReplicationsResourceType}},
|
2020-09-10 17:59:11 +00:00
|
|
|
}
|
|
|
|
if !cmp.Equal(auth.Permissions, expectedPerm) {
|
|
|
|
t.Fatalf("unequal permissions: \n %+v", cmp.Diff(auth.Permissions, expectedPerm))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-11-02 22:38:43 +00:00
|
|
|
|
|
|
|
func TestOnboardService_RetentionPolicy(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-11-02 22:38:43 +00:00
|
|
|
storage := tenant.NewStore(s)
|
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
authStore, err := authorization.NewStore(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
authSvc := authorization.NewService(authStore, ten)
|
|
|
|
|
2020-11-02 22:38:43 +00:00
|
|
|
// we will need an auth service as well
|
refactor(kv): delete deprecated kv service code
This includes removal of a lot of kv.Service responsibilities. However,
it does not finish the re-wiring. It removes documents, telegrafs,
notification rules + endpoints, checks, orgs, users, buckets, passwords,
urms, labels and authorizations. There are some oustanding pieces that
are needed to get kv service compiling (dashboard service urm
dependency). Then all the call sites for kv service need updating and
the new implementations of telegraf and notification rules + endpoints
needed installing (along with any necessary migrations).
2020-10-20 13:25:36 +00:00
|
|
|
svc := tenant.NewOnboardService(ten, authSvc)
|
2020-11-02 22:38:43 +00:00
|
|
|
|
|
|
|
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
|
|
|
UserID: 123,
|
|
|
|
})
|
|
|
|
|
2021-03-01 14:55:39 +00:00
|
|
|
var retention int64 = 72 * 3600 // 72h
|
|
|
|
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
|
|
|
User: "name",
|
|
|
|
Org: "name",
|
|
|
|
Bucket: "name",
|
|
|
|
RetentionPeriodSeconds: retention,
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-05-17 23:01:27 +00:00
|
|
|
assert.Equal(t, onboard.Bucket.RetentionPeriod, time.Duration(retention)*time.Second, "Retention policy should pass through")
|
2021-03-01 14:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOnboardService_RetentionPolicyDeprecated(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2021-03-01 14:55:39 +00:00
|
|
|
storage := tenant.NewStore(s)
|
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
|
|
|
authStore, err := authorization.NewStore(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
authSvc := authorization.NewService(authStore, ten)
|
|
|
|
|
|
|
|
// we will need an auth service as well
|
|
|
|
svc := tenant.NewOnboardService(ten, authSvc)
|
|
|
|
|
|
|
|
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
|
|
|
UserID: 123,
|
|
|
|
})
|
|
|
|
|
2020-11-02 22:38:43 +00:00
|
|
|
retention := 72 * time.Hour
|
|
|
|
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
2021-03-01 14:55:39 +00:00
|
|
|
User: "name",
|
|
|
|
Org: "name",
|
|
|
|
Bucket: "name",
|
|
|
|
RetentionPeriodDeprecated: retention,
|
2020-11-02 22:38:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, onboard.Bucket.RetentionPeriod, retention, "Retention policy should pass through")
|
2020-11-03 14:47:36 +00:00
|
|
|
}
|
2020-12-16 14:43:43 +00:00
|
|
|
|
|
|
|
func TestOnboardService_WeakPassword(t *testing.T) {
|
2021-08-31 20:43:45 +00:00
|
|
|
s := influxdbtesting.NewTestInmemStore(t)
|
2020-12-16 14:43:43 +00:00
|
|
|
storage := tenant.NewStore(s)
|
|
|
|
ten := tenant.NewService(storage)
|
|
|
|
|
|
|
|
authStore, err := authorization.NewStore(s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
authSvc := authorization.NewService(authStore, ten)
|
|
|
|
svc := tenant.NewOnboardService(ten, authSvc)
|
|
|
|
|
|
|
|
ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
|
|
|
|
UserID: 123,
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
|
|
|
|
User: "name",
|
|
|
|
Password: "short",
|
|
|
|
Org: "name",
|
|
|
|
Bucket: "name",
|
|
|
|
})
|
|
|
|
assert.Equal(t, err, tenant.EShortPassword)
|
|
|
|
}
|