-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb341fb
commit 536c945
Showing
4 changed files
with
99 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package nanomdm | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"net/url" | ||
"path" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
"github.com/micromdm/nanomdm/service" | ||
) | ||
|
||
// DeclarativeManagement Check-in message implementation. Calls out to | ||
// the service's DM handler (if configured). | ||
func (s *Service) DeclarativeManagement(r *mdm.Request, message *mdm.DeclarativeManagement) ([]byte, error) { | ||
if err := s.updateEnrollID(r, &message.Enrollment); err != nil { | ||
return nil, err | ||
} | ||
s.logger.Info( | ||
"msg", "DeclarativeManagement", | ||
"id", r.ID, | ||
"type", r.Type, | ||
"endpoint", message.Endpoint, | ||
) | ||
if s.dm == nil { | ||
return nil, errors.New("no Declarative Management handler") | ||
} | ||
return s.dm.DeclarativeManagement(r, message) | ||
} | ||
|
||
const enrollmentIDHeader = "X-Enrollment-ID" | ||
|
||
type DeclarativeManagementHTTPCaller struct { | ||
urlPrefix string | ||
client *http.Client | ||
} | ||
|
||
// NewDeclarativeManagementHTTPCaller creates a new DeclarativeManagementHTTPCaller | ||
func NewDeclarativeManagementHTTPCaller(urlPrefix string) *DeclarativeManagementHTTPCaller { | ||
return &DeclarativeManagementHTTPCaller{ | ||
urlPrefix: urlPrefix, | ||
client: http.DefaultClient, | ||
} | ||
} | ||
|
||
func (c *DeclarativeManagementHTTPCaller) DeclarativeManagement(r *mdm.Request, message *mdm.DeclarativeManagement) ([]byte, error) { | ||
if c.urlPrefix == "" { | ||
return nil, errors.New("missing URL") | ||
} | ||
u, err := url.Parse(c.urlPrefix) | ||
if err != nil { | ||
return nil, err | ||
} | ||
u.Path = path.Join(u.Path, message.Endpoint) | ||
method := http.MethodGet | ||
if len(message.Data) > 0 { | ||
method = http.MethodPut | ||
} | ||
req, err := http.NewRequestWithContext(r.Context, method, u.String(), bytes.NewBuffer(message.Data)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Set(enrollmentIDHeader, r.ID) | ||
if len(message.Data) > 0 { | ||
req.Header.Set("Content-Type", "application/json") | ||
} | ||
resp, err := c.client.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
bodyBytes, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != 200 { | ||
return bodyBytes, service.NewHTTPStatusError( | ||
resp.StatusCode, | ||
fmt.Errorf("unexpected HTTP status: %s", resp.Status), | ||
) | ||
} | ||
return bodyBytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters