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
|
|
|
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
2017-06-22 19:50:45 +00:00
|
|
|
"github.com/kolide/fleet/server/kolide"
|
2018-05-17 14:29:04 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-11-09 17:19:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type setupRequest struct {
|
|
|
|
Admin *kolide.UserPayload `json:"admin"`
|
|
|
|
OrgInfo *kolide.OrgInfo `json:"org_info"`
|
2018-05-04 16:53:21 +00:00
|
|
|
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 {
|
|
|
|
Admin *kolide.User `json:"admin,omitempty"`
|
|
|
|
OrgInfo *kolide.OrgInfo `json:"org_info,omitempty"`
|
|
|
|
KolideServerURL *string `json:"kolide_server_url"`
|
2017-01-20 19:48:54 +00:00
|
|
|
EnrollSecret *string `json:"osquery_enroll_secret"`
|
2017-01-17 20:24:13 +00:00
|
|
|
Token *string `json:"token,omitempty"`
|
2016-11-09 17:19:07 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r setupResponse) error() error { return r.Err }
|
|
|
|
|
|
|
|
func makeSetupEndpoint(svc kolide.Service) endpoint.Endpoint {
|
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
var (
|
|
|
|
admin *kolide.User
|
|
|
|
config *kolide.AppConfig
|
|
|
|
configPayload kolide.AppConfigPayload
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
req := request.(setupRequest)
|
|
|
|
if req.OrgInfo != nil {
|
|
|
|
configPayload.OrgInfo = req.OrgInfo
|
|
|
|
}
|
2017-01-20 19:48:54 +00:00
|
|
|
configPayload.ServerSettings = &kolide.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
|
|
|
|
}
|
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.
|
|
|
|
if req.Admin != nil {
|
2018-05-17 14:29:04 +00:00
|
|
|
if *req.Admin.Email == "" {
|
|
|
|
err := errors.Errorf("admin email cannot be empty")
|
|
|
|
return setupResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
if *req.Admin.Password == "" {
|
|
|
|
err := errors.Errorf("admin password cannot be empty")
|
|
|
|
return setupResponse{Err: err}, nil
|
|
|
|
}
|
2020-11-05 01:06:55 +00:00
|
|
|
admin, err = svc.CreateUser(ctx, *req.Admin)
|
2017-01-20 19:48:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return setupResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
OrgInfo: &kolide.OrgInfo{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|