2021-06-06 22:07:29 +00:00
|
|
|
// Package service holds the implementation of the fleet interface and HTTP
|
|
|
|
// endpoints for the API
|
2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
import (
|
2022-01-18 14:29:57 +00:00
|
|
|
"context"
|
2021-11-22 14:13:26 +00:00
|
|
|
"fmt"
|
2019-08-02 21:08:42 +00:00
|
|
|
"html/template"
|
2021-04-12 23:22:22 +00:00
|
|
|
"sync"
|
2022-01-18 14:29:57 +00:00
|
|
|
"time"
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2016-09-21 03:08:11 +00:00
|
|
|
"github.com/WatchBeam/clock"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/authz"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/config"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2023-04-05 14:50:36 +00:00
|
|
|
apple_mdm "github.com/fleetdm/fleet/v4/server/mdm/apple"
|
2023-06-29 22:31:53 +00:00
|
|
|
microsoft_mdm "github.com/fleetdm/fleet/v4/server/mdm/microsoft"
|
2024-01-12 02:28:48 +00:00
|
|
|
nanomdm_push "github.com/fleetdm/fleet/v4/server/mdm/nanomdm/push"
|
|
|
|
nanomdm_storage "github.com/fleetdm/fleet/v4/server/mdm/nanomdm/storage"
|
2021-11-01 18:13:16 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/service/async"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/sso"
|
2020-12-16 17:16:55 +00:00
|
|
|
kitlog "github.com/go-kit/kit/log"
|
2022-10-05 22:53:54 +00:00
|
|
|
nanodep_storage "github.com/micromdm/nanodep/storage"
|
2016-09-01 04:51:38 +00:00
|
|
|
)
|
2016-08-28 03:59:17 +00:00
|
|
|
|
2022-03-21 16:29:52 +00:00
|
|
|
var _ fleet.Service = (*Service)(nil)
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
// Service is the struct implementing fleet.Service. Create a new one with NewService.
|
2021-06-01 00:07:51 +00:00
|
|
|
type Service struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
ds fleet.Datastore
|
2021-11-01 18:13:16 +00:00
|
|
|
task *async.Task
|
2021-06-06 22:07:29 +00:00
|
|
|
carveStore fleet.CarveStore
|
2022-07-18 16:44:30 +00:00
|
|
|
installerStore fleet.InstallerStore
|
2021-06-06 22:07:29 +00:00
|
|
|
resultStore fleet.QueryResultStore
|
|
|
|
liveQueryStore fleet.LiveQueryStore
|
2021-06-01 00:07:51 +00:00
|
|
|
logger kitlog.Logger
|
2021-06-06 22:07:29 +00:00
|
|
|
config config.FleetConfig
|
2021-06-01 00:07:51 +00:00
|
|
|
clock clock.Clock
|
|
|
|
|
2022-12-23 22:04:13 +00:00
|
|
|
osqueryLogWriter *OsqueryLogger
|
2021-06-01 00:07:51 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
mailService fleet.MailService
|
2021-06-01 00:07:51 +00:00
|
|
|
ssoSessionStore sso.SessionStore
|
|
|
|
|
2022-06-13 20:29:32 +00:00
|
|
|
failingPolicySet fleet.FailingPolicySet
|
|
|
|
enrollHostLimiter fleet.EnrollHostLimiter
|
2021-12-23 21:26:55 +00:00
|
|
|
|
2021-06-03 23:24:15 +00:00
|
|
|
authz *authz.Authorizer
|
2022-01-18 14:29:57 +00:00
|
|
|
|
2022-02-03 17:56:11 +00:00
|
|
|
jitterMu *sync.Mutex
|
|
|
|
jitterH map[time.Duration]*jitterHashTable
|
2022-03-21 16:29:52 +00:00
|
|
|
|
|
|
|
geoIP fleet.GeoIP
|
2022-08-30 11:13:09 +00:00
|
|
|
|
|
|
|
*fleet.EnterpriseOverrides
|
2022-10-05 22:53:54 +00:00
|
|
|
|
2023-02-17 19:26:51 +00:00
|
|
|
depStorage nanodep_storage.AllStorage
|
|
|
|
mdmStorage nanomdm_storage.AllStorage
|
|
|
|
mdmPushService nanomdm_push.Pusher
|
|
|
|
mdmPushCertTopic string
|
2023-04-05 14:50:36 +00:00
|
|
|
mdmAppleCommander *apple_mdm.MDMAppleCommander
|
2022-11-28 19:28:06 +00:00
|
|
|
|
|
|
|
cronSchedulesService fleet.CronSchedulesService
|
2023-06-29 22:31:53 +00:00
|
|
|
|
|
|
|
wstepCertManager microsoft_mdm.CertManager
|
2022-03-21 16:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 22:14:18 +00:00
|
|
|
func (svc *Service) LookupGeoIP(ctx context.Context, ip string) *fleet.GeoLocation {
|
|
|
|
return svc.geoIP.Lookup(ctx, ip)
|
2021-06-01 00:07:51 +00:00
|
|
|
}
|
|
|
|
|
2023-01-03 22:14:18 +00:00
|
|
|
func (svc *Service) SetEnterpriseOverrides(overrides fleet.EnterpriseOverrides) {
|
|
|
|
svc.EnterpriseOverrides = &overrides
|
2022-08-30 11:13:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 22:04:13 +00:00
|
|
|
// OsqueryLogger holds osqueryd's status and result loggers.
|
|
|
|
type OsqueryLogger struct {
|
|
|
|
// Status holds the osqueryd's status logger.
|
|
|
|
//
|
|
|
|
// See https://osquery.readthedocs.io/en/stable/deployment/logging/#status-logs
|
|
|
|
Status fleet.JSONLogger
|
|
|
|
// Result holds the osqueryd's result logger.
|
|
|
|
//
|
|
|
|
// See https://osquery.readthedocs.io/en/stable/deployment/logging/#results-logs
|
|
|
|
Result fleet.JSONLogger
|
|
|
|
}
|
|
|
|
|
2016-09-01 04:51:38 +00:00
|
|
|
// NewService creates a new service from the config struct
|
2021-09-21 17:21:44 +00:00
|
|
|
func NewService(
|
2022-01-18 14:29:57 +00:00
|
|
|
ctx context.Context,
|
2021-09-21 17:21:44 +00:00
|
|
|
ds fleet.Datastore,
|
2021-11-01 18:13:16 +00:00
|
|
|
task *async.Task,
|
2021-09-21 17:21:44 +00:00
|
|
|
resultStore fleet.QueryResultStore,
|
|
|
|
logger kitlog.Logger,
|
2022-12-23 22:04:13 +00:00
|
|
|
osqueryLogger *OsqueryLogger,
|
2021-09-21 17:21:44 +00:00
|
|
|
config config.FleetConfig,
|
|
|
|
mailService fleet.MailService,
|
|
|
|
c clock.Clock,
|
|
|
|
sso sso.SessionStore,
|
|
|
|
lq fleet.LiveQueryStore,
|
|
|
|
carveStore fleet.CarveStore,
|
2022-07-18 16:44:30 +00:00
|
|
|
installerStore fleet.InstallerStore,
|
2021-12-23 21:26:55 +00:00
|
|
|
failingPolicySet fleet.FailingPolicySet,
|
2022-03-21 16:29:52 +00:00
|
|
|
geoIP fleet.GeoIP,
|
2022-06-13 20:29:32 +00:00
|
|
|
enrollHostLimiter fleet.EnrollHostLimiter,
|
2022-10-05 22:53:54 +00:00
|
|
|
depStorage nanodep_storage.AllStorage,
|
2024-02-13 18:03:53 +00:00
|
|
|
mdmStorage fleet.MDMAppleStore,
|
2022-10-05 22:53:54 +00:00
|
|
|
mdmPushService nanomdm_push.Pusher,
|
|
|
|
mdmPushCertTopic string,
|
2022-11-28 19:28:06 +00:00
|
|
|
cronSchedulesService fleet.CronSchedulesService,
|
2023-06-29 22:31:53 +00:00
|
|
|
wstepCertManager microsoft_mdm.CertManager,
|
2021-09-21 17:21:44 +00:00
|
|
|
) (fleet.Service, error) {
|
2021-06-03 23:24:15 +00:00
|
|
|
authorizer, err := authz.NewAuthorizer()
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("new authorizer: %w", err)
|
2021-06-03 23:24:15 +00:00
|
|
|
}
|
|
|
|
|
2022-01-18 14:29:57 +00:00
|
|
|
svc := &Service{
|
2023-02-17 19:26:51 +00:00
|
|
|
ds: ds,
|
|
|
|
task: task,
|
|
|
|
carveStore: carveStore,
|
|
|
|
installerStore: installerStore,
|
|
|
|
resultStore: resultStore,
|
|
|
|
liveQueryStore: lq,
|
|
|
|
logger: logger,
|
|
|
|
config: config,
|
|
|
|
clock: c,
|
|
|
|
osqueryLogWriter: osqueryLogger,
|
|
|
|
mailService: mailService,
|
|
|
|
ssoSessionStore: sso,
|
|
|
|
failingPolicySet: failingPolicySet,
|
|
|
|
authz: authorizer,
|
|
|
|
jitterH: make(map[time.Duration]*jitterHashTable),
|
|
|
|
jitterMu: new(sync.Mutex),
|
|
|
|
geoIP: geoIP,
|
|
|
|
enrollHostLimiter: enrollHostLimiter,
|
|
|
|
depStorage: depStorage,
|
|
|
|
// TODO: remove mdmStorage and mdmPushService when
|
|
|
|
// we remove deprecated top-level service methods
|
|
|
|
// from the prototype.
|
2022-11-28 19:28:06 +00:00
|
|
|
mdmStorage: mdmStorage,
|
|
|
|
mdmPushService: mdmPushService,
|
|
|
|
mdmPushCertTopic: mdmPushCertTopic,
|
2023-04-05 14:50:36 +00:00
|
|
|
mdmAppleCommander: apple_mdm.NewMDMAppleCommander(mdmStorage, mdmPushService),
|
2022-11-28 19:28:06 +00:00
|
|
|
cronSchedulesService: cronSchedulesService,
|
2023-06-29 22:31:53 +00:00
|
|
|
wstepCertManager: wstepCertManager,
|
2016-08-28 03:59:17 +00:00
|
|
|
}
|
2022-01-18 14:29:57 +00:00
|
|
|
return validationMiddleware{svc, ds, sso}, nil
|
|
|
|
}
|
|
|
|
|
2023-01-03 22:14:18 +00:00
|
|
|
func (svc *Service) SendEmail(mail fleet.Email) error {
|
|
|
|
return svc.mailService.SendEmail(mail)
|
2016-12-20 21:54:30 +00:00
|
|
|
}
|
2017-01-04 21:16:17 +00:00
|
|
|
|
2017-05-09 00:43:48 +00:00
|
|
|
type validationMiddleware struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.Service
|
|
|
|
ds fleet.Datastore
|
2017-05-09 00:43:48 +00:00
|
|
|
ssoSessionStore sso.SessionStore
|
|
|
|
}
|
2019-08-02 21:08:42 +00:00
|
|
|
|
2021-09-03 22:00:30 +00:00
|
|
|
// getAssetURL simply returns the base url used for retrieving image assets from fleetdm.com.
|
2019-08-02 21:08:42 +00:00
|
|
|
func getAssetURL() template.URL {
|
2021-09-03 22:00:30 +00:00
|
|
|
return template.URL("https://fleetdm.com/images/permanent")
|
2019-08-02 21:08:42 +00:00
|
|
|
}
|