Skip to content

Commit

Permalink
Split out http library into MDM, API, and core packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jun 2, 2022
1 parent 2fb9eee commit 2e67ef6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
28 changes: 15 additions & 13 deletions cmd/nanomdm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"github.com/micromdm/nanomdm/certverify"
"github.com/micromdm/nanomdm/cmd/cli"
mdmhttp "github.com/micromdm/nanomdm/http"
httpapi "github.com/micromdm/nanomdm/http/api"
httpmdm "github.com/micromdm/nanomdm/http/mdm"
"github.com/micromdm/nanomdm/log/stdlogfmt"
"github.com/micromdm/nanomdm/push/buford"
pushsvc "github.com/micromdm/nanomdm/push/service"
Expand Down Expand Up @@ -119,28 +121,28 @@ func main() {
var mdmHandler http.Handler
if *flCheckin {
// if we use the check-in handler then only handle commands
mdmHandler = mdmhttp.CommandAndReportResultsHandler(mdmService, logger.With("handler", "command"))
mdmHandler = httpmdm.CommandAndReportResultsHandler(mdmService, logger.With("handler", "command"))
} else {
// if we don't use a check-in handler then do both
mdmHandler = mdmhttp.CheckinAndCommandHandler(mdmService, logger.With("handler", "checkin-command"))
mdmHandler = httpmdm.CheckinAndCommandHandler(mdmService, logger.With("handler", "checkin-command"))
}
mdmHandler = mdmhttp.CertVerifyMiddleware(mdmHandler, verifier, logger.With("handler", "cert-verify"))
mdmHandler = httpmdm.CertVerifyMiddleware(mdmHandler, verifier, logger.With("handler", "cert-verify"))
if *flCertHeader != "" {
mdmHandler = mdmhttp.CertExtractPEMHeaderMiddleware(mdmHandler, *flCertHeader, logger.With("handler", "cert-extract"))
mdmHandler = httpmdm.CertExtractPEMHeaderMiddleware(mdmHandler, *flCertHeader, logger.With("handler", "cert-extract"))
} else {
mdmHandler = mdmhttp.CertExtractMdmSignatureMiddleware(mdmHandler, logger.With("handler", "cert-extract"))
mdmHandler = httpmdm.CertExtractMdmSignatureMiddleware(mdmHandler, logger.With("handler", "cert-extract"))
}
mux.Handle(endpointMDM, mdmHandler)

if *flCheckin {
// if we specified a separate check-in handler, set it up
var checkinHandler http.Handler
checkinHandler = mdmhttp.CheckinHandler(mdmService, logger.With("handler", "checkin"))
checkinHandler = mdmhttp.CertVerifyMiddleware(checkinHandler, verifier, logger.With("handler", "cert-verify"))
checkinHandler = httpmdm.CheckinHandler(mdmService, logger.With("handler", "checkin"))
checkinHandler = httpmdm.CertVerifyMiddleware(checkinHandler, verifier, logger.With("handler", "cert-verify"))
if *flCertHeader != "" {
checkinHandler = mdmhttp.CertExtractPEMHeaderMiddleware(checkinHandler, *flCertHeader, logger.With("handler", "cert-extract"))
checkinHandler = httpmdm.CertExtractPEMHeaderMiddleware(checkinHandler, *flCertHeader, logger.With("handler", "cert-extract"))
} else {
checkinHandler = mdmhttp.CertExtractMdmSignatureMiddleware(checkinHandler, logger.With("handler", "cert-extract"))
checkinHandler = httpmdm.CertExtractMdmSignatureMiddleware(checkinHandler, logger.With("handler", "cert-extract"))
}
mux.Handle(endpointCheckin, checkinHandler)
}
Expand All @@ -155,22 +157,22 @@ func main() {

// register API handler for push cert storage/upload.
var pushCertHandler http.Handler
pushCertHandler = mdmhttp.StorePushCertHandler(mdmStorage, logger.With("handler", "store-cert"))
pushCertHandler = httpapi.StorePushCertHandler(mdmStorage, logger.With("handler", "store-cert"))
pushCertHandler = mdmhttp.BasicAuthMiddleware(pushCertHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIPushCert, pushCertHandler)

// register API handler for push notifications.
// we strip the prefix to use the path as an id.
var pushHandler http.Handler
pushHandler = mdmhttp.PushHandler(pushService, logger.With("handler", "push"))
pushHandler = httpapi.PushHandler(pushService, logger.With("handler", "push"))
pushHandler = http.StripPrefix(endpointAPIPush, pushHandler)
pushHandler = mdmhttp.BasicAuthMiddleware(pushHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIPush, pushHandler)

// register API handler for new command queueing.
// we strip the prefix to use the path as an id.
var enqueueHandler http.Handler
enqueueHandler = mdmhttp.RawCommandEnqueueHandler(mdmStorage, pushService, logger.With("handler", "enqueue"))
enqueueHandler = httpapi.RawCommandEnqueueHandler(mdmStorage, pushService, logger.With("handler", "enqueue"))
enqueueHandler = http.StripPrefix(endpointAPIEnqueue, enqueueHandler)
enqueueHandler = mdmhttp.BasicAuthMiddleware(enqueueHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIEnqueue, enqueueHandler)
Expand All @@ -185,7 +187,7 @@ func main() {
// generate "enrollments" then this effively allows us to
// migrate MDM enrollments between servers.
var migHandler http.Handler
migHandler = mdmhttp.CheckinHandler(nano, logger.With("handler", "migration"))
migHandler = httpmdm.CheckinHandler(nano, logger.With("handler", "migration"))
migHandler = mdmhttp.BasicAuthMiddleware(migHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIMigration, migHandler)
}
Expand Down
7 changes: 4 additions & 3 deletions http/api.go → http/api/api.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package api

import (
"bytes"
Expand All @@ -13,6 +13,7 @@ import (
"strings"

"github.com/micromdm/nanomdm/cryptoutil"
mdmhttp "github.com/micromdm/nanomdm/http"
"github.com/micromdm/nanomdm/log"
"github.com/micromdm/nanomdm/log/ctxlog"
"github.com/micromdm/nanomdm/mdm"
Expand Down Expand Up @@ -141,7 +142,7 @@ func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Push
return func(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.URL.Path, ",")
ctx, logger := setupCtxLog(r.Context(), ids, logger)
b, err := ReadAllAndReplaceBody(r)
b, err := mdmhttp.ReadAllAndReplaceBody(r)
if err != nil {
logger.Info("msg", "reading body", "err", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down Expand Up @@ -295,7 +296,7 @@ func readPEMCertAndKey(input []byte) (cert []byte, key []byte, err error) {
func StorePushCertHandler(storage storage.PushCertStore, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
b, err := ReadAllAndReplaceBody(r)
b, err := mdmhttp.ReadAllAndReplaceBody(r)
if err != nil {
logger.Info("msg", "reading body", "err", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
7 changes: 4 additions & 3 deletions http/mdm.go → http/mdm/mdm.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package http
package mdm

import (
"errors"
"net/http"
"strings"

mdmhttp "github.com/micromdm/nanomdm/http"
"github.com/micromdm/nanomdm/log"
"github.com/micromdm/nanomdm/log/ctxlog"
"github.com/micromdm/nanomdm/mdm"
Expand All @@ -28,7 +29,7 @@ func mdmReqFromHTTPReq(r *http.Request) *mdm.Request {
func CheckinHandler(svc service.Checkin, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
bodyBytes, err := ReadAllAndReplaceBody(r)
bodyBytes, err := mdmhttp.ReadAllAndReplaceBody(r)
if err != nil {
logger.Info("msg", "reading body", "err", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand All @@ -52,7 +53,7 @@ func CheckinHandler(svc service.Checkin, logger log.Logger) http.HandlerFunc {
func CommandAndReportResultsHandler(svc service.CommandAndReportResults, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
bodyBytes, err := ReadAllAndReplaceBody(r)
bodyBytes, err := mdmhttp.ReadAllAndReplaceBody(r)
if err != nil {
logger.Info("msg", "reading body", "err", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down
5 changes: 3 additions & 2 deletions http/mdm_cert.go → http/mdm/mdm_cert.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package http
package mdm

import (
"context"
Expand All @@ -7,6 +7,7 @@ import (
"net/url"

"github.com/micromdm/nanomdm/cryptoutil"
mdmhttp "github.com/micromdm/nanomdm/http"
"github.com/micromdm/nanomdm/log"
"github.com/micromdm/nanomdm/log/ctxlog"
)
Expand Down Expand Up @@ -80,7 +81,7 @@ func CertExtractMdmSignatureMiddleware(next http.Handler, logger log.Logger) htt
next.ServeHTTP(w, r)
return
}
b, err := ReadAllAndReplaceBody(r)
b, err := mdmhttp.ReadAllAndReplaceBody(r)
if err != nil {
logger.Info("msg", "reading body", "err", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
Expand Down

0 comments on commit 2e67ef6

Please sign in to comment.