Skip to content

add compatibility with CEPH S3 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
log "github.com/sirupsen/logrus"
)

var awsAuthorizationCredentialRegexp = regexp.MustCompile("Credential=([a-zA-Z0-9]+)/[0-9]+/([a-z]+-?[a-z]+-?[0-9]+)/s3/aws4_request")
// - new less strict regexp in order to allow different region naming (compatibility with other providers)
// - east-eu-1 => pass (aws style)
// - gra => pass (ceph style)
var awsAuthorizationCredentialRegexp = regexp.MustCompile("Credential=([a-zA-Z0-9]+)/[0-9]+/([a-zA-Z-0-9]+)/s3/aws4_request")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a code comment with sample values for the new less-strict regex.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok i will refactor this and add comments

var awsAuthorizationSignedHeadersRegexp = regexp.MustCompile("SignedHeaders=([a-zA-Z0-9;-]+)")

// Handler is a special handler that re-signs any AWS S3 request and sends it upstream
Expand Down Expand Up @@ -225,9 +227,16 @@ func (h *Handler) buildUpstreamRequest(req *http.Request) (*http.Request, error)
return nil, err
}

// WORKAROUND S3CMD which dont use white space before the some commas in the authorization header
fakeAuthorizationStr := fakeReq.Header.Get("Authorization")
// Sanitize fakeReq to remove white spaces before the comma signature
authorizationStr := strings.Replace(req.Header["Authorization"][0],",Signature",", Signature",1)
// Sanitize fakeReq to remove white spaces before the comma signheaders
authorizationStr = strings.Replace(authorizationStr,",SignedHeaders",", SignedHeaders",1)

// Verify that the fake request and the incoming request have the same signature
// This ensures it was sent and signed by a client with correct AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY
cmpResult := subtle.ConstantTimeCompare([]byte(fakeReq.Header["Authorization"][0]), []byte(req.Header["Authorization"][0]))
cmpResult := subtle.ConstantTimeCompare([]byte(fakeAuthorizationStr), []byte(authorizationStr))
if cmpResult == 0 {
v, _ := httputil.DumpRequest(fakeReq, false)
log.Debugf("Fake request: %v", string(v))
Expand Down
20 changes: 20 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ func verifySignature(w http.ResponseWriter, r *http.Request) {
signer.Sign(r, body, "s3", "eu-test-1", signTime)
expectedAuthorization := r.Header["Authorization"][0]

// WORKAROUND S3CMD who dont use white space before the comma in the authorization header
// Sanitize fakeReq to remove white spaces before the comma signature
receivedAuthorization = strings.Replace(receivedAuthorization,",Signature",", Signature",1)
// Sanitize fakeReq to remove white spaces before the comma signheaders
receivedAuthorization = strings.Replace(receivedAuthorization,",SignedHeaders",", SignedHeaders",1)

// verify signature
fmt.Fprintln(w, receivedAuthorization, expectedAuthorization)
if receivedAuthorization == expectedAuthorization {
Expand Down Expand Up @@ -143,6 +149,20 @@ func TestHandlerValidSignature(t *testing.T) {
assert.Equal(t, 200, resp.Code)
assert.Contains(t, resp.Body.String(), "Hello, client")
}
func TestHandlerValidSignatureS3cmd(t *testing.T) {
h := newTestProxy(t)

req := httptest.NewRequest(http.MethodGet, "http://foobar.example.com", nil)
signRequest(req)
authorizationReq := req.Header.Get("Authorization");
authorizationReq = strings.Replace(authorizationReq,", Signature",",Signature",1)
authorizationReq = strings.Replace(authorizationReq,", SignedHeaders",",SignedHeaders",1)
req.Header.Set("Authorization",authorizationReq);
Copy link
Owner

@Kriechi Kriechi Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please run go fmt or similar. The whitespace around the commas inside a string and as function argument is confusing to read + also fix indentation.

resp := httptest.NewRecorder()
h.ServeHTTP(resp, req)
assert.Equal(t, 200, resp.Code)
assert.Contains(t, resp.Body.String(), "Hello, client")
}

func TestHandlerInvalidCredential(t *testing.T) {
h := newTestProxy(t)
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ func NewAwsS3ReverseProxy(opts Options) (*Handler, error) {
}
return handler, nil
}

//handle /health path
func health(w http.ResponseWriter, req *http.Request){
fmt.Fprintf(w,"ok")
}
func main() {
opts := NewOptions()
handler, err := NewAwsS3ReverseProxy(opts)
Expand Down Expand Up @@ -136,6 +139,8 @@ func main() {
var wrappedHandler http.Handler = handler
if len(opts.MetricsListenAddr) > 0 && len(strings.Split(opts.MetricsListenAddr, ":")) == 2 {
metricsHandler := http.NewServeMux()
//add health on metrics http to serve k8s liveness
metricsHandler.HandleFunc("/health",health)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated changes - please send as separate PR - and address indentation + formatting.

metricsHandler.Handle("/metrics", promhttp.Handler())

log.Infof("Listening for secure Prometheus metrics on %s", opts.MetricsListenAddr)
Expand Down