Skip to content

Commit

Permalink
Minor change to URL parsing of DDM.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Jul 14, 2022
1 parent 6772a99 commit 3368d22
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
5 changes: 4 additions & 1 deletion cmd/nanomdm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ func main() {
nanoOpts := []nanomdm.Option{nanomdm.WithLogger(logger.With("service", "nanomdm"))}
if *flDMURLPfx != "" {
logger.Debug("msg", "declarative management setup", "url", *flDMURLPfx)
dm := nanomdm.NewDeclarativeManagementHTTPCaller(*flDMURLPfx)
dm, err := nanomdm.NewDeclarativeManagementHTTPCaller(*flDMURLPfx, http.DefaultClient)
if err != nil {
stdlog.Fatal(err)
}
nanoOpts = append(nanoOpts, nanomdm.WithDeclarativeManagement(dm))
}
nano := nanomdm.New(mdmStorage, nanoOpts...)
Expand Down
21 changes: 9 additions & 12 deletions service/nanomdm/dm.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"path"

"github.com/micromdm/nanomdm/mdm"
"github.com/micromdm/nanomdm/service"
Expand All @@ -16,28 +15,26 @@ import (
const enrollmentIDHeader = "X-Enrollment-ID"

type DeclarativeManagementHTTPCaller struct {
urlPrefix string
client *http.Client
url *url.URL
client *http.Client
}

// NewDeclarativeManagementHTTPCaller creates a new DeclarativeManagementHTTPCaller
func NewDeclarativeManagementHTTPCaller(urlPrefix string) *DeclarativeManagementHTTPCaller {
return &DeclarativeManagementHTTPCaller{
urlPrefix: urlPrefix,
client: http.DefaultClient,
}
func NewDeclarativeManagementHTTPCaller(urlPrefix string, client *http.Client) (*DeclarativeManagementHTTPCaller, error) {
url, err := url.Parse(urlPrefix)
return &DeclarativeManagementHTTPCaller{url: url, client: client}, err
}

// DeclarativeManagement calls out to an HTTP URL to handle the actual Declarative Management protocol
func (c *DeclarativeManagementHTTPCaller) DeclarativeManagement(r *mdm.Request, message *mdm.DeclarativeManagement) ([]byte, error) {
if c.urlPrefix == "" {
if c.url == nil {
return nil, errors.New("missing URL")
}
u, err := url.Parse(c.urlPrefix)
endpointURL, err := url.Parse(message.Endpoint)
if err != nil {
return nil, err
return nil, fmt.Errorf("parsing endpoint URL: %w", err)
}
u.Path = path.Join(u.Path, message.Endpoint)
u := c.url.ResolveReference(endpointURL)
method := http.MethodGet
if len(message.Data) > 0 {
method = http.MethodPut
Expand Down

0 comments on commit 3368d22

Please sign in to comment.