Retrieve certificate (#503)

This commit is contained in:
Jesse Peterson
2018-09-07 06:10:43 -07:00
committed by Victor Vrantchan
parent 06121223b0
commit 39c3dfb8eb
4 changed files with 76 additions and 2 deletions

View File

@@ -77,6 +77,17 @@ func (db *DB) serverConfig() (*config.ServerConfig, error) {
return &conf, errors.Wrap(err, "get server config from bolt")
}
func (db *DB) GetPushCertificate() ([]byte, error) {
cert, err := db.PushCertificate()
if err != nil {
return nil, err
}
if len(cert.Certificate) > 0 {
return cert.Certificate[0], nil
}
return nil, nil
}
func (db *DB) PushCertificate() (*tls.Certificate, error) {
conf, err := db.serverConfig()
if err != nil {

View File

@@ -0,0 +1,51 @@
package config
import (
"context"
"net/http"
"github.com/go-kit/kit/endpoint"
"github.com/micromdm/micromdm/pkg/httputil"
"github.com/pkg/errors"
)
func (svc *ConfigService) GetPushCertificate(ctx context.Context) ([]byte, error) {
cert, err := svc.store.GetPushCertificate()
if err != nil {
return cert, errors.Wrap(err, "get push certificate")
}
return cert, nil
}
type getResponse struct {
Cert []byte `json:"cert"`
Err error `json:"err,omitempty"`
}
func (r getResponse) Failed() error { return r.Err }
func decodeGetPushCertificateRequest(ctx context.Context, r *http.Request) (interface{}, error) {
return nil, nil
}
func decodeGetPushCertificateResponse(_ context.Context, r *http.Response) (interface{}, error) {
var resp getResponse
err := httputil.DecodeJSONResponse(r, &resp)
return resp, err
}
func MakeGetPushCertificateEndpoint(svc Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
cert, err := svc.GetPushCertificate(ctx)
return getResponse{Err: err, Cert: cert}, nil
}
}
func (e Endpoints) GetPushCertificate(ctx context.Context) ([]byte, error) {
response, err := e.GetPushCertificateEndpoint(ctx, nil)
if err != nil {
return nil, err
}
return response.(getResponse).Cert, response.(getResponse).Err
}

View File

@@ -8,21 +8,24 @@ import (
)
type Endpoints struct {
ApplyDEPTokensEndpoint endpoint.Endpoint
SavePushCertificateEndpoint endpoint.Endpoint
GetPushCertificateEndpoint endpoint.Endpoint
ApplyDEPTokensEndpoint endpoint.Endpoint
GetDEPTokensEndpoint endpoint.Endpoint
}
func MakeServerEndpoints(s Service, outer endpoint.Middleware, others ...endpoint.Middleware) Endpoints {
return Endpoints{
ApplyDEPTokensEndpoint: endpoint.Chain(outer, others...)(MakeApplyDEPTokensEndpoint(s)),
SavePushCertificateEndpoint: endpoint.Chain(outer, others...)(MakeSavePushCertificateEndpoint(s)),
GetPushCertificateEndpoint: endpoint.Chain(outer, others...)(MakeGetPushCertificateEndpoint(s)),
ApplyDEPTokensEndpoint: endpoint.Chain(outer, others...)(MakeApplyDEPTokensEndpoint(s)),
GetDEPTokensEndpoint: endpoint.Chain(outer, others...)(MakeGetDEPTokensEndpoint(s)),
}
}
func RegisterHTTPHandlers(r *mux.Router, e Endpoints, options ...httptransport.ServerOption) {
// PUT /v1/config/certificate create or replace the MDM Push Certificate
// GET /v1/config/certificate retrieve the MDM Push Certificate
// PUT /v1/dep-tokens create or replace a DEP OAuth token
// GET /v1/dep-tokens get the OAuth Token used for the DEP client
@@ -33,6 +36,13 @@ func RegisterHTTPHandlers(r *mux.Router, e Endpoints, options ...httptransport.S
options...,
))
r.Methods("GET").Path("/v1/config/certificate").Handler(httptransport.NewServer(
e.GetPushCertificateEndpoint,
decodeGetPushCertificateRequest,
httputil.EncodeJSONResponse,
options...,
))
r.Methods("PUT").Path("/v1/dep-tokens").Handler(httptransport.NewServer(
e.ApplyDEPTokensEndpoint,
decodeApplyDEPTokensRequest,

View File

@@ -9,12 +9,14 @@ import (
type Service interface {
SavePushCertificate(ctx context.Context, cert, key []byte) error
GetPushCertificate(ctx context.Context) ([]byte, error)
ApplyDEPToken(ctx context.Context, P7MContent []byte) error
GetDEPTokens(ctx context.Context) ([]DEPToken, []byte, error)
}
type Store interface {
SavePushCertificate(cert, key []byte) error
GetPushCertificate() ([]byte, error)
PushCertificate() (*tls.Certificate, error)
PushTopic() (string, error)
DEPKeypair() (key *rsa.PrivateKey, cert *x509.Certificate, err error)