From 52391de5ee1223313ebd86c4db921c0f53b28410 Mon Sep 17 00:00:00 2001 From: Joakim Bygdell Date: Fri, 4 Nov 2022 12:11:34 +0100 Subject: [PATCH] Set username in message from JWT sub field --- proxy.go | 6 +----- proxy_test.go | 7 +++++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/proxy.go b/proxy.go index 1401144..2f88bd2 100644 --- a/proxy.go +++ b/proxy.go @@ -289,10 +289,6 @@ func (p *Proxy) detectRequestType(r *http.Request) S3RequestType { // CreateMessageFromRequest is a function that can take a http request and // figure out the correct rabbitmq message to send from it. func (p *Proxy) CreateMessageFromRequest(r *http.Request, claims jwt.MapClaims) (Event, error) { - // Extract username for request's url path - re := regexp.MustCompile("/[^/]+/([^/]+)/") - username := re.FindStringSubmatch(r.URL.Path)[1] - event := Event{} checksum := Checksum{} var err error @@ -305,7 +301,7 @@ func (p *Proxy) CreateMessageFromRequest(r *http.Request, claims jwt.MapClaims) // Case for simple upload event.Operation = "upload" event.Filepath = strings.Replace(r.URL.Path, "/"+p.s3.bucket+"/", "", 1) - event.Username = username + event.Username = fmt.Sprintf("%v", claims["sub"]) checksum.Type = "sha256" event.Checksum = []interface{}{checksum} log.Info("user ", event.Username, " with pilot ", claims["pilot"], " uploaded file ", event.Filepath, " with checksum ", checksum.Value, " at ", time.Now()) diff --git a/proxy_test.go b/proxy_test.go index 0403428..bbec2e9 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -299,6 +299,9 @@ func TestMessageFormatting(t *testing.T) { r.Header.Set("content-length", "1234") r.Header.Set("x-amz-content-sha256", "checksum") + claims := jwt.MapClaims{} + claims["sub"] = "user@host.domain" + s3conf := S3Config{ url: "http://localhost:9023", accessKey: "someAccess", @@ -310,13 +313,13 @@ func TestMessageFormatting(t *testing.T) { messenger := NewMockMessenger() proxy := NewProxy(s3conf, &AlwaysDeny{}, messenger, new(tls.Config)) f.resp = "test/user/new_file.txt12false/user/new_file.txt2020-03-10T13:20:15.000Z"0a44282bd39178db9680f24813c41aec-1"1234STANDARD" - msg, err := proxy.CreateMessageFromRequest(r, nil) + msg, err := proxy.CreateMessageFromRequest(r, claims) assert.Nil(t, err) assert.IsType(t, Event{}, msg) assert.Equal(t, int64(1234), msg.Filesize) assert.Equal(t, "user/new_file.txt", msg.Filepath) - assert.Equal(t, "user", msg.Username) + assert.Equal(t, "user@host.domain", msg.Username) c, _ := json.Marshal(msg.Checksum[0]) checksum := Checksum{}