2021-10-04 14:32:35 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2022-07-22 06:05:07 +00:00
|
|
|
"go.uber.org/zap"
|
2021-10-04 14:32:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-04-13 16:56:58 +00:00
|
|
|
// DefaultMaxAge is a default value of Access-Control-Max-Age if this value is not set in a rule.
|
2021-10-04 14:32:35 +00:00
|
|
|
DefaultMaxAge = 600
|
|
|
|
wildcard = "*"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (h *handler) GetBucketCorsHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-10-04 14:32:35 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
cors, err := h.obj.GetBucketCORS(r.Context(), bktInfo)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get cors", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, cors); err != nil {
|
2021-10-13 18:50:02 +00:00
|
|
|
h.logAndSendError(w, "could not encode cors to response", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) PutBucketCorsHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-10-04 14:32:35 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.PutCORSParams{
|
2023-10-09 12:34:51 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
Reader: r.Body,
|
|
|
|
NewDecoder: h.cfg.NewXMLDecoder,
|
2023-04-24 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
p.CopiesNumbers, err = h.pickCopiesNumbers(parseMetadata(r), reqInfo.Namespace, bktInfo.LocationConstraint)
|
2023-04-24 23:49:12 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "invalid copies number", reqInfo, err)
|
|
|
|
return
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.PutBucketCORS(r.Context(), p); err != nil {
|
|
|
|
h.logAndSendError(w, "could not put cors configuration", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-04 12:53:00 +00:00
|
|
|
if err = middleware.WriteSuccessResponseHeadersOnly(w); err != nil {
|
|
|
|
h.logAndSendError(w, "write response", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) DeleteBucketCorsHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-10-04 14:32:35 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
if err = h.obj.DeleteBucketCORS(r.Context(), bktInfo); err != nil {
|
2021-10-04 14:32:35 +00:00
|
|
|
h.logAndSendError(w, "could not delete cors", reqInfo, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) AppendCORSHeaders(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == http.MethodOptions {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
origin := r.Header.Get(api.Origin)
|
|
|
|
if origin == "" {
|
|
|
|
return
|
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
|
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-10-04 14:32:35 +00:00
|
|
|
if reqInfo.BucketName == "" {
|
|
|
|
return
|
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
bktInfo, err := h.obj.GetBucketInfo(ctx, reqInfo.BucketName)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Warn(logs.GetBucketInfo, zap.Error(err))
|
2021-10-04 14:32:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
cors, err := h.obj.GetBucketCORS(ctx, bktInfo)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Warn(logs.GetBucketCors, zap.Error(err))
|
2021-10-04 14:32:35 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-13 18:50:02 +00:00
|
|
|
|
2021-10-04 14:32:35 +00:00
|
|
|
withCredentials := r.Header.Get(api.Authorization) != ""
|
|
|
|
|
|
|
|
for _, rule := range cors.CORSRules {
|
|
|
|
for _, o := range rule.AllowedOrigins {
|
|
|
|
if o == origin {
|
|
|
|
for _, m := range rule.AllowedMethods {
|
|
|
|
if m == r.Method {
|
|
|
|
w.Header().Set(api.AccessControlAllowOrigin, origin)
|
2021-10-07 16:22:54 +00:00
|
|
|
w.Header().Set(api.AccessControlAllowMethods, strings.Join(rule.AllowedMethods, ", "))
|
2021-10-04 14:32:35 +00:00
|
|
|
w.Header().Set(api.AccessControlAllowCredentials, "true")
|
|
|
|
w.Header().Set(api.Vary, api.Origin)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if o == wildcard {
|
|
|
|
for _, m := range rule.AllowedMethods {
|
|
|
|
if m == r.Method {
|
|
|
|
if withCredentials {
|
|
|
|
w.Header().Set(api.AccessControlAllowOrigin, origin)
|
|
|
|
w.Header().Set(api.AccessControlAllowCredentials, "true")
|
|
|
|
w.Header().Set(api.Vary, api.Origin)
|
|
|
|
} else {
|
|
|
|
w.Header().Set(api.AccessControlAllowOrigin, o)
|
|
|
|
}
|
2021-10-07 16:22:54 +00:00
|
|
|
w.Header().Set(api.AccessControlAllowMethods, strings.Join(rule.AllowedMethods, ", "))
|
2021-10-04 14:32:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Preflight(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2021-10-04 14:32:35 +00:00
|
|
|
bktInfo, err := h.obj.GetBucketInfo(r.Context(), reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
origin := r.Header.Get(api.Origin)
|
|
|
|
if origin == "" {
|
|
|
|
h.logAndSendError(w, "origin request header needed", reqInfo, errors.GetAPIError(errors.ErrBadRequest))
|
|
|
|
}
|
|
|
|
|
|
|
|
method := r.Header.Get(api.AccessControlRequestMethod)
|
|
|
|
if method == "" {
|
|
|
|
h.logAndSendError(w, "Access-Control-Request-Method request header needed", reqInfo, errors.GetAPIError(errors.ErrBadRequest))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var headers []string
|
|
|
|
requestHeaders := r.Header.Get(api.AccessControlRequestHeaders)
|
|
|
|
if requestHeaders != "" {
|
|
|
|
headers = strings.Split(requestHeaders, ", ")
|
|
|
|
}
|
|
|
|
|
2021-10-13 18:50:02 +00:00
|
|
|
cors, err := h.obj.GetBucketCORS(r.Context(), bktInfo)
|
2021-10-04 14:32:35 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get cors", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rule := range cors.CORSRules {
|
|
|
|
for _, o := range rule.AllowedOrigins {
|
|
|
|
if o == origin || o == wildcard {
|
|
|
|
for _, m := range rule.AllowedMethods {
|
|
|
|
if m == method {
|
|
|
|
if !checkSubslice(rule.AllowedHeaders, headers) {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-31 13:55:37 +00:00
|
|
|
w.Header().Set(api.AccessControlAllowOrigin, origin)
|
|
|
|
w.Header().Set(api.AccessControlAllowMethods, method)
|
2021-10-04 14:32:35 +00:00
|
|
|
if headers != nil {
|
|
|
|
w.Header().Set(api.AccessControlAllowHeaders, requestHeaders)
|
|
|
|
}
|
|
|
|
if rule.ExposeHeaders != nil {
|
|
|
|
w.Header().Set(api.AccessControlExposeHeaders, strings.Join(rule.ExposeHeaders, ", "))
|
|
|
|
}
|
|
|
|
if rule.MaxAgeSeconds > 0 || rule.MaxAgeSeconds == -1 {
|
|
|
|
w.Header().Set(api.AccessControlMaxAge, strconv.Itoa(rule.MaxAgeSeconds))
|
|
|
|
} else {
|
2023-09-08 11:17:14 +00:00
|
|
|
w.Header().Set(api.AccessControlMaxAge, strconv.Itoa(h.cfg.DefaultMaxAge()))
|
2021-10-04 14:32:35 +00:00
|
|
|
}
|
|
|
|
if o != wildcard {
|
|
|
|
w.Header().Set(api.AccessControlAllowCredentials, "true")
|
|
|
|
}
|
2024-03-04 12:53:00 +00:00
|
|
|
if err = middleware.WriteSuccessResponseHeadersOnly(w); err != nil {
|
|
|
|
h.logAndSendError(w, "write response", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-04 14:32:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h.logAndSendError(w, "Forbidden", reqInfo, errors.GetAPIError(errors.ErrAccessDenied))
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkSubslice(slice []string, subSlice []string) bool {
|
|
|
|
if sliceContains(slice, wildcard) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if len(subSlice) > len(slice) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, r := range subSlice {
|
|
|
|
if !sliceContains(slice, r) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func sliceContains(slice []string, str string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == str {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|