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
|
|
|
|
2023-10-05 13:25:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
2024-02-21 13:16:14 +00:00
|
|
|
apiErrors "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"
|
2024-02-21 13:16:14 +00:00
|
|
|
frostfsErrors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/errors"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-10-05 13:25:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-21 13:16:14 +00:00
|
|
|
err = frostfsErrors.UnwrapErr(err)
|
|
|
|
if _, ok := err.(apiErrors.Error); !ok {
|
|
|
|
err = apiErrors.GetAPIError(apiErrors.ErrAccessDenied)
|
2021-08-09 08:53:58 +00:00
|
|
|
}
|
|
|
|
WriteErrorResponse(w, GetReqInfo(r.Context()), err)
|
2021-06-11 16:29:55 +00:00
|
|
|
return
|
|
|
|
}
|
2021-06-11 11:52:03 +00:00
|
|
|
} else {
|
2023-08-14 15:34:41 +00:00
|
|
|
ctx = SetBoxData(ctx, box.AccessBox)
|
2022-11-08 09:12:55 +00:00
|
|
|
if !box.ClientTime.IsZero() {
|
2023-08-14 15:34:41 +00:00
|
|
|
ctx = SetClientTime(ctx, box.ClientTime)
|
2022-11-08 09:12:55 +00:00
|
|
|
}
|
2023-08-14 15:34:41 +00:00
|
|
|
ctx = SetAuthHeaders(ctx, box.AuthHeaders)
|
2024-02-26 13:18:34 +00:00
|
|
|
|
|
|
|
if box.AccessBox.Gate.BearerToken != nil {
|
|
|
|
reqInfo.User = bearer.ResolveIssuer(*box.AccessBox.Gate.BearerToken).String()
|
|
|
|
}
|
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))
|
|
|
|
WriteErrorResponse(w, GetReqInfo(r.Context()), err)
|
|
|
|
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
|
|
|
|
}
|