2016-11-09 17:19:07 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-11-09 17:19:07 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2021-05-31 16:02:05 +00:00
|
|
|
"github.com/fleetdm/fleet/server/ptr"
|
2021-04-07 01:27:10 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2018-05-17 14:29:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-09 17:19:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type setupRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Admin *fleet.UserPayload `json:"admin"`
|
|
|
|
OrgInfo *fleet.OrgInfo `json:"org_info"`
|
|
|
|
KolideServerURL *string `json:"kolide_server_url,omitempty"`
|
|
|
|
EnrollSecret *string `json:"osquery_enroll_secret,omitempty"`
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type setupResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Admin *fleet.User `json:"admin,omitempty"`
|
|
|
|
OrgInfo *fleet.OrgInfo `json:"org_info,omitempty"`
|
|
|
|
KolideServerURL *string `json:"kolide_server_url"`
|
|
|
|
EnrollSecret *string `json:"osquery_enroll_secret"`
|
|
|
|
Token *string `json:"token,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r setupResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeSetupEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-11-09 17:19:07 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
var (
|
2021-06-06 22:07:29 +00:00
|
|
|
admin *fleet.User
|
|
|
|
config *fleet.AppConfig
|
|
|
|
configPayload fleet.AppConfigPayload
|
2016-11-09 17:19:07 +00:00
|
|
|
err error
|
|
|
|
)
|
|
|
|
req := request.(setupRequest)
|
|
|
|
if req.OrgInfo != nil {
|
|
|
|
configPayload.OrgInfo = req.OrgInfo
|
|
|
|
}
|
2021-06-06 22:07:29 +00:00
|
|
|
configPayload.ServerSettings = &fleet.ServerSettings{}
|
2016-11-09 17:19:07 +00:00
|
|
|
if req.KolideServerURL != nil {
|
2017-01-20 19:48:54 +00:00
|
|
|
configPayload.ServerSettings.KolideServerURL = req.KolideServerURL
|
|
|
|
}
|
2016-11-09 17:19:07 +00:00
|
|
|
config, err = svc.NewAppConfig(ctx, configPayload)
|
|
|
|
if err != nil {
|
|
|
|
return setupResponse{Err: err}, nil
|
|
|
|
}
|
2021-05-03 16:31:51 +00:00
|
|
|
|
|
|
|
if req.Admin == nil {
|
|
|
|
return setupResponse{Err: errors.New("setup request must provide admin")}, nil
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:48:54 +00:00
|
|
|
// creating the user should be the last action. If there's a user
|
|
|
|
// present and other errors occur, the setup endpoint closes.
|
2021-05-03 16:31:51 +00:00
|
|
|
adminPayload := *req.Admin
|
|
|
|
if adminPayload.Email == nil || *adminPayload.Email == "" {
|
|
|
|
err := errors.Errorf("admin email cannot be empty")
|
|
|
|
return setupResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
if adminPayload.Password == nil || *adminPayload.Password == "" {
|
|
|
|
err := errors.Errorf("admin password cannot be empty")
|
|
|
|
return setupResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
// Make the user an admin
|
2021-06-06 22:07:29 +00:00
|
|
|
adminPayload.GlobalRole = ptr.String(fleet.RoleAdmin)
|
2021-06-03 23:24:15 +00:00
|
|
|
admin, err = svc.CreateInitialUser(ctx, adminPayload)
|
2021-05-03 16:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return setupResponse{Err: err}, nil
|
2017-01-20 19:48:54 +00:00
|
|
|
}
|
|
|
|
|
2017-01-17 20:24:13 +00:00
|
|
|
// If everything works to this point, log the user in and return token. If
|
|
|
|
// the login fails for some reason, ignore the error and don't return
|
|
|
|
// a token, forcing the user to log in manually
|
|
|
|
token := new(string)
|
|
|
|
_, *token, err = svc.Login(ctx, *req.Admin.Username, *req.Admin.Password)
|
|
|
|
if err != nil {
|
|
|
|
token = nil
|
|
|
|
}
|
2016-11-09 17:19:07 +00:00
|
|
|
return setupResponse{
|
|
|
|
Admin: admin,
|
2021-06-06 22:07:29 +00:00
|
|
|
OrgInfo: &fleet.OrgInfo{
|
2016-11-09 17:19:07 +00:00
|
|
|
OrgName: &config.OrgName,
|
|
|
|
OrgLogoURL: &config.OrgLogoURL,
|
|
|
|
},
|
|
|
|
KolideServerURL: &config.KolideServerURL,
|
2017-01-17 20:24:13 +00:00
|
|
|
Token: token,
|
2016-11-09 17:19:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|