From 5bc9dc85b9858718a9321bc62a7ee99e7259d708 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Mon, 3 Dec 2018 14:35:20 -0500 Subject: [PATCH] Move some handle request functions up a level (#5889) * Move some handle request functions up a level Add clearing of token entry from request, fixing a test * Update request_handling.go --- vault/request_handling.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/vault/request_handling.go b/vault/request_handling.go index ab19a0c124..7b40a1d9b4 100644 --- a/vault/request_handling.go +++ b/vault/request_handling.go @@ -345,17 +345,16 @@ func (c *Core) checkToken(ctx context.Context, req *logical.Request, unauth bool // HandleRequest is used to handle a new incoming request func (c *Core) HandleRequest(httpCtx context.Context, req *logical.Request) (resp *logical.Response, err error) { c.stateLock.RLock() - defer c.stateLock.RUnlock() if c.Sealed() { + c.stateLock.RUnlock() return nil, consts.ErrSealed } if c.standby && !c.perfStandby { + c.stateLock.RUnlock() return nil, consts.ErrStandby } ctx, cancel := context.WithCancel(c.activeContext) - defer cancel() - go func(ctx context.Context, httpCtx context.Context) { select { case <-ctx.Done(): @@ -364,6 +363,23 @@ func (c *Core) HandleRequest(httpCtx context.Context, req *logical.Request) (res } }(ctx, httpCtx) + ns, err := namespace.FromContext(httpCtx) + if err != nil { + cancel() + c.stateLock.RUnlock() + return nil, errwrap.Wrapf("could not parse namespace from http context: {{err}}", err) + } + ctx = namespace.ContextWithNamespace(ctx, ns) + + resp, err = c.handleCancelableRequest(ctx, ns, req) + + req.SetTokenEntry(nil) + cancel() + c.stateLock.RUnlock() + return resp, err +} + +func (c *Core) handleCancelableRequest(ctx context.Context, ns *namespace.Namespace, req *logical.Request) (resp *logical.Response, err error) { // Allowing writing to a path ending in / makes it extremely difficult to // understand user intent for the filesystem-like backends (kv, // cubbyhole) -- did they want a key named foo/ or did they want to write @@ -381,12 +397,6 @@ func (c *Core) HandleRequest(httpCtx context.Context, req *logical.Request) (res return nil, err } - ns, err := namespace.FromContext(httpCtx) - if err != nil { - return nil, errwrap.Wrapf("could not parse namespace from http context: {{err}}", err) - } - ctx = namespace.ContextWithNamespace(ctx, ns) - if !hasNamespaces(c) && ns.Path != "" { return nil, logical.CodedError(403, "namespaces feature not enabled") }