2021-03-31 16:58:42 +00:00
|
|
|
package uploader
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-26 12:44:07 +00:00
|
|
|
"fmt"
|
2022-09-01 09:21:27 +00:00
|
|
|
"math"
|
2021-11-26 12:44:07 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-11-26 12:44:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2021-11-30 07:22:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-http-gw/utils"
|
2021-01-25 19:36:46 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-09-08 06:15:42 +00:00
|
|
|
var neofsAttributeHeaderPrefixes = [...][]byte{[]byte("Neofs-"), []byte("NEOFS-"), []byte("neofs-")}
|
|
|
|
|
|
|
|
func systemTranslator(key, prefix []byte) []byte {
|
2022-04-21 08:35:57 +00:00
|
|
|
// replace the specified prefix with `__NEOFS__`
|
2021-11-30 07:22:05 +00:00
|
|
|
key = bytes.Replace(key, prefix, []byte(utils.SystemAttributePrefix), 1)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// replace `-` with `_`
|
|
|
|
key = bytes.ReplaceAll(key, []byte("-"), []byte("_"))
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// replace with uppercase
|
|
|
|
return bytes.ToUpper(key)
|
2021-01-25 19:36:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
func filterHeaders(l *zap.Logger, header *fasthttp.RequestHeader) map[string]string {
|
2021-01-25 19:36:46 +00:00
|
|
|
result := make(map[string]string)
|
2021-11-30 07:22:05 +00:00
|
|
|
prefix := []byte(utils.UserAttributeHeaderPrefix)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
|
|
|
header.VisitAll(func(key, val []byte) {
|
2022-04-21 08:35:57 +00:00
|
|
|
// checks that the key and the val not empty
|
2021-01-25 19:36:46 +00:00
|
|
|
if len(key) == 0 || len(val) == 0 {
|
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-21 08:35:57 +00:00
|
|
|
// checks that the key has attribute prefix
|
2021-01-26 15:36:53 +00:00
|
|
|
if !bytes.HasPrefix(key, prefix) {
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
2021-01-26 15:36:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// removing attribute prefix
|
|
|
|
key = bytes.TrimPrefix(key, prefix)
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// checks that it's a system NeoFS header
|
2021-09-08 06:15:42 +00:00
|
|
|
for _, system := range neofsAttributeHeaderPrefixes {
|
|
|
|
if bytes.HasPrefix(key, system) {
|
|
|
|
key = systemTranslator(key, system)
|
|
|
|
break
|
|
|
|
}
|
2021-02-03 13:01:30 +00:00
|
|
|
}
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2022-04-21 08:35:57 +00:00
|
|
|
// checks that the attribute key is not empty
|
2021-02-03 13:01:30 +00:00
|
|
|
if len(key) == 0 {
|
2021-01-25 19:36:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-03 13:01:30 +00:00
|
|
|
// make string representation of key / val
|
|
|
|
k, v := string(key), string(val)
|
|
|
|
|
|
|
|
result[k] = v
|
|
|
|
|
|
|
|
l.Debug("add attribute to result object",
|
|
|
|
zap.String("key", k),
|
|
|
|
zap.String("val", v))
|
2021-01-25 19:36:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2021-11-26 12:44:07 +00:00
|
|
|
|
|
|
|
func prepareExpirationHeader(headers map[string]string, epochDurations *epochDurations) error {
|
|
|
|
expirationInEpoch := headers[object.SysAttributeExpEpoch]
|
|
|
|
|
2021-11-30 07:22:05 +00:00
|
|
|
if timeRFC3339, ok := headers[utils.ExpirationRFC3339Attr]; ok {
|
2021-11-26 12:44:07 +00:00
|
|
|
expTime, err := time.Parse(time.RFC3339, timeRFC3339)
|
|
|
|
if err != nil {
|
2021-11-30 07:22:05 +00:00
|
|
|
return fmt.Errorf("couldn't parse value %s of header %s", timeRFC3339, utils.ExpirationRFC3339Attr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now().UTC()
|
|
|
|
if expTime.Before(now) {
|
2021-11-30 07:22:05 +00:00
|
|
|
return fmt.Errorf("value %s of header %s must be in the future", timeRFC3339, utils.ExpirationRFC3339Attr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
updateExpirationHeader(headers, epochDurations, expTime.Sub(now))
|
2021-11-30 07:22:05 +00:00
|
|
|
delete(headers, utils.ExpirationRFC3339Attr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 07:22:05 +00:00
|
|
|
if timestamp, ok := headers[utils.ExpirationTimestampAttr]; ok {
|
2021-11-26 12:44:07 +00:00
|
|
|
value, err := strconv.ParseInt(timestamp, 10, 64)
|
|
|
|
if err != nil {
|
2021-11-30 07:22:05 +00:00
|
|
|
return fmt.Errorf("couldn't parse value %s of header %s", timestamp, utils.ExpirationTimestampAttr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
expTime := time.Unix(value, 0)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
if expTime.Before(now) {
|
2021-11-30 07:22:05 +00:00
|
|
|
return fmt.Errorf("value %s of header %s must be in the future", timestamp, utils.ExpirationTimestampAttr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
updateExpirationHeader(headers, epochDurations, expTime.Sub(now))
|
2021-11-30 07:22:05 +00:00
|
|
|
delete(headers, utils.ExpirationTimestampAttr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
|
2021-11-30 07:22:05 +00:00
|
|
|
if duration, ok := headers[utils.ExpirationDurationAttr]; ok {
|
2021-11-26 12:44:07 +00:00
|
|
|
expDuration, err := time.ParseDuration(duration)
|
|
|
|
if err != nil {
|
2021-11-30 07:22:05 +00:00
|
|
|
return fmt.Errorf("couldn't parse value %s of header %s", duration, utils.ExpirationDurationAttr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
if expDuration <= 0 {
|
2021-11-30 07:22:05 +00:00
|
|
|
return fmt.Errorf("value %s of header %s must be positive", expDuration, utils.ExpirationDurationAttr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
updateExpirationHeader(headers, epochDurations, expDuration)
|
2021-11-30 07:22:05 +00:00
|
|
|
delete(headers, utils.ExpirationDurationAttr)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if expirationInEpoch != "" {
|
|
|
|
headers[object.SysAttributeExpEpoch] = expirationInEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateExpirationHeader(headers map[string]string, durations *epochDurations, expDuration time.Duration) {
|
2022-09-01 09:21:27 +00:00
|
|
|
epochDuration := uint64(durations.msPerBlock) * durations.blockPerEpoch
|
|
|
|
currentEpoch := durations.currentEpoch
|
|
|
|
numEpoch := uint64(expDuration.Milliseconds()) / epochDuration
|
|
|
|
|
|
|
|
if uint64(expDuration.Milliseconds())%epochDuration != 0 {
|
|
|
|
numEpoch++
|
|
|
|
}
|
|
|
|
|
|
|
|
expirationEpoch := uint64(math.MaxUint64)
|
|
|
|
if numEpoch < math.MaxUint64-currentEpoch {
|
|
|
|
expirationEpoch = currentEpoch + numEpoch
|
|
|
|
}
|
|
|
|
|
|
|
|
headers[object.SysAttributeExpEpoch] = strconv.FormatUint(expirationEpoch, 10)
|
2021-11-26 12:44:07 +00:00
|
|
|
}
|