Skip to content

Commit

Permalink
refactor config api (micromdm#352)
Browse files Browse the repository at this point in the history
  • Loading branch information
groob committed Dec 10, 2017
1 parent ce56b48 commit 982e441
Show file tree
Hide file tree
Showing 25 changed files with 420 additions and 457 deletions.
2 changes: 1 addition & 1 deletion cmd/mdmctl/apply.go
Expand Up @@ -184,7 +184,7 @@ func (cmd *applyCommand) applyDEPTokens(args []string) error {
return err
}
ctx := context.Background()
err = cmd.applysvc.ApplyDEPToken(ctx, p7mBytes)
err = cmd.configsvc.ApplyDEPToken(ctx, p7mBytes)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/mdmctl/get.go
Expand Up @@ -162,7 +162,7 @@ func (cmd *getCommand) getDepTokens(args []string) error {
w := tabwriter.NewWriter(os.Stdout, 0, 4, 2, ' ', 0)
fmt.Fprintf(w, "ConsumerKey\tAccessTokenExpiry\n")
ctx := context.Background()
tokens, certBytes, err := cmd.list.GetDEPTokens(ctx)
tokens, certBytes, err := cmd.configsvc.GetDEPTokens(ctx)
if err != nil {
return err
}
Expand Down
37 changes: 18 additions & 19 deletions cmd/mdmctl/mdmcert.go
Expand Up @@ -13,16 +13,26 @@ import (
"strings"

"github.com/go-kit/kit/log"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/pkg/errors"
"golang.org/x/crypto/pkcs12"

"github.com/micromdm/micromdm/pkg/crypto"
"github.com/micromdm/micromdm/pkg/crypto/mdmcertutil"
"github.com/micromdm/micromdm/platform/config"
)

type mdmcertCommand struct{}
type mdmcertCommand struct {
*remoteServices
}

func (cmd *mdmcertCommand) setup() error {
logger := log.NewLogfmtLogger(os.Stderr)
remote, err := setupClient(logger)
if err != nil {
return err
}
cmd.remoteServices = remote
return nil
}

func (cmd *mdmcertCommand) Usage() error {
const usageText = `
Expand Down Expand Up @@ -62,6 +72,10 @@ func (cmd *mdmcertCommand) Run(args []string) error {
os.Exit(1)
}

if err := cmd.setup(); err != nil {
return err
}

var run func([]string) error
switch strings.ToLower(args[0]) {
case "vendor":
Expand Down Expand Up @@ -197,27 +211,12 @@ func (cmd *mdmcertCommand) runUpload(args []string) error {
return err
}

cfg, err := LoadServerConfig()
if err != nil {
return errors.Wrap(err, "load mdmctl client config")
}
logger := log.NewLogfmtLogger(os.Stderr)
configsvc, err := config.NewClient(
cfg.ServerURL,
logger,
cfg.APIToken,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)),
)
if err != nil {
return errors.Wrap(err, "create config service from mdmctl config")
}

cert, key, err := loadPushCerts(*flCertPath, *flKeyPath, *flKeyPass)
if err != nil {
return errors.Wrap(err, "load push certificate")
}

if err := configsvc.SavePushCertificate(context.Background(), cert, key); err != nil {
if err := cmd.configsvc.SavePushCertificate(context.Background(), cert, key); err != nil {
return errors.Wrap(err, "upload push certificate and key to server")
}

Expand Down
10 changes: 10 additions & 0 deletions cmd/mdmctl/setup.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/micromdm/micromdm/platform/api/server/apply"
"github.com/micromdm/micromdm/platform/api/server/list"
"github.com/micromdm/micromdm/platform/blueprint"
"github.com/micromdm/micromdm/platform/config"
"github.com/micromdm/micromdm/platform/profile"
"github.com/micromdm/micromdm/platform/remove"
"github.com/micromdm/micromdm/platform/user"
Expand All @@ -17,6 +18,7 @@ type remoteServices struct {
blueprintsvc blueprint.Service
blocksvc remove.Service
usersvc user.Service
configsvc config.Service
applysvc apply.Service
list list.Service
}
Expand Down Expand Up @@ -55,6 +57,13 @@ func setupClient(logger log.Logger) (*remoteServices, error) {
return nil, err
}

configsvc, err := config.NewHTTPClient(
cfg.ServerURL, cfg.APIToken, logger,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
if err != nil {
return nil, err
}

applysvc, err := apply.NewClient(
cfg.ServerURL, logger, cfg.APIToken,
httptransport.SetClient(skipVerifyHTTPClient(cfg.SkipVerify)))
Expand All @@ -74,6 +83,7 @@ func setupClient(logger log.Logger) (*remoteServices, error) {
blueprintsvc: blueprintsvc,
blocksvc: blocksvc,
usersvc: usersvc,
configsvc: configsvc,
applysvc: applysvc,
list: listsvc,
}, nil
Expand Down
43 changes: 16 additions & 27 deletions cmd/micromdm/serve.go
Expand Up @@ -51,7 +51,7 @@ import (
blueprintbuiltin "github.com/micromdm/micromdm/platform/blueprint/builtin"
"github.com/micromdm/micromdm/platform/command"
"github.com/micromdm/micromdm/platform/config"
"github.com/micromdm/micromdm/platform/deptoken"
configbuiltin "github.com/micromdm/micromdm/platform/config/builtin"
"github.com/micromdm/micromdm/platform/device"
"github.com/micromdm/micromdm/platform/profile"
profilebuiltin "github.com/micromdm/micromdm/platform/profile/builtin"
Expand Down Expand Up @@ -202,19 +202,6 @@ func serve(args []string) error {
ctx := context.Background()
httpLogger := log.With(logger, "transport", "http")

var configHandlers config.HTTPHandlers
{
pushCertEndpoint := config.MakeSavePushCertificateEndpoint(sm.configService)
configEndpoints := config.Endpoints{
SavePushCertificateEndpoint: pushCertEndpoint,
}
configOpts := []httptransport.ServerOption{
httptransport.ServerErrorLogger(httpLogger),
httptransport.ServerErrorEncoder(checkin.EncodeError),
}
configHandlers = config.MakeHTTPHandlers(ctx, configEndpoints, configOpts...)
}

var checkinHandlers checkin.HTTPHandlers
{
e := checkin.Endpoints{
Expand Down Expand Up @@ -269,7 +256,6 @@ func serve(args []string) error {
if err != nil {
stdlog.Fatalf("creating DEP client: %s\n", err)
}
tokenDB := &deptoken.DB{DB: sm.db, Publisher: sm.pubclient}
appDB := &appstore.Repo{Path: *flRepoPath}

var profilesvc profile.Service
Expand All @@ -294,12 +280,18 @@ func serve(args []string) error {

userEndpoints := user.MakeServerEndpoints(usersvc)

var configsvc config.Service
{
configsvc = config.New(sm.configDB)
}

configEndpoints := config.MakeServerEndpoints(configsvc)

var listsvc list.Service
{
l := &list.ListService{
DEPClient: dc,
Devices: devDB,
Tokens: tokenDB,
Apps: appDB,
}
listsvc = l
Expand All @@ -315,7 +307,6 @@ func serve(args []string) error {
}
listEndpoints := list.Endpoints{
ListDevicesEndpoint: listDevicesEndpoint,
GetDEPTokensEndpoint: list.MakeGetDEPTokensEndpoint(listsvc),
GetDEPAccountInfoEndpoint: list.MakeGetDEPAccountInfoEndpoint(listsvc),
GetDEPProfileEndpoint: list.MakeGetDEPProfileEndpoint(listsvc),
GetDEPDeviceEndpoint: list.MakeGetDEPDeviceDetailsEndpoint(listsvc),
Expand All @@ -326,7 +317,6 @@ func serve(args []string) error {
{
l := &apply.ApplyService{
DEPClient: dc,
Tokens: tokenDB,
Apps: appDB,
}
applysvc = l
Expand All @@ -346,7 +336,6 @@ func serve(args []string) error {
}

applyEndpoints := apply.Endpoints{
ApplyDEPTokensEndpoint: apply.MakeApplyDEPTokensEndpoint(applysvc),
DefineDEPProfileEndpoint: defineDEPProfileEndpoint,
AppUploadEndpoint: appUploadEndpoint,
}
Expand Down Expand Up @@ -375,6 +364,7 @@ func serve(args []string) error {
blueprintsHandler := blueprint.MakeHTTPHandler(blueprintEndpoints, logger)
blockhandler := block.MakeHTTPHandler(blockEndpoints, logger)
userHandler := user.MakeHTTPHandler(userEndpoints, logger)
configHandler := config.MakeHTTPHandler(configEndpoints, logger)

// API commands. Only handled if the user provides an api key.
if *flAPIKey != "" {
Expand All @@ -383,18 +373,18 @@ func serve(args []string) error {
r.Handle("/v1/users", apiAuthMiddleware(*flAPIKey, userHandler))
r.Handle("/v1/devices/{udid}/block", apiAuthMiddleware(*flAPIKey, blockhandler))
r.Handle("/v1/devices/{udid}/unblock", apiAuthMiddleware(*flAPIKey, blockhandler))
r.Handle("/v1/dep-tokens", apiAuthMiddleware(*flAPIKey, configHandler))
r.Handle("/v1/dep-tokens", apiAuthMiddleware(*flAPIKey, configHandler))
r.Handle("/v1/config/certificate", apiAuthMiddleware(*flAPIKey, configHandler))
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")
r.Handle("/v1/dep-tokens", apiAuthMiddleware(*flAPIKey, listAPIHandlers.GetDEPTokensHandler)).Methods("GET")
r.Handle("/v1/dep-tokens", apiAuthMiddleware(*flAPIKey, applyAPIHandlers.DEPTokensHandler)).Methods("PUT")
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")
r.Handle("/v1/dep/profiles", apiAuthMiddleware(*flAPIKey, applyAPIHandlers.DefineDEPProfileHandler)).Methods("POST")
r.Handle("/v1/apps", apiAuthMiddleware(*flAPIKey, applyAPIHandlers.AppUploadHandler)).Methods("POST")
r.Handle("/v1/apps", apiAuthMiddleware(*flAPIKey, listAPIHandlers.ListAppsHandler)).Methods("GET")
r.Handle("/v1/config/certificate", apiAuthMiddleware(*flAPIKey, configHandlers.SavePushCertificateHandler)).Methods("PUT")
}

if *flRepoPath != "" {
Expand Down Expand Up @@ -490,7 +480,7 @@ type server struct {
tlsCertPath string
scepDepot *boltdepot.Depot
profileDB profile.Store
configDB *config.DB
configDB config.Store
removeDB block.Store
CommandWebhookURL string

Expand Down Expand Up @@ -678,13 +668,13 @@ func (c *server) setupConfigStore() {
if c.err != nil {
return
}
db, err := config.NewDB(c.db, c.pubclient)
db, err := configbuiltin.NewDB(c.db, c.pubclient)
if err != nil {
c.err = err
return
}
c.configDB = db
c.configService = config.NewService(db)
c.configService = config.New(db)

}

Expand Down Expand Up @@ -781,9 +771,8 @@ func (c *server) depClient() (dep.Client, error) {
depsim := c.depsim
var conf *dep.Config

tokenDB := &deptoken.DB{DB: c.db}
// try getting the oauth config from bolt
tokens, err := tokenDB.DEPTokens()
tokens, err := c.configDB.DEPTokens()
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions dep/depsync/depsync.go
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/micromdm/dep"
"github.com/pkg/errors"

"github.com/micromdm/micromdm/platform/deptoken"
conf "github.com/micromdm/micromdm/platform/config"
"github.com/micromdm/micromdm/platform/pubsub"
)

Expand Down Expand Up @@ -105,7 +105,7 @@ func New(pub pubsub.PublishSubscriber, db *bolt.DB, opts ...Option) (Syncer, err
}

func (w *watcher) updateClient(pubsub pubsub.Subscriber) error {
tokenAdded, err := pubsub.Subscribe(context.TODO(), "token-events", deptoken.DEPTokenTopic)
tokenAdded, err := pubsub.Subscribe(context.TODO(), "token-events", conf.DEPTokenTopic)
if err != nil {
return err
}
Expand All @@ -114,7 +114,7 @@ func (w *watcher) updateClient(pubsub pubsub.Subscriber) error {
for {
select {
case event := <-tokenAdded:
var token deptoken.DEPToken
var token conf.DEPToken
if err := json.Unmarshal(event.Message, &token); err != nil {
log.Printf("unmarshalling tokenAdded to token: %s\n", err)
continue
Expand Down
12 changes: 0 additions & 12 deletions platform/api/server/apply/client.go
Expand Up @@ -16,17 +16,6 @@ func NewClient(instance string, logger log.Logger, token string, opts ...httptra
return nil, err
}

var applyDEPTokensEndpoint endpoint.Endpoint
{
applyDEPTokensEndpoint = httptransport.NewClient(
"PUT",
copyURL(u, "/v1/dep-tokens"),
encodeRequestWithToken(token, EncodeHTTPGenericRequest),
DecodeDEPTokensResponse,
opts...,
).Endpoint()
}

var defineDEPProfileEndpoint endpoint.Endpoint
{
defineDEPProfileEndpoint = httptransport.NewClient(
Expand All @@ -50,7 +39,6 @@ func NewClient(instance string, logger log.Logger, token string, opts ...httptra
}

return Endpoints{
ApplyDEPTokensEndpoint: applyDEPTokensEndpoint,
DefineDEPProfileEndpoint: defineDEPProfileEndpoint,
AppUploadEndpoint: uploadAppEndpoint,
}, nil
Expand Down
30 changes: 0 additions & 30 deletions platform/api/server/apply/endpoint.go
Expand Up @@ -9,7 +9,6 @@ import (
)

type Endpoints struct {
ApplyDEPTokensEndpoint endpoint.Endpoint
DefineDEPProfileEndpoint endpoint.Endpoint
AppUploadEndpoint endpoint.Endpoint
}
Expand Down Expand Up @@ -38,25 +37,6 @@ func (e Endpoints) DefineDEPProfile(ctx context.Context, p *dep.Profile) (*dep.P
return response.ProfileResponse, response.Err
}

func (e Endpoints) ApplyDEPToken(ctx context.Context, P7MContent []byte) error {
req := depTokensRequest{P7MContent: P7MContent}
resp, err := e.ApplyDEPTokensEndpoint(ctx, req)
if err != nil {
return err
}
return resp.(depTokensResponse).Err
}

func MakeApplyDEPTokensEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(depTokensRequest)
err = svc.ApplyDEPToken(ctx, req.P7MContent)
return depTokensResponse{
Err: err,
}, nil
}
}

func MakeDefineDEPProfile(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(depProfileRequest)
Expand Down Expand Up @@ -92,16 +72,6 @@ type appUploadResponse struct {

func (r appUploadResponse) error() error { return r.Err }

type depTokensRequest struct {
P7MContent []byte `json:"p7m_content"`
}

type depTokensResponse struct {
Err error `json:"err,omitempty"`
}

func (r depTokensResponse) error() error { return r.Err }

type depProfileRequest struct{ *dep.Profile }
type depProfileResponse struct {
*dep.ProfileResponse
Expand Down

0 comments on commit 982e441

Please sign in to comment.