From cc9e07d6ea1e592f7e273784769896f56bfb6eda Mon Sep 17 00:00:00 2001 From: Otto Bittner Date: Wed, 27 Sep 2023 11:40:32 +0200 Subject: [PATCH] s3proxy: add intial implementation The proxy intercepts GetObject and PutObject. It does not encrypt/decrypt data. A manual deployment guide is included. --- bazel/oci/containers.bzl | 8 + s3proxy/cmd/BUILD.bazel | 46 +++ s3proxy/cmd/main.go | 110 ++++++ s3proxy/deploy/README.md | 61 ++++ s3proxy/deploy/create_cert.sh | 78 +++++ s3proxy/deploy/deployment-s3proxy.yaml | 60 ++++ s3proxy/internal/router/BUILD.bazel | 24 ++ s3proxy/internal/router/object.go | 154 +++++++++ s3proxy/internal/router/router.go | 459 +++++++++++++++++++++++++ s3proxy/internal/router/router_test.go | 48 +++ s3proxy/internal/s3/BUILD.bazel | 13 + s3proxy/internal/s3/s3.go | 88 +++++ 12 files changed, 1149 insertions(+) create mode 100644 s3proxy/cmd/BUILD.bazel create mode 100644 s3proxy/cmd/main.go create mode 100644 s3proxy/deploy/README.md create mode 100755 s3proxy/deploy/create_cert.sh create mode 100644 s3proxy/deploy/deployment-s3proxy.yaml create mode 100644 s3proxy/internal/router/BUILD.bazel create mode 100644 s3proxy/internal/router/object.go create mode 100644 s3proxy/internal/router/router.go create mode 100644 s3proxy/internal/router/router_test.go create mode 100644 s3proxy/internal/s3/BUILD.bazel create mode 100644 s3proxy/internal/s3/s3.go diff --git a/bazel/oci/containers.bzl b/bazel/oci/containers.bzl index 9d6a026547..8f9e47db1b 100644 --- a/bazel/oci/containers.bzl +++ b/bazel/oci/containers.bzl @@ -55,6 +55,14 @@ def containers(): "repotag_file": "//bazel/release:libvirt_tag.txt", "used_by": ["config"], }, + { + "identifier": "s3proxy", + "image_name": "s3proxy", + "name": "s3proxy", + "oci": "//s3proxy/cmd:s3proxy", + "repotag_file": "//bazel/release:s3proxy_tag.txt", + "used_by": ["config"], + }, ] def helm_containers(): diff --git a/s3proxy/cmd/BUILD.bazel b/s3proxy/cmd/BUILD.bazel new file mode 100644 index 0000000000..d426ff5bb5 --- /dev/null +++ b/s3proxy/cmd/BUILD.bazel @@ -0,0 +1,46 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary", "go_library") +load("@rules_oci//oci:defs.bzl", "oci_image") +load("@rules_pkg//:pkg.bzl", "pkg_tar") + +go_library( + name = "cmd_lib", + srcs = ["main.go"], + importpath = "github.com/edgelesssys/constellation/v2/s3proxy/cmd", + visibility = ["//visibility:private"], + deps = [ + "//internal/logger", + "//s3proxy/internal/router", + ], +) + +go_binary( + name = "cmd", + embed = [":cmd_lib"], + visibility = ["//visibility:public"], +) + +go_cross_binary( + name = "s3proxy_linux_amd64", + platform = "@io_bazel_rules_go//go/toolchain:linux_amd64", + target = ":cmd", + visibility = ["//visibility:public"], +) + +pkg_tar( + name = "layer", + srcs = [ + ":s3proxy_linux_amd64", + ], + mode = "0755", + remap_paths = {"/s3proxy_linux_amd64": "/s3proxy"}, +) + +oci_image( + name = "s3proxy", + base = "@distroless_static_linux_amd64", + entrypoint = ["/s3proxy"], + tars = [ + ":layer", + ], + visibility = ["//visibility:public"], +) diff --git a/s3proxy/cmd/main.go b/s3proxy/cmd/main.go new file mode 100644 index 0000000000..7eb03e7f05 --- /dev/null +++ b/s3proxy/cmd/main.go @@ -0,0 +1,110 @@ +package main + +import ( + "crypto/tls" + "flag" + "fmt" + "net" + "net/http" + + "github.com/edgelesssys/constellation/v2/internal/logger" + "github.com/edgelesssys/constellation/v2/s3proxy/internal/router" +) + +const ( + // defaultPort is the default port to listen on. + defaultPort = 443 + // defaultIP is the default IP to listen on. + defaultIP = "172.18.0.1" + // defaultRegion is the default AWS region to use. + defaultRegion = "eu-west-1" + // defaultCertLocation is the default location of the TLS certificate. + defaultCertLocation = "/etc/s3proxy/certs" + // defaultLogLevel is the default log level. + defaultLogLevel = 0 +) + +func main() { + flags, err := parseFlags() + if err != nil { + panic(err) + } + + // logLevel can be made a public variable so logging level can be changed dynamically. + // TODO (derpsteb): enable once we are on go 1.21. + // logLevel := new(slog.LevelVar) + // handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: logLevel}) + // logger := slog.New(handler) + // logLevel.Set(flags.logLevel) + + logger := logger.New(logger.JSONLog, logger.VerbosityFromInt(flags.logLevel)) + + if err := runServer(flags, logger); err != nil { + panic(err) + } +} + +func runServer(flags cmdFlags, log *logger.Logger) error { + log.Infof("listening", "ip", flags.ip, "port", flags.port, "region", flags.region) + + router := router.New(flags.region, log) + + server := http.Server{ + Addr: fmt.Sprintf("%s:%d", flags.ip, flags.port), + Handler: http.HandlerFunc(router.Serve), + // Disable HTTP/2. Serving HTTP/2 will cause some clients to use HTTP/2. + // It seems like AWS S3 does not support HTTP/2. + // Having HTTP/2 enabled will at least cause the aws-sdk-go V1 copy-object operation to fail. + TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){}, + } + + if flags.port == 443 { + cert, err := tls.LoadX509KeyPair(flags.certLocation+"/s3proxy.crt", flags.certLocation+"/s3proxy.key") + if err != nil { + return fmt.Errorf("loading TLS certificate: %w", err) + } + + server.TLSConfig = &tls.Config{ + Certificates: []tls.Certificate{cert}, + } + + // TLSConfig is populated, so we can safely pass empty strings to ListenAndServeTLS. + return server.ListenAndServeTLS("", "") + } + + log.Warnf("TLS is disabled") + return server.ListenAndServe() +} + +func parseFlags() (cmdFlags, error) { + port := flag.Int("port", defaultPort, "port to listen on") + ip := flag.String("ip", defaultIP, "ip to listen on") + region := flag.String("region", defaultRegion, "AWS region in which target bucket is located") + certLocation := flag.String("cert", defaultCertLocation, "location of TLS certificate") + level := flag.Int("level", defaultLogLevel, "log level") + + flag.Parse() + + netIP := net.ParseIP(*ip) + if netIP == nil { + return cmdFlags{}, fmt.Errorf("not a valid IPv4 address: %s", *ip) + } + + // TODO(derpsteb): enable once we are on go 1.21. + // logLevel := new(slog.Level) + // if err := logLevel.UnmarshalText([]byte(*level)); err != nil { + // return cmdFlags{}, fmt.Errorf("parsing log level: %w", err) + // } + + return cmdFlags{port: *port, ip: netIP.String(), region: *region, certLocation: *certLocation, logLevel: *level}, nil +} + +type cmdFlags struct { + port int + ip string + region string + certLocation string + // TODO(derpsteb): enable once we are on go 1.21. + // logLevel slog.Level + logLevel int +} diff --git a/s3proxy/deploy/README.md b/s3proxy/deploy/README.md new file mode 100644 index 0000000000..f1f3877b23 --- /dev/null +++ b/s3proxy/deploy/README.md @@ -0,0 +1,61 @@ +# Deploying s3proxy + +Disclaimer: the following steps will be automated next. +- Within `constellation/build`: `bazel run //:devbuild` +- Copy the container name displayed for the s3proxy image. Look for the line starting with `[@//bazel/release:s3proxy_push]`. +- Replace the image key in `deployment-s3proxy.yaml` with the image value you just copied. Use the sha256 hash instead of the tag to make sure you use the latest image. +- Run the script `create_cert.sh`. This will create a certificate signed by the Kubernetes CA and store it in the cluster, including the private key. The s3proxy uses that certificate to serve HTTPS. +- Replace the `replaceme` values with valid AWS credentials. The s3proxy uses those credentials to access S3. +- Run `kubectl apply -f deployment-s3proxy.yaml` + +# Deploying Filestash + +Filestash is a demo application that can be used to see s3proxy in action. +To deploy Filestash, first deploy s3proxy as described above. +Then run the below commands: + +```sh +$ cat << EOF > "deployment-filestash.yaml" +apiVersion: apps/v1 +kind: Deployment +metadata: + name: filestash +spec: + replicas: 1 + selector: + matchLabels: + app: filestash + template: + metadata: + labels: + app: filestash + spec: + imagePullSecrets: + - name: regcred + hostAliases: + - ip: $(kubectl get svc s3proxy-service -o=jsonpath='{.spec.clusterIP}') + hostnames: + - "s3.eu-west-1.amazonaws.com" + containers: + - name: filestash + image: machines/filestash:latest + ports: + - containerPort: 8334 + volumeMounts: + - name: kube-ca + mountPath: /etc/ssl/certs/kube-ca.crt + subPath: kube-ca.crt + volumes: + - name: kube-ca + configMap: + name: kube-root-ca.crt + items: + - key: ca.crt + path: kube-ca.crt +EOF + +$ kubectl apply -f deployment-filestash.yaml +``` + +Afterwards you can use a port forward to access the Filestash pod: +- `kubectl port-forward pod/$(kubectl get pod --selector='app=filestash' -o=jsonpath='{.items[*].metadata.name}') 8443:8443` diff --git a/s3proxy/deploy/create_cert.sh b/s3proxy/deploy/create_cert.sh new file mode 100755 index 0000000000..1a41090eaa --- /dev/null +++ b/s3proxy/deploy/create_cert.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash +# SERVICE is the name of the s3proxy service in kubernetes. +# It does not have to match the actual running service, though it may help for consistency. +export SERVICE=s3proxy + +# NAMESPACE where the s3proxy service is running. +export NAMESPACE=default + +# SECRET_NAME to create in the kubernetes secrets store. +export SECRET_NAME=s3proxy-tls + +# TMPDIR is a temporary working directory. +export TMPDIR=$(mktemp -d) + +# CSR_NAME will be the name of our certificate signing request as seen by kubernetes. +export CSR_NAME=s3proxy-csr + +openssl genrsa -out ${TMPDIR}/s3proxy.key 2048 + +cat << EOF > ${TMPDIR}/csr.conf +[req] +req_extensions = v3_req +distinguished_name = req_distinguished_name +[req_distinguished_name] +[ v3_req ] +basicConstraints = CA:FALSE +keyUsage = nonRepudiation, digitalSignature, keyEncipherment +extendedKeyUsage = serverAuth +subjectAltName = @alt_names +[alt_names] +DNS.1 = *.${SERVICE} +DNS.2 = *.${SERVICE}.${NAMESPACE} +DNS.3 = *.${SERVICE}.${NAMESPACE}.svc +DNS.4 = *.${SERVICE}.${NAMESPACE}.svc.cluster.local +DNS.5 = *.${SERVICE}-internal +DNS.6 = *.${SERVICE}-internal.${NAMESPACE} +DNS.7 = *.${SERVICE}-internal.${NAMESPACE}.svc +DNS.8 = *.${SERVICE}-internal.${NAMESPACE}.svc.cluster.local +DNS.9 = s3.eu-west-1.amazonaws.com +IP.1 = 127.0.0.1 +EOF + +openssl req -new -key ${TMPDIR}/s3proxy.key \ + -subj "/O=system:nodes/CN=system:node:${SERVICE}.${NAMESPACE}.svc" \ + -out ${TMPDIR}/server.csr \ + -config ${TMPDIR}/csr.conf + +cat << EOF > ${TMPDIR}/csr.yaml +apiVersion: certificates.k8s.io/v1 +kind: CertificateSigningRequest +metadata: + name: ${CSR_NAME} +spec: + groups: + - system:authenticated + request: $(cat ${TMPDIR}/server.csr | base64 | tr -d '\r\n') + signerName: kubernetes.io/kubelet-serving + usages: + - digital signature + - key encipherment + - server auth +EOF + +kubectl create -f ${TMPDIR}/csr.yaml --dry-run=client -o yaml --save-config | kubectl apply -f - +kubectl certificate approve ${CSR_NAME} +kubectl get csr ${CSR_NAME} + +serverCert=$(kubectl get csr ${CSR_NAME} -o jsonpath='{.status.certificate}') +echo "${serverCert}" | openssl base64 -d -A -out ${TMPDIR}/s3proxy.crt +kubectl config view --raw --minify --flatten -o jsonpath='{.clusters[].cluster.certificate-authority-data}' | base64 -d > ${TMPDIR}/s3proxy.ca +kubectl create namespace ${NAMESPACE} --dry-run=client -o yaml | kubectl apply -f - +kubectl create secret generic ${SECRET_NAME} \ + --namespace ${NAMESPACE} \ + --from-file=s3proxy.key=${TMPDIR}/s3proxy.key \ + --from-file=s3proxy.crt=${TMPDIR}/s3proxy.crt \ + --from-file=s3proxy.ca=${TMPDIR}/s3proxy.ca --dry-run=client -o yaml --save-config | kubectl apply -f - + +rm -rf ${TMPDIR} diff --git a/s3proxy/deploy/deployment-s3proxy.yaml b/s3proxy/deploy/deployment-s3proxy.yaml new file mode 100644 index 0000000000..b94c63bb27 --- /dev/null +++ b/s3proxy/deploy/deployment-s3proxy.yaml @@ -0,0 +1,60 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: s3proxy +spec: + replicas: 1 + selector: + matchLabels: + app: s3proxy + template: + metadata: + labels: + app: s3proxy + spec: + imagePullSecrets: + - name: regcred + containers: + - name: s3proxy + image: ghcr.io/derpsteb/constellation/s3proxy@sha256:57dbcf394e1464c07f2ef5c5e7fd1a87d7477c15394faa81601901f6956c06e3 + args: + - "--ip=0.0.0.0" + - "--port=443" + - "--level=debug" + ports: + - containerPort: 443 + volumeMounts: + - name: tls-cert-data + mountPath: /etc/s3proxy/certs + envFrom: + - secretRef: + name: s3-creds + volumes: + - name: tls-cert-data + secret: + secretName: s3proxy-tls + - name: s3-creds + secret: + secretName: s3-creds +--- +apiVersion: v1 +kind: Service +metadata: + name: s3proxy-service +spec: + selector: + app: s3proxy + ports: + - name: https + port: 443 + targetPort: 443 + type: ClusterIP +--- +apiVersion: v1 +kind: Secret +metadata: + name: s3-creds +type: Opaque +stringData: + AWS_ACCESS_KEY_ID: "replaceme" + AWS_SECRET_ACCESS_KEY: "replaceme" diff --git a/s3proxy/internal/router/BUILD.bazel b/s3proxy/internal/router/BUILD.bazel new file mode 100644 index 0000000000..d2202ab6f9 --- /dev/null +++ b/s3proxy/internal/router/BUILD.bazel @@ -0,0 +1,24 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("//bazel/go:go_test.bzl", "go_test") + +go_library( + name = "router", + srcs = [ + "object.go", + "router.go", + ], + importpath = "github.com/edgelesssys/constellation/v2/s3proxy/internal/router", + visibility = ["//s3proxy:__subpackages__"], + deps = [ + "//internal/logger", + "//s3proxy/internal/s3", + "@com_github_aws_aws_sdk_go_v2_service_s3//:s3", + ], +) + +go_test( + name = "router_test", + srcs = ["router_test.go"], + embed = [":router"], + deps = ["@com_github_stretchr_testify//assert"], +) diff --git a/s3proxy/internal/router/object.go b/s3proxy/internal/router/object.go new file mode 100644 index 0000000000..f7bf9ee2c9 --- /dev/null +++ b/s3proxy/internal/router/object.go @@ -0,0 +1,154 @@ +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ + +package router + +import ( + "context" + "io" + "net/http" + "net/url" + "regexp" + "strconv" + "strings" + "time" + + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/edgelesssys/constellation/v2/internal/logger" +) + + +// object bundles data to implement http.Handler methods that use data from incoming requests. +type object struct { + client s3Client + key string + bucket string + data []byte + query url.Values + tags string + contentType string + metadata map[string]string + objectLockLegalHoldStatus string + objectLockMode string + objectLockRetainUntilDate time.Time + log *logger.Logger +} + +// TODO(derpsteb): serve all headers present in s3.GetObjectOutput in s3 proxy response. currently we only serve those required to make minio/mint pass. +func (o object) get(w http.ResponseWriter, r *http.Request) { + o.log.Debugf("getObject", "key", o.key, "host", o.bucket) + + versionID, ok := o.query["versionId"] + if !ok { + versionID = []string{""} + } + + data, err := o.client.GetObject(r.Context(), o.bucket, o.key, versionID[0]) + if err != nil { + // log with Info as it might be expected behavior (e.g. object not found). + o.log.Errorf("GetObject sending request to S3", "error", err) + + // We want to forward error codes from the s3 API to clients as much as possible. + code := parseErrorCode(err) + if code != 0 { + http.Error(w, err.Error(), code) + return + } + + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if data.ETag != nil { + w.Header().Set("ETag", strings.Trim(*data.ETag, "\"")) + } + + body, err := io.ReadAll(data.Body) + if err != nil { + o.log.Errorf("GetObject reading S3 response", "error", err) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) + if _, err := w.Write(body); err != nil { + o.log.Errorf("GetObject sending response", "error", err) + } +} + +func (o object) put(w http.ResponseWriter, r *http.Request) { + output, err := o.client.PutObject(r.Context(), o.bucket, o.key, o.tags, o.contentType, o.objectLockLegalHoldStatus, o.objectLockMode, o.objectLockRetainUntilDate, o.metadata, o.data) + if err != nil { + o.log.Errorf("PutObject sending request to S3", "error", err) + + // We want to forward error codes from the s3 API to clients whenever possible. + code := parseErrorCode(err) + if code != 0 { + http.Error(w, err.Error(), code) + return + } + + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("x-amz-server-side-encryption", string(output.ServerSideEncryption)) + + if output.VersionId != nil { + w.Header().Set("x-amz-version-id", *output.VersionId) + } + if output.ETag != nil { + w.Header().Set("ETag", *output.ETag) + } + if output.Expiration != nil { + w.Header().Set("x-amz-expiration", *output.Expiration) + } + if output.ChecksumCRC32 != nil { + w.Header().Set("x-amz-checksum-crc32", *output.ChecksumCRC32) + } + if output.ChecksumCRC32C != nil { + w.Header().Set("x-amz-checksum-crc32c", *output.ChecksumCRC32C) + } + if output.ChecksumSHA1 != nil { + w.Header().Set("x-amz-checksum-sha1", *output.ChecksumSHA1) + } + if output.ChecksumSHA256 != nil { + w.Header().Set("x-amz-checksum-sha256", *output.ChecksumSHA256) + } + if output.SSECustomerAlgorithm != nil { + w.Header().Set("x-amz-server-side-encryption-customer-algorithm", *output.SSECustomerAlgorithm) + } + if output.SSECustomerKeyMD5 != nil { + w.Header().Set("x-amz-server-side-encryption-customer-key-MD5", *output.SSECustomerKeyMD5) + } + if output.SSEKMSKeyId != nil { + w.Header().Set("x-amz-server-side-encryption-aws-kms-key-id", *output.SSEKMSKeyId) + } + if output.SSEKMSEncryptionContext != nil { + w.Header().Set("x-amz-server-side-encryption-context", *output.SSEKMSEncryptionContext) + } + + w.WriteHeader(http.StatusOK) + if _, err := w.Write(nil); err != nil { + o.log.Errorf("PutObject sending response", "error", err) + } +} + +func parseErrorCode(err error) int { + regex := regexp.MustCompile(`https response error StatusCode: (\d+)`) + matches := regex.FindStringSubmatch(err.Error()) + if len(matches) > 1 { + code, _ := strconv.Atoi(matches[1]) + return code + } + + return 0 +} + +type s3Client interface { + GetObject(ctx context.Context, bucket, key, versionID string) (*s3.GetObjectOutput, error) + PutObject(ctx context.Context, bucket, key, tags, contentType, objectLockLegalHoldStatus, objectLockMode string, objectLockRetainUntilDate time.Time, metadata map[string]string, body []byte) (*s3.PutObjectOutput, error) +} diff --git a/s3proxy/internal/router/router.go b/s3proxy/internal/router/router.go new file mode 100644 index 0000000000..b502e9afdc --- /dev/null +++ b/s3proxy/internal/router/router.go @@ -0,0 +1,459 @@ +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ + +/* +Package router implements the main interception logic of s3proxy. +It decides which packages to forward and which to intercept. + +The routing logic in this file is taken from this blog post: https://benhoyt.com/writings/go-routing/#regex-switch. +We should be able to replace this once this is part of the stdlib: https://github.com/golang/go/issues/61410. +*/ +package router + +import ( + "bytes" + "crypto/md5" + "crypto/sha256" + "encoding/base64" + "encoding/xml" + "fmt" + "io" + "net/http" + "net/url" + "regexp" + "strconv" + "strings" + "sync" + "time" + + "github.com/edgelesssys/constellation/v2/internal/logger" + "github.com/edgelesssys/constellation/v2/s3proxy/internal/s3" +) + +var ( + regexen = make(map[string]*regexp.Regexp) + relock sync.Mutex +) + +// Router implements the interception logic for the s3proxy. +type Router struct { + region string + log *logger.Logger +} + +// New creates a new Router. +func New(region string, log *logger.Logger) Router { + return Router{region: region, log: log} +} + +// Serve implements the routing logic for the s3 proxy. +// It intercepts GetObject and PutObject requests, encrypting/decrypting their bodies if necessary. +// All other requests are forwarded to the S3 API. +// Ideally we could separate routing logic, request handling and s3 interactions. +// Currently routing logic and request handling are integrated. +func (r Router) Serve(w http.ResponseWriter, req *http.Request) { + var h http.Handler + var key string + var bucket string + + client, err := s3.NewClient(r.region) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + path := req.URL.Path + switch { + // intercept GetObject. + case containsBucket(req.Host) && match(path, "/(.+)", &key) && req.Method == "GET" && !isGetObjectX(req.URL.Query()): + // BUCKET.s3.REGION.amazonaws.com + parts := strings.Split(req.Host, ".") + bucket := parts[0] + + obj := object{ + client: client, + key: key, + bucket: bucket, + query: req.URL.Query(), + log: r.log, + } + h = get(obj.get) + case !containsBucket(req.Host) && match(path, "/([^/?]+)/(.+)", &bucket, &key) && req.Method == "GET" && !isGetObjectX(req.URL.Query()): + obj := object{ + client: client, + key: key, + bucket: bucket, + query: req.URL.Query(), + log: r.log, + } + h = get(obj.get) + + case containsBucket(req.Host) && match(path, "/(.+)", &key) && req.Method == "PUT" && !isUnwantedPutEndpoint(req.Header, req.URL.Query()): + // BUCKET.s3.REGION.amazonaws.com + parts := strings.Split(req.Host, ".") + bucket := parts[0] + + r.log.Debugf("intercepting", "path", req.URL.Path, "method", req.Method, "host", req.Host) + body, err := io.ReadAll(req.Body) + if err != nil { + r.log.Errorf("PutObject", "error", err) + 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. + if clientDigest != "" && clientDigest != serverDigest { + r.log.Debugf("PutObject", "error", "x-amz-content-sha256 mismatch") + // The S3 API responds with an XML formatted error message. + mismatchErr := NewContentSHA256MismatchError(clientDigest, serverDigest) + marshalled, err := xml.Marshal(mismatchErr) + if err != nil { + r.log.Errorf("PutObject", "error", err) + 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 { + r.log.Errorf("parsing lock retention time", "data", raw, "error", err.Error()) + 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 { + r.log.Errorf("validating content md5", "error", err.Error()) + 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, + log: r.log, + } + + h = put(obj.put) + + case !containsBucket(req.Host) && match(path, "/([^/?]+)/(.+)", &bucket, &key) && req.Method == "PUT" && !isUnwantedPutEndpoint(req.Header, req.URL.Query()): + r.log.Debugf("intercepting", "path", req.URL.Path, "method", req.Method, "host", req.Host) + body, err := io.ReadAll(req.Body) + if err != nil { + r.log.Errorf("PutObject", "error", err) + 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. + if clientDigest != "" && clientDigest != serverDigest { + r.log.Debugf("PutObject", "error", "x-amz-content-sha256 mismatch") + // The S3 API responds with an XML formatted error message. + mismatchErr := NewContentSHA256MismatchError(clientDigest, serverDigest) + marshalled, err := xml.Marshal(mismatchErr) + if err != nil { + r.log.Errorf("PutObject", "error", err) + 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 { + r.log.Errorf("parsing lock retention time", "data", raw, "error", err.Error()) + 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 { + r.log.Errorf("validating content md5", "error", err.Error()) + 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, + log: r.log, + } + + h = put(obj.put) + + // Forward all other requests. + default: + r.log.Debugf("forwarding", "path", req.URL.Path, "method", req.Method, "host", req.Host, "headers", req.Header) + + newReq := repackage(req) + + httpClient := http.DefaultClient + resp, err := httpClient.Do(&newReq) + if err != nil { + r.log.Errorf("do request", "error", err) + 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 { + r.log.Errorf("ReadAll", "error", err) + 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 { + r.log.Errorf("Write", "error", err) + http.Error(w, fmt.Sprintf("writing body: %s", err.Error()), http.StatusInternalServerError) + return + } + return + } + h.ServeHTTP(w, req) +} + +// ContentSHA256MismatchError is a helper struct to create an XML formatted error message. +// s3 clients might try to parse error messages, so we need to serve correctly formatted messages. +type ContentSHA256MismatchError struct { + XMLName xml.Name `xml:"Error"` + Code string `xml:"Code"` + Message string `xml:"Message"` + ClientComputedContentSHA256 string `xml:"ClientComputedContentSHA256"` + S3ComputedContentSHA256 string `xml:"S3ComputedContentSHA256"` +} + +// NewContentSHA256MismatchError creates a new ContentSHA256MismatchError. +func NewContentSHA256MismatchError(clientComputedContentSHA256, s3ComputedContentSHA256 string) ContentSHA256MismatchError { + return ContentSHA256MismatchError{ + Code: "XAmzContentSHA256Mismatch", + Message: "The provided 'x-amz-content-sha256' header does not match what was computed.", + ClientComputedContentSHA256: clientComputedContentSHA256, + S3ComputedContentSHA256: s3ComputedContentSHA256, + } +} + +// containsBucket is a helper to recognizes cases where the bucket name is sent as part of the host. +// In other cases the bucket name is sent as part of the path. +func containsBucket(host string) bool { + parts := strings.Split(host, ".") + return len(parts) > 4 +} + +// isGetObjectX returns true if the request is any of these requests: GetObjectAcl, GetObjectAttributes, GetObjectLegalHold, GetObjectRetention, GetObjectTagging, GetObjectTorrent, ListParts. +// These requests are all structured similarly: they all have a query param that is not present in GetObject. +// Otherwise those endpoints are similar to GetObject. +func isGetObjectX(query url.Values) bool { + _, acl := query["acl"] + _, attributes := query["attributes"] + _, legalHold := query["legal-hold"] + _, retention := query["retention"] + _, tagging := query["tagging"] + _, torrent := query["torrent"] + _, uploadID := query["uploadId"] + + return acl || attributes || legalHold || retention || tagging || torrent || uploadID +} + +// isUnwantedPutEndpoint returns true if the request is any of these requests: UploadPart, PutObjectTagging. +// These requests are all structured similarly: they all have a query param that is not present in PutObject. +// Otherwise those endpoints are similar to PutObject. +func isUnwantedPutEndpoint(header http.Header, query url.Values) bool { + if header.Get("x-amz-copy-source") != "" { + return true + } + + _, partNumber := query["partNumber"] + _, uploadID := query["uploadId"] + _, tagging := query["tagging"] + _, legalHold := query["legal-hold"] + _, objectLock := query["object-lock"] + _, retention := query["retention"] + _, publicAccessBlock := query["publicAccessBlock"] + _, acl := query["acl"] + + return partNumber || uploadID || tagging || legalHold || objectLock || retention || publicAccessBlock || acl +} + +func sha256sum(data []byte) string { + digest := sha256.Sum256(data) + return fmt.Sprintf("%x", digest) +} + +// getMetadataHeaders parses user-defined metadata headers from a +// http.Header object. Users can define custom headers by taking +// HEADERNAME and prefixing it with "x-amz-meta-". +func getMetadataHeaders(header http.Header) map[string]string { + result := map[string]string{} + + for key := range header { + key = strings.ToLower(key) + + if strings.HasPrefix(key, "x-amz-meta-") { + name := strings.TrimPrefix(key, "x-amz-meta-") + result[name] = strings.Join(header.Values(key), ",") + } + } + + return result +} + +func parseRetentionTime(raw string) (time.Time, error) { + if raw == "" { + return time.Time{}, nil + } + return time.Parse(time.RFC3339, raw) +} + +// repackage implements all modifications we need to do to an incoming request that we want to forward to the s3 API. +func repackage(r *http.Request) http.Request { + req := r.Clone(r.Context()) + + // HTTP clients are not supposed to set this field, however when we receive a request it is set. + // So, we unset it. + req.RequestURI = "" + + req.URL.Host = r.Host + // We always want to use HTTPS when talking to S3. + req.URL.Scheme = "https" + + return *req +} + +// validateContentMD5 checks if the content-md5 header matches the body. +func validateContentMD5(contentMD5 string, body []byte) error { + if contentMD5 == "" { + return nil + } + + expected, err := base64.StdEncoding.DecodeString(contentMD5) + if err != nil { + return fmt.Errorf("decoding base64: %w", err) + } + + if len(expected) != 16 { + return fmt.Errorf("content-md5 must be 16 bytes long, got %d bytes", len(expected)) + } + + actual := md5.Sum(body) + + if !bytes.Equal(actual[:], expected) { + return fmt.Errorf("content-md5 mismatch, header is %x, body is %x", expected, actual) + } + + return nil +} + +// match reports whether path matches pattern, and if it matches, +// assigns any capture groups to the *string or *int vars. +func match(path, pattern string, vars ...interface{}) bool { + regex := mustCompileCached(pattern) + matches := regex.FindStringSubmatch(path) + if len(matches) <= 0 { + return false + } + for i, match := range matches[1:] { + switch p := vars[i].(type) { + case *string: + *p = match + case *int: + n, err := strconv.Atoi(match) + if err != nil { + return false + } + *p = n + default: + panic("vars must be *string or *int") + } + } + return true +} + +func mustCompileCached(pattern string) *regexp.Regexp { + relock.Lock() + defer relock.Unlock() + + regex := regexen[pattern] + if regex == nil { + regex = regexp.MustCompile("^" + pattern + "$") + regexen[pattern] = regex + } + return regex +} + +// allowMethod takes a HandlerFunc and wraps it in a handler that only +// responds if the request method is the given method, otherwise it +// responds with HTTP 405 Method Not Allowed. +func allowMethod(h http.HandlerFunc, method string) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if method != r.Method { + w.Header().Set("Allow", method) + http.Error(w, "405 method not allowed", http.StatusMethodNotAllowed) + return + } + h(w, r) + } +} + +// get takes a HandlerFunc and wraps it to only allow the GET method. +func get(h http.HandlerFunc) http.HandlerFunc { + return allowMethod(h, "GET") +} + +// put takes a HandlerFunc and wraps it to only allow the POST method. +func put(h http.HandlerFunc) http.HandlerFunc { + return allowMethod(h, "PUT") +} diff --git a/s3proxy/internal/router/router_test.go b/s3proxy/internal/router/router_test.go new file mode 100644 index 0000000000..2a51b9da0d --- /dev/null +++ b/s3proxy/internal/router/router_test.go @@ -0,0 +1,48 @@ +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ +package router + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidateContentMD5(t *testing.T) { + tests := map[string]struct { + contentMD5 string + body []byte + expectedErrMsg string + }{ + "empty content-md5": { + contentMD5: "", + body: []byte("hello, world"), + }, + // https://datatracker.ietf.org/doc/html/rfc1864#section-2 + "valid content-md5": { + contentMD5: "Q2hlY2sgSW50ZWdyaXR5IQ==", + body: []byte("Check Integrity!"), + }, + "invalid content-md5": { + contentMD5: "invalid base64", + body: []byte("hello, world"), + expectedErrMsg: "decoding base64", + }, + } + + // Iterate over the test cases + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + // Call the validateContentMD5 function + err := validateContentMD5(tc.contentMD5, tc.body) + + // Check the result against the expected value + if tc.expectedErrMsg != "" { + assert.ErrorContains(t, err, tc.expectedErrMsg) + } + }) + } +} diff --git a/s3proxy/internal/s3/BUILD.bazel b/s3proxy/internal/s3/BUILD.bazel new file mode 100644 index 0000000000..e095631974 --- /dev/null +++ b/s3proxy/internal/s3/BUILD.bazel @@ -0,0 +1,13 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "s3", + srcs = ["s3.go"], + importpath = "github.com/edgelesssys/constellation/v2/s3proxy/internal/s3", + visibility = ["//s3proxy:__subpackages__"], + deps = [ + "@com_github_aws_aws_sdk_go_v2_config//:config", + "@com_github_aws_aws_sdk_go_v2_service_s3//:s3", + "@com_github_aws_aws_sdk_go_v2_service_s3//types", + ], +) diff --git a/s3proxy/internal/s3/s3.go b/s3proxy/internal/s3/s3.go new file mode 100644 index 0000000000..25aca2970b --- /dev/null +++ b/s3proxy/internal/s3/s3.go @@ -0,0 +1,88 @@ +package s3 + +import ( + "bytes" + "context" + "crypto/md5" + "encoding/base64" + "fmt" + "time" + + "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/aws-sdk-go-v2/service/s3/types" +) + +// Client is a wrapper around the AWS S3 client. +type Client struct { + s3client *s3.Client +} + +// NewClient creates a new AWS S3 client. +func NewClient(region string) (*Client, error) { + // Use context.Background here because this context will not influence the later operations of the client. + // The context given here is used for http requests that are made during client construction. + // Client construction happens once during proxy setup. + clientCfg, err := config.LoadDefaultConfig( + context.Background(), + config.WithRegion(region), + ) + if err != nil { + return nil, fmt.Errorf("loading AWS S3 client config: %w", err) + } + + client := s3.NewFromConfig(clientCfg) + + return &Client{client}, nil +} + +// GetObject returns the object with the given key from the given bucket. +// If a versionID is given, the specific version of the object is returned. +func (c Client) GetObject(ctx context.Context, bucket, key, versionID string) (*s3.GetObjectOutput, error) { + getObjectInput := &s3.GetObjectInput{ + Bucket: &bucket, + Key: &key, + } + if versionID != "" { + getObjectInput.VersionId = &versionID + } + + return c.s3client.GetObject(ctx, getObjectInput) +} + +// PutObject creates a new object in the given bucket with the given key and body. +// Various optional parameters can be set. +func (c Client) PutObject(ctx context.Context, bucket, key, tags, contentType, objectLockLegalHoldStatus, objectLockMode string, objectLockRetainUntilDate time.Time, metadata map[string]string, body []byte) (*s3.PutObjectOutput, error) { + // The AWS Go SDK has two versions. V1 does not set the Content-Type header. + // V2 always sets the Content-Type header. We use V2. + // The s3 API sets an object's content-type to binary/octet-stream if + // it receives a request without a Content-Type header set. + // Since a client using V1 may depend on the Content-Type binary/octet-stream + // we have to explicitly emulate the S3 API behavior, if we receive a request + // without a Content-Type. + if contentType == "" { + contentType = "binary/octet-stream" + } + + contentMD5 := md5.Sum(body) + encodedContentMD5 := base64.StdEncoding.EncodeToString(contentMD5[:]) + + putObjectInput := &s3.PutObjectInput{ + Bucket: &bucket, + Key: &key, + Body: bytes.NewReader(body), + Tagging: &tags, + Metadata: metadata, + ContentMD5: &encodedContentMD5, + ContentType: &contentType, + ObjectLockLegalHoldStatus: types.ObjectLockLegalHoldStatus(objectLockLegalHoldStatus), + } + + // It is not allowed to only set one of these two properties. + if objectLockMode != "" && !objectLockRetainUntilDate.IsZero() { + putObjectInput.ObjectLockMode = types.ObjectLockMode(objectLockMode) + putObjectInput.ObjectLockRetainUntilDate = &objectLockRetainUntilDate + } + + return c.s3client.PutObject(ctx, putObjectInput) +}