perf(files): 优化私有文件鉴权与高成本查询隔离
This commit is contained in:
@@ -3,10 +3,16 @@ package file
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/filekey"
|
||||
"hfb_sys/backend/internal/logging"
|
||||
"hfb_sys/backend/internal/middleware"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -19,6 +25,8 @@ type Handler struct {
|
||||
objectAuthorizer ObjectAuthorizer
|
||||
adminObjectAuthorizer AdminObjectAuthorizer
|
||||
uploadOwnerRecorder UploadOwnerRecorder
|
||||
accessTickets *AccessTicketManager
|
||||
authGate chan struct{}
|
||||
}
|
||||
|
||||
// ObjectAuthorizer 校验用户是否可读取指定私有对象。
|
||||
@@ -30,6 +38,8 @@ type AdminObjectAuthorizer func(ctx context.Context, adminID uint64, key string)
|
||||
// UploadOwnerRecorder 保存用户或管理员上传私有文件的归属。
|
||||
type UploadOwnerRecorder func(ctx context.Context, userID, adminID uint64, objectKeys []string) error
|
||||
|
||||
const maxConcurrentFileAuthorizations = 8
|
||||
|
||||
func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAuthorizer, uploadOwnerRecorder UploadOwnerRecorder, adminObjectAuthorizers ...AdminObjectAuthorizer) *Handler {
|
||||
var adminObjectAuthorizer AdminObjectAuthorizer
|
||||
if len(adminObjectAuthorizers) > 0 {
|
||||
@@ -41,9 +51,14 @@ func NewHandler(service *Service, storage *Storage, objectAuthorizer ObjectAutho
|
||||
objectAuthorizer: objectAuthorizer,
|
||||
adminObjectAuthorizer: adminObjectAuthorizer,
|
||||
uploadOwnerRecorder: uploadOwnerRecorder,
|
||||
authGate: make(chan struct{}, maxConcurrentFileAuthorizations),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) SetAccessTicketManager(manager *AccessTicketManager) {
|
||||
h.accessTickets = manager
|
||||
}
|
||||
|
||||
func (h *Handler) Upload(c *gin.Context) {
|
||||
header, err := c.FormFile("file")
|
||||
if err != nil {
|
||||
@@ -103,6 +118,77 @@ func (h *Handler) Upload(c *gin.Context) {
|
||||
response.Created(c, item)
|
||||
}
|
||||
|
||||
type batchAccessRequest struct {
|
||||
Keys []string `json:"keys"`
|
||||
}
|
||||
|
||||
type batchAccessResponse struct {
|
||||
Tickets map[string]string `json:"tickets"`
|
||||
ExpiresInSeconds int64 `json:"expires_in_seconds"`
|
||||
}
|
||||
|
||||
// BatchAccess 在同一请求内校验相册图片,并签发短期对象访问票据。
|
||||
func (h *Handler) BatchAccess(c *gin.Context) {
|
||||
if h.accessTickets == nil {
|
||||
response.ServiceUnavailable(c, "文件访问票据未启用")
|
||||
return
|
||||
}
|
||||
var req batchAccessRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "文件列表格式不正确")
|
||||
return
|
||||
}
|
||||
if len(req.Keys) == 0 || len(req.Keys) > 100 {
|
||||
response.BadRequest(c, "每次最多校验 100 个文件")
|
||||
return
|
||||
}
|
||||
subjectType, subjectID := fileAccessSubject(c)
|
||||
if subjectID == 0 {
|
||||
response.Unauthorized(c, "缺少文件访问身份")
|
||||
return
|
||||
}
|
||||
if !h.acquireAuthorizationSlot(c.Request.Context(), 500*time.Millisecond) {
|
||||
c.Header("Retry-After", "1")
|
||||
response.Error(c, http.StatusTooManyRequests, "file_auth_busy", "图片鉴权繁忙,请稍后重试")
|
||||
return
|
||||
}
|
||||
defer h.releaseAuthorizationSlot()
|
||||
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
tenantID := tenancy.IDFromContext(ctx)
|
||||
tickets := make(map[string]string, len(req.Keys))
|
||||
seen := make(map[string]struct{}, len(req.Keys))
|
||||
for _, rawKey := range req.Keys {
|
||||
rawKey = strings.TrimSpace(rawKey)
|
||||
canonical := filekey.Canonical(rawKey)
|
||||
if rawKey == "" || canonical == "" {
|
||||
continue
|
||||
}
|
||||
identity := rawKey + "\x00" + canonical
|
||||
if _, ok := seen[identity]; ok {
|
||||
continue
|
||||
}
|
||||
seen[identity] = struct{}{}
|
||||
|
||||
allowed, err := h.authorizeObject(ctx, subjectType, subjectID, rawKey)
|
||||
if err != nil {
|
||||
response.ServiceUnavailable(c, "文件权限校验失败")
|
||||
return
|
||||
}
|
||||
if !allowed {
|
||||
continue
|
||||
}
|
||||
if ticket := h.accessTickets.Issue(subjectType, subjectID, tenantID, canonical); ticket != "" {
|
||||
tickets[rawKey] = ticket
|
||||
}
|
||||
}
|
||||
response.OK(c, batchAccessResponse{
|
||||
Tickets: tickets,
|
||||
ExpiresInSeconds: int64(accessTicketTTL.Seconds()),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) Object(c *gin.Context) {
|
||||
h.writeObject(c, false)
|
||||
}
|
||||
@@ -112,6 +198,7 @@ func (h *Handler) PublicObject(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
|
||||
totalStart := time.Now()
|
||||
if h.storage == nil {
|
||||
response.ServiceUnavailable(c, "文件存储未连接")
|
||||
return
|
||||
@@ -125,34 +212,28 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
|
||||
response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问")
|
||||
return
|
||||
}
|
||||
authMS := 0.0
|
||||
accessSource := "public"
|
||||
if !publicOnly {
|
||||
if adminIDValue, isAdmin := c.Get("admin_id"); isAdmin {
|
||||
adminID, ok := adminIDValue.(uint64)
|
||||
if !ok || h.adminObjectAuthorizer == nil {
|
||||
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件")
|
||||
return
|
||||
}
|
||||
allowed, err := h.adminObjectAuthorizer(c.Request.Context(), adminID, key)
|
||||
if err != nil {
|
||||
response.ServiceUnavailable(c, "文件权限校验失败")
|
||||
return
|
||||
}
|
||||
if !allowed {
|
||||
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件")
|
||||
return
|
||||
}
|
||||
authStart := time.Now()
|
||||
subjectType, subjectID := fileAccessSubject(c)
|
||||
tenantID := tenancy.IDFromContext(c.Request.Context())
|
||||
if ticket := strings.TrimSpace(c.Query("ticket")); ticket != "" &&
|
||||
h.accessTickets != nil &&
|
||||
h.accessTickets.Valid(ticket, subjectType, subjectID, tenantID, key) {
|
||||
accessSource = "ticket"
|
||||
} else {
|
||||
userID, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
response.Unauthorized(c, "缺少用户上下文")
|
||||
accessSource = "database"
|
||||
if !h.acquireAuthorizationSlot(c.Request.Context(), 500*time.Millisecond) {
|
||||
c.Header("Retry-After", "1")
|
||||
response.Error(c, http.StatusTooManyRequests, "file_auth_busy", "图片鉴权繁忙,请稍后重试")
|
||||
return
|
||||
}
|
||||
id, ok := userID.(uint64)
|
||||
if !ok || h.objectAuthorizer == nil {
|
||||
response.Error(c, http.StatusForbidden, "forbidden", "无权访问该文件")
|
||||
return
|
||||
}
|
||||
allowed, err := h.objectAuthorizer(c.Request.Context(), id, key)
|
||||
defer h.releaseAuthorizationSlot()
|
||||
|
||||
authCtx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
|
||||
allowed, err := h.authorizeObject(authCtx, subjectType, subjectID, key)
|
||||
cancel()
|
||||
if err != nil {
|
||||
response.ServiceUnavailable(c, "文件权限校验失败")
|
||||
return
|
||||
@@ -162,9 +243,25 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
|
||||
return
|
||||
}
|
||||
}
|
||||
authMS = float64(time.Since(authStart).Microseconds()) / 1000
|
||||
}
|
||||
|
||||
storageStart := time.Now()
|
||||
object, err := h.storage.Get(c.Request.Context(), key)
|
||||
storageMS := float64(time.Since(storageStart).Microseconds()) / 1000
|
||||
if err != nil {
|
||||
fields := []zap.Field{
|
||||
zap.String("access_source", accessSource),
|
||||
zap.String("object_prefix", fileObjectPrefix(key)),
|
||||
zap.Float64("auth_ms", authMS),
|
||||
zap.Float64("storage_open_ms", storageMS),
|
||||
zap.Error(err),
|
||||
}
|
||||
logger := logging.FromContext(c.Request.Context())
|
||||
logger.Debug("文件存储打开失败", fields...)
|
||||
if storageMS >= 500 {
|
||||
logger.Warn("文件存储打开慢请求", fields...)
|
||||
}
|
||||
response.Error(c, http.StatusNotFound, "not_found", "文件不存在或暂不可访问")
|
||||
return
|
||||
}
|
||||
@@ -181,7 +278,34 @@ func (h *Handler) writeObject(c *gin.Context, publicOnly bool) {
|
||||
} else {
|
||||
c.Header("Cache-Control", "private, max-age=300")
|
||||
}
|
||||
c.DataFromReader(http.StatusOK, object.Size, contentType, object.Reader, nil)
|
||||
c.Header("Content-Length", strconv.FormatInt(object.Size, 10))
|
||||
c.Status(http.StatusOK)
|
||||
streamStart := time.Now()
|
||||
_, streamErr := io.Copy(c.Writer, object.Reader)
|
||||
streamMS := float64(time.Since(streamStart).Microseconds()) / 1000
|
||||
totalMS := float64(time.Since(totalStart).Microseconds()) / 1000
|
||||
if streamErr != nil {
|
||||
response.RecordError(c, streamErr)
|
||||
}
|
||||
fields := []zap.Field{
|
||||
zap.Bool("public", publicOnly),
|
||||
zap.String("access_source", accessSource),
|
||||
zap.String("object_prefix", fileObjectPrefix(key)),
|
||||
zap.Int64("object_size", object.Size),
|
||||
zap.Int("status", c.Writer.Status()),
|
||||
zap.Float64("auth_ms", authMS),
|
||||
zap.Float64("storage_open_ms", storageMS),
|
||||
zap.Float64("stream_ms", streamMS),
|
||||
zap.Float64("total_ms", totalMS),
|
||||
}
|
||||
if streamErr != nil {
|
||||
fields = append(fields, zap.Error(streamErr))
|
||||
}
|
||||
logger := logging.FromContext(c.Request.Context())
|
||||
logger.Debug("私有文件读取", fields...)
|
||||
if totalMS >= 1000 {
|
||||
logger.Warn("文件读取慢请求", fields...)
|
||||
}
|
||||
}
|
||||
|
||||
func isPublicObjectKey(key string) bool {
|
||||
@@ -203,6 +327,74 @@ func isPublicObjectKey(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func fileAccessSubject(c *gin.Context) (string, uint64) {
|
||||
if value, exists := c.Get(middleware.ContextAdminID); exists {
|
||||
if adminID, ok := value.(uint64); ok && adminID > 0 {
|
||||
return "admin", adminID
|
||||
}
|
||||
}
|
||||
if value, exists := c.Get(middleware.ContextUserID); exists {
|
||||
if userID, ok := value.(uint64); ok && userID > 0 {
|
||||
return "user", userID
|
||||
}
|
||||
}
|
||||
return "", 0
|
||||
}
|
||||
|
||||
func (h *Handler) authorizeObject(ctx context.Context, subjectType string, subjectID uint64, key string) (bool, error) {
|
||||
if subjectID == 0 {
|
||||
return false, nil
|
||||
}
|
||||
if subjectType == "admin" {
|
||||
if h.adminObjectAuthorizer == nil {
|
||||
return false, nil
|
||||
}
|
||||
return h.adminObjectAuthorizer(ctx, subjectID, key)
|
||||
}
|
||||
if h.objectAuthorizer == nil {
|
||||
return false, nil
|
||||
}
|
||||
return h.objectAuthorizer(ctx, subjectID, key)
|
||||
}
|
||||
|
||||
func (h *Handler) acquireAuthorizationSlot(ctx context.Context, wait time.Duration) bool {
|
||||
if h.authGate == nil {
|
||||
return true
|
||||
}
|
||||
select {
|
||||
case h.authGate <- struct{}{}:
|
||||
return true
|
||||
default:
|
||||
}
|
||||
timer := time.NewTimer(wait)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case h.authGate <- struct{}{}:
|
||||
return true
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
case <-timer.C:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) releaseAuthorizationSlot() {
|
||||
if h.authGate == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case <-h.authGate:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func fileObjectPrefix(key string) string {
|
||||
if index := strings.IndexByte(key, '/'); index > 0 {
|
||||
return key[:index]
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
func writeFileError(c *gin.Context, err error) {
|
||||
response.RecordError(c, err)
|
||||
switch {
|
||||
|
||||
Reference in New Issue
Block a user