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"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/models"
|
|
|
|
"github.com/nspcc-dev/neofs-rest-gw/gen/restapi/operations"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// API is a REST v1 request handler.
|
|
|
|
type API struct {
|
|
|
|
log *zap.Logger
|
|
|
|
pool *pool.Pool
|
|
|
|
key *keys.PrivateKey
|
|
|
|
defaultTimestamp bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrmAPI groups parameters to init rest API.
|
|
|
|
type PrmAPI struct {
|
|
|
|
Logger *zap.Logger
|
|
|
|
Pool *pool.Pool
|
|
|
|
Key *keys.PrivateKey
|
|
|
|
DefaultTimestamp bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type BearerToken struct {
|
|
|
|
Token string
|
|
|
|
Signature string
|
|
|
|
Key string
|
|
|
|
}
|
|
|
|
|
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-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 {
|
|
|
|
return &API{
|
|
|
|
log: prm.Logger,
|
|
|
|
pool: prm.Pool,
|
|
|
|
key: prm.Key,
|
|
|
|
defaultTimestamp: prm.DefaultTimestamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) Configure(api *operations.NeofsRestGwAPI) http.Handler {
|
|
|
|
api.ServeError = errors.ServeError
|
|
|
|
|
|
|
|
api.AuthHandler = operations.AuthHandlerFunc(a.PostAuth)
|
2022-04-11 11:18:26 +00:00
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
api.PutObjectHandler = operations.PutObjectHandlerFunc(a.PutObjects)
|
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-04-11 11:18:26 +00:00
|
|
|
|
2022-04-11 09:35:06 +00:00
|
|
|
api.PutContainerHandler = operations.PutContainerHandlerFunc(a.PutContainers)
|
|
|
|
api.GetContainerHandler = operations.GetContainerHandlerFunc(a.GetContainer)
|
2022-04-13 08:41:04 +00:00
|
|
|
api.DeleteContainerHandler = operations.DeleteContainerHandlerFunc(a.DeleteContainer)
|
2022-04-13 13:00:04 +00:00
|
|
|
api.PutContainerEACLHandler = operations.PutContainerEACLHandlerFunc(a.PutContainerEACL)
|
|
|
|
api.GetContainerEACLHandler = operations.GetContainerEACLHandlerFunc(a.GetContainerEACL)
|
2022-04-13 15:23:03 +00:00
|
|
|
api.ListContainersHandler = operations.ListContainersHandlerFunc(a.ListContainer)
|
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() {}
|
|
|
|
|
|
|
|
api.ServerShutdown = func() {}
|
|
|
|
|
2022-04-11 10:26:19 +00:00
|
|
|
return a.setupGlobalMiddleware(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
|
|
|
}
|