reorganize profile service (#347)

Moved all the endpoints into the profile package.
Defined a Store interface which includes all the used BoltDB methods.
Moved the BoltDB implementation into a subpackage.
This commit is contained in:
Victor Vrantchan
2017-12-09 16:50:10 -05:00
committed by GitHub
parent ce6e18f18d
commit 83ea8491aa
27 changed files with 487 additions and 265 deletions

View File

@@ -13,18 +13,16 @@ import (
"strings"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
"github.com/micromdm/micromdm/platform/api/server/apply"
"github.com/micromdm/micromdm/platform/blueprint"
"github.com/micromdm/micromdm/platform/profile"
)
type applyCommand struct {
config *ServerConfig
applysvc apply.Service
config *ServerConfig
*remoteServices
}
func (cmd *applyCommand) setup() error {
@@ -34,11 +32,11 @@ func (cmd *applyCommand) setup() error {
}
cmd.config = cfg
logger := log.NewLogfmtLogger(os.Stderr)
applysvc, err := apply.NewClient(cfg.ServerURL, logger, cfg.APIToken, httptransport.SetClient(skipVerifyHTTPClient(cmd.config.SkipVerify)))
remote, err := setupClient(logger)
if err != nil {
return err
}
cmd.applysvc = applysvc
cmd.remoteServices = remote
return nil
}
@@ -256,7 +254,7 @@ func (cmd *applyCommand) applyProfile(args []string) error {
}
ctx := context.Background()
err = cmd.applysvc.ApplyProfile(ctx, &p)
err = cmd.profilesvc.ApplyProfile(ctx, &p)
if err != nil {
return err
}

View File

@@ -13,16 +13,16 @@ import (
"crypto/x509"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/pkg/errors"
"github.com/micromdm/micromdm/pkg/crypto"
"github.com/micromdm/micromdm/platform/api/server/list"
"github.com/micromdm/micromdm/platform/profile"
)
type getCommand struct {
config *ServerConfig
list list.Service
*remoteServices
}
func (cmd *getCommand) setup() error {
@@ -32,11 +32,12 @@ func (cmd *getCommand) setup() error {
}
cmd.config = cfg
logger := log.NewLogfmtLogger(os.Stderr)
listsvc, err := list.NewClient(cfg.ServerURL, logger, cfg.APIToken, httptransport.SetClient(skipVerifyHTTPClient(cmd.config.SkipVerify)))
remote, err := setupClient(logger)
if err != nil {
return err
}
cmd.list = listsvc
cmd.remoteServices = remote
return nil
}
@@ -304,7 +305,7 @@ func (cmd *getCommand) getProfiles(args []string) error {
}
ctx := context.Background()
profiles, err := cmd.list.GetProfiles(ctx, list.GetProfilesOption{Identifier: *flIdentifier})
profiles, err := cmd.profilesvc.GetProfiles(ctx, profile.GetProfilesOption{Identifier: *flIdentifier})
if err != nil {
return err
}

View File

@@ -6,15 +6,11 @@ import (
"strings"
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/micromdm/micromdm/platform/api/server/remove"
)
type removeCommand struct {
config *ServerConfig
remove remove.Service
*remoteServices
}
func (cmd *removeCommand) setup() error {
@@ -24,11 +20,11 @@ func (cmd *removeCommand) setup() error {
}
cmd.config = cfg
logger := log.NewLogfmtLogger(os.Stderr)
rmsvc, err := remove.NewClient(cfg.ServerURL, logger, cfg.APIToken, httptransport.SetClient(skipVerifyHTTPClient(cmd.config.SkipVerify)))
remote, err := setupClient(logger)
if err != nil {
return err
}
cmd.remove = rmsvc
cmd.remoteServices = remote
return nil
}

View File

@@ -18,7 +18,7 @@ func (cmd *removeCommand) removeProfiles(args []string) error {
}
ctx := context.Background()
err := cmd.remove.RemoveProfiles(ctx, strings.Split(*flIdentifier, ","))
err := cmd.profilesvc.RemoveProfiles(ctx, strings.Split(*flIdentifier, ","))
if err != nil {
return err
}

59
cmd/mdmctl/setup.go Normal file
View File

@@ -0,0 +1,59 @@
package main
import (
"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/micromdm/micromdm/platform/api/server/apply"
"github.com/micromdm/micromdm/platform/api/server/list"
"github.com/micromdm/micromdm/platform/api/server/remove"
"github.com/micromdm/micromdm/platform/profile"
)
type remoteServices struct {
profilesvc profile.Service
applysvc apply.Service
list list.Service
remove remove.Service
}
func setupClient(logger log.Logger) (*remoteServices, error) {
cfg, err := LoadServerConfig()
if err != nil {
return nil, err
}
applysvc, err := apply.NewClient(
cfg.ServerURL, logger, cfg.APIToken,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
if err != nil {
return nil, err
}
profilesvc, err := profile.NewHTTPClient(
cfg.ServerURL, cfg.APIToken, logger,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
if err != nil {
return nil, err
}
listsvc, err := list.NewClient(
cfg.ServerURL, logger, cfg.APIToken,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
if err != nil {
return nil, err
}
rmsvc, err := remove.NewClient(
cfg.ServerURL, logger, cfg.APIToken,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
if err != nil {
return nil, err
}
return &remoteServices{
profilesvc: profilesvc,
applysvc: applysvc,
list: listsvc,
remove: rmsvc,
}, nil
}

View File

@@ -54,6 +54,7 @@ import (
"github.com/micromdm/micromdm/platform/deptoken"
"github.com/micromdm/micromdm/platform/device"
"github.com/micromdm/micromdm/platform/profile"
profilebuiltin "github.com/micromdm/micromdm/platform/profile/builtin"
"github.com/micromdm/micromdm/platform/pubsub"
"github.com/micromdm/micromdm/platform/pubsub/inmem"
"github.com/micromdm/micromdm/platform/queue"
@@ -175,7 +176,7 @@ func serve(args []string) error {
stdlog.Fatal(err)
}
sm.profileDB, err = profile.NewDB(sm.db)
sm.profileDB, err = profilebuiltin.NewDB(sm.db)
if err != nil {
stdlog.Fatal(err)
}
@@ -269,6 +270,13 @@ func serve(args []string) error {
tokenDB := &deptoken.DB{DB: sm.db, Publisher: sm.pubclient}
appDB := &appstore.Repo{Path: *flRepoPath}
var profilesvc profile.Service
{
profilesvc = profile.New(sm.profileDB)
}
profileEndpoints := profile.MakeServerEndpoints(profilesvc)
var listsvc list.Service
{
l := &list.ListService{
@@ -276,7 +284,6 @@ func serve(args []string) error {
Devices: devDB,
Tokens: tokenDB,
Blueprints: bpDB,
Profiles: sm.profileDB,
Apps: appDB,
Users: userDB,
}
@@ -295,7 +302,6 @@ func serve(args []string) error {
ListDevicesEndpoint: listDevicesEndpoint,
GetDEPTokensEndpoint: list.MakeGetDEPTokensEndpoint(listsvc),
GetBlueprintsEndpoint: list.MakeGetBlueprintsEndpoint(listsvc),
GetProfilesEndpoint: list.MakeGetProfilesEndpoint(listsvc),
GetDEPAccountInfoEndpoint: list.MakeGetDEPAccountInfoEndpoint(listsvc),
GetDEPProfileEndpoint: list.MakeGetDEPProfileEndpoint(listsvc),
GetDEPDeviceEndpoint: list.MakeGetDEPDeviceDetailsEndpoint(listsvc),
@@ -309,7 +315,6 @@ func serve(args []string) error {
DEPClient: dc,
Blueprints: bpDB,
Tokens: tokenDB,
Profiles: sm.profileDB,
Apps: appDB,
Users: userDB,
RemoveService: removeService,
@@ -325,11 +330,6 @@ func serve(args []string) error {
applyBlueprintEndpoint = apply.MakeApplyBlueprintEndpoint(applysvc)
}
var applyProfileEndpoint endpoint.Endpoint
{
applyProfileEndpoint = apply.MakeApplyProfileEndpoint(applysvc)
}
var defineDEPProfileEndpoint endpoint.Endpoint
{
defineDEPProfileEndpoint = apply.MakeDefineDEPProfile(applysvc)
@@ -348,7 +348,6 @@ func serve(args []string) error {
applyEndpoints := apply.Endpoints{
ApplyBlueprintEndpoint: applyBlueprintEndpoint,
ApplyDEPTokensEndpoint: apply.MakeApplyDEPTokensEndpoint(applysvc),
ApplyProfileEndpoint: applyProfileEndpoint,
DefineDEPProfileEndpoint: defineDEPProfileEndpoint,
AppUploadEndpoint: appUploadEndpoint,
ApplyUserEndpoint: applyUserEndpoint,
@@ -359,7 +358,7 @@ func serve(args []string) error {
listAPIHandlers := list.MakeHTTPHandlers(ctx, listEndpoints, connectOpts...)
rmsvc := &remove.RemoveService{Blueprints: bpDB, Profiles: sm.profileDB, RemoveService: removeService}
rmsvc := &remove.RemoveService{Blueprints: bpDB, RemoveService: removeService}
removeAPIHandlers := remove.MakeHTTPHandlers(ctx, remove.MakeEndpoints(rmsvc), connectOpts...)
connectHandlers := connect.MakeHTTPHandlers(ctx, connectEndpoints, connectOpts...)
@@ -378,8 +377,11 @@ func serve(args []string) error {
io.WriteString(w, homePage)
})
profilesHandler := profile.MakeHTTPHandler(profileEndpoints, logger)
// API commands. Only handled if the user provides an api key.
if *flAPIKey != "" {
r.Handle("/v1/profiles", apiAuthMiddleware(*flAPIKey, profilesHandler))
r.Handle("/push/{udid}", apiAuthMiddleware(*flAPIKey, pushHandlers.PushHandler))
r.Handle("/v1/commands", apiAuthMiddleware(*flAPIKey, commandHandlers.NewCommandHandler)).Methods("POST")
r.Handle("/v1/devices", apiAuthMiddleware(*flAPIKey, listAPIHandlers.ListDevicesHandler)).Methods("GET")
@@ -390,9 +392,6 @@ func serve(args []string) error {
r.Handle("/v1/blueprints", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetBlueprintsHandler)).Methods("GET")
r.Handle("/v1/blueprints", apiAuthMiddleware(*flAPIKey, applyAPIHandlers.BlueprintHandler)).Methods("PUT")
r.Handle("/v1/blueprints", apiAuthMiddleware(*flAPIKey, removeAPIHandlers.BlueprintHandler)).Methods("DELETE")
r.Handle("/v1/profiles", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetProfilesHandler)).Methods("GET")
r.Handle("/v1/profiles", apiAuthMiddleware(*flAPIKey, applyAPIHandlers.ProfileHandler)).Methods("PUT")
r.Handle("/v1/profiles", apiAuthMiddleware(*flAPIKey, removeAPIHandlers.ProfileHandler)).Methods("DELETE")
r.Handle("/v1/dep/devices", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPDeviceDetailsHandler)).Methods("GET")
r.Handle("/v1/dep/account", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPAccountInfoHandler)).Methods("GET")
r.Handle("/v1/dep/profiles", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPProfileHandler)).Methods("GET")
@@ -496,7 +495,7 @@ type server struct {
APNSPrivateKeyPass string
tlsCertPath string
scepDepot *boltdepot.Depot
profileDB *profile.DB
profileDB profile.Store
configDB *config.DB
removeDB *block.DB
CommandWebhookURL string