2022-04-11 09:35:06 +00:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2022-04-11 10:26:19 +00:00
|
|
|
"context"
|
2022-04-11 09:35:06 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
2022-08-11 14:07:53 +00:00
|
|
|
"time"
|
2022-04-11 09:35:06 +00:00
|
|
|
|
|
|
|
"github.com/go-openapi/errors"
|
2022-04-11 10:26:19 +00:00
|
|
|
"github.com/google/uuid"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-07-07 14:24:41 +00:00
|
|
|
sessionv2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/models"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations"
|
2022-07-07 12:59:38 +00:00
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/internal/util"
|
2022-08-11 14:07:53 +00:00
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/metrics"
|
2022-04-11 09:35:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2022-07-12 08:01:21 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-04-11 09:35:06 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// API is a REST v1 request handler.
|
|
|
|
type API struct {
|
|
|
|
log *zap.Logger
|
|
|
|
pool *pool.Pool
|
|
|
|
key *keys.PrivateKey
|
2022-07-12 08:01:21 +00:00
|
|
|
owner *user.ID
|
2022-04-11 09:35:06 +00:00
|
|
|
defaultTimestamp bool
|
2022-08-11 14:07:53 +00:00
|
|
|
|
|
|
|
gateMetric *metrics.GateMetrics
|
|
|
|
prometheusService *metrics.Service
|
|
|
|
pprofService *metrics.Service
|
|
|
|
serviceShutdownTimeout time.Duration
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrmAPI groups parameters to init rest API.
|
|
|
|
type PrmAPI struct {
|
|
|
|
Logger *zap.Logger
|
|
|
|
Pool *pool.Pool
|
|
|
|
Key *keys.PrivateKey
|
|
|
|
DefaultTimestamp bool
|
2022-08-11 14:07:53 +00:00
|
|
|
|
|
|
|
GateMetric *metrics.GateMetrics
|
|
|
|
PrometheusService *metrics.Service
|
|
|
|
PprofService *metrics.Service
|
|
|
|
ServiceShutdownTimeout time.Duration
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type BearerToken struct {
|
|
|
|
Token string
|
|
|
|
Signature string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
2022-07-07 14:24:41 +00:00
|
|
|
type SessionToken struct {
|
|
|
|
BearerToken
|
|
|
|
Verb sessionv2.ContainerSessionVerb
|
|
|
|
}
|
|
|
|
|
2022-04-11 10:26:19 +00:00
|
|
|
// ContextKey is used for context.Context value. The value requires a key that is not primitive type.
|
|
|
|
type ContextKey string
|
|
|
|
|
|
|
|
const (
|
2022-04-11 11:18:26 +00:00
|
|
|
// BearerPrefix is the prefix for authorization token.
|
2022-04-11 10:26:19 +00:00
|
|
|
BearerPrefix = "Bearer "
|
|
|
|
|
2022-04-11 11:18:26 +00:00
|
|
|
// ContextKeyRequestID is the ContextKey for RequestID.
|
2022-04-11 10:26:19 +00:00
|
|
|
ContextKeyRequestID ContextKey = "requestID"
|
2022-07-08 10:34:34 +00:00
|
|
|
|
2022-07-12 07:31:06 +00:00
|
|
|
docsPrefix = "/docs"
|
2022-04-11 10:26:19 +00:00
|
|
|
)
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
// New creates a new API using specified logger, connection pool and other parameters.
|
|
|
|
func New(prm *PrmAPI) *API {
|
2022-07-12 08:01:21 +00:00
|
|
|
var owner user.ID
|
|
|
|
user.IDFromKey(&owner, prm.Key.PrivateKey.PublicKey)
|
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
return &API{
|
|
|
|
log: prm.Logger,
|
|
|
|
pool: prm.Pool,
|
|
|
|
key: prm.Key,
|
2022-07-12 08:01:21 +00:00
|
|
|
owner: &owner,
|
2022-04-11 09:35:06 +00:00
|
|
|
defaultTimestamp: prm.DefaultTimestamp,
|
2022-08-11 14:07:53 +00:00
|
|
|
|
|
|
|
prometheusService: prm.PrometheusService,
|
|
|
|
pprofService: prm.PprofService,
|
|
|
|
gateMetric: prm.GateMetric,
|
|
|
|
serviceShutdownTimeout: prm.ServiceShutdownTimeout,
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) Configure(api *operations.NeofsRestGwAPI) http.Handler {
|
|
|
|
api.ServeError = errors.ServeError
|
|
|
|
|
2022-07-07 15:21:41 +00:00
|
|
|
api.UseSwaggerUI()
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
api.OptionsAuthHandler = operations.OptionsAuthHandlerFunc(a.OptionsAuth)
|
2022-04-11 09:35:06 +00:00
|
|
|
api.AuthHandler = operations.AuthHandlerFunc(a.PostAuth)
|
2022-04-11 11:18:26 +00:00
|
|
|
|
2022-07-28 16:57:40 +00:00
|
|
|
api.GetBalanceHandler = operations.GetBalanceHandlerFunc(a.Balance)
|
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
api.OptionsObjectsPutHandler = operations.OptionsObjectsPutHandlerFunc(a.OptionsObjectsPut)
|
2022-04-11 09:35:06 +00:00
|
|
|
api.PutObjectHandler = operations.PutObjectHandlerFunc(a.PutObjects)
|
2022-08-05 19:09:19 +00:00
|
|
|
|
|
|
|
api.OptionsObjectsGetDeleteHandler = operations.OptionsObjectsGetDeleteHandlerFunc(a.OptionsObjectsGetDelete)
|
2022-04-11 11:18:26 +00:00
|
|
|
api.GetObjectInfoHandler = operations.GetObjectInfoHandlerFunc(a.GetObjectInfo)
|
2022-04-14 08:53:13 +00:00
|
|
|
api.DeleteObjectHandler = operations.DeleteObjectHandlerFunc(a.DeleteObject)
|
2022-08-05 19:09:19 +00:00
|
|
|
|
|
|
|
api.OptionsObjectsSearchHandler = operations.OptionsObjectsSearchHandlerFunc(a.OptionsObjectSearch)
|
2022-04-18 08:30:34 +00:00
|
|
|
api.SearchObjectsHandler = operations.SearchObjectsHandlerFunc(a.SearchObjects)
|
2022-04-11 11:18:26 +00:00
|
|
|
|
2022-08-05 19:09:19 +00:00
|
|
|
api.OptionsContainersPutListHandler = operations.OptionsContainersPutListHandlerFunc(a.OptionsContainersPutList)
|
2022-04-11 09:35:06 +00:00
|
|
|
api.PutContainerHandler = operations.PutContainerHandlerFunc(a.PutContainers)
|
2022-08-05 19:09:19 +00:00
|
|
|
api.ListContainersHandler = operations.ListContainersHandlerFunc(a.ListContainer)
|
|
|
|
|
|
|
|
api.OptionsContainersGetDeleteHandler = operations.OptionsContainersGetDeleteHandlerFunc(a.OptionsContainersGetDelete)
|
2022-04-11 09:35:06 +00:00
|
|
|
api.GetContainerHandler = operations.GetContainerHandlerFunc(a.GetContainer)
|
2022-04-13 08:41:04 +00:00
|
|
|
api.DeleteContainerHandler = operations.DeleteContainerHandlerFunc(a.DeleteContainer)
|
2022-08-05 19:09:19 +00:00
|
|
|
|
|
|
|
api.OptionsContainersEACLHandler = operations.OptionsContainersEACLHandlerFunc(a.OptionsContainersEACL)
|
2022-04-13 13:00:04 +00:00
|
|
|
api.PutContainerEACLHandler = operations.PutContainerEACLHandlerFunc(a.PutContainerEACL)
|
|
|
|
api.GetContainerEACLHandler = operations.GetContainerEACLHandlerFunc(a.GetContainerEACL)
|
2022-04-11 11:18:26 +00:00
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
api.BearerAuthAuth = func(s string) (*models.Principal, error) {
|
2022-04-11 10:26:19 +00:00
|
|
|
if !strings.HasPrefix(s, BearerPrefix) {
|
2022-04-11 09:35:06 +00:00
|
|
|
return nil, fmt.Errorf("has not bearer token")
|
|
|
|
}
|
2022-04-11 10:26:19 +00:00
|
|
|
if s = strings.TrimPrefix(s, BearerPrefix); len(s) == 0 {
|
2022-04-11 09:35:06 +00:00
|
|
|
return nil, fmt.Errorf("bearer token is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*models.Principal)(&s), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
api.PreServerShutdown = func() {}
|
|
|
|
|
2022-08-11 14:07:53 +00:00
|
|
|
api.ServerShutdown = func() {
|
|
|
|
shutDownCtx, cancel := context.WithTimeout(context.Background(), a.serviceShutdownTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
a.prometheusService.ShutDown(shutDownCtx)
|
|
|
|
a.pprofService.ShutDown(shutDownCtx)
|
|
|
|
}
|
2022-04-11 09:35:06 +00:00
|
|
|
|
2022-07-08 10:34:34 +00:00
|
|
|
return a.setupGlobalMiddleware(a.docMiddleware(api.Serve(setupMiddlewares)))
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The middleware configuration is for the handler executors. These do not apply to the swagger.json document.
|
|
|
|
// The middleware executes after routing but before authentication, binding and validation.
|
|
|
|
func setupMiddlewares(handler http.Handler) http.Handler {
|
|
|
|
return handler
|
|
|
|
}
|
|
|
|
|
|
|
|
// The middleware configuration happens before anything, this middleware also applies to serving the swagger.json document.
|
|
|
|
// So this is a good place to plug in a panic handling middleware, logging and metrics.
|
2022-04-11 10:26:19 +00:00
|
|
|
func (a *API) setupGlobalMiddleware(handler http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestID := uuid.NewString()
|
|
|
|
a.log.Info("request", zap.String("remote", r.RemoteAddr),
|
2022-04-13 08:41:04 +00:00
|
|
|
zap.String("method", r.Method), zap.String("url", r.URL.String()),
|
2022-04-11 10:26:19 +00:00
|
|
|
zap.String("id", requestID))
|
|
|
|
|
|
|
|
ctx := context.WithValue(r.Context(), ContextKeyRequestID, requestID)
|
|
|
|
|
|
|
|
handler.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
2022-04-11 09:35:06 +00:00
|
|
|
}
|
2022-07-07 12:59:38 +00:00
|
|
|
|
2022-07-08 10:34:34 +00:00
|
|
|
func (a *API) docMiddleware(handler http.Handler) http.Handler {
|
2022-07-12 07:31:06 +00:00
|
|
|
fh := http.StripPrefix(docsPrefix, http.FileServer(http.Dir("static/docs")))
|
2022-07-08 10:34:34 +00:00
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-07-12 07:31:06 +00:00
|
|
|
if strings.HasPrefix(r.URL.Path, docsPrefix) {
|
2022-07-08 10:34:34 +00:00
|
|
|
fh.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
handler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:59:38 +00:00
|
|
|
func (a *API) logAndGetErrorResponse(msg string, err error, fields ...zap.Field) *models.ErrorResponse {
|
|
|
|
fields = append(fields, zap.Error(err))
|
|
|
|
a.log.Error(msg, fields...)
|
|
|
|
return util.NewErrorResponse(fmt.Errorf("%s: %w", msg, err))
|
|
|
|
}
|
2022-08-11 14:07:53 +00:00
|
|
|
|
|
|
|
func (a API) StartCallback() {
|
|
|
|
if a.gateMetric == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.gateMetric.SetHealth(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a API) RunServices() {
|
|
|
|
go a.pprofService.Start()
|
|
|
|
go a.prometheusService.Start()
|
|
|
|
}
|