2023-10-09 09:18:12 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package router
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-02-08 09:20:01 -05:00
|
|
|
"log/slog"
|
2023-10-09 09:18:12 -04:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/edgelesssys/constellation/v2/s3proxy/internal/s3"
|
|
|
|
)
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
func handleGetObject(client *s3.Client, key string, bucket string, log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("intercepting")
|
2023-10-09 09:18:12 -04:00
|
|
|
if req.Header.Get("Range") != "" {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Error("GetObject Range header unsupported")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, "s3proxy currently does not support Range headers", http.StatusNotImplemented)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := object{
|
|
|
|
client: client,
|
|
|
|
key: key,
|
|
|
|
bucket: bucket,
|
|
|
|
query: req.URL.Query(),
|
|
|
|
sseCustomerAlgorithm: req.Header.Get("x-amz-server-side-encryption-customer-algorithm"),
|
|
|
|
sseCustomerKey: req.Header.Get("x-amz-server-side-encryption-customer-key"),
|
|
|
|
sseCustomerKeyMD5: req.Header.Get("x-amz-server-side-encryption-customer-key-MD5"),
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
get(obj.get)(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
func handlePutObject(client *s3.Client, key string, bucket string, log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("intercepting")
|
2023-10-09 09:18:12 -04:00
|
|
|
body, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.Any("error", err)).Error("PutObject")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("reading body: %s", err.Error()), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
clientDigest := req.Header.Get("x-amz-content-sha256")
|
|
|
|
serverDigest := sha256sum(body)
|
|
|
|
|
|
|
|
// There may be a client that wants to test that incorrect content digests result in API errors.
|
|
|
|
// For encrypting the body we have to recalculate the content digest.
|
|
|
|
// If the client intentionally sends a mismatching content digest, we would take the client request, rewrap it,
|
|
|
|
// calculate the correct digest for the new body and NOT get an error.
|
|
|
|
// Thus we have to check incoming requets for matching content digests.
|
|
|
|
// UNSIGNED-PAYLOAD can be used to disabled payload signing. In that case we don't check the content digest.
|
|
|
|
if clientDigest != "" && clientDigest != "UNSIGNED-PAYLOAD" && clientDigest != serverDigest {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Debug("PutObject", "error", "x-amz-content-sha256 mismatch")
|
2023-10-09 09:18:12 -04:00
|
|
|
// The S3 API responds with an XML formatted error message.
|
|
|
|
mismatchErr := NewContentSHA256MismatchError(clientDigest, serverDigest)
|
|
|
|
marshalled, err := xml.Marshal(mismatchErr)
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.Any("error", err)).Error("PutObject")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("marshalling error: %s", err.Error()), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Error(w, string(marshalled), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
metadata := getMetadataHeaders(req.Header)
|
|
|
|
|
|
|
|
raw := req.Header.Get("x-amz-object-lock-retain-until-date")
|
|
|
|
retentionTime, err := parseRetentionTime(raw)
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("data", raw), slog.Any("error", err)).Error("parsing lock retention time")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("parsing x-amz-object-lock-retain-until-date: %s", err.Error()), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = validateContentMD5(req.Header.Get("content-md5"), body)
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.Any("error", err)).Error("validating content md5")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("validating content md5: %s", err.Error()), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
obj := object{
|
|
|
|
client: client,
|
|
|
|
key: key,
|
|
|
|
bucket: bucket,
|
|
|
|
data: body,
|
|
|
|
query: req.URL.Query(),
|
|
|
|
tags: req.Header.Get("x-amz-tagging"),
|
|
|
|
contentType: req.Header.Get("Content-Type"),
|
|
|
|
metadata: metadata,
|
|
|
|
objectLockLegalHoldStatus: req.Header.Get("x-amz-object-lock-legal-hold"),
|
|
|
|
objectLockMode: req.Header.Get("x-amz-object-lock-mode"),
|
|
|
|
objectLockRetainUntilDate: retentionTime,
|
|
|
|
sseCustomerAlgorithm: req.Header.Get("x-amz-server-side-encryption-customer-algorithm"),
|
|
|
|
sseCustomerKey: req.Header.Get("x-amz-server-side-encryption-customer-key"),
|
|
|
|
sseCustomerKeyMD5: req.Header.Get("x-amz-server-side-encryption-customer-key-MD5"),
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
put(obj.put)(w, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
func handleForwards(log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("forwarding")
|
2023-10-09 09:18:12 -04:00
|
|
|
|
|
|
|
newReq := repackage(req)
|
|
|
|
|
|
|
|
httpClient := http.DefaultClient
|
|
|
|
resp, err := httpClient.Do(&newReq)
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.Any("error", err)).Error("do request")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("do request: %s", err.Error()), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
for key := range resp.Header {
|
|
|
|
w.Header().Set(key, resp.Header.Get(key))
|
|
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.Any("error", err)).Error("ReadAll")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("reading body: %s", err.Error()), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(resp.StatusCode)
|
|
|
|
if body == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := w.Write(body); err != nil {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.Any("error", err)).Error("Write")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, fmt.Sprintf("writing body: %s", err.Error()), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCreateMultipartUpload logs the request and blocks with an error message.
|
2024-02-08 09:20:01 -05:00
|
|
|
func handleCreateMultipartUpload(log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("intercepting CreateMultipartUpload")
|
2023-10-09 09:18:12 -04:00
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Error("Blocking CreateMultipartUpload request")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, "s3proxy is configured to block CreateMultipartUpload requests", http.StatusNotImplemented)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleUploadPart logs the request and blocks with an error message.
|
2024-02-08 09:20:01 -05:00
|
|
|
func handleUploadPart(log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("intercepting UploadPart")
|
2023-10-09 09:18:12 -04:00
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Error("Blocking UploadPart request")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, "s3proxy is configured to block UploadPart requests", http.StatusNotImplemented)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCompleteMultipartUpload logs the request and blocks with an error message.
|
2024-02-08 09:20:01 -05:00
|
|
|
func handleCompleteMultipartUpload(log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("intercepting CompleteMultipartUpload")
|
2023-10-09 09:18:12 -04:00
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Error("Blocking CompleteMultipartUpload request")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, "s3proxy is configured to block CompleteMultipartUpload requests", http.StatusNotImplemented)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleAbortMultipartUpload logs the request and blocks with an error message.
|
2024-02-08 09:20:01 -05:00
|
|
|
func handleAbortMultipartUpload(log *slog.Logger) http.HandlerFunc {
|
2023-10-09 09:18:12 -04:00
|
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
2024-02-08 09:20:01 -05:00
|
|
|
log.With(slog.String("path", req.URL.Path), slog.String("method", req.Method), slog.String("host", req.Host)).Debug("intercepting AbortMultipartUpload")
|
2023-10-09 09:18:12 -04:00
|
|
|
|
2024-02-08 09:20:01 -05:00
|
|
|
log.Error("Blocking AbortMultipartUpload request")
|
2023-10-09 09:18:12 -04:00
|
|
|
http.Error(w, "s3proxy is configured to block AbortMultipartUpload requests", http.StatusNotImplemented)
|
|
|
|
}
|
|
|
|
}
|