Handle missing server_url in setup (#1093)

Improve error handling to avoid a nil pointer panic in the setup endpoint.
This commit is contained in:
Zach Wasserman 2021-06-15 11:25:52 -07:00 committed by GitHub
parent 2dbeea528e
commit 233cce6120
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View File

@ -10,19 +10,19 @@ import (
)
type setupRequest struct {
Admin *fleet.UserPayload `json:"admin"`
OrgInfo *fleet.OrgInfo `json:"org_info"`
ServerURL *string `json:"server_url,omitempty"`
EnrollSecret *string `json:"osquery_enroll_secret,omitempty"`
Admin *fleet.UserPayload `json:"admin"`
OrgInfo *fleet.OrgInfo `json:"org_info"`
ServerURL *string `json:"server_url,omitempty"`
EnrollSecret *string `json:"osquery_enroll_secret,omitempty"`
}
type setupResponse struct {
Admin *fleet.User `json:"admin,omitempty"`
OrgInfo *fleet.OrgInfo `json:"org_info,omitempty"`
ServerURL *string `json:"server_url"`
EnrollSecret *string `json:"osquery_enroll_secret"`
Token *string `json:"token,omitempty"`
Err error `json:"error,omitempty"`
Admin *fleet.User `json:"admin,omitempty"`
OrgInfo *fleet.OrgInfo `json:"org_info,omitempty"`
ServerURL *string `json:"server_url"`
EnrollSecret *string `json:"osquery_enroll_secret"`
Token *string `json:"token,omitempty"`
Err error `json:"error,omitempty"`
}
func (r setupResponse) error() error { return r.Err }
@ -85,7 +85,7 @@ func makeSetupEndpoint(svc fleet.Service) endpoint.Endpoint {
OrgLogoURL: &config.OrgLogoURL,
},
ServerURL: &config.ServerURL,
Token: token,
Token: token,
}, nil
}
}

View File

@ -11,7 +11,8 @@ import (
func (mw validationMiddleware) NewAppConfig(ctx context.Context, payload fleet.AppConfigPayload) (*fleet.AppConfig, error) {
invalid := &fleet.InvalidArgumentError{}
var serverURLString string
if payload.ServerSettings == nil {
if payload.ServerSettings == nil || payload.ServerSettings.ServerURL == nil ||
*payload.ServerSettings.ServerURL == "" {
invalid.Append("server_url", "missing required argument")
} else {
serverURLString = cleanupURL(*payload.ServerSettings.ServerURL)