Fix admin setup (#701)

Fixes a null pointer issue and clarifies setup logic.
This commit is contained in:
Zach Wasserman 2021-05-03 09:31:51 -07:00 committed by GitHub
parent 665345c334
commit 596e017d44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -47,23 +47,27 @@ func makeSetupEndpoint(svc kolide.Service) endpoint.Endpoint {
if err != nil {
return setupResponse{Err: err}, nil
}
if req.Admin == nil {
return setupResponse{Err: errors.New("setup request must provide admin")}, nil
}
// 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 {
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
}
// Make the user an admin
req.Admin.GlobalRole = null.StringFrom("admin")
admin, err = svc.CreateUser(ctx, *req.Admin)
if err != nil {
return setupResponse{Err: err}, nil
}
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
adminPayload.GlobalRole = null.StringFrom("admin")
admin, err = svc.CreateUser(ctx, adminPayload)
if err != nil {
return setupResponse{Err: err}, nil
}
// If everything works to this point, log the user in and return token. If