2016-11-09 17:19:07 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2018-05-04 16:53:21 +00:00
|
|
|
"errors"
|
2016-11-09 17:19:07 +00:00
|
|
|
"net/url"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2016-11-09 17:19:07 +00:00
|
|
|
)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func (mw validationMiddleware) NewAppConfig(ctx context.Context, payload fleet.AppConfigPayload) (*fleet.AppConfig, error) {
|
|
|
|
invalid := &fleet.InvalidArgumentError{}
|
2016-11-09 17:19:07 +00:00
|
|
|
var serverURLString string
|
2021-06-15 18:25:52 +00:00
|
|
|
if payload.ServerSettings == nil || payload.ServerSettings.ServerURL == nil ||
|
|
|
|
*payload.ServerSettings.ServerURL == "" {
|
2021-06-06 23:58:23 +00:00
|
|
|
invalid.Append("server_url", "missing required argument")
|
2016-11-09 17:19:07 +00:00
|
|
|
} else {
|
2021-06-06 23:58:23 +00:00
|
|
|
serverURLString = cleanupURL(*payload.ServerSettings.ServerURL)
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
2021-06-06 23:58:23 +00:00
|
|
|
if err := validateServerURL(serverURLString); err != nil {
|
|
|
|
invalid.Append("server_url", err.Error())
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
if invalid.HasErrors() {
|
|
|
|
return nil, invalid
|
|
|
|
}
|
|
|
|
return mw.Service.NewAppConfig(ctx, payload)
|
|
|
|
}
|
|
|
|
|
2021-06-06 23:58:23 +00:00
|
|
|
func validateServerURL(urlString string) error {
|
2016-11-09 17:19:07 +00:00
|
|
|
serverURL, err := url.Parse(urlString)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if serverURL.Scheme != "https" {
|
2018-05-04 16:53:21 +00:00
|
|
|
return errors.New("url scheme must be https")
|
2016-11-09 17:19:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|