2023-07-05 14:04:52 +00:00
|
|
|
package middleware
|
2020-07-16 15:33:47 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-05 13:25:25 +00:00
|
|
|
"crypto/elliptic"
|
2024-02-21 13:16:14 +00:00
|
|
|
"errors"
|
2023-10-05 13:25:25 +00:00
|
|
|
"fmt"
|
2020-07-16 15:33:47 +00:00
|
|
|
"net/http"
|
2023-10-05 08:05:21 +00:00
|
|
|
"time"
|
2020-07-16 15:33:47 +00:00
|
|
|
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-10-05 08:05:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2024-11-27 13:16:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/acl"
|
2023-10-05 13:25:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
2024-04-16 08:20:35 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
2023-10-05 13:25:25 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2020-07-16 15:33:47 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-10-05 08:05:21 +00:00
|
|
|
type (
|
|
|
|
// Box contains access box and additional info.
|
|
|
|
Box struct {
|
|
|
|
AccessBox *accessbox.Box
|
|
|
|
ClientTime time.Time
|
|
|
|
AuthHeaders *AuthHeader
|
2024-04-16 08:20:35 +00:00
|
|
|
Attributes []object.Attribute
|
2023-10-05 08:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Center is a user authentication interface.
|
|
|
|
Center interface {
|
|
|
|
// Authenticate validate and authenticate request.
|
|
|
|
// Must return ErrNoAuthorizationHeader if auth header is missed.
|
|
|
|
Authenticate(request *http.Request) (*Box, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:revive
|
|
|
|
AuthHeader struct {
|
|
|
|
AccessKeyID string
|
|
|
|
Region string
|
|
|
|
SignatureV4 string
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNoAuthorizationHeader is returned for unauthenticated requests.
|
2024-02-21 13:16:14 +00:00
|
|
|
var ErrNoAuthorizationHeader = errors.New("no authorization header")
|
2023-10-05 08:05:21 +00:00
|
|
|
|
|
|
|
func Auth(center Center, log *zap.Logger) Func {
|
2022-12-27 12:30:11 +00:00
|
|
|
return func(h http.Handler) http.Handler {
|
2020-07-16 15:33:47 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2023-07-06 09:13:45 +00:00
|
|
|
ctx := r.Context()
|
2024-02-26 13:18:34 +00:00
|
|
|
reqInfo := GetReqInfo(ctx)
|
|
|
|
reqInfo.User = "anon"
|
2021-07-16 12:35:07 +00:00
|
|
|
box, err := center.Authenticate(r)
|
2020-07-16 15:33:47 +00:00
|
|
|
if err != nil {
|
2024-02-21 13:16:14 +00:00
|
|
|
if errors.Is(err, ErrNoAuthorizationHeader) {
|
|
|
|
reqLogOrDefault(ctx, log).Debug(logs.CouldntReceiveAccessBoxForGateKeyRandomKeyWillBeUsed, zap.Error(err))
|
2021-06-11 16:29:55 +00:00
|
|
|
} else {
|
2023-08-23 11:07:52 +00:00
|
|
|
reqLogOrDefault(ctx, log).Error(logs.FailedToPassAuthentication, zap.Error(err))
|
2024-09-30 08:34:45 +00:00
|
|
|
err = apierr.TransformToS3Error(err)
|
|
|
|
if err.(apierr.Error).ErrCode == apierr.ErrInternalError {
|
2024-09-27 09:18:41 +00:00
|
|
|
err = apierr.GetAPIError(apierr.ErrAccessDenied)
|
2021-08-09 08:53:58 +00:00
|
|
|
}
|
2024-03-04 12:53:00 +00:00
|
|
|
if _, wrErr := WriteErrorResponse(w, GetReqInfo(r.Context()), err); wrErr != nil {
|
|
|
|
reqLogOrDefault(ctx, log).Error(logs.FailedToWriteResponse, zap.Error(wrErr))
|
|
|
|
}
|
2021-06-11 16:29:55 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-11 11:52:03 +00:00
|
|
|
} else {
|
2024-04-16 08:20:35 +00:00
|
|
|
ctx = SetBox(ctx, box)
|
2024-02-26 13:18:34 +00:00
|
|
|
|
|
|
|
if box.AccessBox.Gate.BearerToken != nil {
|
|
|
|
reqInfo.User = bearer.ResolveIssuer(*box.AccessBox.Gate.BearerToken).String()
|
|
|
|
}
|
2024-02-21 13:41:56 +00:00
|
|
|
reqLogOrDefault(ctx, log).Debug(logs.SuccessfulAuth, zap.String("accessKeyID", box.AuthHeaders.AccessKeyID))
|
2021-06-11 11:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r.WithContext(ctx))
|
2020-07-16 15:33:47 +00:00
|
|
|
})
|
2022-12-27 12:30:11 +00:00
|
|
|
}
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|
2023-10-05 13:25:25 +00:00
|
|
|
|
2023-12-13 14:44:18 +00:00
|
|
|
type FrostFSIDValidator interface {
|
2023-10-05 13:25:25 +00:00
|
|
|
ValidatePublicKey(key *keys.PublicKey) error
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:44:18 +00:00
|
|
|
func FrostfsIDValidation(frostfsID FrostFSIDValidator, log *zap.Logger) Func {
|
2023-10-05 13:25:25 +00:00
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
bd, err := GetBoxData(ctx)
|
|
|
|
if err != nil || bd.Gate.BearerToken == nil {
|
|
|
|
reqLogOrDefault(ctx, log).Debug(logs.AnonRequestSkipFrostfsIDValidation)
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = validateBearerToken(frostfsID, bd.Gate.BearerToken); err != nil {
|
|
|
|
reqLogOrDefault(ctx, log).Error(logs.FrostfsIDValidationFailed, zap.Error(err))
|
2024-03-04 12:53:00 +00:00
|
|
|
if _, wrErr := WriteErrorResponse(w, GetReqInfo(r.Context()), err); wrErr != nil {
|
|
|
|
reqLogOrDefault(ctx, log).Error(logs.FailedToWriteResponse, zap.Error(wrErr))
|
|
|
|
}
|
2023-10-05 13:25:25 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 14:44:18 +00:00
|
|
|
func validateBearerToken(frostfsID FrostFSIDValidator, bt *bearer.Token) error {
|
2023-10-05 13:25:25 +00:00
|
|
|
m := new(acl.BearerToken)
|
|
|
|
bt.WriteToV2(m)
|
|
|
|
|
|
|
|
pk, err := keys.NewPublicKeyFromBytes(m.GetSignature().GetKey(), elliptic.P256())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid bearer token public key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = frostfsID.ValidatePublicKey(pk); err != nil {
|
|
|
|
return fmt.Errorf("validation data user key failed: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|