Skip to content
This repository has been archived by the owner on Jun 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #290 from neicnordic/user_from_sub
Browse files Browse the repository at this point in the history
Set username in message from JWT sub field
  • Loading branch information
dbampalikis authored Nov 4, 2022
2 parents 0686961 + 52391de commit e9db635
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
6 changes: 1 addition & 5 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand Down
7 changes: 5 additions & 2 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"] = "[email protected]"

s3conf := S3Config{
url: "http://localhost:9023",
accessKey: "someAccess",
Expand All @@ -310,13 +313,13 @@ func TestMessageFormatting(t *testing.T) {
messenger := NewMockMessenger()
proxy := NewProxy(s3conf, &AlwaysDeny{}, messenger, new(tls.Config))
f.resp = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>test</Name><Prefix>/user/new_file.txt</Prefix><KeyCount>1</KeyCount><MaxKeys>2</MaxKeys><Delimiter></Delimiter><IsTruncated>false</IsTruncated><Contents><Key>/user/new_file.txt</Key><LastModified>2020-03-10T13:20:15.000Z</LastModified><ETag>&#34;0a44282bd39178db9680f24813c41aec-1&#34;</ETag><Size>1234</Size><Owner><ID></ID><DisplayName></DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>"
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{}
Expand Down

0 comments on commit e9db635

Please sign in to comment.