2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-05 14:15:58 +00:00
|
|
|
|
|
|
|
|
import (
|
2024-09-05 11:47:34 +00:00
|
|
|
"fmt"
|
2016-09-05 14:15:58 +00:00
|
|
|
"html/template"
|
2022-10-12 13:19:21 +00:00
|
|
|
"io"
|
2016-09-05 14:15:58 +00:00
|
|
|
"net/http"
|
2024-09-05 11:47:34 +00:00
|
|
|
"net/url"
|
2016-09-05 14:15:58 +00:00
|
|
|
|
|
|
|
|
assetfs "github.com/elazarl/go-bindata-assetfs"
|
2025-08-18 16:31:53 +00:00
|
|
|
shared_mdm "github.com/fleetdm/fleet/v4/pkg/mdm"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/bindata"
|
2024-10-18 10:10:17 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2025-02-18 17:09:43 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/service/middleware/endpoint_utils"
|
2024-06-17 13:27:31 +00:00
|
|
|
"github.com/go-kit/log"
|
2025-01-22 14:51:45 +00:00
|
|
|
"github.com/klauspost/compress/gzhttp"
|
2016-09-05 14:15:58 +00:00
|
|
|
)
|
|
|
|
|
|
2017-01-16 15:16:50 +00:00
|
|
|
func newBinaryFileSystem(root string) *assetfs.AssetFS {
|
|
|
|
|
return &assetfs.AssetFS{
|
2020-03-30 02:22:04 +00:00
|
|
|
Asset: bindata.Asset,
|
|
|
|
|
AssetDir: bindata.AssetDir,
|
|
|
|
|
AssetInfo: bindata.AssetInfo,
|
2017-01-16 15:16:50 +00:00
|
|
|
Prefix: root,
|
2016-09-05 14:15:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-01 10:27:12 +00:00
|
|
|
func ServeFrontend(urlPrefix string, sandbox bool, logger log.Logger) http.Handler {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr := func(w http.ResponseWriter, err string) {
|
|
|
|
|
logger.Log("err", err)
|
|
|
|
|
http.Error(w, err, http.StatusInternalServerError)
|
|
|
|
|
}
|
2016-09-07 19:19:54 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2025-02-18 17:09:43 +00:00
|
|
|
endpoint_utils.WriteBrowserSecurityHeaders(w)
|
2022-10-12 13:19:21 +00:00
|
|
|
|
2024-02-14 15:40:43 +00:00
|
|
|
// The following check is to prevent a misconfigured osquery from submitting
|
|
|
|
|
// data to the root endpoint (the osquery remote API uses POST for all its endpoints).
|
|
|
|
|
// See https://github.com/fleetdm/fleet/issues/16182.
|
|
|
|
|
if r.Method == "POST" {
|
|
|
|
|
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-19 23:06:41 +00:00
|
|
|
fs := newBinaryFileSystem("/frontend")
|
|
|
|
|
file, err := fs.Open("templates/react.tmpl")
|
2016-09-07 19:19:54 +00:00
|
|
|
if err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "load react template: "+err.Error())
|
2016-09-19 23:06:41 +00:00
|
|
|
return
|
|
|
|
|
}
|
2022-10-12 13:19:21 +00:00
|
|
|
data, err := io.ReadAll(file)
|
2016-09-19 23:06:41 +00:00
|
|
|
if err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "read bindata file: "+err.Error())
|
2016-09-19 23:06:41 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
t, err := template.New("react").Parse(string(data))
|
|
|
|
|
if err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "create react template: "+err.Error())
|
2016-09-19 23:06:41 +00:00
|
|
|
return
|
|
|
|
|
}
|
2022-08-01 10:27:12 +00:00
|
|
|
serverType := "on-premise"
|
|
|
|
|
if sandbox {
|
|
|
|
|
serverType = "sandbox"
|
|
|
|
|
}
|
|
|
|
|
if err := t.Execute(w, struct {
|
|
|
|
|
URLPrefix string
|
|
|
|
|
ServerType string
|
|
|
|
|
}{
|
|
|
|
|
URLPrefix: urlPrefix,
|
|
|
|
|
ServerType: serverType,
|
|
|
|
|
}); err != nil {
|
2017-03-04 00:49:55 +00:00
|
|
|
herr(w, "execute react template: "+err.Error())
|
2016-09-07 19:19:54 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:31:53 +00:00
|
|
|
// ServeEndUserEnrollOTA implements the entrypoint handler for the /enroll
|
|
|
|
|
// path, used to add hosts in "BYOD" mode (currently, iPhone/iPad/Android).
|
2025-02-27 19:35:42 +00:00
|
|
|
func ServeEndUserEnrollOTA(
|
|
|
|
|
svc fleet.Service,
|
|
|
|
|
urlPrefix string,
|
2025-03-11 15:06:25 +00:00
|
|
|
ds fleet.Datastore,
|
2025-02-27 19:35:42 +00:00
|
|
|
logger log.Logger,
|
|
|
|
|
) http.Handler {
|
2024-09-05 11:47:34 +00:00
|
|
|
herr := func(w http.ResponseWriter, err string) {
|
|
|
|
|
logger.Log("err", err)
|
|
|
|
|
http.Error(w, err, http.StatusInternalServerError)
|
|
|
|
|
}
|
2025-08-18 16:31:53 +00:00
|
|
|
|
2024-09-05 11:47:34 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2025-02-18 17:09:43 +00:00
|
|
|
endpoint_utils.WriteBrowserSecurityHeaders(w)
|
2024-10-18 10:10:17 +00:00
|
|
|
setupRequired, err := svc.SetupRequired(r.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
herr(w, "setup required err: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if setupRequired {
|
|
|
|
|
herr(w, "fleet instance not setup")
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-09-05 11:47:34 +00:00
|
|
|
|
2025-03-11 15:06:25 +00:00
|
|
|
appCfg, err := ds.AppConfig(r.Context())
|
|
|
|
|
if err != nil {
|
|
|
|
|
herr(w, "load appconfig err: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:31:53 +00:00
|
|
|
errorMsg := r.URL.Query().Get("error")
|
|
|
|
|
if errorMsg != "" {
|
|
|
|
|
if err := renderEnrollPage(w, appCfg, urlPrefix, "", errorMsg); err != nil {
|
|
|
|
|
herr(w, err.Error())
|
|
|
|
|
}
|
2024-09-05 11:47:34 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:31:53 +00:00
|
|
|
enrollSecret := r.URL.Query().Get("enroll_secret")
|
|
|
|
|
if enrollSecret == "" {
|
|
|
|
|
if err := renderEnrollPage(w, appCfg, urlPrefix, "", "This URL is invalid. : Enroll secret is invalid. Please contact your IT admin."); err != nil {
|
|
|
|
|
herr(w, err.Error())
|
|
|
|
|
}
|
2024-09-05 11:47:34 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:31:53 +00:00
|
|
|
authRequired, err := shared_mdm.RequiresEnrollOTAAuthentication(r.Context(), ds,
|
|
|
|
|
enrollSecret, appCfg.MDM.MacOSSetup.EnableEndUserAuthentication)
|
2024-09-05 11:47:34 +00:00
|
|
|
if err != nil {
|
2025-08-18 16:31:53 +00:00
|
|
|
herr(w, "check if authentication is required err: "+err.Error())
|
2024-09-05 11:47:34 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:31:53 +00:00
|
|
|
if authRequired {
|
|
|
|
|
// check if authentication cookie is present, in which case we go ahead with
|
|
|
|
|
// offering the enrollment profile to download.
|
|
|
|
|
var cookieIdPRef string
|
|
|
|
|
if byodCookie, _ := r.Cookie(shared_mdm.BYODIdpCookieName); byodCookie != nil {
|
|
|
|
|
cookieIdPRef = byodCookie.Value
|
|
|
|
|
|
|
|
|
|
// if the cookie is present, we should also receive a (matching) enroll reference
|
|
|
|
|
if cookieIdPRef != "" {
|
|
|
|
|
enrollRef := r.URL.Query().Get("enrollment_reference")
|
|
|
|
|
if cookieIdPRef != enrollRef {
|
|
|
|
|
cookieIdPRef = "" // cookie does not match the enroll reference, so we ignore it and require authentication
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if cookieIdPRef == "" {
|
|
|
|
|
// IdP authentication has not been completed yet, initiate it by
|
|
|
|
|
// redirecting to the configured IdP provider.
|
|
|
|
|
if err := initiateOTAEnrollSSO(svc, w, r, enrollSecret); err != nil {
|
|
|
|
|
herr(w, "initiate IdP SSO authentication err: "+err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-09-05 11:47:34 +00:00
|
|
|
}
|
2025-08-18 16:31:53 +00:00
|
|
|
|
|
|
|
|
// if we get here, IdP SSO authentication is either not required, or has
|
|
|
|
|
// been successfully completed (we have a cookie with the IdP account
|
|
|
|
|
// reference).
|
|
|
|
|
if err := renderEnrollPage(w, appCfg, urlPrefix, enrollSecret, ""); err != nil {
|
|
|
|
|
herr(w, err.Error())
|
2024-09-05 11:47:34 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func generateEnrollOTAURL(fleetURL string, enrollSecret string) (string, error) {
|
|
|
|
|
path, err := url.JoinPath(fleetURL, "/api/v1/fleet/enrollment_profiles/ota")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("creating path for end user ota enrollment url: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enrollURL, err := url.Parse(path)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", fmt.Errorf("parsing end user ota enrollment url: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q := enrollURL.Query()
|
|
|
|
|
q.Set("enroll_secret", enrollSecret)
|
|
|
|
|
enrollURL.RawQuery = q.Encode()
|
|
|
|
|
return enrollURL.String(), nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-18 16:31:53 +00:00
|
|
|
func renderEnrollPage(w io.Writer, appCfg *fleet.AppConfig, urlPrefix, enrollSecret, errorMessage string) error {
|
|
|
|
|
fs := newBinaryFileSystem("/frontend")
|
|
|
|
|
file, err := fs.Open("templates/enroll-ota.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("load enroll ota template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := io.ReadAll(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("read bindata file: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t, err := template.New("enroll-ota").Parse(string(data))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("create react template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enrollURL, err := generateEnrollOTAURL(urlPrefix, enrollSecret)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("generate enroll ota url: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if err := t.Execute(w, struct {
|
|
|
|
|
EnrollURL string
|
|
|
|
|
URLPrefix string
|
|
|
|
|
ErrorMessage string
|
|
|
|
|
AndroidMDMEnabled bool
|
|
|
|
|
MacMDMEnabled bool
|
|
|
|
|
AndroidFeatureEnabled bool
|
|
|
|
|
}{
|
|
|
|
|
URLPrefix: urlPrefix,
|
|
|
|
|
EnrollURL: enrollURL,
|
|
|
|
|
ErrorMessage: errorMessage,
|
|
|
|
|
AndroidMDMEnabled: appCfg.MDM.AndroidEnabledAndConfigured,
|
|
|
|
|
MacMDMEnabled: appCfg.MDM.EnabledAndConfigured,
|
|
|
|
|
AndroidFeatureEnabled: true,
|
|
|
|
|
}); err != nil {
|
|
|
|
|
return fmt.Errorf("execute react template: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func initiateOTAEnrollSSO(svc fleet.Service, w http.ResponseWriter, r *http.Request, enrollSecret string) error {
|
|
|
|
|
ssnID, ssnDurationSecs, idpURL, err := svc.InitiateMDMSSO(r.Context(), "ota_enroll", "/enroll?enroll_secret="+url.QueryEscape(enrollSecret))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
setSSOCookie(w, ssnID, ssnDurationSecs)
|
|
|
|
|
http.Redirect(w, r, idpURL, http.StatusSeeOther)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-07 19:19:54 +00:00
|
|
|
func ServeStaticAssets(path string) http.Handler {
|
2025-01-22 02:15:08 +00:00
|
|
|
contentTypes := []string{"text/javascript", "text/css"}
|
|
|
|
|
withoutGzip := http.StripPrefix(path, http.FileServer(newBinaryFileSystem("/assets")))
|
|
|
|
|
|
2025-01-22 14:51:45 +00:00
|
|
|
withOpts, err := gzhttp.NewWrapper(gzhttp.ContentTypes(contentTypes))
|
2025-01-22 02:15:08 +00:00
|
|
|
if err != nil { // fall back to serving without gzip if serving with gzip somehow fails
|
|
|
|
|
return withoutGzip
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return withOpts(withoutGzip)
|
2016-09-05 14:15:58 +00:00
|
|
|
}
|