Skip to content

Commit

Permalink
Report restful info to hook
Browse files Browse the repository at this point in the history
Signed-off-by: aoiasd <[email protected]>
  • Loading branch information
aoiasd committed Nov 29, 2024
1 parent fbb68ca commit 2659a17
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 28 deletions.
35 changes: 7 additions & 28 deletions internal/distributed/proxy/httpserver/handler_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ import (
"google.golang.org/protobuf/proto"

"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
"github.com/milvus-io/milvus-proto/go-api/v2/hook"
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
"github.com/milvus-io/milvus-proto/go-api/v2/schemapb"
"github.com/milvus-io/milvus/internal/json"
"github.com/milvus-io/milvus/internal/proto/internalpb"
"github.com/milvus-io/milvus/internal/proxy"
"github.com/milvus-io/milvus/internal/types"
"github.com/milvus-io/milvus/internal/util/hookutil"
"github.com/milvus-io/milvus/pkg/common"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/metrics"
Expand Down Expand Up @@ -315,13 +317,15 @@ func wrapperProxyWithLimit(ctx context.Context, c *gin.Context, req any, checkAu
if checkAuth {
err := checkAuthorizationV2(ctx, c, ignoreErr, req)
if err != nil {
hookutil.GetExtension().ReportRefused(ctx, req, WrapErrorToResponse(err), nil, c.FullPath())

Check failure on line 320 in internal/distributed/proxy/httpserver/handler_v2.go

View workflow job for this annotation

GitHub Actions / Code Checker MacOS 13

hookutil.GetExtension().ReportRefused undefined (type hook.Extension has no field or method ReportRefused)
return nil, err
}
}
if checkLimit {
_, err := CheckLimiter(ctx, req, pxy)
if err != nil {
log.Warn("high level restful api, fail to check limiter", zap.Error(err), zap.String("method", fullMethod))
hookutil.GetExtension().ReportRefused(ctx, req, WrapErrorToResponse(merr.ErrHTTPRateLimit), nil, c.FullPath())

Check failure on line 328 in internal/distributed/proxy/httpserver/handler_v2.go

View workflow job for this annotation

GitHub Actions / Code Checker MacOS 13

hookutil.GetExtension().ReportRefused undefined (type hook.Extension has no field or method ReportRefused)
HTTPAbortReturn(c, http.StatusOK, gin.H{
HTTPReturnCode: merr.Code(merr.ErrHTTPRateLimit),
HTTPReturnMessage: merr.ErrHTTPRateLimit.Error() + ", error: " + err.Error(),
Expand All @@ -334,13 +338,15 @@ func wrapperProxyWithLimit(ctx context.Context, c *gin.Context, req any, checkAu
if !ok {
username = ""
}
response, err := proxy.HookInterceptor(ctx, req, username.(string), fullMethod, handler)

response, err := proxy.HookInterceptor(context.WithValue(ctx, hook.GinParamsKey, c.Keys), req, username.(string), fullMethod, handler)

Check failure on line 342 in internal/distributed/proxy/httpserver/handler_v2.go

View workflow job for this annotation

GitHub Actions / Code Checker MacOS 13

undefined: hook.GinParamsKey
if err == nil {
status, ok := requestutil.GetStatusFromResponse(response)
if ok {
err = merr.Error(status)
}
}

if err != nil {
log.Ctx(ctx).Warn("high level restful api, grpc call failed", zap.Error(err))
if !ignoreErr {
Expand Down Expand Up @@ -2162,15 +2168,6 @@ func (h *HandlersV2) listImportJob(ctx context.Context, c *gin.Context, anyReq a
}
c.Set(ContextRequest, req)

if h.checkAuth {
err := checkAuthorizationV2(ctx, c, false, &milvuspb.ListImportsAuthPlaceholder{
DbName: dbName,
CollectionName: collectionName,
})
if err != nil {
return nil, err
}
}
resp, err := wrapperProxy(ctx, c, req, false, false, "/milvus.proto.milvus.MilvusService/ListImports", func(reqCtx context.Context, req any) (interface{}, error) {
return h.proxy.ListImports(reqCtx, req.(*internalpb.ListImportsRequest))
})
Expand Down Expand Up @@ -2214,16 +2211,6 @@ func (h *HandlersV2) createImportJob(ctx context.Context, c *gin.Context, anyReq
}
c.Set(ContextRequest, req)

if h.checkAuth {
err := checkAuthorizationV2(ctx, c, false, &milvuspb.ImportAuthPlaceholder{
DbName: dbName,
CollectionName: collectionGetter.GetCollectionName(),
PartitionName: partitionGetter.GetPartitionName(),
})
if err != nil {
return nil, err
}
}
resp, err := wrapperProxy(ctx, c, req, false, false, "/milvus.proto.milvus.MilvusService/ImportV2", func(reqCtx context.Context, req any) (interface{}, error) {
return h.proxy.ImportV2(reqCtx, req.(*internalpb.ImportRequest))
})
Expand All @@ -2243,14 +2230,6 @@ func (h *HandlersV2) getImportJobProcess(ctx context.Context, c *gin.Context, an
}
c.Set(ContextRequest, req)

if h.checkAuth {
err := checkAuthorizationV2(ctx, c, false, &milvuspb.GetImportProgressAuthPlaceholder{
DbName: dbName,
})
if err != nil {
return nil, err
}
}
resp, err := wrapperProxy(ctx, c, req, false, false, "/milvus.proto.milvus.MilvusService/GetImportProgress", func(reqCtx context.Context, req any) (interface{}, error) {
return h.proxy.GetImportProgress(reqCtx, req.(*internalpb.GetImportProgressRequest))
})
Expand Down
6 changes: 6 additions & 0 deletions internal/distributed/proxy/httpserver/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,3 +1679,9 @@ func RequestHandlerFunc(c *gin.Context) {
}
c.Next()
}

func WrapErrorToResponse(err error) *milvuspb.BoolResponse {
return &milvuspb.BoolResponse{
Status: merr.Status(err),
}
}
5 changes: 5 additions & 0 deletions internal/distributed/proxy/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
"github.com/milvus-io/milvus/internal/util/componentutil"
"github.com/milvus-io/milvus/internal/util/dependency"
_ "github.com/milvus-io/milvus/internal/util/grpcclient"
"github.com/milvus-io/milvus/internal/util/hookutil"
"github.com/milvus-io/milvus/pkg/log"
"github.com/milvus-io/milvus/pkg/metrics"
"github.com/milvus-io/milvus/pkg/tracer"
Expand Down Expand Up @@ -141,6 +142,10 @@ func authenticate(c *gin.Context) {
c.Set(httpserver.ContextUsername, user)
return
}

hookutil.GetExtension().ReportRefused(context.Background(), nil, &milvuspb.BoolResponse{
Status: merr.Status(err),
}, nil, c.FullPath())
log.Warn("fail to verify apikey", zap.Error(err))
}
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{mhttp.HTTPReturnCode: merr.Code(merr.ErrNeedAuthenticate), mhttp.HTTPReturnMessage: merr.ErrNeedAuthenticate.Error()})
Expand Down
4 changes: 4 additions & 0 deletions internal/util/hookutil/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ var _ hook.Extension = (*DefaultExtension)(nil)
func (d DefaultExtension) Report(info any) int {
return 0
}

func (d DefaultExtension) ReportRefused(ctx context.Context, req interface{}, resp interface{}, err error, fullMethod string) error {
return nil
}

0 comments on commit 2659a17

Please sign in to comment.