mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-11 12:15:34 +08:00
reorganize MDM service into a single package. (#423)
This commit is contained in:
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/micromdm/micromdm/mdm/checkin"
|
||||
"github.com/micromdm/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/platform/apns"
|
||||
"github.com/micromdm/micromdm/platform/pubsub"
|
||||
)
|
||||
@@ -81,17 +81,17 @@ func (db *DB) Save(info *apns.PushInfo) error {
|
||||
}
|
||||
|
||||
func (db *DB) pollCheckin(sub pubsub.Subscriber) error {
|
||||
tokenUpdateEvents, err := sub.Subscribe(context.TODO(), "push-info", checkin.TokenUpdateTopic)
|
||||
tokenUpdateEvents, err := sub.Subscribe(context.TODO(), "push-info", mdm.TokenUpdateTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing push to %s topic", checkin.TokenUpdateTopic)
|
||||
"subscribing push to %s topic", mdm.TokenUpdateTopic)
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case event := <-tokenUpdateEvents:
|
||||
var ev checkin.Event
|
||||
if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
var ev mdm.CheckinEvent
|
||||
if err := mdm.UnmarshalCheckinEvent(event.Message, &ev); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/micromdm/micromdm/mdm/checkin"
|
||||
mdmsvc "github.com/micromdm/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/mdm/mdm"
|
||||
"github.com/micromdm/micromdm/platform/blueprint"
|
||||
"github.com/micromdm/micromdm/platform/command"
|
||||
@@ -99,8 +99,8 @@ func (db *DB) StartListener(sub pubsub.Subscriber, cmdSvc command.Service) error
|
||||
for {
|
||||
select {
|
||||
case event := <-tokenUpdateEvents:
|
||||
var ev checkin.Event
|
||||
if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
var ev mdmsvc.CheckinEvent
|
||||
if err := mdmsvc.UnmarshalCheckinEvent(event.Message, &ev); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ import (
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
"github.com/micromdm/micromdm/dep/depsync"
|
||||
"github.com/micromdm/micromdm/mdm/checkin"
|
||||
"github.com/micromdm/micromdm/mdm/connect"
|
||||
"github.com/micromdm/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/platform/device"
|
||||
"github.com/micromdm/micromdm/platform/pubsub"
|
||||
)
|
||||
@@ -175,37 +174,37 @@ func isNotFound(err error) bool {
|
||||
}
|
||||
|
||||
func (db *DB) pollCheckin(pubsubSvc pubsub.PublishSubscriber) error {
|
||||
authenticateEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", checkin.AuthenticateTopic)
|
||||
authenticateEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", mdm.AuthenticateTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing devices to %s topic", checkin.AuthenticateTopic)
|
||||
"subscribing devices to %s topic", mdm.AuthenticateTopic)
|
||||
}
|
||||
tokenUpdateEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", checkin.TokenUpdateTopic)
|
||||
tokenUpdateEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", mdm.TokenUpdateTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing devices to %s topic", checkin.TokenUpdateTopic)
|
||||
"subscribing devices to %s topic", mdm.TokenUpdateTopic)
|
||||
}
|
||||
checkoutEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", checkin.CheckoutTopic)
|
||||
checkoutEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", mdm.CheckoutTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing devices to %s topic", checkin.CheckoutTopic)
|
||||
"subscribing devices to %s topic", mdm.CheckoutTopic)
|
||||
}
|
||||
depSyncEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", depsync.SyncTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing devices to %s topic", depsync.SyncTopic)
|
||||
}
|
||||
connectEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", connect.ConnectTopic)
|
||||
connectEvents, err := pubsubSvc.Subscribe(context.TODO(), "devices", mdm.ConnectTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing devices to %s topic", connect.ConnectTopic)
|
||||
"subscribing devices to %s topic", mdm.ConnectTopic)
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case event := <-authenticateEvents:
|
||||
var ev checkin.Event
|
||||
if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
var ev mdm.CheckinEvent
|
||||
if err := mdm.UnmarshalCheckinEvent(event.Message, &ev); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
@@ -252,8 +251,8 @@ func (db *DB) pollCheckin(pubsubSvc pubsub.PublishSubscriber) error {
|
||||
continue
|
||||
}
|
||||
case event := <-tokenUpdateEvents:
|
||||
var ev checkin.Event
|
||||
if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
var ev mdm.CheckinEvent
|
||||
if err := mdm.UnmarshalCheckinEvent(event.Message, &ev); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
@@ -339,8 +338,8 @@ func (db *DB) pollCheckin(pubsubSvc pubsub.PublishSubscriber) error {
|
||||
}
|
||||
}
|
||||
case event := <-connectEvents:
|
||||
var ev connect.Event
|
||||
if err := connect.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
var ev mdm.AcknowledgeEvent
|
||||
if err := mdm.UnmarshalAcknowledgeEvent(event.Message, &ev); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
@@ -355,8 +354,8 @@ func (db *DB) pollCheckin(pubsubSvc pubsub.PublishSubscriber) error {
|
||||
continue
|
||||
}
|
||||
case event := <-checkoutEvents:
|
||||
var ev checkin.Event
|
||||
if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
var ev mdm.CheckinEvent
|
||||
if err := mdm.UnmarshalCheckinEvent(event.Message, &ev); err != nil {
|
||||
fmt.Println(err)
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/groob/plist"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/platform/command"
|
||||
"github.com/micromdm/micromdm/platform/pubsub"
|
||||
)
|
||||
@@ -24,7 +24,18 @@ type Store struct {
|
||||
*bolt.DB
|
||||
}
|
||||
|
||||
func (db *Store) Next(ctx context.Context, resp mdm.Response) (*Command, error) {
|
||||
func (db *Store) Next(ctx context.Context, resp mdm.Response) ([]byte, error) {
|
||||
cmd, err := db.nextCommand(ctx, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cmd == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return cmd.Payload, nil
|
||||
}
|
||||
|
||||
func (db *Store) nextCommand(ctx context.Context, resp mdm.Response) (*Command, error) {
|
||||
udid := resp.UDID
|
||||
if resp.UserID != nil {
|
||||
// use the user id for user level commands
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/mdm"
|
||||
)
|
||||
|
||||
func TestNext_Error(t *testing.T) {
|
||||
@@ -29,7 +29,7 @@ func TestNext_Error(t *testing.T) {
|
||||
Status: "Error",
|
||||
}
|
||||
for range dc.Commands {
|
||||
cmd, err := store.Next(ctx, resp)
|
||||
cmd, err := store.nextCommand(ctx, resp)
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil, but got err: %s", err)
|
||||
}
|
||||
@@ -57,16 +57,15 @@ func TestNext_NotNow(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
tf := func(t *testing.T) {
|
||||
|
||||
|
||||
resp := mdm.Response{
|
||||
UDID: dc.DeviceUDID,
|
||||
CommandUUID: "yCmd",
|
||||
Status: "NotNow",
|
||||
}
|
||||
cmd, err := store.Next(ctx, resp)
|
||||
cmd, err := store.nextCommand(ctx, resp)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil, but got err: %s", err)
|
||||
t.Fatalf("expected nil, but got err: %s", err)
|
||||
}
|
||||
|
||||
resp = mdm.Response{
|
||||
@@ -75,7 +74,7 @@ func TestNext_NotNow(t *testing.T) {
|
||||
Status: "NotNow",
|
||||
}
|
||||
|
||||
cmd, err = store.Next(ctx, resp)
|
||||
cmd, err = store.nextCommand(ctx, resp)
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil, but got err: %s", err)
|
||||
}
|
||||
@@ -111,7 +110,7 @@ func TestNext_Idle(t *testing.T) {
|
||||
Status: "Idle",
|
||||
}
|
||||
for i, _ := range dc.Commands {
|
||||
cmd, err := store.Next(ctx, resp)
|
||||
cmd, err := store.nextCommand(ctx, resp)
|
||||
if err != nil {
|
||||
t.Fatalf("expected nil, but got err: %s", err)
|
||||
}
|
||||
@@ -143,7 +142,7 @@ func TestNext_zeroCommands(t *testing.T) {
|
||||
for _, s := range allStatuses {
|
||||
t.Run(s, func(t *testing.T) {
|
||||
resp := mdm.Response{CommandUUID: s, Status: s}
|
||||
cmd, err := store.Next(ctx, resp)
|
||||
cmd, err := store.nextCommand(ctx, resp)
|
||||
if err != nil {
|
||||
t.Errorf("expected nil, but got err: %s", err)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
"github.com/gogo/protobuf/proto"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/micromdm/micromdm/mdm/connect"
|
||||
"github.com/micromdm/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/platform/remove/internal/removeproto"
|
||||
)
|
||||
|
||||
@@ -30,8 +30,8 @@ func UnmarshalDevice(data []byte, dev *Device) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func RemoveMiddleware(store Store) connect.Middleware {
|
||||
return func(next connect.Service) connect.Service {
|
||||
func RemoveMiddleware(store Store) mdm.Middleware {
|
||||
return func(next mdm.Service) mdm.Service {
|
||||
return &removeMiddleware{
|
||||
store: store,
|
||||
next: next,
|
||||
@@ -41,11 +41,11 @@ func RemoveMiddleware(store Store) connect.Middleware {
|
||||
|
||||
type removeMiddleware struct {
|
||||
store Store
|
||||
next connect.Service
|
||||
next mdm.Service
|
||||
}
|
||||
|
||||
func (mw removeMiddleware) Acknowledge(ctx context.Context, req connect.MDMConnectRequest) ([]byte, error) {
|
||||
udid := req.MDMResponse.UDID
|
||||
func (mw removeMiddleware) Acknowledge(ctx context.Context, req mdm.AcknowledgeEvent) ([]byte, error) {
|
||||
udid := req.Response.UDID
|
||||
_, err := mw.store.DeviceByUDID(udid)
|
||||
if err != nil {
|
||||
if !isNotFound(err) {
|
||||
@@ -58,6 +58,10 @@ func (mw removeMiddleware) Acknowledge(ctx context.Context, req connect.MDMConne
|
||||
return mw.next.Acknowledge(ctx, req)
|
||||
}
|
||||
|
||||
func (mw removeMiddleware) Checkin(ctx context.Context, req mdm.CheckinEvent) error {
|
||||
return mw.next.Checkin(ctx, req)
|
||||
}
|
||||
|
||||
type checkoutErr struct{}
|
||||
|
||||
func (checkoutErr) Error() string {
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
|
||||
"github.com/micromdm/micromdm/mdm/checkin"
|
||||
"github.com/micromdm/micromdm/mdm"
|
||||
"github.com/micromdm/micromdm/platform/pubsub"
|
||||
"github.com/micromdm/micromdm/platform/user"
|
||||
)
|
||||
@@ -201,10 +201,10 @@ func (e *notFound) NotFound() bool {
|
||||
}
|
||||
|
||||
func (db *DB) pollCheckin(pubsubSvc pubsub.PublishSubscriber) error {
|
||||
tokenUpdateEvents, err := pubsubSvc.Subscribe(context.TODO(), "users", checkin.TokenUpdateTopic)
|
||||
tokenUpdateEvents, err := pubsubSvc.Subscribe(context.TODO(), "users", mdm.TokenUpdateTopic)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err,
|
||||
"subscribing devices to %s topic", checkin.TokenUpdateTopic)
|
||||
"subscribing devices to %s topic", mdm.TokenUpdateTopic)
|
||||
}
|
||||
go func() {
|
||||
for {
|
||||
@@ -252,10 +252,10 @@ func (db *DB) pollCheckin(pubsubSvc pubsub.PublishSubscriber) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmarshalCheckin(event pubsub.Event) (checkin.Event, error) {
|
||||
var ev checkin.Event
|
||||
if err := checkin.UnmarshalEvent(event.Message, &ev); err != nil {
|
||||
return checkin.Event{}, err
|
||||
func unmarshalCheckin(event pubsub.Event) (mdm.CheckinEvent, error) {
|
||||
var ev mdm.CheckinEvent
|
||||
if err := mdm.UnmarshalCheckinEvent(event.Message, &ev); err != nil {
|
||||
return mdm.CheckinEvent{}, err
|
||||
}
|
||||
return ev, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user