2020-07-15 13:48:25 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-08-06 15:23:01 +00:00
|
|
|
"context"
|
2021-08-30 19:44:53 +00:00
|
|
|
"crypto/hmac"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
2020-08-19 13:28:17 +00:00
|
|
|
"fmt"
|
2020-11-27 12:31:39 +00:00
|
|
|
"io"
|
2021-08-30 19:44:53 +00:00
|
|
|
"mime/multipart"
|
2020-07-16 15:33:47 +00:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
2020-07-20 23:43:40 +00:00
|
|
|
"strings"
|
2020-08-06 15:23:01 +00:00
|
|
|
"time"
|
2020-07-15 13:48:25 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
v4 "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth/signer/v4"
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2024-03-27 06:14:58 +00:00
|
|
|
v4a "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/auth/signer/v4asdk2"
|
2023-10-05 08:05:21 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/accessbox"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/creds/tokens"
|
2024-08-30 12:05:32 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
2023-03-07 14:38:08 +00:00
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
2024-03-27 06:14:58 +00:00
|
|
|
credentialsv2 "github.com/aws/aws-sdk-go-v2/credentials"
|
2020-10-13 09:33:33 +00:00
|
|
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
2020-07-15 13:48:25 +00:00
|
|
|
)
|
|
|
|
|
2024-03-27 06:14:58 +00:00
|
|
|
var (
|
|
|
|
// AuthorizationFieldRegexp -- is regexp for credentials with Base58 encoded cid and oid and '0' (zero) as delimiter.
|
|
|
|
AuthorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request,\s*SignedHeaders=(?P<signed_header_fields>.+),\s*Signature=(?P<v4_signature>.+)`)
|
2020-07-16 15:33:47 +00:00
|
|
|
|
2024-03-27 06:14:58 +00:00
|
|
|
// authorizationFieldV4aRegexp -- is regexp for credentials with Base58 encoded cid and oid and '0' (zero) as delimiter.
|
|
|
|
authorizationFieldV4aRegexp = regexp.MustCompile(`AWS4-ECDSA-P256-SHA256 Credential=(?P<access_key_id>[^/]+)/(?P<date>[^/]+)/(?P<service>[^/]+)/aws4_request,\s*SignedHeaders=(?P<signed_header_fields>.+),\s*Signature=(?P<v4_signature>.+)`)
|
|
|
|
|
|
|
|
// postPolicyCredentialRegexp -- is regexp for credentials when uploading file using POST with policy.
|
|
|
|
postPolicyCredentialRegexp = regexp.MustCompile(`(?P<access_key_id>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request`)
|
|
|
|
)
|
2021-08-30 19:44:53 +00:00
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
type (
|
2023-10-05 08:05:21 +00:00
|
|
|
Center struct {
|
2022-08-31 22:43:36 +00:00
|
|
|
reg *RegexpSubmatcher
|
2024-03-27 06:14:58 +00:00
|
|
|
regV4a *RegexpSubmatcher
|
2022-08-31 22:43:36 +00:00
|
|
|
postReg *RegexpSubmatcher
|
|
|
|
cli tokens.Credentials
|
|
|
|
allowedAccessKeyIDPrefixes []string // empty slice means all access key ids are allowed
|
2024-08-30 12:05:32 +00:00
|
|
|
settings CenterSettings
|
|
|
|
}
|
|
|
|
|
|
|
|
CenterSettings interface {
|
|
|
|
AccessBoxContainer() (cid.ID, bool)
|
2020-07-21 10:21:03 +00:00
|
|
|
}
|
2020-07-15 20:16:27 +00:00
|
|
|
|
2023-06-02 07:44:25 +00:00
|
|
|
//nolint:revive
|
|
|
|
AuthHeader struct {
|
2021-08-09 09:35:42 +00:00
|
|
|
AccessKeyID string
|
|
|
|
Service string
|
|
|
|
Region string
|
2024-03-27 06:14:58 +00:00
|
|
|
Signature string
|
2021-08-09 09:35:42 +00:00
|
|
|
SignedFields []string
|
|
|
|
Date string
|
2022-06-14 15:15:29 +00:00
|
|
|
IsPresigned bool
|
|
|
|
Expiration time.Duration
|
2024-03-27 06:14:58 +00:00
|
|
|
Preamble string
|
|
|
|
PayloadHash string
|
2021-08-09 09:35:42 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-03-27 06:14:58 +00:00
|
|
|
authHeaderPartsNum = 6
|
|
|
|
authHeaderV4aPartsNum = 5
|
|
|
|
maxFormSizeMemory = 50 * 1048576 // 50 MB
|
2022-06-14 15:15:29 +00:00
|
|
|
|
|
|
|
AmzAlgorithm = "X-Amz-Algorithm"
|
|
|
|
AmzCredential = "X-Amz-Credential"
|
|
|
|
AmzSignature = "X-Amz-Signature"
|
|
|
|
AmzSignedHeaders = "X-Amz-SignedHeaders"
|
2024-03-27 06:14:58 +00:00
|
|
|
AmzRegionSet = "X-Amz-Region-Set"
|
2022-06-14 15:15:29 +00:00
|
|
|
AmzExpires = "X-Amz-Expires"
|
|
|
|
AmzDate = "X-Amz-Date"
|
2023-11-13 08:01:47 +00:00
|
|
|
AmzContentSHA256 = "X-Amz-Content-Sha256"
|
2022-06-14 15:15:29 +00:00
|
|
|
AuthorizationHdr = "Authorization"
|
|
|
|
ContentTypeHdr = "Content-Type"
|
2023-11-13 08:01:47 +00:00
|
|
|
|
|
|
|
UnsignedPayload = "UNSIGNED-PAYLOAD"
|
|
|
|
StreamingUnsignedPayloadTrailer = "STREAMING-UNSIGNED-PAYLOAD-TRAILER"
|
|
|
|
StreamingContentSHA256 = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"
|
|
|
|
StreamingContentSHA256Trailer = "STREAMING-AWS4-HMAC-SHA256-PAYLOAD-TRAILER"
|
|
|
|
StreamingContentECDSASHA256 = "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD"
|
|
|
|
StreamingContentECDSASHA256Trailer = "STREAMING-AWS4-ECDSA-P256-SHA256-PAYLOAD-TRAILER"
|
2020-10-13 09:33:33 +00:00
|
|
|
)
|
|
|
|
|
2023-11-13 08:01:47 +00:00
|
|
|
var ContentSHA256HeaderStandardValue = map[string]struct{}{
|
|
|
|
UnsignedPayload: {},
|
|
|
|
StreamingUnsignedPayloadTrailer: {},
|
|
|
|
StreamingContentSHA256: {},
|
|
|
|
StreamingContentSHA256Trailer: {},
|
|
|
|
StreamingContentECDSASHA256: {},
|
|
|
|
StreamingContentECDSASHA256Trailer: {},
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:33:33 +00:00
|
|
|
// New creates an instance of AuthCenter.
|
2024-08-30 12:05:32 +00:00
|
|
|
func New(creds tokens.Credentials, prefixes []string, settings CenterSettings) *Center {
|
2023-10-05 08:05:21 +00:00
|
|
|
return &Center{
|
2024-02-06 13:44:49 +00:00
|
|
|
cli: creds,
|
2024-07-12 12:31:43 +00:00
|
|
|
reg: NewRegexpMatcher(AuthorizationFieldRegexp),
|
2024-03-27 06:14:58 +00:00
|
|
|
regV4a: NewRegexpMatcher(authorizationFieldV4aRegexp),
|
2022-08-31 22:43:36 +00:00
|
|
|
postReg: NewRegexpMatcher(postPolicyCredentialRegexp),
|
|
|
|
allowedAccessKeyIDPrefixes: prefixes,
|
2024-08-30 12:05:32 +00:00
|
|
|
settings: settings,
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
2020-07-15 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2024-03-27 06:14:58 +00:00
|
|
|
const (
|
|
|
|
signaturePreambleSigV4 = "AWS4-HMAC-SHA256"
|
|
|
|
signaturePreambleSigV4A = "AWS4-ECDSA-P256-SHA256"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Center) parseAuthHeader(authHeader string, headers http.Header) (*AuthHeader, error) {
|
|
|
|
preamble, _, _ := strings.Cut(authHeader, " ")
|
2021-08-09 09:35:42 +00:00
|
|
|
|
2024-03-27 06:14:58 +00:00
|
|
|
var (
|
|
|
|
submatches map[string]string
|
|
|
|
region string
|
|
|
|
)
|
|
|
|
|
|
|
|
switch preamble {
|
|
|
|
case signaturePreambleSigV4:
|
|
|
|
submatches = c.reg.GetSubmatches(authHeader)
|
|
|
|
if len(submatches) != authHeaderPartsNum {
|
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrAuthorizationHeaderMalformed), authHeader)
|
|
|
|
}
|
|
|
|
region = submatches["region"]
|
|
|
|
case signaturePreambleSigV4A:
|
|
|
|
submatches = c.regV4a.GetSubmatches(authHeader)
|
|
|
|
if len(submatches) != authHeaderV4aPartsNum {
|
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrAuthorizationHeaderMalformed), authHeader)
|
|
|
|
}
|
|
|
|
region = headers.Get(AmzRegionSet)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrAuthorizationHeaderMalformed), authHeader)
|
|
|
|
}
|
|
|
|
// AWS4-ECDSA-P256-SHA256
|
|
|
|
// Credential=2XGRML5EW3LMHdf64W2DkBy1Nkuu4y4wGhUj44QjbXBi05ZNvs8WVwy1XTmSEkcVkydPKzCgtmR7U3zyLYTj3Snxf/20240326/s3/aws4_request,
|
|
|
|
// SignedHeaders=amz-sdk-invocation-id;amz-sdk-request;host;x-amz-content-sha256;x-amz-date;x-amz-region-set,
|
|
|
|
// Signature=3044022006a2bc760140834101d0a79667d6aa75768c1a28e9cafc8963484d0752a6c6050220629dc06d7d6505e1b1e2a5d1f974b25ba32fdffc6f3f70dc4dda31b8a6f7ea2b
|
2021-08-09 09:35:42 +00:00
|
|
|
|
2023-06-02 07:44:25 +00:00
|
|
|
return &AuthHeader{
|
2021-08-09 09:35:42 +00:00
|
|
|
AccessKeyID: submatches["access_key_id"],
|
|
|
|
Service: submatches["service"],
|
2024-03-27 06:14:58 +00:00
|
|
|
Region: region,
|
|
|
|
Signature: submatches["v4_signature"],
|
|
|
|
SignedFields: strings.Split(submatches["signed_header_fields"], ";"),
|
2021-08-09 09:35:42 +00:00
|
|
|
Date: submatches["date"],
|
2024-03-27 06:14:58 +00:00
|
|
|
Preamble: preamble,
|
|
|
|
PayloadHash: headers.Get(AmzContentSHA256),
|
2021-08-09 09:35:42 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-11-13 08:01:47 +00:00
|
|
|
func IsStandardContentSHA256(key string) bool {
|
|
|
|
_, ok := ContentSHA256HeaderStandardValue[key]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-10-05 08:05:21 +00:00
|
|
|
func (c *Center) Authenticate(r *http.Request) (*middleware.Box, error) {
|
2022-06-14 15:15:29 +00:00
|
|
|
var (
|
|
|
|
err error
|
2023-06-02 07:44:25 +00:00
|
|
|
authHdr *AuthHeader
|
2022-06-14 15:15:29 +00:00
|
|
|
signatureDateTimeStr string
|
2022-11-08 09:12:55 +00:00
|
|
|
needClientTime bool
|
2022-06-14 15:15:29 +00:00
|
|
|
)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
queryValues := r.URL.Query()
|
2024-03-27 06:14:58 +00:00
|
|
|
if queryValues.Get(AmzAlgorithm) == signaturePreambleSigV4 {
|
2022-06-14 15:15:29 +00:00
|
|
|
creds := strings.Split(queryValues.Get(AmzCredential), "/")
|
|
|
|
if len(creds) != 5 || creds[4] != "aws4_request" {
|
|
|
|
return nil, fmt.Errorf("bad X-Amz-Credential")
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
2023-06-02 07:44:25 +00:00
|
|
|
authHdr = &AuthHeader{
|
2022-06-14 15:15:29 +00:00
|
|
|
AccessKeyID: creds[0],
|
|
|
|
Service: creds[3],
|
|
|
|
Region: creds[2],
|
2024-03-27 06:14:58 +00:00
|
|
|
Signature: queryValues.Get(AmzSignature),
|
|
|
|
SignedFields: strings.Split(queryValues.Get(AmzSignedHeaders), ";"),
|
|
|
|
Date: creds[1],
|
|
|
|
IsPresigned: true,
|
|
|
|
Preamble: signaturePreambleSigV4,
|
|
|
|
}
|
|
|
|
authHdr.Expiration, err = time.ParseDuration(queryValues.Get(AmzExpires) + "s")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse X-Amz-Expires: %w", err)
|
|
|
|
}
|
|
|
|
signatureDateTimeStr = queryValues.Get(AmzDate)
|
|
|
|
} else if queryValues.Get(AmzAlgorithm) == signaturePreambleSigV4A {
|
|
|
|
creds := strings.Split(queryValues.Get(AmzCredential), "/")
|
|
|
|
if len(creds) != 4 || creds[3] != "aws4_request" {
|
|
|
|
return nil, fmt.Errorf("bad X-Amz-Credential")
|
|
|
|
}
|
|
|
|
authHdr = &AuthHeader{
|
|
|
|
AccessKeyID: creds[0],
|
|
|
|
Service: creds[2],
|
|
|
|
Region: queryValues.Get(AmzRegionSet),
|
|
|
|
Signature: queryValues.Get(AmzSignature),
|
2023-08-08 06:17:35 +00:00
|
|
|
SignedFields: strings.Split(queryValues.Get(AmzSignedHeaders), ";"),
|
2022-06-14 15:15:29 +00:00
|
|
|
Date: creds[1],
|
|
|
|
IsPresigned: true,
|
2024-03-27 06:14:58 +00:00
|
|
|
Preamble: signaturePreambleSigV4A,
|
2022-06-14 15:15:29 +00:00
|
|
|
}
|
|
|
|
authHdr.Expiration, err = time.ParseDuration(queryValues.Get(AmzExpires) + "s")
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse X-Amz-Expires: %w", err)
|
|
|
|
}
|
|
|
|
signatureDateTimeStr = queryValues.Get(AmzDate)
|
|
|
|
} else {
|
|
|
|
authHeaderField := r.Header[AuthorizationHdr]
|
|
|
|
if len(authHeaderField) != 1 {
|
|
|
|
if strings.HasPrefix(r.Header.Get(ContentTypeHdr), "multipart/form-data") {
|
|
|
|
return c.checkFormData(r)
|
|
|
|
}
|
2024-02-21 13:16:14 +00:00
|
|
|
return nil, fmt.Errorf("%w: %v", middleware.ErrNoAuthorizationHeader, authHeaderField)
|
2022-06-14 15:15:29 +00:00
|
|
|
}
|
2024-03-27 06:14:58 +00:00
|
|
|
authHdr, err = c.parseAuthHeader(authHeaderField[0], r.Header)
|
2022-06-14 15:15:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
signatureDateTimeStr = r.Header.Get(AmzDate)
|
2022-11-08 09:12:55 +00:00
|
|
|
needClientTime = true
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
signatureDateTime, err := time.Parse("20060102T150405Z", signatureDateTimeStr)
|
2020-08-06 15:23:01 +00:00
|
|
|
if err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse x-amz-date header field: %w", err)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2024-02-21 13:16:14 +00:00
|
|
|
if err = c.checkAccessKeyID(authHdr.AccessKeyID); err != nil {
|
2022-08-31 22:43:36 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
cnrID, err := c.getAccessBoxContainer(authHdr.AccessKeyID)
|
2021-08-09 09:35:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-24 06:59:01 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
box, attrs, err := c.cli.GetBox(r.Context(), cnrID, authHdr.AccessKeyID)
|
2020-07-24 14:03:02 +00:00
|
|
|
if err != nil {
|
2024-08-30 12:05:32 +00:00
|
|
|
return nil, fmt.Errorf("get box by access key '%s': %w", authHdr.AccessKeyID, err)
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2023-11-13 08:01:47 +00:00
|
|
|
if err = checkFormatHashContentSHA256(r.Header.Get(AmzContentSHA256)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
clonedRequest := cloneRequest(r, authHdr)
|
|
|
|
if err = c.checkSign(authHdr, box, clonedRequest, signatureDateTime); err != nil {
|
2021-08-09 09:35:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-05 08:05:21 +00:00
|
|
|
result := &middleware.Box{
|
|
|
|
AccessBox: box,
|
|
|
|
AuthHeaders: &middleware.AuthHeader{
|
|
|
|
AccessKeyID: authHdr.AccessKeyID,
|
|
|
|
Region: authHdr.Region,
|
2024-03-27 06:14:58 +00:00
|
|
|
SignatureV4: authHdr.Signature,
|
2023-10-05 08:05:21 +00:00
|
|
|
},
|
2024-04-16 08:20:35 +00:00
|
|
|
Attributes: attrs,
|
2023-06-02 07:44:25 +00:00
|
|
|
}
|
2022-11-08 09:12:55 +00:00
|
|
|
if needClientTime {
|
|
|
|
result.ClientTime = signatureDateTime
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2021-08-09 09:35:42 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
func (c *Center) getAccessBoxContainer(accessKeyID string) (cid.ID, error) {
|
|
|
|
var addr oid.Address
|
|
|
|
if err := addr.DecodeString(strings.ReplaceAll(accessKeyID, "0", "/")); err == nil {
|
|
|
|
return addr.Container(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cnrID, ok := c.settings.AccessBoxContainer()
|
|
|
|
if ok {
|
|
|
|
return cnrID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return cid.ID{}, fmt.Errorf("%w: unknown container for creds '%s'", apierr.GetAPIError(apierr.ErrInvalidAccessKeyID), accessKeyID)
|
|
|
|
}
|
|
|
|
|
2023-11-13 08:01:47 +00:00
|
|
|
func checkFormatHashContentSHA256(hash string) error {
|
|
|
|
if !IsStandardContentSHA256(hash) {
|
|
|
|
hashBinary, err := hex.DecodeString(hash)
|
|
|
|
if err != nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return fmt.Errorf("%w: decode hash: %s: %s", apierr.GetAPIError(apierr.ErrContentSHA256Mismatch),
|
2024-02-21 13:16:14 +00:00
|
|
|
hash, err.Error())
|
2023-11-13 08:01:47 +00:00
|
|
|
}
|
|
|
|
if len(hashBinary) != sha256.Size && len(hash) != 0 {
|
2024-09-27 09:18:41 +00:00
|
|
|
return fmt.Errorf("%w: invalid hash size %d", apierr.GetAPIError(apierr.ErrContentSHA256Mismatch), len(hashBinary))
|
2023-11-13 08:01:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 08:05:21 +00:00
|
|
|
func (c Center) checkAccessKeyID(accessKeyID string) error {
|
2022-08-31 22:43:36 +00:00
|
|
|
if len(c.allowedAccessKeyIDPrefixes) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, prefix := range c.allowedAccessKeyIDPrefixes {
|
|
|
|
if strings.HasPrefix(accessKeyID, prefix) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 09:18:41 +00:00
|
|
|
return fmt.Errorf("%w: accesskeyID prefix isn't allowed", apierr.GetAPIError(apierr.ErrAccessDenied))
|
2022-08-31 22:43:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-05 08:05:21 +00:00
|
|
|
func (c *Center) checkFormData(r *http.Request) (*middleware.Box, error) {
|
2021-08-30 19:44:53 +00:00
|
|
|
if err := r.ParseMultipartForm(maxFormSizeMemory); err != nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: parse multipart form with max size %d", apierr.GetAPIError(apierr.ErrInvalidArgument), maxFormSizeMemory)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := prepareForm(r.MultipartForm); err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse form: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
policy := MultipartFormValue(r, "policy")
|
|
|
|
if policy == "" {
|
2024-02-21 13:16:14 +00:00
|
|
|
return nil, fmt.Errorf("%w: missing policy", middleware.ErrNoAuthorizationHeader)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 13:16:14 +00:00
|
|
|
creds := MultipartFormValue(r, "x-amz-credential")
|
|
|
|
submatches := c.postReg.GetSubmatches(creds)
|
2021-08-30 19:44:53 +00:00
|
|
|
if len(submatches) != 4 {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apierr.GetAPIError(apierr.ErrAuthorizationHeaderMalformed), creds)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signatureDateTime, err := time.Parse("20060102T150405Z", MultipartFormValue(r, "x-amz-date"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse x-amz-date field: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:17:16 +00:00
|
|
|
accessKeyID := submatches["access_key_id"]
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
cnrID, err := c.getAccessBoxContainer(accessKeyID)
|
2024-02-21 13:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2024-08-30 12:05:32 +00:00
|
|
|
box, attrs, err := c.cli.GetBox(r.Context(), cnrID, accessKeyID)
|
2021-08-30 19:44:53 +00:00
|
|
|
if err != nil {
|
2024-08-30 12:05:32 +00:00
|
|
|
return nil, fmt.Errorf("get box by accessKeyID '%s': %w", accessKeyID, err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2023-10-02 09:42:02 +00:00
|
|
|
secret := box.Gate.SecretKey
|
2021-08-30 19:44:53 +00:00
|
|
|
service, region := submatches["service"], submatches["region"]
|
|
|
|
|
2024-08-12 12:24:58 +00:00
|
|
|
signature := SignStr(secret, service, region, signatureDateTime, policy)
|
2024-02-21 13:16:14 +00:00
|
|
|
reqSignature := MultipartFormValue(r, "x-amz-signature")
|
|
|
|
if signature != reqSignature {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s != %s", apierr.GetAPIError(apierr.ErrSignatureDoesNotMatch),
|
2024-02-21 13:16:14 +00:00
|
|
|
reqSignature, signature)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 08:17:16 +00:00
|
|
|
return &middleware.Box{
|
|
|
|
AccessBox: box,
|
|
|
|
AuthHeaders: &middleware.AuthHeader{
|
|
|
|
AccessKeyID: accessKeyID,
|
|
|
|
Region: region,
|
|
|
|
SignatureV4: signature,
|
|
|
|
},
|
|
|
|
Attributes: attrs,
|
|
|
|
}, nil
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
2023-06-02 07:44:25 +00:00
|
|
|
func cloneRequest(r *http.Request, authHeader *AuthHeader) *http.Request {
|
2020-11-24 06:59:01 +00:00
|
|
|
otherRequest := r.Clone(context.TODO())
|
2021-02-01 16:59:42 +00:00
|
|
|
otherRequest.Header = make(http.Header)
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-02-01 16:59:42 +00:00
|
|
|
for key, val := range r.Header {
|
2021-08-09 09:35:42 +00:00
|
|
|
for _, name := range authHeader.SignedFields {
|
2021-02-01 16:59:42 +00:00
|
|
|
if strings.EqualFold(key, name) {
|
|
|
|
otherRequest.Header[key] = val
|
2020-07-24 14:03:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2022-06-14 15:15:29 +00:00
|
|
|
if authHeader.IsPresigned {
|
|
|
|
otherQuery := otherRequest.URL.Query()
|
|
|
|
otherQuery.Del(AmzSignature)
|
|
|
|
otherRequest.URL.RawQuery = otherQuery.Encode()
|
|
|
|
}
|
|
|
|
|
2021-08-09 09:35:42 +00:00
|
|
|
return otherRequest
|
|
|
|
}
|
|
|
|
|
2023-10-05 08:05:21 +00:00
|
|
|
func (c *Center) checkSign(authHeader *AuthHeader, box *accessbox.Box, request *http.Request, signatureDateTime time.Time) error {
|
2022-06-14 15:15:29 +00:00
|
|
|
var signature string
|
2024-03-27 06:14:58 +00:00
|
|
|
|
|
|
|
switch authHeader.Preamble {
|
|
|
|
case signaturePreambleSigV4:
|
|
|
|
awsCreds := credentials.NewStaticCredentials(authHeader.AccessKeyID, box.Gate.SecretKey, "")
|
|
|
|
signer := v4.NewSigner(awsCreds)
|
|
|
|
signer.DisableURIPathEscaping = true
|
|
|
|
|
|
|
|
if authHeader.IsPresigned {
|
|
|
|
if err := checkPresignedDate(authHeader, signatureDateTime); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := signer.Presign(request, nil, authHeader.Service, authHeader.Region, authHeader.Expiration, signatureDateTime); err != nil {
|
|
|
|
return fmt.Errorf("failed to pre-sign temporary HTTP request: %w", err)
|
|
|
|
}
|
|
|
|
signature = request.URL.Query().Get(AmzSignature)
|
|
|
|
} else {
|
|
|
|
if _, err := signer.Sign(request, nil, authHeader.Service, authHeader.Region, signatureDateTime); err != nil {
|
|
|
|
return fmt.Errorf("failed to sign temporary HTTP request: %w", err)
|
|
|
|
}
|
|
|
|
signature = c.reg.GetSubmatches(request.Header.Get(AuthorizationHdr))["v4_signature"]
|
|
|
|
}
|
|
|
|
if authHeader.Signature != signature {
|
|
|
|
return fmt.Errorf("%w: %s != %s: headers %v", apierr.GetAPIError(apierr.ErrSignatureDoesNotMatch),
|
|
|
|
authHeader.Signature, signature, authHeader.SignedFields)
|
2022-06-14 15:15:29 +00:00
|
|
|
}
|
2024-03-27 06:14:58 +00:00
|
|
|
|
|
|
|
case signaturePreambleSigV4A:
|
|
|
|
signer := v4a.NewSigner(func(options *v4a.SignerOptions) {
|
|
|
|
options.DisableURIPathEscaping = true
|
|
|
|
})
|
|
|
|
|
|
|
|
credAdapter := v4a.SymmetricCredentialAdaptor{
|
|
|
|
SymmetricProvider: credentialsv2.NewStaticCredentialsProvider(authHeader.AccessKeyID, box.Gate.SecretKey, ""),
|
2022-06-14 15:15:29 +00:00
|
|
|
}
|
2024-03-27 06:14:58 +00:00
|
|
|
|
|
|
|
creds, err := credAdapter.RetrievePrivateKey(request.Context())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to derive assymetric key from credentials: %w", err)
|
2022-06-14 15:15:29 +00:00
|
|
|
}
|
2024-03-27 06:14:58 +00:00
|
|
|
|
2024-04-23 11:49:34 +00:00
|
|
|
if !authHeader.IsPresigned {
|
|
|
|
return signer.VerifySignature(creds, request, authHeader.PayloadHash, authHeader.Service,
|
|
|
|
strings.Split(authHeader.Region, ","), signatureDateTime, authHeader.Signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkPresignedDate(authHeader, signatureDateTime); err != nil {
|
|
|
|
return err
|
2022-06-14 15:15:29 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2024-04-23 11:49:34 +00:00
|
|
|
return signer.VerifyPresigned(creds, request, authHeader.PayloadHash, authHeader.Service,
|
2024-03-27 06:14:58 +00:00
|
|
|
strings.Split(authHeader.Region, ","), signatureDateTime, authHeader.Signature)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid preamble: %s", authHeader.Preamble)
|
2020-08-06 15:23:01 +00:00
|
|
|
}
|
2020-11-24 06:59:01 +00:00
|
|
|
|
2021-08-09 09:35:42 +00:00
|
|
|
return nil
|
2020-07-16 15:33:47 +00:00
|
|
|
}
|
2021-08-30 19:44:53 +00:00
|
|
|
|
2024-03-27 06:14:58 +00:00
|
|
|
func checkPresignedDate(authHeader *AuthHeader, signatureDateTime time.Time) error {
|
|
|
|
now := time.Now()
|
|
|
|
if signatureDateTime.Add(authHeader.Expiration).Before(now) {
|
|
|
|
return fmt.Errorf("%w: expired: now %s, signature %s", apierr.GetAPIError(apierr.ErrExpiredPresignRequest),
|
|
|
|
now.Format(time.RFC3339), signatureDateTime.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
if now.Before(signatureDateTime) {
|
|
|
|
return fmt.Errorf("%w: signature time from the future: now %s, signature %s", apierr.GetAPIError(apierr.ErrBadRequest),
|
|
|
|
now.Format(time.RFC3339), signatureDateTime.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 12:24:58 +00:00
|
|
|
func SignStr(secret, service, region string, t time.Time, strToSign string) string {
|
2021-08-30 19:44:53 +00:00
|
|
|
creds := deriveKey(secret, service, region, t)
|
|
|
|
signature := hmacSHA256(creds, []byte(strToSign))
|
|
|
|
return hex.EncodeToString(signature)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deriveKey(secret, service, region string, t time.Time) []byte {
|
|
|
|
hmacDate := hmacSHA256([]byte("AWS4"+secret), []byte(t.UTC().Format("20060102")))
|
|
|
|
hmacRegion := hmacSHA256(hmacDate, []byte(region))
|
|
|
|
hmacService := hmacSHA256(hmacRegion, []byte(service))
|
|
|
|
return hmacSHA256(hmacService, []byte("aws4_request"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func hmacSHA256(key []byte, data []byte) []byte {
|
|
|
|
hash := hmac.New(sha256.New, key)
|
|
|
|
hash.Write(data)
|
|
|
|
return hash.Sum(nil)
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// MultipartFormValue gets value by key from multipart form.
|
2021-08-30 19:44:53 +00:00
|
|
|
func MultipartFormValue(r *http.Request, key string) string {
|
|
|
|
if r.MultipartForm == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
if vs := r.MultipartForm.Value[key]; len(vs) > 0 {
|
|
|
|
return vs[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepareForm(form *multipart.Form) error {
|
|
|
|
var oldKeysValue []string
|
|
|
|
var oldKeysFile []string
|
|
|
|
|
|
|
|
for k, v := range form.Value {
|
|
|
|
lowerKey := strings.ToLower(k)
|
|
|
|
if lowerKey != k {
|
|
|
|
form.Value[lowerKey] = v
|
|
|
|
oldKeysValue = append(oldKeysValue, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, k := range oldKeysValue {
|
|
|
|
delete(form.Value, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range form.File {
|
|
|
|
lowerKey := strings.ToLower(k)
|
|
|
|
if lowerKey != "file" {
|
|
|
|
oldKeysFile = append(oldKeysFile, k)
|
|
|
|
if len(v) > 0 {
|
|
|
|
field, err := v[0].Open()
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("file header open: %w", err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := io.ReadAll(field)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return fmt.Errorf("read field: %w", err)
|
2021-08-30 19:44:53 +00:00
|
|
|
}
|
|
|
|
form.Value[lowerKey] = []string{string(data)}
|
|
|
|
}
|
|
|
|
} else if lowerKey != k {
|
|
|
|
form.File[lowerKey] = v
|
|
|
|
oldKeysFile = append(oldKeysFile, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, k := range oldKeysFile {
|
|
|
|
delete(form.File, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|