2026-01-27 13:25:45: merge [main:e5e723811e7a9dc57a6ff5d1c067c07c23a97210];
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
Create and publish a Docker image / build-and-push-image (push) Canceled after 0s
This commit is contained in:
1 parent
6a3abf67a2
commit
602a2a1ac4
24 files changed
+222
-114
No files matched your search
@@ -9,19 +9,16 @@ import (
|
||||
"isp-gate-service/request"
|
||||
)
|
||||
|
||||
type AdminMethodStore interface {
|
||||
IsInnerEndpoint(endpoint string) bool
|
||||
RequiredAdminPermission(endpoint string) (string, bool)
|
||||
}
|
||||
|
||||
type AdminAuthorizer interface {
|
||||
AdminAuthorize(ctx context.Context, adminId int, permission string) (bool, error)
|
||||
}
|
||||
|
||||
func AdminAuthorize(store AdminMethodStore, authorizer AdminAuthorizer) Middleware {
|
||||
func AdminAuthorize(authorizer AdminAuthorizer) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
isInner := store.IsInnerEndpoint(ctx.Endpoint())
|
||||
endpointMeta := ctx.EndpointMeta()
|
||||
|
||||
isInner := endpointMeta.Inner
|
||||
if !isInner {
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
@@ -30,12 +27,13 @@ func AdminAuthorize(store AdminMethodStore, authorizer AdminAuthorizer) Middlewa
|
||||
return httperrors.New(
|
||||
http.StatusForbidden,
|
||||
"admin authentication required",
|
||||
errors.Errorf("admin authorization: admin authentication required for '%s'", ctx.Endpoint()),
|
||||
errors.Errorf("admin authorization: admin authentication required for '%s'",
|
||||
endpointMeta.Endpoint),
|
||||
)
|
||||
}
|
||||
|
||||
requiredPerm, ok := store.RequiredAdminPermission(ctx.Endpoint())
|
||||
if !ok {
|
||||
requiredPerm := endpointMeta.RequiredAdminPermission
|
||||
if requiredPerm == "" {
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
|
||||
@@ -49,7 +47,7 @@ func AdminAuthorize(store AdminMethodStore, authorizer AdminAuthorizer) Middlewa
|
||||
"endpoint is not allowed",
|
||||
errors.Errorf(
|
||||
"admin authorization: endpoint '%s' requires '%s' permission, but admin '%d' doesn't have it",
|
||||
ctx.Endpoint(), requiredPerm, ctx.AdminId(),
|
||||
endpointMeta.Endpoint, requiredPerm, ctx.AdminId(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
+11
-3
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
type Authorizer interface {
|
||||
Authorize(ctx context.Context, applicationId int, endpoint string) (bool, error)
|
||||
Authorize(ctx context.Context, applicationId int, httpMethod string, pathSchema string) (bool, error)
|
||||
}
|
||||
|
||||
func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
|
||||
@@ -22,7 +22,13 @@ func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
|
||||
return errors.WithMessage(err, "authorize: get auth data")
|
||||
}
|
||||
|
||||
ok, err := authorizer.Authorize(ctx.Context(), authData.ApplicationId, ctx.Endpoint())
|
||||
endpointMeta := ctx.EndpointMeta()
|
||||
ok, err := authorizer.Authorize(
|
||||
ctx.Context(),
|
||||
authData.ApplicationId,
|
||||
ctx.Request().Method,
|
||||
endpointMeta.PathSchema,
|
||||
)
|
||||
if err != nil {
|
||||
return errors.WithMessage(err, "authorize: authorize")
|
||||
}
|
||||
@@ -40,7 +46,9 @@ func Authorize(authorizer Authorizer, logger log.Logger) Middleware {
|
||||
return httperrors.New(
|
||||
http.StatusForbidden,
|
||||
"endpoint is not allowed",
|
||||
errors.Errorf("authorize: endpoint '%s' is not allowed for application '%d'", ctx.Endpoint(), authData.ApplicationId),
|
||||
errors.Errorf("authorize: endpoint '%s' with http method '%s' is not allowed for application '%d'",
|
||||
endpointMeta.PathSchema, ctx.Request().Method, authData.ApplicationId,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,23 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
"isp-gate-service/domain"
|
||||
"isp-gate-service/request"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/txix-open/isp-kit/log"
|
||||
)
|
||||
|
||||
type EntryPointConfig struct {
|
||||
PathPrefix string
|
||||
WithPrefix bool
|
||||
PathPrefix string
|
||||
WithPrefix bool
|
||||
ErrorOnUnknownEndpoint bool
|
||||
WithLendingSlash bool
|
||||
}
|
||||
|
||||
type EndpointResolver interface {
|
||||
ResolveEndpoint(path string, cfg EntryPointConfig) string
|
||||
ResolveEndpoint(method string, path string, cfg EntryPointConfig) (*domain.EndpointMeta, error)
|
||||
}
|
||||
|
||||
func Entrypoint(
|
||||
@@ -26,10 +30,15 @@ func Entrypoint(
|
||||
return http.HandlerFunc(func(writer http.ResponseWriter, req *http.Request) {
|
||||
req.Body = http.MaxBytesReader(writer, req.Body, maxReqBodySize)
|
||||
|
||||
endpoint := entryPointResolver.ResolveEndpoint(req.URL.Path, cfg)
|
||||
endpoint, err := entryPointResolver.ResolveEndpoint(req.Method, req.URL.Path, cfg)
|
||||
if err != nil {
|
||||
http.Error(writer, err.Error(), http.StatusNotImplemented)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.NewContext(req, writer, endpoint)
|
||||
|
||||
err := next.Handle(ctx)
|
||||
err = next.Handle(ctx)
|
||||
if err != nil {
|
||||
logger.Error(req.Context(), errors.WithMessage(err, "uncaught error"))
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ func Logger( // nolint:gocognit
|
||||
logBodyFromCurrenRequest := enableBodyLogging
|
||||
if logBodyFromCurrenRequest {
|
||||
for _, prefix := range skipBodyLoggingEndpointPrefixes {
|
||||
if strings.HasPrefix(ctx.Endpoint(), prefix) {
|
||||
if strings.HasPrefix(ctx.EndpointMeta().Endpoint, prefix) {
|
||||
logBodyFromCurrenRequest = false
|
||||
break
|
||||
}
|
||||
@@ -110,7 +110,7 @@ func Logger( // nolint:gocognit
|
||||
log.String("xForwardedFor", r.Header.Get("X-Forwarded-For")),
|
||||
log.Int("statusCode", scSrc.StatusCode()),
|
||||
log.String("path", originalPath),
|
||||
log.String("endpoint", ctx.Endpoint()),
|
||||
log.String("endpoint", ctx.EndpointMeta().Endpoint),
|
||||
log.Int("applicationId", authData.ApplicationId),
|
||||
log.String("applicationName", authData.AppName),
|
||||
log.Int("adminId", ctx.AdminId()),
|
||||
|
||||
@@ -10,8 +10,8 @@ import (
|
||||
func Metrics(storage *http_metrics.ServerStorage) Middleware {
|
||||
return func(next Handler) Handler {
|
||||
return HandlerFunc(func(ctx *request.Context) error {
|
||||
endpoint := ctx.Endpoint()
|
||||
if endpoint == "" {
|
||||
pathSchema := ctx.EndpointMeta().PathSchema
|
||||
if pathSchema == "" {
|
||||
return next.Handle(ctx)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ func Metrics(storage *http_metrics.ServerStorage) Middleware {
|
||||
return err
|
||||
}
|
||||
|
||||
storage.ObserveDuration(r.Method, endpoint, time.Since(start))
|
||||
storage.ObserveDuration(r.Method, pathSchema, time.Since(start))
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user