Skip to content

Commit 6e56122

Browse files
authored
refactor dep api endpoints (micromdm#355)
Closes micromdm#331 Closes micromdm#329 Closes micromdm#323
1 parent 6dbbd93 commit 6e56122

23 files changed

Lines changed: 420 additions & 672 deletions

cmd/mdmctl/apply_dep_profile.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (cmd *applyCommand) applyDEPProfile(args []string) error {
8989
return errors.Wrap(err, "decode DEP Profile JSON")
9090
}
9191

92-
resp, err := cmd.applysvc.DefineDEPProfile(context.TODO(), &profile)
92+
resp, err := cmd.depsvc.DefineProfile(context.TODO(), &profile)
9393
if err != nil {
9494
return errors.Wrap(err, "define dep profile")
9595
}

cmd/mdmctl/get_dep_account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (cmd *getCommand) getDEPAccount(args []string) error {
2929
out.BasicHeader()
3030
defer out.BasicFooter()
3131
ctx := context.Background()
32-
resp, err := cmd.list.GetDEPAccountInfo(ctx)
32+
resp, err := cmd.depsvc.GetAccountInfo(ctx)
3333
if err != nil {
3434
return err
3535
}

cmd/mdmctl/get_dep_devices.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (cmd *getCommand) getDEPDevices(args []string) error {
3737
defer out.BasicFooter()
3838
ctx := context.Background()
3939
serials := strings.Split(*flSerials, ",")
40-
resp, err := cmd.list.GetDEPDevice(ctx, serials)
40+
resp, err := cmd.depsvc.GetDeviceDetails(ctx, serials)
4141
if err != nil {
4242
return err
4343
}

cmd/mdmctl/get_dep_profiles.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (cmd *getCommand) getDEPProfiles(args []string) error {
4444
}
4545

4646
ctx := context.Background()
47-
resp, err := cmd.list.GetDEPProfile(ctx, *flUUID)
47+
resp, err := cmd.depsvc.FetchProfile(ctx, *flUUID)
4848
if err != nil {
4949
return err
5050
}

cmd/mdmctl/setup.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"github.com/go-kit/kit/log"
55
httptransport "github.com/go-kit/kit/transport/http"
66

7-
"github.com/micromdm/micromdm/platform/api/server/apply"
8-
"github.com/micromdm/micromdm/platform/api/server/list"
97
"github.com/micromdm/micromdm/platform/appstore"
108
"github.com/micromdm/micromdm/platform/blueprint"
119
"github.com/micromdm/micromdm/platform/config"
10+
"github.com/micromdm/micromdm/platform/dep"
1211
"github.com/micromdm/micromdm/platform/device"
1312
"github.com/micromdm/micromdm/platform/profile"
1413
"github.com/micromdm/micromdm/platform/remove"
@@ -23,8 +22,7 @@ type remoteServices struct {
2322
devicesvc device.Service
2423
configsvc config.Service
2524
appsvc appstore.Service
26-
applysvc apply.Service
27-
list list.Service
25+
depsvc dep.Service
2826
}
2927

3028
func setupClient(logger log.Logger) (*remoteServices, error) {
@@ -82,15 +80,8 @@ func setupClient(logger log.Logger) (*remoteServices, error) {
8280
return nil, err
8381
}
8482

85-
applysvc, err := apply.NewClient(
86-
cfg.ServerURL, logger, cfg.APIToken,
87-
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
88-
if err != nil {
89-
return nil, err
90-
}
91-
92-
listsvc, err := list.NewClient(
93-
cfg.ServerURL, logger, cfg.APIToken,
83+
depsvc, err := dep.NewHTTPClient(
84+
cfg.ServerURL, cfg.APIToken, logger,
9485
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
9586
if err != nil {
9687
return nil, err
@@ -104,7 +95,6 @@ func setupClient(logger log.Logger) (*remoteServices, error) {
10495
devicesvc: devicesvc,
10596
configsvc: configsvc,
10697
appsvc: appsvc,
107-
applysvc: applysvc,
108-
list: listsvc,
98+
depsvc: depsvc,
10999
}, nil
110100
}

cmd/micromdm/serve.go

Lines changed: 17 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ import (
4343
"github.com/micromdm/micromdm/mdm/connect"
4444
"github.com/micromdm/micromdm/mdm/enroll"
4545
"github.com/micromdm/micromdm/pkg/crypto"
46-
"github.com/micromdm/micromdm/platform/api/server/apply"
47-
"github.com/micromdm/micromdm/platform/api/server/list"
4846
"github.com/micromdm/micromdm/platform/apns"
4947
"github.com/micromdm/micromdm/platform/appstore"
5048
appsbuiltin "github.com/micromdm/micromdm/platform/appstore/builtin"
@@ -53,6 +51,7 @@ import (
5351
"github.com/micromdm/micromdm/platform/command"
5452
"github.com/micromdm/micromdm/platform/config"
5553
configbuiltin "github.com/micromdm/micromdm/platform/config/builtin"
54+
depapi "github.com/micromdm/micromdm/platform/dep"
5655
"github.com/micromdm/micromdm/platform/device"
5756
devicebuiltin "github.com/micromdm/micromdm/platform/device/builtin"
5857
"github.com/micromdm/micromdm/platform/profile"
@@ -160,6 +159,7 @@ func serve(args []string) error {
160159
sm.setupCommandService()
161160
sm.setupWebhooks()
162161
sm.setupCommandQueue(logger)
162+
sm.setupDepClient()
163163
sm.setupDEPSync()
164164
if sm.err != nil {
165165
stdlog.Fatal(sm.err)
@@ -254,10 +254,7 @@ func serve(args []string) error {
254254
ConnectEndpoint: connectEndpoint,
255255
}
256256

257-
dc, err := sm.depClient()
258-
if err != nil {
259-
stdlog.Fatalf("creating DEP client: %s\n", err)
260-
}
257+
dc := sm.depClient
261258
appDB := &appsbuiltin.Repo{Path: *flRepoPath}
262259

263260
var profilesvc profile.Service
@@ -302,46 +299,11 @@ func serve(args []string) error {
302299
}
303300
deviceEndpoints := device.MakeServerEndpoints(devicesvc)
304301

305-
var listsvc list.Service
302+
var depsvc depapi.Service
306303
{
307-
l := &list.ListService{
308-
DEPClient: dc,
309-
}
310-
listsvc = l
311-
312-
if err := l.WatchTokenUpdates(sm.pubclient); err != nil {
313-
stdlog.Fatal(err)
314-
}
315-
}
316-
listEndpoints := list.Endpoints{
317-
GetDEPAccountInfoEndpoint: list.MakeGetDEPAccountInfoEndpoint(listsvc),
318-
GetDEPProfileEndpoint: list.MakeGetDEPProfileEndpoint(listsvc),
319-
GetDEPDeviceEndpoint: list.MakeGetDEPDeviceDetailsEndpoint(listsvc),
304+
depsvc = depapi.New(dc, sm.pubclient)
320305
}
321-
322-
var applysvc apply.Service
323-
{
324-
l := &apply.ApplyService{
325-
DEPClient: dc,
326-
}
327-
applysvc = l
328-
if err := l.WatchTokenUpdates(sm.pubclient); err != nil {
329-
stdlog.Fatal(err)
330-
}
331-
}
332-
333-
var defineDEPProfileEndpoint endpoint.Endpoint
334-
{
335-
defineDEPProfileEndpoint = apply.MakeDefineDEPProfile(applysvc)
336-
}
337-
338-
applyEndpoints := apply.Endpoints{
339-
DefineDEPProfileEndpoint: defineDEPProfileEndpoint,
340-
}
341-
342-
applyAPIHandlers := apply.MakeHTTPHandlers(ctx, applyEndpoints, connectOpts...)
343-
344-
listAPIHandlers := list.MakeHTTPHandlers(ctx, listEndpoints, connectOpts...)
306+
depEndpoints := depapi.MakeServerEndpoints(depsvc)
345307

346308
connectHandlers := connect.MakeHTTPHandlers(ctx, connectEndpoints, connectOpts...)
347309

@@ -366,6 +328,7 @@ func serve(args []string) error {
366328
configHandler := config.MakeHTTPHandler(configEndpoints, logger)
367329
appsHandler := appstore.MakeHTTPHandler(appEndpoints, logger)
368330
deviceHandler := device.MakeHTTPHandler(deviceEndpoints, logger)
331+
depHandlers := depapi.MakeHTTPHandler(depEndpoints, logger)
369332

370333
// API commands. Only handled if the user provides an api key.
371334
if *flAPIKey != "" {
@@ -379,12 +342,11 @@ func serve(args []string) error {
379342
r.Handle("/v1/dep-tokens", apiAuthMiddleware(*flAPIKey, configHandler))
380343
r.Handle("/v1/dep-tokens", apiAuthMiddleware(*flAPIKey, configHandler))
381344
r.Handle("/v1/config/certificate", apiAuthMiddleware(*flAPIKey, configHandler))
382-
r.Handle("/push/{udid}", apiAuthMiddleware(*flAPIKey, pushHandlers.PushHandler))
345+
r.Handle("/v1/dep/devices", apiAuthMiddleware(*flAPIKey, depHandlers))
346+
r.Handle("/v1/dep/account", apiAuthMiddleware(*flAPIKey, depHandlers))
347+
r.Handle("/v1/dep/profiles", apiAuthMiddleware(*flAPIKey, depHandlers))
383348
r.Handle("/v1/commands", apiAuthMiddleware(*flAPIKey, commandHandlers.NewCommandHandler)).Methods("POST")
384-
r.Handle("/v1/dep/devices", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPDeviceDetailsHandler)).Methods("GET")
385-
r.Handle("/v1/dep/account", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPAccountInfoHandler)).Methods("GET")
386-
r.Handle("/v1/dep/profiles", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPProfileHandler)).Methods("GET")
387-
r.Handle("/v1/dep/profiles", apiAuthMiddleware(*flAPIKey, applyAPIHandlers.DefineDEPProfileHandler)).Methods("POST")
349+
r.Handle("/push/{udid}", apiAuthMiddleware(*flAPIKey, pushHandlers.PushHandler))
388350
}
389351

390352
if *flRepoPath != "" {
@@ -483,6 +445,7 @@ type server struct {
483445
configDB config.Store
484446
removeDB block.Store
485447
CommandWebhookURL string
448+
depClient dep.Client
486449

487450
// TODO: refactor enroll service and remove the need to reference
488451
// this on-disk cert. but it might be useful to keep the PEM
@@ -763,7 +726,7 @@ func (p staticTopicProvider) PushTopic() (string, error) {
763726
return p.topic, nil
764727
}
765728

766-
func (c *server) depClient() (dep.Client, error) {
729+
func (c *server) setupDepClient() (dep.Client, error) {
767730
if c.err != nil {
768731
return nil, c.err
769732
}
@@ -808,6 +771,8 @@ func (c *server) depClient() (dep.Client, error) {
808771
return nil, err
809772
}
810773

774+
c.depClient = client
775+
811776
return client, nil
812777
}
813778

@@ -816,17 +781,13 @@ func (c *server) setupDEPSync() {
816781
return
817782
}
818783

819-
client, err := c.depClient()
820-
if err != nil {
821-
c.err = err
822-
return
823-
}
784+
client := c.depClient
824785
var opts []depsync.Option
825786
if client != nil {
826787
opts = append(opts, depsync.WithClient(client))
827788
}
828789
_, c.err = depsync.New(c.pubclient, c.db, opts...)
829-
if err != nil {
790+
if c.err != nil {
830791
return
831792
}
832793
}

platform/api/server/apply/client.go

Lines changed: 0 additions & 46 deletions
This file was deleted.

platform/api/server/apply/endpoint.go

Lines changed: 0 additions & 41 deletions
This file was deleted.

platform/api/server/apply/service.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)