fleet/server/service/endpoint_middleware_test.go
Lucas Manuel Rodriguez 3757aace08
Add UUID to Fleet errors and clean up error msgs (#10411)
#8129 

Apart from fixing the issue in #8129, this change also introduces UUIDs
to Fleet errors. To be able to match a returned error from the API to a
error in the Fleet logs. See
https://fleetdm.slack.com/archives/C019WG4GH0A/p1677780622769939 for
more context.

Samples with the changes in this PR:
```
curl -k -H "Authorization: Bearer $TEST_TOKEN" -H 'Content-Type:application/json' "https://localhost:8080/api/v1/fleet/sso" -d ''
{
  "message": "Bad request",
  "errors": [
    {
      "name": "base",
      "reason": "Expected JSON Body"
    }
  ],
  "uuid": "a01f6e10-354c-4ff0-b96e-1f64adb500b0"
}
```
```
curl -k -H "Authorization: Bearer $TEST_TOKEN" -H 'Content-Type:application/json' "https://localhost:8080/api/v1/fleet/sso" -d 'asd'
{
  "message": "Bad request",
  "errors": [
    {
      "name": "base",
      "reason": "json decoder error"
    }
  ],
  "uuid": "5f716a64-7550-464b-a1dd-e6a505a9f89d"
}
```
```
curl -k -X GET -H "Authorization: Bearer badtoken" "https://localhost:8080/api/latest/fleet/teams"
{
  "message": "Authentication required",
  "errors": [
    {
      "name": "base",
      "reason": "Authentication required"
    }
  ],
  "uuid": "efe45bc0-f956-4bf9-ba4f-aa9020a9aaaf"
}
```
```
curl -k -X PATCH -H "Authorization: Bearer $TEST_TOKEN" "https://localhost:8080/api/latest/fleet/users/14" -d '{"name": "Manuel2", "password": "what", "new_password": "p4ssw0rd.12345"}'
{
  "message": "Authorization header required",
  "errors": [
    {
      "name": "base",
      "reason": "Authorization header required"
    }
  ],
  "uuid": "57f78cd0-4559-464f-9df7-36c9ef7c89b3"
}
```
```
curl -k -X PATCH -H "Authorization: Bearer $TEST_TOKEN" "https://localhost:8080/api/latest/fleet/users/14" -d '{"name": "Manuel2", "password": "what", "new_password": "p4ssw0rd.12345"}'
{
  "message": "Permission Denied",
  "uuid": "7f0220ad-6de7-4faf-8b6c-8d7ff9d2ca06"
}
```

- [X] Changes file added for user-visible changes in `changes/` or
`orbit/changes/`.
See [Changes
files](https://fleetdm.com/docs/contributing/committing-changes#changes-files)
for more information.
- [X] Documented any API changes (docs/Using-Fleet/REST-API.md or
docs/Contributing/API-for-contributors.md)
- ~[ ] Documented any permissions changes~
- ~[ ] Input data is properly validated, `SELECT *` is avoided, SQL
injection is prevented (using placeholders for values in statements)~
- ~[ ] Added support on fleet's osquery simulator `cmd/osquery-perf` for
new osquery data ingestion features.~
- [X] Added/updated tests
- [X] Manual QA for all new/changed functionality
  - For Orbit and Fleet Desktop changes:
- [X] Manual QA must be performed in the three main OSs, macOS, Windows
and Linux.
- ~[ ] Auto-update manual QA, from released version of component to new
version (see [tools/tuf/test](../tools/tuf/test/README.md)).~
2023-03-13 13:44:06 -03:00

245 lines
5.9 KiB
Go

package service
import (
"context"
"errors"
"testing"
hostctx "github.com/fleetdm/fleet/v4/server/contexts/host"
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mock"
kitlog "github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO update this test for new patterns
// TestEndpointPermissions tests that
// the endpoint.Middleware correctly grants or denies
// permissions to access or modify resources
// func TestEndpointPermissions(t *testing.T) {
// req := struct{}{}
// ds, err := inmem.New(config.TestConfig())
// assert.Nil(t, err)
// createTestUsers(t, ds)
// admin1, err := ds.User("admin1")
// assert.Nil(t, err)
// admin1Session, err := ds.NewSession(&fleet.Session{
// UserID: admin1.ID,
// Key: "admin1",
// })
// assert.Nil(t, err)
// user1, err := ds.User("user1")
// assert.Nil(t, err)
// user1Session, err := ds.NewSession(&fleet.Session{
// UserID: user1.ID,
// Key: "user1",
// })
// assert.Nil(t, err)
// user2, err := ds.User("user2")
// assert.Nil(t, err)
// user2Session, err := ds.NewSession(&fleet.Session{
// UserID: user2.ID,
// Key: "user2",
// })
// assert.Nil(t, err)
// user2.Enabled = false
// 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),
// wantErr: fleet.ErrNoContext,
// },
// {
// endpoint: canModifyUser(e),
// wantErr: fleet.ErrNoContext,
// },
// {
// 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"},
// },
// }
// 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())
// }
// })
// }
// }
type testNodeKeyRequest struct {
NodeKey string
}
func (r *testNodeKeyRequest) hostNodeKey() string { return r.NodeKey }
func TestAuthenticatedHost(t *testing.T) {
ds := new(mock.Store)
svc, ctx := newTestService(t, ds, nil, nil)
expectedHost := fleet.Host{Hostname: "foo!"}
goodNodeKey := "foo bar baz bing bang boom"
ds.AppConfigFunc = func(ctx context.Context) (*fleet.AppConfig, error) {
return &fleet.AppConfig{}, nil
}
ds.LoadHostByNodeKeyFunc = func(ctx context.Context, nodeKey string) (*fleet.Host, error) {
switch nodeKey {
case goodNodeKey:
return &expectedHost, nil
default:
return nil, errors.New("no host found")
}
}
endpoint := authenticatedHost(
svc,
kitlog.NewNopLogger(),
func(ctx context.Context, request interface{}) (interface{}, error) {
host, ok := hostctx.FromContext(ctx)
assert.True(t, ok)
assert.Equal(t, &expectedHost, host)
return nil, nil
},
)
authenticatedHostTests := []struct {
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) {
r := &testNodeKeyRequest{NodeKey: tt.nodeKey}
_, err := endpoint(ctx, r)
if tt.shouldErr {
assert.IsType(t, &osqueryError{}, err)
} else {
assert.Nil(t, err)
}
})
}
}
func TestAuthenticatedUserMW(t *testing.T) {
ds := new(mock.Store)
svc, ctx := newTestService(t, ds, nil, nil)
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(ctx, 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)
}
})
}
}