2019-04-22 21:31:16 +00:00
|
|
|
package authorizer_test
|
2019-01-11 20:09:31 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2019-10-18 00:23:45 +00:00
|
|
|
"time"
|
2019-01-11 20:09:31 +00:00
|
|
|
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2"
|
|
|
|
"github.com/influxdata/influxdb/v2/authorizer"
|
|
|
|
pctx "github.com/influxdata/influxdb/v2/context"
|
|
|
|
"github.com/influxdata/influxdb/v2/http"
|
|
|
|
"github.com/influxdata/influxdb/v2/inmem"
|
|
|
|
"github.com/influxdata/influxdb/v2/kv"
|
2020-07-01 11:08:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/kv/migration/all"
|
2020-04-03 17:39:20 +00:00
|
|
|
"github.com/influxdata/influxdb/v2/mock"
|
2020-11-06 00:54:28 +00:00
|
|
|
_ "github.com/influxdata/influxdb/v2/fluxinit/static"
|
2019-07-26 15:37:45 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-03-14 20:26:36 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2019-01-11 20:09:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestOnboardingValidation(t *testing.T) {
|
2019-12-28 00:58:57 +00:00
|
|
|
svc := newKVSVC(t)
|
2019-09-26 16:52:24 +00:00
|
|
|
ts := authorizer.NewTaskService(zaptest.NewLogger(t), mockTaskService(3, 2, 1))
|
2019-01-11 20:09:31 +00:00
|
|
|
|
2020-03-19 22:53:54 +00:00
|
|
|
r, err := svc.OnboardInitialUser(context.Background(), &influxdb.OnboardingRequest{
|
2019-02-19 23:47:19 +00:00
|
|
|
User: "Setec Astronomy",
|
|
|
|
Password: "too many secrets",
|
2019-01-11 20:09:31 +00:00
|
|
|
Org: "thing",
|
|
|
|
Bucket: "holder",
|
|
|
|
RetentionPeriod: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := pctx.SetAuthorizer(context.Background(), r.Auth)
|
|
|
|
|
2019-04-22 21:31:16 +00:00
|
|
|
_, err = ts.CreateTask(ctx, influxdb.TaskCreate{
|
2019-01-18 16:10:14 +00:00
|
|
|
OrganizationID: r.Org.ID,
|
2019-08-20 14:42:40 +00:00
|
|
|
OwnerID: r.Auth.GetUserID(),
|
2019-01-11 20:09:31 +00:00
|
|
|
Flux: `option task = {
|
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 18:31:45 +00:00
|
|
|
func mockTaskService(orgID, taskID, runID influxdb.ID) influxdb.TaskService {
|
2019-01-11 20:09:31 +00:00
|
|
|
task := influxdb.Task{
|
2019-02-21 18:31:45 +00:00
|
|
|
ID: taskID,
|
|
|
|
OrganizationID: orgID,
|
2019-01-18 16:10:14 +00:00
|
|
|
Name: "cows",
|
2020-03-06 22:19:32 +00:00
|
|
|
Status: string(influxdb.TaskActive),
|
2019-01-11 20:09:31 +00:00
|
|
|
Flux: `option task = {
|
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`,
|
|
|
|
Every: "1s",
|
|
|
|
}
|
|
|
|
|
2019-03-05 22:58:33 +00:00
|
|
|
log := influxdb.Log{Message: "howdy partner"}
|
2019-01-11 20:09:31 +00:00
|
|
|
|
|
|
|
run := influxdb.Run{
|
2019-02-21 18:31:45 +00:00
|
|
|
ID: runID,
|
|
|
|
TaskID: taskID,
|
2019-01-11 20:09:31 +00:00
|
|
|
Status: "completed",
|
2019-10-18 00:23:45 +00:00
|
|
|
ScheduledFor: time.Now().UTC(),
|
|
|
|
StartedAt: time.Now().UTC().Add(time.Second * 3),
|
|
|
|
FinishedAt: time.Now().UTC().Add(time.Second * 10),
|
2019-03-05 22:58:33 +00:00
|
|
|
Log: []influxdb.Log{log},
|
2019-01-11 20:09:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &mock.TaskService{
|
|
|
|
FindTaskByIDFn: func(context.Context, influxdb.ID) (*influxdb.Task, error) {
|
|
|
|
return &task, nil
|
|
|
|
},
|
|
|
|
FindTasksFn: func(context.Context, influxdb.TaskFilter) ([]*influxdb.Task, int, error) {
|
|
|
|
return []*influxdb.Task{&task}, 1, nil
|
|
|
|
},
|
2019-02-09 01:34:44 +00:00
|
|
|
CreateTaskFn: func(_ context.Context, tc influxdb.TaskCreate) (*influxdb.Task, error) {
|
|
|
|
taskCopy := task
|
|
|
|
return &taskCopy, nil
|
2019-01-11 20:09:31 +00:00
|
|
|
},
|
|
|
|
UpdateTaskFn: func(context.Context, influxdb.ID, influxdb.TaskUpdate) (*influxdb.Task, error) {
|
|
|
|
return &task, nil
|
|
|
|
},
|
|
|
|
DeleteTaskFn: func(context.Context, influxdb.ID) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
FindLogsFn: func(context.Context, influxdb.LogFilter) ([]*influxdb.Log, int, error) {
|
|
|
|
return []*influxdb.Log{&log}, 1, nil
|
|
|
|
},
|
|
|
|
FindRunsFn: func(context.Context, influxdb.RunFilter) ([]*influxdb.Run, int, error) {
|
|
|
|
return []*influxdb.Run{&run}, 1, nil
|
|
|
|
},
|
|
|
|
FindRunByIDFn: func(context.Context, influxdb.ID, influxdb.ID) (*influxdb.Run, error) {
|
|
|
|
return &run, nil
|
|
|
|
},
|
|
|
|
CancelRunFn: func(context.Context, influxdb.ID, influxdb.ID) error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
RetryRunFn: func(context.Context, influxdb.ID, influxdb.ID) (*influxdb.Run, error) {
|
|
|
|
return &run, nil
|
|
|
|
},
|
|
|
|
ForceRunFn: func(context.Context, influxdb.ID, int64) (*influxdb.Run, error) {
|
|
|
|
return &run, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidations(t *testing.T) {
|
2019-02-21 18:31:45 +00:00
|
|
|
var (
|
2019-07-26 15:37:45 +00:00
|
|
|
taskID = influxdb.ID(0x7456)
|
|
|
|
runID = influxdb.ID(0x402)
|
|
|
|
otherOrg = &influxdb.Organization{Name: "other_org"}
|
2019-02-21 18:31:45 +00:00
|
|
|
)
|
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
svc := newKVSVC(t)
|
2019-01-11 20:09:31 +00:00
|
|
|
|
2020-03-19 22:53:54 +00:00
|
|
|
r, err := svc.OnboardInitialUser(context.Background(), &influxdb.OnboardingRequest{
|
2019-02-19 23:47:19 +00:00
|
|
|
User: "Setec Astronomy",
|
|
|
|
Password: "too many secrets",
|
2019-01-11 20:09:31 +00:00
|
|
|
Org: "thing",
|
|
|
|
Bucket: "holder",
|
|
|
|
RetentionPeriod: 1,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-07-26 15:37:45 +00:00
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
if err := svc.CreateOrganization(context.Background(), otherOrg); err != nil {
|
2019-07-26 15:37:45 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
otherBucket := &influxdb.Bucket{
|
|
|
|
Name: "other_bucket",
|
|
|
|
OrgID: otherOrg.ID,
|
|
|
|
}
|
|
|
|
|
2019-12-28 00:58:57 +00:00
|
|
|
if err = svc.CreateBucket(context.Background(), otherBucket); err != nil {
|
2019-07-26 15:37:45 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-01-11 20:09:31 +00:00
|
|
|
|
2019-02-21 18:31:45 +00:00
|
|
|
var (
|
2019-07-26 15:37:45 +00:00
|
|
|
orgID = r.Org.ID
|
2019-09-26 16:52:24 +00:00
|
|
|
validTaskService = authorizer.NewTaskService(zaptest.NewLogger(t), mockTaskService(orgID, taskID, runID))
|
2019-07-26 15:37:45 +00:00
|
|
|
|
2019-02-21 18:31:45 +00:00
|
|
|
// Read all tasks in org.
|
|
|
|
orgReadAllTaskPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &orgID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read all tasks in some other org.
|
|
|
|
wrongOrgReadAllTaskPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &taskID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all tasks in org, no specific bucket permissions.
|
|
|
|
orgWriteAllTaskPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &orgID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write all tasks in org, and read/write the onboarding bucket.
|
|
|
|
orgWriteAllTaskBucketPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &orgID}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.BucketsResourceType, OrgID: &orgID, ID: &r.Bucket.ID}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.BucketsResourceType, OrgID: &orgID, ID: &r.Bucket.ID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the specific task, and read/write the onboarding bucket.
|
|
|
|
orgWriteTaskBucketPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &orgID, ID: &taskID}},
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.BucketsResourceType, OrgID: &orgID, ID: &r.Bucket.ID}},
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.BucketsResourceType, OrgID: &orgID, ID: &r.Bucket.ID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permission only to specifically write the target task.
|
|
|
|
orgWriteTaskPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.WriteAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &orgID, ID: &taskID}},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permission only to specifically read the target task.
|
|
|
|
orgReadTaskPermissions = []influxdb.Permission{
|
|
|
|
{Action: influxdb.ReadAction, Resource: influxdb.Resource{Type: influxdb.TasksResourceType, OrgID: &orgID, ID: &taskID}},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-01-11 20:09:31 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
check func(context.Context, influxdb.TaskService) error
|
|
|
|
auth *influxdb.Authorization
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "create failure",
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-09 01:34:44 +00:00
|
|
|
_, err := svc.CreateTask(ctx, influxdb.TaskCreate{
|
2019-01-18 16:10:14 +00:00
|
|
|
OrganizationID: r.Org.ID,
|
2019-01-11 20:09:31 +00:00
|
|
|
Flux: `option task = {
|
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("failed to error without permission")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
auth: &influxdb.Authorization{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "create success",
|
|
|
|
auth: r.Auth,
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-09 01:34:44 +00:00
|
|
|
_, err := svc.CreateTask(ctx, influxdb.TaskCreate{
|
2019-01-18 16:10:14 +00:00
|
|
|
OrganizationID: r.Org.ID,
|
2019-08-20 14:42:40 +00:00
|
|
|
OwnerID: r.Auth.GetUserID(),
|
2019-01-11 20:09:31 +00:00
|
|
|
Flux: `option task = {
|
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindTaskByID missing auth",
|
|
|
|
auth: &influxdb.Authorization{Permissions: []influxdb.Permission{}},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.FindTaskByID(ctx, taskID)
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned without error without permission")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "FindTaskByID with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.FindTaskByID(ctx, taskID)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindTaskByID with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.FindTaskByID(ctx, taskID)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindTasks with bad auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: wrongOrgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
ts, _, err := svc.FindTasks(ctx, influxdb.TaskFilter{
|
2019-02-27 15:56:32 +00:00
|
|
|
OrganizationID: &orgID,
|
2019-01-11 20:09:31 +00:00
|
|
|
})
|
2019-02-21 18:31:45 +00:00
|
|
|
if err == nil && len(ts) > 0 {
|
2019-01-11 20:09:31 +00:00
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "FindTasks with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindTasks(ctx, influxdb.TaskFilter{
|
2019-02-27 15:56:32 +00:00
|
|
|
OrganizationID: &orgID,
|
2019-01-11 20:09:31 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "FindTasks with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindTasks(ctx, influxdb.TaskFilter{
|
2019-02-27 15:56:32 +00:00
|
|
|
OrganizationID: &orgID,
|
2019-02-21 18:31:45 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindTasks without org filter",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindTasks(ctx, influxdb.TaskFilter{})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "UpdateTask with readonly auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
flux := `option task = {
|
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.UpdateTask(ctx, taskID, influxdb.TaskUpdate{
|
2019-01-11 20:09:31 +00:00
|
|
|
Flux: &flux,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "UpdateTask with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteAllTaskBucketPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
flux := `option task = {
|
2019-03-08 21:18:11 +00:00
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.UpdateTask(ctx, taskID, influxdb.TaskUpdate{
|
|
|
|
Flux: &flux,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "UpdateTask with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteTaskBucketPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
flux := `option task = {
|
|
|
|
name: "my_task",
|
|
|
|
every: 1s,
|
|
|
|
}
|
|
|
|
from(bucket:"holder") |> range(start:-5m) |> to(bucket:"holder", org:"thing")`
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.UpdateTask(ctx, taskID, influxdb.TaskUpdate{
|
2019-01-11 20:09:31 +00:00
|
|
|
Flux: &flux,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DeleteTask missing auth",
|
|
|
|
auth: &influxdb.Authorization{Permissions: []influxdb.Permission{}},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
err := svc.DeleteTask(ctx, taskID)
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned without error without permission")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DeleteTask readonly auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Permissions: orgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
err := svc.DeleteTask(ctx, taskID)
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned without error without permission")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "DeleteTask with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
err := svc.DeleteTask(ctx, taskID)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "DeleteTask with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
err := svc.DeleteTask(ctx, taskID)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindLogs with bad auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: wrongOrgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindLogs(ctx, influxdb.LogFilter{
|
2019-02-16 00:04:54 +00:00
|
|
|
Task: taskID,
|
2019-01-11 20:09:31 +00:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "FindLogs with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindLogs(ctx, influxdb.LogFilter{
|
|
|
|
Task: taskID,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindLogs with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindLogs(ctx, influxdb.LogFilter{
|
2019-02-16 00:04:54 +00:00
|
|
|
Task: taskID,
|
2019-01-11 20:09:31 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindRuns with bad auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: wrongOrgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindRuns(ctx, influxdb.RunFilter{
|
2019-02-16 00:04:54 +00:00
|
|
|
Task: taskID,
|
2019-01-11 20:09:31 +00:00
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "FindRuns with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindRuns(ctx, influxdb.RunFilter{
|
|
|
|
Task: taskID,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindRuns with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, _, err := svc.FindRuns(ctx, influxdb.RunFilter{
|
2019-02-16 00:04:54 +00:00
|
|
|
Task: taskID,
|
2019-01-11 20:09:31 +00:00
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindRunByID missing auth",
|
|
|
|
auth: &influxdb.Authorization{Permissions: []influxdb.Permission{}},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.FindRunByID(ctx, taskID, 10)
|
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned without error without permission")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "FindRunByID with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.FindRunByID(ctx, taskID, 10)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "FindRunByID with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgReadTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.FindRunByID(ctx, taskID, 10)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CancelRun with bad auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: wrongOrgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
err := svc.CancelRun(ctx, taskID, 10)
|
2019-01-11 20:09:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "CancelRun with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
err := svc.CancelRun(ctx, taskID, 10)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "CancelRun with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
err := svc.CancelRun(ctx, taskID, 10)
|
2019-01-11 20:09:31 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RetryRun with bad auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: wrongOrgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.RetryRun(ctx, taskID, 10)
|
2019-01-11 20:09:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "RetryRun with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.RetryRun(ctx, taskID, 10)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "RetryRun with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.RetryRun(ctx, taskID, 10)
|
2019-01-11 20:09:31 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ForceRun with bad auth",
|
2019-02-21 18:31:45 +00:00
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: wrongOrgReadAllTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.ForceRun(ctx, taskID, 10000)
|
2019-01-11 20:09:31 +00:00
|
|
|
if err == nil {
|
|
|
|
return errors.New("returned no error with a invalid auth")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-02-21 18:31:45 +00:00
|
|
|
name: "ForceRun with org auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteAllTaskPermissions},
|
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
|
|
|
_, err := svc.ForceRun(ctx, taskID, 10000)
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ForceRun with task auth",
|
|
|
|
auth: &influxdb.Authorization{Status: "active", Permissions: orgWriteTaskPermissions},
|
2019-01-11 20:09:31 +00:00
|
|
|
check: func(ctx context.Context, svc influxdb.TaskService) error {
|
2019-02-21 18:31:45 +00:00
|
|
|
_, err := svc.ForceRun(ctx, taskID, 10000)
|
2019-01-11 20:09:31 +00:00
|
|
|
return err
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
ctx := pctx.SetAuthorizer(context.Background(), test.auth)
|
|
|
|
if err := test.check(ctx, validTaskService); err != nil {
|
|
|
|
if aerr, ok := err.(http.AuthzError); ok {
|
|
|
|
t.Error(aerr.AuthzError())
|
|
|
|
}
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-28 00:58:57 +00:00
|
|
|
|
|
|
|
func newKVSVC(t *testing.T) *kv.Service {
|
|
|
|
t.Helper()
|
|
|
|
|
2020-07-01 11:08:20 +00:00
|
|
|
store := inmem.NewKVStore()
|
|
|
|
|
|
|
|
if err := all.Up(context.Background(), zaptest.NewLogger(t), store); err != nil {
|
2019-12-28 00:58:57 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-07-01 11:08:20 +00:00
|
|
|
|
|
|
|
return kv.NewService(zaptest.NewLogger(t), store)
|
2019-12-28 00:58:57 +00:00
|
|
|
}
|