2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2022-06-24 12:58:02 +00:00
|
|
|
"bytes"
|
2023-08-28 23:16:57 +00:00
|
|
|
"context"
|
2020-06-26 21:13:16 +00:00
|
|
|
"fmt"
|
2023-12-05 01:45:23 +00:00
|
|
|
"io"
|
2020-06-26 21:13:16 +00:00
|
|
|
"net"
|
2018-09-18 03:03:00 +00:00
|
|
|
"net/http"
|
2020-06-26 21:13:16 +00:00
|
|
|
"strings"
|
2018-09-18 03:03:00 +00:00
|
|
|
|
2023-12-05 01:45:23 +00:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2020-07-07 04:05:28 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
|
2018-09-18 03:03:00 +00:00
|
|
|
"github.com/hashicorp/vault/helper/namespace"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
2020-06-26 21:13:16 +00:00
|
|
|
"github.com/hashicorp/vault/vault/quotas"
|
2018-09-18 03:03:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
adjustRequest = func(c *vault.Core, r *http.Request) (*http.Request, int) {
|
2020-06-26 21:13:16 +00:00
|
|
|
return r, 0
|
2018-09-18 03:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
genericWrapping = func(core *vault.Core, in http.Handler, props *vault.HandlerProperties) http.Handler {
|
|
|
|
// Wrap the help wrapped handler with another layer with a generic
|
|
|
|
// handler
|
2020-05-14 13:19:27 +00:00
|
|
|
return wrapGenericHandler(core, in, props)
|
2018-09-18 03:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
additionalRoutes = func(mux *http.ServeMux, core *vault.Core) {}
|
2019-10-11 18:56:59 +00:00
|
|
|
|
2021-02-10 21:41:58 +00:00
|
|
|
nonVotersAllowed = false
|
2021-02-24 11:58:10 +00:00
|
|
|
|
|
|
|
adjustResponse = func(core *vault.Core, w http.ResponseWriter, req *logical.Request) {}
|
2018-09-18 03:03:00 +00:00
|
|
|
)
|
2020-06-26 21:13:16 +00:00
|
|
|
|
2023-12-05 01:45:23 +00:00
|
|
|
func wrapMaxRequestSizeHandler(handler http.Handler, props *vault.HandlerProperties) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var maxRequestSize int64
|
|
|
|
if props.ListenerConfig != nil {
|
|
|
|
maxRequestSize = props.ListenerConfig.MaxRequestSize
|
|
|
|
}
|
|
|
|
if maxRequestSize == 0 {
|
|
|
|
maxRequestSize = DefaultMaxRequestSize
|
|
|
|
}
|
|
|
|
ctx := r.Context()
|
|
|
|
originalBody := r.Body
|
|
|
|
if maxRequestSize > 0 {
|
|
|
|
r.Body = http.MaxBytesReader(w, r.Body, maxRequestSize)
|
|
|
|
}
|
|
|
|
ctx = logical.CreateContextOriginalBody(ctx, originalBody)
|
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:13:16 +00:00
|
|
|
func rateLimitQuotaWrapping(handler http.Handler, core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ns, err := namespace.FromContext(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
2020-07-07 04:05:28 +00:00
|
|
|
|
|
|
|
// We don't want to do buildLogicalRequestNoAuth here because, if the
|
|
|
|
// request gets allowed by the quota, the same function will get called
|
|
|
|
// again, which is not desired.
|
|
|
|
path, status, err := buildLogicalPath(r)
|
|
|
|
if err != nil || status != 0 {
|
|
|
|
respondError(w, status, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-24 12:58:02 +00:00
|
|
|
mountPath := strings.TrimPrefix(core.MatchingMount(r.Context(), path), ns.Path)
|
|
|
|
|
2023-08-30 15:28:32 +00:00
|
|
|
quotaReq := "as.Request{
|
2020-06-26 21:13:16 +00:00
|
|
|
Type: quotas.TypeRateLimit,
|
2020-07-07 04:05:28 +00:00
|
|
|
Path: path,
|
2022-06-24 12:58:02 +00:00
|
|
|
MountPath: mountPath,
|
2020-06-26 21:13:16 +00:00
|
|
|
NamespacePath: ns.Path,
|
|
|
|
ClientAddress: parseRemoteIPAddress(r),
|
2023-08-30 15:28:32 +00:00
|
|
|
}
|
2023-09-01 13:07:47 +00:00
|
|
|
|
|
|
|
// This checks if any role based quota is required (LCQ or RLQ).
|
2023-08-30 15:28:32 +00:00
|
|
|
requiresResolveRole, err := core.ResolveRoleForQuotas(r.Context(), quotaReq)
|
|
|
|
if err != nil {
|
|
|
|
core.Logger().Error("failed to lookup quotas", "path", path, "error", err)
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any role-based quotas are enabled for this namespace/mount, just
|
|
|
|
// do the role resolution once here.
|
|
|
|
if requiresResolveRole {
|
2023-12-05 01:45:23 +00:00
|
|
|
buf := bytes.Buffer{}
|
|
|
|
teeReader := io.TeeReader(r.Body, &buf)
|
|
|
|
role := core.DetermineRoleFromLoginRequestFromReader(r.Context(), mountPath, teeReader)
|
|
|
|
|
|
|
|
// Reset the body if it was read
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
r.Body = io.NopCloser(&buf)
|
|
|
|
originalBody, ok := logical.ContextOriginalBodyValue(r.Context())
|
|
|
|
if ok {
|
|
|
|
r = r.WithContext(logical.CreateContextOriginalBody(r.Context(), newMultiReaderCloser(&buf, originalBody)))
|
|
|
|
}
|
|
|
|
}
|
2023-08-30 15:28:32 +00:00
|
|
|
// add an entry to the context to prevent recalculating request role unnecessarily
|
|
|
|
r = r.WithContext(context.WithValue(r.Context(), logical.CtxKeyRequestRole{}, role))
|
|
|
|
quotaReq.Role = role
|
|
|
|
}
|
|
|
|
|
|
|
|
quotaResp, err := core.ApplyRateLimitQuota(r.Context(), quotaReq)
|
2020-06-26 21:13:16 +00:00
|
|
|
if err != nil {
|
2020-07-07 04:05:28 +00:00
|
|
|
core.Logger().Error("failed to apply quota", "path", path, "error", err)
|
2023-08-30 15:28:32 +00:00
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
2020-06-26 21:13:16 +00:00
|
|
|
return
|
|
|
|
}
|
2020-07-29 19:15:05 +00:00
|
|
|
|
|
|
|
if core.RateLimitResponseHeadersEnabled() {
|
|
|
|
for h, v := range quotaResp.Headers {
|
|
|
|
w.Header().Set(h, v)
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 21:13:16 +00:00
|
|
|
|
|
|
|
if !quotaResp.Allowed {
|
2021-04-26 17:33:48 +00:00
|
|
|
quotaErr := fmt.Errorf("request path %q: %w", path, quotas.ErrRateLimitQuotaExceeded)
|
2020-06-26 21:13:16 +00:00
|
|
|
respondError(w, http.StatusTooManyRequests, quotaErr)
|
|
|
|
|
|
|
|
if core.Logger().IsTrace() {
|
2020-08-31 15:10:52 +00:00
|
|
|
core.Logger().Trace("request rejected due to rate limit quota violation", "request_path", path)
|
2020-06-26 21:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if core.RateLimitAuditLoggingEnabled() {
|
2020-07-07 04:05:28 +00:00
|
|
|
req, _, status, err := buildLogicalRequestNoAuth(core.PerfStandby(), w, r)
|
|
|
|
if err != nil || status != 0 {
|
|
|
|
respondError(w, status, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = core.AuditLogger().AuditRequest(r.Context(), &logical.LogInput{
|
2020-06-26 21:13:16 +00:00
|
|
|
Request: req,
|
|
|
|
OuterErr: quotaErr,
|
|
|
|
})
|
2020-07-07 04:05:28 +00:00
|
|
|
if err != nil {
|
|
|
|
core.Logger().Warn("failed to audit log request rejection caused by rate limit quota violation", "error", err)
|
|
|
|
}
|
2020-06-26 21:13:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseRemoteIPAddress(r *http.Request) string {
|
|
|
|
ip, _, err := net.SplitHostPort(r.RemoteAddr)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip
|
|
|
|
}
|
2023-12-05 01:45:23 +00:00
|
|
|
|
|
|
|
type multiReaderCloser struct {
|
|
|
|
readers []io.Reader
|
|
|
|
io.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMultiReaderCloser(readers ...io.Reader) *multiReaderCloser {
|
|
|
|
return &multiReaderCloser{
|
|
|
|
readers: readers,
|
|
|
|
Reader: io.MultiReader(readers...),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multiReaderCloser) Close() error {
|
|
|
|
var err error
|
|
|
|
for _, r := range m.readers {
|
|
|
|
if c, ok := r.(io.Closer); ok {
|
|
|
|
err = multierror.Append(err, c.Close())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|