2023-07-05 14:04:52 +00:00
|
|
|
package middleware
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2021-09-15 09:32:26 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2023-10-27 15:15:33 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-10-23 14:06:21 +00:00
|
|
|
treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree"
|
2023-07-05 14:04:52 +00:00
|
|
|
"github.com/google/uuid"
|
2023-06-09 13:19:23 +00:00
|
|
|
"go.uber.org/zap"
|
2023-07-05 14:04:52 +00:00
|
|
|
"google.golang.org/grpc/metadata"
|
2021-09-15 09:32:26 +00:00
|
|
|
)
|
|
|
|
|
2020-07-22 13:02:32 +00:00
|
|
|
type (
|
2022-04-13 16:56:58 +00:00
|
|
|
// KeyVal -- appended to ReqInfo.Tags.
|
2021-09-15 09:32:26 +00:00
|
|
|
KeyVal struct {
|
|
|
|
Key string
|
|
|
|
Val string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReqInfo stores the request info.
|
|
|
|
ReqInfo struct {
|
|
|
|
sync.RWMutex
|
2024-07-31 06:45:46 +00:00
|
|
|
RemoteHost string // Client Host/IP
|
|
|
|
Host string // Node Host/IP
|
|
|
|
UserAgent string // User Agent
|
|
|
|
DeploymentID string // random generated s3-deployment-id
|
|
|
|
RequestID string // x-amz-request-id
|
|
|
|
API string // API name -- GetObject PutObject NewMultipartUpload etc.
|
|
|
|
BucketName string // Bucket name
|
|
|
|
ObjectName string // Object name
|
|
|
|
TraceID string // Trace ID
|
|
|
|
URL *url.URL // Request url
|
|
|
|
Namespace string
|
|
|
|
User string // User owner id
|
|
|
|
Tagging *data.Tagging
|
|
|
|
RequestVHSEnabled bool
|
|
|
|
RequestType ReqType
|
2021-09-15 09:32:26 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// ObjectRequest represents object request data.
|
2020-08-11 11:27:31 +00:00
|
|
|
ObjectRequest struct {
|
|
|
|
Bucket string
|
|
|
|
Object string
|
|
|
|
Method string
|
|
|
|
}
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2023-07-05 14:04:52 +00:00
|
|
|
// Key used for custom key/value in context.
|
|
|
|
contextKeyType string
|
|
|
|
)
|
2020-07-22 13:02:32 +00:00
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
const (
|
|
|
|
ctxRequestInfo = contextKeyType("FrostFS-S3-GW")
|
|
|
|
ctxRequestLogger = contextKeyType("FrostFS-S3-GW-Logger")
|
|
|
|
)
|
2021-09-15 09:32:26 +00:00
|
|
|
|
2023-07-05 14:04:52 +00:00
|
|
|
const HdrAmzRequestID = "x-amz-request-id"
|
|
|
|
|
|
|
|
var deploymentID = uuid.Must(uuid.NewRandom())
|
|
|
|
|
2021-09-15 09:32:26 +00:00
|
|
|
var (
|
|
|
|
// De-facto standard header keys.
|
2024-08-06 12:43:45 +00:00
|
|
|
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
|
|
|
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
|
|
|
xForwardedProto = http.CanonicalHeaderKey("X-Forwarded-Proto")
|
|
|
|
xForwardedScheme = http.CanonicalHeaderKey("X-Forwarded-Scheme")
|
2021-09-15 09:32:26 +00:00
|
|
|
|
|
|
|
// RFC7239 defines a new "Forwarded: " header designed to replace the
|
|
|
|
// existing use of X-Forwarded-* headers.
|
|
|
|
// e.g. Forwarded: for=192.0.2.60;proto=https;by=203.0.113.43.
|
|
|
|
forwarded = http.CanonicalHeaderKey("Forwarded")
|
|
|
|
// Allows for a sub-match of the first value after 'for=' to the next
|
|
|
|
// comma, semi-colon or space. The match is case-insensitive.
|
|
|
|
forRegex = regexp.MustCompile(`(?i)(?:for=)([^(;|, )]+)(.*)`)
|
2024-08-06 12:43:45 +00:00
|
|
|
// Allows for a sub-match for the first instance of scheme (http|https)
|
|
|
|
// prefixed by 'proto='. The match is case-insensitive.
|
|
|
|
protoRegex = regexp.MustCompile(`(?i)^(;|,| )+(?:proto=)(https|http)`)
|
2021-09-15 09:32:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NewReqInfo returns new ReqInfo based on parameters.
|
2024-04-17 14:08:55 +00:00
|
|
|
func NewReqInfo(w http.ResponseWriter, r *http.Request, req ObjectRequest, sourceIPHeader string) *ReqInfo {
|
|
|
|
reqInfo := &ReqInfo{
|
2021-09-15 09:32:26 +00:00
|
|
|
API: req.Method,
|
|
|
|
BucketName: req.Bucket,
|
|
|
|
ObjectName: req.Object,
|
|
|
|
UserAgent: r.UserAgent(),
|
|
|
|
RequestID: GetRequestID(w),
|
|
|
|
DeploymentID: deploymentID.String(),
|
|
|
|
URL: r.URL,
|
|
|
|
}
|
2024-04-17 14:08:55 +00:00
|
|
|
|
|
|
|
if sourceIPHeader != "" {
|
|
|
|
reqInfo.RemoteHost = r.Header.Get(sourceIPHeader)
|
|
|
|
} else {
|
|
|
|
reqInfo.RemoteHost = getSourceIP(r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return reqInfo
|
2021-09-15 09:32:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 14:04:52 +00:00
|
|
|
// GetRequestID returns the request ID from the response writer or the context.
|
|
|
|
func GetRequestID(v interface{}) string {
|
|
|
|
switch t := v.(type) {
|
|
|
|
case context.Context:
|
|
|
|
return GetReqInfo(t).RequestID
|
|
|
|
case http.ResponseWriter:
|
|
|
|
return t.Header().Get(HdrAmzRequestID)
|
|
|
|
default:
|
|
|
|
panic("unknown type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 09:32:26 +00:00
|
|
|
// SetReqInfo sets ReqInfo in the context.
|
|
|
|
func SetReqInfo(ctx context.Context, req *ReqInfo) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return context.WithValue(ctx, ctxRequestInfo, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReqInfo returns ReqInfo if set.
|
2023-06-09 13:19:23 +00:00
|
|
|
// If ReqInfo isn't set returns new empty ReqInfo.
|
2021-09-15 09:32:26 +00:00
|
|
|
func GetReqInfo(ctx context.Context) *ReqInfo {
|
|
|
|
if ctx == nil {
|
|
|
|
return &ReqInfo{}
|
|
|
|
} else if r, ok := ctx.Value(ctxRequestInfo).(*ReqInfo); ok {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return &ReqInfo{}
|
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
|
|
|
|
// SetReqLogger sets child zap.Logger in the context.
|
|
|
|
func SetReqLogger(ctx context.Context, log *zap.Logger) context.Context {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return context.WithValue(ctx, ctxRequestLogger, log)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetReqLog returns log if set.
|
|
|
|
// If zap.Logger isn't set returns nil.
|
|
|
|
func GetReqLog(ctx context.Context) *zap.Logger {
|
|
|
|
if ctx == nil {
|
|
|
|
return nil
|
|
|
|
} else if r, ok := ctx.Value(ctxRequestLogger).(*zap.Logger); ok {
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-05 14:04:52 +00:00
|
|
|
|
2023-11-16 12:10:51 +00:00
|
|
|
type RequestSettings interface {
|
|
|
|
NamespaceHeader() string
|
2024-02-09 14:38:14 +00:00
|
|
|
ResolveNamespaceAlias(string) string
|
2024-04-17 14:08:55 +00:00
|
|
|
SourceIPHeader() string
|
2023-11-16 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Request(log *zap.Logger, settings RequestSettings) Func {
|
2023-07-05 14:04:52 +00:00
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// generate random UUIDv4
|
2024-02-29 14:55:25 +00:00
|
|
|
id, err := uuid.NewRandom()
|
|
|
|
if err != nil {
|
|
|
|
log.Error(logs.FailedToGenerateRequestID, zap.Error(err))
|
|
|
|
}
|
2023-07-05 14:04:52 +00:00
|
|
|
|
|
|
|
// set request id into response header
|
|
|
|
// also we have to set request id here
|
|
|
|
// to be able to get it in NewReqInfo
|
|
|
|
w.Header().Set(HdrAmzRequestID, id.String())
|
|
|
|
|
|
|
|
// set request info into context
|
|
|
|
// bucket name and object will be set in reqInfo later (limitation of go-chi)
|
2024-04-17 14:08:55 +00:00
|
|
|
reqInfo := NewReqInfo(w, r, ObjectRequest{}, settings.SourceIPHeader())
|
2024-02-09 14:38:14 +00:00
|
|
|
reqInfo.Namespace = settings.ResolveNamespaceAlias(r.Header.Get(settings.NamespaceHeader()))
|
2023-07-05 14:04:52 +00:00
|
|
|
r = r.WithContext(SetReqInfo(r.Context(), reqInfo))
|
|
|
|
|
|
|
|
// set request id into gRPC meta header
|
|
|
|
r = r.WithContext(metadata.AppendToOutgoingContext(
|
|
|
|
r.Context(), HdrAmzRequestID, reqInfo.RequestID,
|
|
|
|
))
|
|
|
|
|
2023-10-23 14:06:21 +00:00
|
|
|
r = r.WithContext(treepool.SetRequestID(r.Context(), reqInfo.RequestID))
|
|
|
|
|
2024-10-25 01:39:59 +00:00
|
|
|
fields := []zap.Field{zap.String("request_id", reqInfo.RequestID)}
|
|
|
|
ctx, span := StartHTTPServerSpan(r, "REQUEST S3")
|
|
|
|
if traceID := span.SpanContext().TraceID(); traceID.IsValid() {
|
|
|
|
fields = append(fields, zap.String("trace_id", traceID.String()))
|
|
|
|
}
|
|
|
|
lw := &traceResponseWriter{ResponseWriter: w, ctx: ctx, span: span}
|
|
|
|
|
|
|
|
reqLogger := log.With(fields...)
|
|
|
|
r = r.WithContext(SetReqLogger(ctx, reqLogger))
|
2023-07-05 14:04:52 +00:00
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
reqLogger.Info(logs.RequestStart, zap.String("host", r.Host),
|
2024-02-21 12:23:15 +00:00
|
|
|
zap.String("remote_host", reqInfo.RemoteHost), zap.String("namespace", reqInfo.Namespace))
|
2023-07-05 14:04:52 +00:00
|
|
|
|
|
|
|
// continue execution
|
2024-10-25 01:39:59 +00:00
|
|
|
h.ServeHTTP(lw, r)
|
2023-07-05 14:04:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// getSourceIP retrieves the IP from the X-Forwarded-For, X-Real-IP and RFC7239
|
|
|
|
// Forwarded headers (in that order), falls back to r.RemoteAddr when everything
|
|
|
|
// else fails.
|
|
|
|
func getSourceIP(r *http.Request) string {
|
|
|
|
var addr string
|
|
|
|
|
|
|
|
if fwd := r.Header.Get(xForwardedFor); fwd != "" {
|
|
|
|
// Only grabs the first (client) address. Note that '192.168.0.1,
|
|
|
|
// 10.1.1.1' is a valid key for X-Forwarded-For where addresses after
|
|
|
|
// the first one may represent forwarding proxies earlier in the chain.
|
|
|
|
s := strings.Index(fwd, ", ")
|
|
|
|
if s == -1 {
|
|
|
|
s = len(fwd)
|
|
|
|
}
|
|
|
|
addr = fwd[:s]
|
|
|
|
} else if fwd := r.Header.Get(xRealIP); fwd != "" {
|
|
|
|
// X-Real-IP should only contain one IP address (the client making the
|
|
|
|
// request).
|
|
|
|
addr = fwd
|
|
|
|
} else if fwd := r.Header.Get(forwarded); fwd != "" {
|
|
|
|
// match should contain at least two elements if the protocol was
|
|
|
|
// specified in the Forwarded header. The first element will always be
|
|
|
|
// the 'for=' capture, which we ignore. In the case of multiple IP
|
|
|
|
// addresses (for=8.8.8.8, 8.8.4.4, 172.16.1.20 is valid) we only
|
|
|
|
// extract the first, which should be the client IP.
|
|
|
|
if match := forRegex.FindStringSubmatch(fwd); len(match) > 1 {
|
|
|
|
// IPv6 addresses in Forwarded headers are quoted-strings. We strip
|
|
|
|
// these quotes.
|
2023-10-27 15:15:33 +00:00
|
|
|
addr = data.UnQuote(match[1])
|
2023-07-05 14:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 14:08:55 +00:00
|
|
|
if addr == "" {
|
|
|
|
addr = r.RemoteAddr
|
2023-07-05 14:04:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Default to remote address if headers not set.
|
2024-04-17 14:08:55 +00:00
|
|
|
raddr, _, _ := net.SplitHostPort(addr)
|
|
|
|
if raddr == "" {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
return raddr
|
2023-07-05 14:04:52 +00:00
|
|
|
}
|
2024-08-06 12:43:45 +00:00
|
|
|
|
|
|
|
// GetSourceScheme retrieves the scheme from the X-Forwarded-Proto and RFC7239
|
|
|
|
// Forwarded headers (in that order).
|
|
|
|
func GetSourceScheme(r *http.Request) string {
|
|
|
|
var scheme string
|
|
|
|
|
|
|
|
// Retrieve the scheme from X-Forwarded-Proto.
|
|
|
|
if proto := r.Header.Get(xForwardedProto); proto != "" {
|
|
|
|
scheme = strings.ToLower(proto)
|
|
|
|
} else if proto = r.Header.Get(xForwardedScheme); proto != "" {
|
|
|
|
scheme = strings.ToLower(proto)
|
|
|
|
} else if proto := r.Header.Get(forwarded); proto != "" {
|
|
|
|
// match should contain at least two elements if the protocol was
|
|
|
|
// specified in the Forwarded header. The first element will always be
|
|
|
|
// the 'for=', which we ignore, subsequently we proceed to look for
|
|
|
|
// 'proto=' which should precede right after `for=` if not
|
|
|
|
// we simply ignore the values and return empty. This is in line
|
|
|
|
// with the approach we took for returning first ip from multiple
|
|
|
|
// params.
|
|
|
|
if match := forRegex.FindStringSubmatch(proto); len(match) > 1 {
|
|
|
|
if match = protoRegex.FindStringSubmatch(match[2]); len(match) > 1 {
|
|
|
|
scheme = strings.ToLower(match[2])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return scheme
|
|
|
|
}
|