2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-01 04:51:38 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2021-11-22 14:13:26 +00:00
|
|
|
"errors"
|
2016-09-01 04:51:38 +00:00
|
|
|
"testing"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
hostctx "github.com/fleetdm/fleet/v4/server/contexts/host"
|
2021-11-10 22:30:15 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/mock"
|
2021-09-10 17:48:33 +00:00
|
|
|
kitlog "github.com/go-kit/kit/log"
|
2016-09-14 18:40:51 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-11-10 22:30:15 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-09-01 04:51:38 +00:00
|
|
|
)
|
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// TODO update this test for new patterns
|
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
// TestEndpointPermissions tests that
|
|
|
|
// the endpoint.Middleware correctly grants or denies
|
|
|
|
// permissions to access or modify resources
|
2021-04-07 01:27:10 +00:00
|
|
|
// func TestEndpointPermissions(t *testing.T) {
|
|
|
|
// req := struct{}{}
|
|
|
|
// ds, err := inmem.New(config.TestConfig())
|
|
|
|
// assert.Nil(t, err)
|
2017-01-09 15:10:02 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// createTestUsers(t, ds)
|
2017-01-09 15:10:02 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// admin1, err := ds.User("admin1")
|
|
|
|
// assert.Nil(t, err)
|
2021-06-06 22:07:29 +00:00
|
|
|
// admin1Session, err := ds.NewSession(&fleet.Session{
|
2021-04-07 01:27:10 +00:00
|
|
|
// UserID: admin1.ID,
|
|
|
|
// Key: "admin1",
|
|
|
|
// })
|
|
|
|
// assert.Nil(t, err)
|
2017-01-09 15:10:02 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// user1, err := ds.User("user1")
|
|
|
|
// assert.Nil(t, err)
|
2021-06-06 22:07:29 +00:00
|
|
|
// user1Session, err := ds.NewSession(&fleet.Session{
|
2021-04-07 01:27:10 +00:00
|
|
|
// UserID: user1.ID,
|
|
|
|
// Key: "user1",
|
|
|
|
// })
|
|
|
|
// assert.Nil(t, err)
|
2017-01-09 15:10:02 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// user2, err := ds.User("user2")
|
|
|
|
// assert.Nil(t, err)
|
2021-06-06 22:07:29 +00:00
|
|
|
// user2Session, err := ds.NewSession(&fleet.Session{
|
2021-04-07 01:27:10 +00:00
|
|
|
// UserID: user2.ID,
|
|
|
|
// Key: "user2",
|
|
|
|
// })
|
|
|
|
// assert.Nil(t, err)
|
|
|
|
// user2.Enabled = false
|
2016-09-01 04:51:38 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// e := endpoint.Nop // a test endpoint
|
|
|
|
// var endpointTests = []struct {
|
|
|
|
// endpoint endpoint.Endpoint
|
|
|
|
// // who is making the request
|
|
|
|
// vc *viewer.Viewer
|
|
|
|
// // what resource are we editing
|
|
|
|
// requestID uint
|
|
|
|
// // what error to expect
|
|
|
|
// wantErr interface{}
|
|
|
|
// // custom request struct
|
|
|
|
// request interface{}
|
|
|
|
// }{
|
|
|
|
// {
|
|
|
|
// endpoint: canReadUser(e),
|
2021-06-06 22:07:29 +00:00
|
|
|
// wantErr: fleet.ErrNoContext,
|
2021-04-07 01:27:10 +00:00
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// endpoint: canModifyUser(e),
|
2021-06-06 22:07:29 +00:00
|
|
|
// wantErr: fleet.ErrNoContext,
|
2021-04-07 01:27:10 +00:00
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// endpoint: canModifyUser(e),
|
|
|
|
// vc: &viewer.Viewer{User: admin1, Session: admin1Session},
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// endpoint: canModifyUser(e),
|
|
|
|
// vc: &viewer.Viewer{User: user1, Session: user1Session},
|
|
|
|
// wantErr: permissionError{message: "no write permissions on user"},
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// endpoint: canModifyUser(e),
|
|
|
|
// vc: &viewer.Viewer{User: user1, Session: user1Session},
|
|
|
|
// requestID: admin1.ID,
|
|
|
|
// wantErr: permissionError{message: "no write permissions on user"},
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// endpoint: canReadUser(e),
|
|
|
|
// vc: &viewer.Viewer{User: user1, Session: user1Session},
|
|
|
|
// requestID: admin1.ID,
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// endpoint: canReadUser(e),
|
|
|
|
// vc: &viewer.Viewer{User: user2, Session: user2Session},
|
|
|
|
// requestID: admin1.ID,
|
|
|
|
// wantErr: permissionError{message: "no read permissions on user"},
|
|
|
|
// },
|
|
|
|
// }
|
2016-09-01 04:51:38 +00:00
|
|
|
|
2021-04-07 01:27:10 +00:00
|
|
|
// for _, tt := range endpointTests {
|
|
|
|
// tt := tt
|
|
|
|
// t.Run("", func(st *testing.T) {
|
|
|
|
// st.Parallel()
|
|
|
|
// ctx := context.Background()
|
|
|
|
// if tt.vc != nil {
|
|
|
|
// ctx = viewer.NewContext(ctx, *tt.vc)
|
|
|
|
// }
|
|
|
|
// if tt.requestID != 0 {
|
|
|
|
// ctx = context.WithValue(ctx, "request-id", tt.requestID)
|
|
|
|
// }
|
|
|
|
// var request interface{}
|
|
|
|
// if tt.request != nil {
|
|
|
|
// request = tt.request
|
|
|
|
// } else {
|
|
|
|
// request = req
|
|
|
|
// }
|
|
|
|
// _, eerr := tt.endpoint(ctx, request)
|
|
|
|
// assert.IsType(st, tt.wantErr, eerr)
|
|
|
|
// if ferr, ok := eerr.(permissionError); ok {
|
|
|
|
// assert.Equal(st, tt.wantErr.(permissionError).message, ferr.Error())
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// }
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2022-03-09 21:13:56 +00:00
|
|
|
type testNodeKeyRequest struct {
|
|
|
|
NodeKey string
|
2016-09-29 04:21:39 +00:00
|
|
|
}
|
|
|
|
|
2022-03-09 21:13:56 +00:00
|
|
|
func (r *testNodeKeyRequest) hostNodeKey() string { return r.NodeKey }
|
|
|
|
|
2016-09-29 04:21:39 +00:00
|
|
|
func TestAuthenticatedHost(t *testing.T) {
|
2020-05-29 16:12:39 +00:00
|
|
|
ds := new(mock.Store)
|
2022-03-11 15:51:12 +00:00
|
|
|
svc := newTestService(t, ds, nil, nil)
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2021-06-24 00:32:19 +00:00
|
|
|
expectedHost := fleet.Host{Hostname: "foo!"}
|
2020-05-29 16:12:39 +00:00
|
|
|
goodNodeKey := "foo bar baz bing bang boom"
|
|
|
|
|
2021-09-14 12:11:07 +00:00
|
|
|
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
|
2021-09-10 17:48:33 +00:00
|
|
|
return &fleet.AppConfig{}, nil
|
|
|
|
}
|
2022-01-18 01:52:09 +00:00
|
|
|
ds.LoadHostByNodeKeyFunc = func(ctx context.Context, nodeKey string) (*fleet.Host, error) {
|
|
|
|
switch nodeKey {
|
2020-05-29 16:12:39 +00:00
|
|
|
case goodNodeKey:
|
|
|
|
return &expectedHost, nil
|
|
|
|
default:
|
|
|
|
return nil, errors.New("no host found")
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-29 04:21:39 +00:00
|
|
|
endpoint := authenticatedHost(
|
|
|
|
svc,
|
2021-09-10 17:48:33 +00:00
|
|
|
kitlog.NewNopLogger(),
|
2016-09-29 04:21:39 +00:00
|
|
|
func(ctx context.Context, request interface{}) (interface{}, error) {
|
2020-05-29 16:12:39 +00:00
|
|
|
host, ok := hostctx.FromContext(ctx)
|
|
|
|
assert.True(t, ok)
|
2022-01-18 01:52:09 +00:00
|
|
|
assert.Equal(t, &expectedHost, host)
|
2016-09-29 04:21:39 +00:00
|
|
|
return nil, nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-12-01 12:05:23 +00:00
|
|
|
authenticatedHostTests := []struct {
|
2016-09-29 04:21:39 +00:00
|
|
|
nodeKey string
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
nodeKey: "invalid",
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodeKey: "",
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
nodeKey: goodNodeKey,
|
|
|
|
shouldErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range authenticatedHostTests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
2022-03-09 21:13:56 +00:00
|
|
|
r := &testNodeKeyRequest{NodeKey: tt.nodeKey}
|
2021-06-03 23:24:15 +00:00
|
|
|
_, err := endpoint(context.Background(), r)
|
2016-09-29 04:21:39 +00:00
|
|
|
if tt.shouldErr {
|
|
|
|
assert.IsType(t, osqueryError{}, err)
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-11-10 22:30:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestAuthenticatedUserMW(t *testing.T) {
|
|
|
|
ds := new(mock.Store)
|
2022-03-11 15:51:12 +00:00
|
|
|
svc := newTestService(t, ds, nil, nil)
|
2016-09-29 04:21:39 +00:00
|
|
|
|
2021-11-10 22:30:15 +00:00
|
|
|
authenticatedUserTests := []struct {
|
|
|
|
user *fleet.User
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
user: &fleet.User{
|
|
|
|
ID: 32,
|
|
|
|
Name: "name",
|
|
|
|
Email: "em@il.com",
|
|
|
|
AdminForcedPasswordReset: true,
|
|
|
|
SSOEnabled: true,
|
|
|
|
},
|
|
|
|
shouldErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
user: &fleet.User{
|
|
|
|
ID: 32,
|
|
|
|
Name: "name",
|
|
|
|
Email: "em@il.com",
|
|
|
|
AdminForcedPasswordReset: true,
|
|
|
|
SSOEnabled: false,
|
|
|
|
},
|
|
|
|
shouldErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range authenticatedUserTests {
|
|
|
|
t.Run("", func(t *testing.T) {
|
|
|
|
ctx := viewer.NewContext(context.Background(), viewer.Viewer{User: tt.user})
|
|
|
|
|
|
|
|
nextCalled := false
|
|
|
|
endpoint := authenticatedUser(svc, func(ctx context.Context, request interface{}) (response interface{}, err error) {
|
|
|
|
nextCalled = true
|
|
|
|
return nil, nil
|
|
|
|
})
|
|
|
|
_, err := endpoint(ctx, nil)
|
|
|
|
if tt.shouldErr {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, nextCalled)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-09-29 04:21:39 +00:00
|
|
|
}
|