Skip to content

Commit

Permalink
Move id_first and id_count to ctxlog pairs for API endpoints.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessepeterson committed Apr 3, 2022
1 parent fb69bf2 commit b1d46ad
Showing 1 changed file with 37 additions and 8 deletions.
45 changes: 37 additions & 8 deletions http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"bytes"
"context"
"crypto/x509"
"encoding/json"
"encoding/pem"
Expand Down Expand Up @@ -38,6 +39,36 @@ type apiResult struct {
RequestType string `json:"request_type,omitempty"`
}

type (
ctxKeyIDFirst struct{}
ctxKeyIDCount struct{}
)

func setAPIIDs(ctx context.Context, idFirst string, idCount int) context.Context {
ctx = context.WithValue(ctx, ctxKeyIDFirst{}, idFirst)
return context.WithValue(ctx, ctxKeyIDCount{}, idCount)
}

func ctxKVs(ctx context.Context) (out []interface{}) {
id, ok := ctx.Value(ctxKeyIDFirst{}).(string)
if ok {
out = append(out, "id_first", id)
}
eType, ok := ctx.Value(ctxKeyIDCount{}).(int)
if ok {
out = append(out, "id_count", eType)
}
return
}

func setupCtxLog(ctx context.Context, ids []string, logger log.Logger) (context.Context, log.Logger) {
if len(ids) > 0 {
ctx = setAPIIDs(ctx, ids[0], len(ids))
ctx = ctxlog.AddFunc(ctx, ctxKVs)
}
return ctx, ctxlog.Logger(ctx, logger)
}

// PushHandlerFunc sends APNs push notifications to MDM enrollments.
//
// Note the whole URL path is used as the identifier to push to. This
Expand All @@ -46,12 +77,12 @@ type apiResult struct {
// users.
func PushHandlerFunc(pusher push.Pusher, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
ids := strings.Split(r.URL.Path, ",")
ctx, logger := setupCtxLog(r.Context(), ids, logger)
output := apiResult{
Status: make(enrolledAPIResults),
}
pushResp, err := pusher.Push(r.Context(), ids)
pushResp, err := pusher.Push(ctx, ids)
if err != nil {
logger.Info("msg", "push", "err", err)
output.PushError = err.Error()
Expand Down Expand Up @@ -90,7 +121,8 @@ func PushHandlerFunc(pusher push.Pusher, logger log.Logger) http.HandlerFunc {
// for "API" users.
func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Pusher, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
ids := strings.Split(r.URL.Path, ",")
ctx, logger := setupCtxLog(r.Context(), ids, logger)
b, err := ReadAllAndReplaceBody(r)
if err != nil {
logger.Info("msg", "reading body", "err", err)
Expand All @@ -103,22 +135,21 @@ func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Push
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
ids := strings.Split(r.URL.Path, ",")
nopush := r.URL.Query().Get("nopush") != ""
output := apiResult{
Status: make(enrolledAPIResults),
NoPush: nopush,
CommandUUID: command.CommandUUID,
RequestType: command.Command.RequestType,
}
idErrs, err := enqueuer.EnqueueCommand(r.Context(), ids, command)
idErrs, err := enqueuer.EnqueueCommand(ctx, ids, command)
if err != nil {
logger.Info("msg", "enqueue command", "err", err)
output.CommandError = err.Error()
}
pushResp := make(map[string]*push.Response)
if !nopush {
pushResp, err = pusher.Push(r.Context(), ids)
pushResp, err = pusher.Push(ctx, ids)
if err != nil {
logger.Info("msg", "push", "err", err)
output.PushError = err.Error()
Expand Down Expand Up @@ -149,8 +180,6 @@ func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Push
"msg", "enqueue",
"command_uuid", command.CommandUUID,
"request_type", command.Command.RequestType,
"id_count", len(ids),
"id_first", ids[0],
)
logger.Debug("msg", "push", "count", len(pushResp))
json, err := json.MarshalIndent(output, "", "\t")
Expand Down

0 comments on commit b1d46ad

Please sign in to comment.