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-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/go-chi/chi/v5"
|
|
|
|
"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
|
|
|
|
RemoteHost string // Client Host/IP
|
|
|
|
Host string // Node Host/IP
|
|
|
|
UserAgent string // User Agent
|
2022-01-20 14:47:21 +00:00
|
|
|
DeploymentID string // random generated s3-deployment-id
|
2021-09-15 09:32:26 +00:00
|
|
|
RequestID string // x-amz-request-id
|
2022-04-13 16:56:58 +00:00
|
|
|
API string // API name -- GetObject PutObject NewMultipartUpload etc.
|
2021-09-15 09:32:26 +00:00
|
|
|
BucketName string // Bucket name
|
|
|
|
ObjectName string // Object name
|
2023-08-29 09:27:50 +00:00
|
|
|
TraceID string // Trace ID
|
2024-02-26 14:01:13 +00:00
|
|
|
User string // User owner id
|
2021-09-15 09:32:26 +00:00
|
|
|
URL *url.URL // Request url
|
|
|
|
tags []KeyVal // Any additional info not accommodated by above fields
|
|
|
|
}
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
const (
|
|
|
|
BucketURLPrm = "bucket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var deploymentID = uuid.Must(uuid.NewRandom())
|
|
|
|
|
2021-09-15 09:32:26 +00:00
|
|
|
var (
|
|
|
|
// De-facto standard header keys.
|
|
|
|
xForwardedFor = http.CanonicalHeaderKey("X-Forwarded-For")
|
|
|
|
xRealIP = http.CanonicalHeaderKey("X-Real-IP")
|
|
|
|
|
|
|
|
// 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=)([^(;|, )]+)(.*)`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewReqInfo returns new ReqInfo based on parameters.
|
|
|
|
func NewReqInfo(w http.ResponseWriter, r *http.Request, req ObjectRequest) *ReqInfo {
|
|
|
|
return &ReqInfo{
|
|
|
|
API: req.Method,
|
|
|
|
BucketName: req.Bucket,
|
|
|
|
ObjectName: req.Object,
|
|
|
|
UserAgent: r.UserAgent(),
|
2023-07-05 14:04:52 +00:00
|
|
|
RemoteHost: getSourceIP(r),
|
2021-09-15 09:32:26 +00:00
|
|
|
RequestID: GetRequestID(w),
|
|
|
|
DeploymentID: deploymentID.String(),
|
|
|
|
URL: r.URL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// AppendTags -- appends key/val to ReqInfo.tags.
|
2021-09-15 09:32:26 +00:00
|
|
|
func (r *ReqInfo) AppendTags(key string, val string) *ReqInfo {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
r.tags = append(r.tags, KeyVal{key, val})
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// SetTags -- sets key/val to ReqInfo.tags.
|
2021-09-15 09:32:26 +00:00
|
|
|
func (r *ReqInfo) SetTags(key string, val string) *ReqInfo {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
2022-04-13 16:56:58 +00:00
|
|
|
// Search for a tag key already existing in tags
|
2021-09-15 09:32:26 +00:00
|
|
|
var updated bool
|
|
|
|
for _, tag := range r.tags {
|
|
|
|
if tag.Key == key {
|
|
|
|
tag.Val = val
|
|
|
|
updated = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !updated {
|
|
|
|
// Append to the end of tags list
|
|
|
|
r.tags = append(r.tags, KeyVal{key, val})
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// GetTags -- returns the user defined tags.
|
2021-09-15 09:32:26 +00:00
|
|
|
func (r *ReqInfo) GetTags() []KeyVal {
|
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r.RLock()
|
|
|
|
defer r.RUnlock()
|
|
|
|
return append([]KeyVal(nil), r.tags...)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func Request(log *zap.Logger) Func {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// generate random UUIDv4
|
|
|
|
id, _ := uuid.NewRandom()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
reqInfo := NewReqInfo(w, r, ObjectRequest{})
|
|
|
|
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))
|
|
|
|
|
2023-07-05 14:04:52 +00:00
|
|
|
reqLogger := log.With(zap.String("request_id", reqInfo.RequestID))
|
|
|
|
r = r.WithContext(SetReqLogger(r.Context(), reqLogger))
|
|
|
|
|
2023-08-23 11:07:52 +00:00
|
|
|
reqLogger.Info(logs.RequestStart, zap.String("host", r.Host),
|
2023-07-05 14:04:52 +00:00
|
|
|
zap.String("remote_host", reqInfo.RemoteHost))
|
|
|
|
|
|
|
|
// continue execution
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddBucketName adds bucket name to ReqInfo from context.
|
|
|
|
func AddBucketName(l *zap.Logger) Func {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
reqInfo := GetReqInfo(ctx)
|
|
|
|
reqInfo.BucketName = chi.URLParam(r, BucketURLPrm)
|
|
|
|
|
|
|
|
reqLogger := reqLogOrDefault(ctx, l)
|
|
|
|
r = r.WithContext(SetReqLogger(ctx, reqLogger.With(zap.String("bucket", reqInfo.BucketName))))
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddObjectName adds objects name to ReqInfo from context.
|
|
|
|
func AddObjectName(l *zap.Logger) Func {
|
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-07-19 12:25:11 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := GetReqInfo(ctx)
|
|
|
|
|
|
|
|
rctx := chi.RouteContext(ctx)
|
|
|
|
// trim leading slash (always present)
|
2023-08-17 08:54:11 +00:00
|
|
|
reqInfo.ObjectName = rctx.RoutePath[1:]
|
2023-07-05 14:04:52 +00:00
|
|
|
|
|
|
|
reqLogger := reqLogOrDefault(ctx, l)
|
|
|
|
r = r.WithContext(SetReqLogger(ctx, reqLogger.With(zap.String("object", reqInfo.ObjectName))))
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
addr = strings.Trim(match[1], `"`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr != "" {
|
|
|
|
return addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default to remote address if headers not set.
|
|
|
|
addr, _, _ = net.SplitHostPort(r.RemoteAddr)
|
|
|
|
return addr
|
|
|
|
}
|