2021-11-11 20:33:06 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-02-28 12:34:44 +00:00
|
|
|
"database/sql"
|
2022-02-15 20:22:19 +00:00
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"strings"
|
2021-11-11 20:33:06 +00:00
|
|
|
|
2022-02-15 20:22:19 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2022-03-08 16:27:38 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/logging"
|
2022-02-15 20:22:19 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/viewer"
|
2021-11-11 20:33:06 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2022-02-15 20:22:19 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/mail"
|
2021-11-11 20:33:06 +00:00
|
|
|
)
|
|
|
|
|
2022-02-15 20:22:19 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create invite
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type createInviteRequest struct {
|
|
|
|
fleet.InvitePayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type createInviteResponse struct {
|
|
|
|
Invite *fleet.Invite `json:"invite,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r createInviteResponse) error() error { return r.Err }
|
|
|
|
|
2022-12-27 14:26:59 +00:00
|
|
|
func createInviteEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
|
2022-02-15 20:22:19 +00:00
|
|
|
req := request.(*createInviteRequest)
|
|
|
|
invite, err := svc.InviteNewUser(ctx, req.InvitePayload)
|
|
|
|
if err != nil {
|
|
|
|
return createInviteResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return createInviteResponse{invite, nil}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) InviteNewUser(ctx context.Context, payload fleet.InvitePayload) (*fleet.Invite, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Email == nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, fleet.NewInvalidArgumentError("email", "missing required argument"))
|
|
|
|
}
|
|
|
|
*payload.Email = strings.ToLower(*payload.Email)
|
|
|
|
|
|
|
|
// verify that the user with the given email does not already exist
|
|
|
|
_, err := svc.ds.UserByEmail(ctx, *payload.Email)
|
|
|
|
if err == nil {
|
|
|
|
return nil, ctxerr.Wrap(ctx, fleet.NewInvalidArgumentError("email", "a user with this account already exists"))
|
|
|
|
}
|
|
|
|
var nfe fleet.NotFoundError
|
|
|
|
if !errors.As(err, &nfe) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the user who created the invite
|
|
|
|
v, ok := viewer.FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("missing viewer context for create invite")
|
|
|
|
}
|
|
|
|
inviter := v.User
|
|
|
|
|
|
|
|
random, err := server.GenerateRandomText(svc.config.App.TokenKeySize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
token := base64.URLEncoding.EncodeToString([]byte(random))
|
|
|
|
|
|
|
|
invite := &fleet.Invite{
|
|
|
|
Email: *payload.Email,
|
|
|
|
InvitedBy: inviter.ID,
|
|
|
|
Token: token,
|
|
|
|
GlobalRole: payload.GlobalRole,
|
|
|
|
Teams: payload.Teams,
|
|
|
|
}
|
|
|
|
if payload.Position != nil {
|
|
|
|
invite.Position = *payload.Position
|
|
|
|
}
|
|
|
|
if payload.Name != nil {
|
|
|
|
invite.Name = *payload.Name
|
|
|
|
}
|
|
|
|
if payload.SSOEnabled != nil {
|
|
|
|
invite.SSOEnabled = *payload.SSOEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
invite, err = svc.ds.NewInvite(ctx, invite)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-28 18:23:15 +00:00
|
|
|
config, err := svc.ds.AppConfig(ctx)
|
2022-02-15 20:22:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
invitedBy := inviter.Name
|
|
|
|
if invitedBy == "" {
|
|
|
|
invitedBy = inviter.Email
|
|
|
|
}
|
2023-06-07 19:06:36 +00:00
|
|
|
var smtpSettings fleet.SMTPSettings
|
|
|
|
if config.SMTPSettings != nil {
|
|
|
|
smtpSettings = *config.SMTPSettings
|
|
|
|
}
|
2022-02-15 20:22:19 +00:00
|
|
|
inviteEmail := fleet.Email{
|
2023-06-07 19:06:36 +00:00
|
|
|
Subject: "You are Invited to Fleet",
|
|
|
|
To: []string{invite.Email},
|
|
|
|
ServerURL: config.ServerSettings.ServerURL,
|
|
|
|
SMTPSettings: smtpSettings,
|
2022-02-15 20:22:19 +00:00
|
|
|
Mailer: &mail.InviteMailer{
|
|
|
|
Invite: invite,
|
|
|
|
BaseURL: template.URL(config.ServerSettings.ServerURL + svc.config.Server.URLPrefix),
|
|
|
|
AssetURL: getAssetURL(),
|
|
|
|
OrgName: config.OrgInfo.OrgName,
|
|
|
|
InvitedBy: invitedBy,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = svc.mailService.SendEmail(inviteEmail)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return invite, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// List invites
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type listInvitesRequest struct {
|
|
|
|
ListOptions fleet.ListOptions `url:"list_options"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type listInvitesResponse struct {
|
|
|
|
Invites []fleet.Invite `json:"invites"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r listInvitesResponse) error() error { return r.Err }
|
|
|
|
|
2022-12-27 14:26:59 +00:00
|
|
|
func listInvitesEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
|
2022-02-15 20:22:19 +00:00
|
|
|
req := request.(*listInvitesRequest)
|
|
|
|
invites, err := svc.ListInvites(ctx, req.ListOptions)
|
|
|
|
if err != nil {
|
|
|
|
return listInvitesResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := listInvitesResponse{Invites: []fleet.Invite{}}
|
|
|
|
for _, invite := range invites {
|
|
|
|
resp.Invites = append(resp.Invites, *invite)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) ListInvites(ctx context.Context, opt fleet.ListOptions) ([]*fleet.Invite, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionRead); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return svc.ds.ListInvites(ctx, opt)
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:33:06 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Update invite
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type updateInviteRequest struct {
|
|
|
|
ID uint `url:"id"`
|
|
|
|
fleet.InvitePayload
|
|
|
|
}
|
|
|
|
|
|
|
|
type updateInviteResponse struct {
|
|
|
|
Invite *fleet.Invite `json:"invite"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r updateInviteResponse) error() error { return r.Err }
|
|
|
|
|
2022-12-27 14:26:59 +00:00
|
|
|
func updateInviteEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
|
2021-11-11 20:33:06 +00:00
|
|
|
req := request.(*updateInviteRequest)
|
|
|
|
invite, err := svc.UpdateInvite(ctx, req.ID, req.InvitePayload)
|
|
|
|
if err != nil {
|
|
|
|
return updateInviteResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return updateInviteResponse{Invite: invite}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) UpdateInvite(ctx context.Context, id uint, payload fleet.InvitePayload) (*fleet.Invite, error) {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionWrite); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
invite, err := svc.ds.Invite(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-28 15:38:57 +00:00
|
|
|
if payload.Email != nil && *payload.Email != invite.Email {
|
2022-02-28 12:34:44 +00:00
|
|
|
switch _, err := svc.ds.UserByEmail(ctx, *payload.Email); {
|
|
|
|
case err == nil:
|
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 16:44:06 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, newAlreadyExistsError())
|
2022-02-28 12:34:44 +00:00
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
// OK
|
|
|
|
default:
|
|
|
|
return nil, ctxerr.Wrap(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch _, err = svc.ds.InviteByEmail(ctx, *payload.Email); {
|
|
|
|
case err == nil:
|
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 16:44:06 +00:00
|
|
|
return nil, ctxerr.Wrap(ctx, newAlreadyExistsError())
|
2022-02-28 12:34:44 +00:00
|
|
|
case errors.Is(err, sql.ErrNoRows):
|
|
|
|
// OK
|
|
|
|
default:
|
|
|
|
return nil, ctxerr.Wrap(ctx, err)
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:33:06 +00:00
|
|
|
invite.Email = *payload.Email
|
|
|
|
}
|
|
|
|
if payload.Name != nil {
|
|
|
|
invite.Name = *payload.Name
|
|
|
|
}
|
|
|
|
if payload.Position != nil {
|
|
|
|
invite.Position = *payload.Position
|
|
|
|
}
|
|
|
|
if payload.SSOEnabled != nil {
|
|
|
|
invite.SSOEnabled = *payload.SSOEnabled
|
|
|
|
}
|
2022-03-28 15:38:57 +00:00
|
|
|
|
|
|
|
if payload.GlobalRole.Valid || len(payload.Teams) > 0 {
|
|
|
|
if err := fleet.ValidateRole(payload.GlobalRole.Ptr(), payload.Teams); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
invite.GlobalRole = payload.GlobalRole
|
|
|
|
invite.Teams = payload.Teams
|
|
|
|
}
|
2021-11-11 20:33:06 +00:00
|
|
|
|
|
|
|
return svc.ds.UpdateInvite(ctx, id, invite)
|
|
|
|
}
|
2022-02-15 20:22:19 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete invite
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deleteInviteRequest struct {
|
|
|
|
ID uint `url:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteInviteResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deleteInviteResponse) error() error { return r.Err }
|
|
|
|
|
2022-12-27 14:26:59 +00:00
|
|
|
func deleteInviteEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
|
2022-02-15 20:22:19 +00:00
|
|
|
req := request.(*deleteInviteRequest)
|
|
|
|
err := svc.DeleteInvite(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deleteInviteResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deleteInviteResponse{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) DeleteInvite(ctx context.Context, id uint) error {
|
|
|
|
if err := svc.authz.Authorize(ctx, &fleet.Invite{}, fleet.ActionWrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return svc.ds.DeleteInvite(ctx, id)
|
|
|
|
}
|
2022-03-08 16:27:38 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Verify invite
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type verifyInviteRequest struct {
|
|
|
|
Token string `url:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type verifyInviteResponse struct {
|
|
|
|
Invite *fleet.Invite `json:"invite"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r verifyInviteResponse) error() error { return r.Err }
|
|
|
|
|
2022-12-27 14:26:59 +00:00
|
|
|
func verifyInviteEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (errorer, error) {
|
2022-03-08 16:27:38 +00:00
|
|
|
req := request.(*verifyInviteRequest)
|
|
|
|
invite, err := svc.VerifyInvite(ctx, req.Token)
|
|
|
|
if err != nil {
|
|
|
|
return verifyInviteResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return verifyInviteResponse{Invite: invite}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (svc *Service) VerifyInvite(ctx context.Context, token string) (*fleet.Invite, error) {
|
|
|
|
// skipauth: There is no viewer context at this point. We rely on verifying
|
|
|
|
// the invite for authNZ.
|
|
|
|
svc.authz.SkipAuthorization(ctx)
|
|
|
|
|
|
|
|
logging.WithExtras(ctx, "token", token)
|
|
|
|
|
|
|
|
invite, err := svc.ds.InviteByToken(ctx, token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if invite.Token != token {
|
|
|
|
return nil, fleet.NewInvalidArgumentError("invite_token", "Invite Token does not match Email Address.")
|
|
|
|
}
|
|
|
|
|
|
|
|
expiresAt := invite.CreatedAt.Add(svc.config.App.InviteTokenValidityPeriod)
|
|
|
|
if svc.clock.Now().After(expiresAt) {
|
|
|
|
return nil, fleet.NewInvalidArgumentError("invite_token", "Invite token has expired.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return invite, nil
|
|
|
|
}
|