-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
authproxy: MDM device identity authenticated HTTP requests (#80)
- Loading branch information
1 parent
6d0d00b
commit 62a372a
Showing
14 changed files
with
368 additions
and
18 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
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,89 @@ | ||
// Package authproxy is a simple reverse proxy for Apple MDM clients. | ||
package authproxy | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httputil" | ||
"net/url" | ||
|
||
"github.com/micromdm/nanomdm/log" | ||
"github.com/micromdm/nanomdm/log/ctxlog" | ||
) | ||
|
||
// HeaderFunc takes an HTTP request and returns a string value. | ||
// Ostensibly to be set in a header on the proxy target. | ||
type HeaderFunc func(context.Context) string | ||
|
||
type config struct { | ||
logger log.Logger | ||
fwdSig bool | ||
headerFuncs map[string]HeaderFunc | ||
} | ||
|
||
type Option func(*config) | ||
|
||
// WithLogger sets a logger for error reporting. | ||
func WithLogger(logger log.Logger) Option { | ||
return func(c *config) { | ||
c.logger = logger | ||
} | ||
} | ||
|
||
// WithHeaderFunc configures fn to be called and added as an HTTP header to the proxy target request. | ||
func WithHeaderFunc(header string, fn HeaderFunc) Option { | ||
return func(c *config) { | ||
c.headerFuncs[header] = fn | ||
} | ||
} | ||
|
||
// WithForwardMDMSignature forwards the MDM-Signature header onto the proxy destination. | ||
// This option is off by default because the header adds about two kilobytes to the request. | ||
func WithForwardMDMSignature() Option { | ||
return func(c *config) { | ||
c.fwdSig = true | ||
} | ||
} | ||
|
||
// New creates a new NanoMDM enrollment authenticating reverse proxy. | ||
// This reverse proxy is mostly the standard httputil proxy. It depends | ||
// on middleware HTTP handlers to enforce authentication and set the | ||
// context value for the enrollment ID. | ||
func New(dest string, opts ...Option) (*httputil.ReverseProxy, error) { | ||
config := &config{ | ||
logger: log.NopLogger, | ||
headerFuncs: make(map[string]HeaderFunc), | ||
} | ||
for _, opt := range opts { | ||
opt(config) | ||
} | ||
target, err := url.Parse(dest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
proxy := httputil.NewSingleHostReverseProxy(target) | ||
proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) { | ||
ctxlog.Logger(r.Context(), config.logger).Info("err", err) | ||
// use the same error as the standrad reverse proxy | ||
w.WriteHeader(http.StatusBadGateway) | ||
} | ||
dir := proxy.Director | ||
proxy.Director = func(req *http.Request) { | ||
dir(req) | ||
req.Host = target.Host | ||
if !config.fwdSig { | ||
// save the effort of forwarding this huge header | ||
req.Header.Del("Mdm-Signature") | ||
} | ||
// set any headers we want to forward. | ||
for k, fn := range config.headerFuncs { | ||
if k == "" || fn == nil { | ||
continue | ||
} | ||
if v := fn(req.Context()); v != "" { | ||
req.Header.Set(k, v) | ||
} | ||
} | ||
} | ||
return proxy, 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
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,77 @@ | ||
package mdm | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/x509" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/micromdm/nanomdm/log" | ||
) | ||
|
||
const ( | ||
testHash = "ZZZYYYXXX" | ||
testID = "AAABBBCCC" | ||
) | ||
|
||
func testHashCert(_ *x509.Certificate) string { | ||
return testHash | ||
} | ||
|
||
type testCertAuthRetriever struct{} | ||
|
||
func (c *testCertAuthRetriever) EnrollmentFromHash(ctx context.Context, hash string) (string, error) { | ||
if hash != testHash { | ||
return "", errors.New("invalid test hash") | ||
} | ||
return testID, nil | ||
} | ||
|
||
func TestCertWithEnrollmentIDMiddleware(t *testing.T) { | ||
response := []byte("mock response") | ||
|
||
// mock handler | ||
var handler http.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(response) | ||
}) | ||
|
||
handler = CertWithEnrollmentIDMiddleware(handler, testHashCert, &testCertAuthRetriever{}, true, log.NopLogger) | ||
|
||
req, err := http.NewRequest("GET", "/test", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
handler.ServeHTTP(rr, req) | ||
|
||
// we requested enforcement, and did not include a cert, so make sure we get a BadResponse | ||
if have, want := rr.Code, http.StatusBadRequest; have != want { | ||
t.Errorf("have: %d, want: %d", have, want) | ||
} | ||
|
||
req, err = http.NewRequest("GET", "/test", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// mock "cert" | ||
req = req.WithContext(context.WithValue(req.Context(), contextKeyCert{}, &x509.Certificate{})) | ||
|
||
rr = httptest.NewRecorder() | ||
handler.ServeHTTP(rr, req) | ||
|
||
// now that we have a "cert" included, we should get an OK | ||
if have, want := rr.Code, http.StatusOK; have != want { | ||
t.Errorf("have: %d, want: %d", have, want) | ||
} | ||
|
||
// verify the actual body, too | ||
if !bytes.Equal(rr.Body.Bytes(), response) { | ||
t.Error("body not equal") | ||
} | ||
} |
Oops, something went wrong.