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 #294 from neicnordic/switches_instead_of_if-else
Browse files Browse the repository at this point in the history
Convert if-else cases to switches
  • Loading branch information
dbampalikis authored Nov 14, 2022
2 parents 482ef9d + 37a3bdb commit eba5676
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
71 changes: 46 additions & 25 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,23 @@ func (p *Proxy) allowedResponse(w http.ResponseWriter, r *http.Request) {
func (p *Proxy) uploadFinishedSuccessfully(req *http.Request, response *http.Response) bool {
if response.StatusCode != 200 {
return false
} else if req.Method == http.MethodPut && !strings.Contains(req.URL.String(), "partNumber") {
return true
} else if req.Method == http.MethodPost && strings.Contains(req.URL.String(), "uploadId") {
return true
} else {
}

switch req.Method {
case http.MethodPut:
if !strings.Contains(req.URL.String(), "partNumber") {
return true
}

return false
case http.MethodPost:
if strings.Contains(req.URL.String(), "uploadId") {
return true
}

return false
default:

return false
}
}
Expand Down Expand Up @@ -190,19 +202,25 @@ func (p *Proxy) prependBucketToHostPath(r *http.Request) {
log.Debugf("incoming raw: %s", r.URL.RawQuery)

// Restructure request to query the users folder instead of the general bucket
if r.Method == http.MethodGet && strings.Contains(r.URL.String(), "?delimiter") {
r.URL.Path = "/" + bucket + "/"
if strings.Contains(r.URL.RawQuery, "&prefix") {
params := strings.Split(r.URL.RawQuery, "&prefix=")
r.URL.RawQuery = params[0] + "&prefix=" + username + "%2F" + params[1]
} else {
r.URL.RawQuery = r.URL.RawQuery + "&prefix=" + username + "%2F"
switch r.Method {
case http.MethodGet:
if strings.Contains(r.URL.String(), "?delimiter") {
r.URL.Path = "/" + bucket + "/"
if strings.Contains(r.URL.RawQuery, "&prefix") {
params := strings.Split(r.URL.RawQuery, "&prefix=")
r.URL.RawQuery = params[0] + "&prefix=" + username + "%2F" + params[1]
} else {
r.URL.RawQuery = r.URL.RawQuery + "&prefix=" + username + "%2F"
}
log.Debug("new Raw Query: ", r.URL.RawQuery)
} else if strings.Contains(r.URL.String(), "?location") || strings.Contains(r.URL.String(), "&prefix") {
r.URL.Path = "/" + bucket + "/"
log.Debug("new Path: ", r.URL.Path)
}
log.Debug("new Raw Query: ", r.URL.RawQuery)
} else if r.Method == http.MethodGet && (strings.Contains(r.URL.String(), "?location") || strings.Contains(r.URL.String(), "&prefix")) {
r.URL.Path = "/" + bucket + "/"
case http.MethodPost:
r.URL.Path = "/" + bucket + r.URL.Path
log.Debug("new Path: ", r.URL.Path)
} else if r.Method == http.MethodPost || r.Method == http.MethodPut {
case http.MethodPut:
r.URL.Path = "/" + bucket + r.URL.Path
log.Debug("new Path: ", r.URL.Path)
}
Expand Down Expand Up @@ -236,44 +254,47 @@ func (p *Proxy) resignHeader(r *http.Request, accessKey string, secretKey string
func (p *Proxy) detectRequestType(r *http.Request) S3RequestType {
switch r.Method {
case http.MethodGet:
if strings.HasSuffix(r.URL.String(), "/") {
switch {
case strings.HasSuffix(r.URL.String(), "/"):
log.Debug("detect Get")

return Get
} else if strings.Contains(r.URL.String(), "?acl") {
case strings.Contains(r.URL.String(), "?acl"):
log.Debug("detect Policy")

return Policy
} else {
default:
log.Debug("detect List")

return List
}
case http.MethodDelete:
if strings.HasSuffix(r.URL.String(), "/") {
switch {
case strings.HasSuffix(r.URL.String(), "/"):
log.Debug("detect RemoveBucket")

return RemoveBucket
} else if strings.Contains(r.URL.String(), "uploadId") {
case strings.Contains(r.URL.String(), "uploadId"):
log.Debug("detect AbortMultipart")

return AbortMultipart
} else {
default:
// Do we allow deletion of files?
log.Debug("detect Delete")

return Delete
}
case http.MethodPut:
if strings.HasSuffix(r.URL.String(), "/") {
switch {
case strings.HasSuffix(r.URL.String(), "/"):
log.Debug("detect MakeBucket")

return MakeBucket
} else if strings.Contains(r.URL.String(), "?policy") {
case strings.Contains(r.URL.String(), "?policy"):
log.Debug("detect Policy")

return Policy
} else {
default:
// Should decide if we will handle copy here or through authentication
log.Debug("detect Put")

Expand Down
19 changes: 7 additions & 12 deletions userauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ func (u *ValidateFromToken) Authenticate(r *http.Request) (claims jwt.MapClaims,
log.Debugf("Looking for key for %s", strIss)

re := regexp.MustCompile(`//([^/]*)`)
//nolint:nestif
if token.Header["alg"] == "ES256" {
switch token.Header["alg"] {
case "ES256":
key, err := jwt.ParseECPublicKeyFromPEM(u.pubkeys[re.FindStringSubmatch(strIss)[1]])
if err != nil {
return nil, fmt.Errorf("failed to parse EC public key (%v)", err)
Expand All @@ -200,7 +200,7 @@ func (u *ValidateFromToken) Authenticate(r *http.Request) (claims jwt.MapClaims,
if err != nil && v.Errors != jwt.ValidationErrorExpired {
return nil, fmt.Errorf("signed token (ES256) not valid: %v, (token was %s)", err, tokenStr)
}
} else if token.Header["alg"] == "RS256" {
case "RS256":
key, err := jwt.ParseRSAPublicKeyFromPEM(u.pubkeys[re.FindStringSubmatch(strIss)[1]])
if err != nil {
return nil, fmt.Errorf("failed to parse RSA256 public key (%v)", err)
Expand All @@ -212,26 +212,21 @@ func (u *ValidateFromToken) Authenticate(r *http.Request) (claims jwt.MapClaims,
if err != nil && v.Errors != jwt.ValidationErrorExpired {
return nil, fmt.Errorf("signed token (RS256) not valid: %v, (token was %s)", err, tokenStr)
}
} else {
default:
return nil, fmt.Errorf("unsupported algorithm %s", token.Header["alg"])
}

// Check whether token username and filepath match
re = regexp.MustCompile("/([^/]+)/")
username := re.FindStringSubmatch(r.URL.Path)[1]
//nolint:nestif
// Case for Elixir and CEGA usernames: Replace @ with _ character
if strings.Contains(fmt.Sprintf("%v", claims["sub"]), "@") {
claimString := fmt.Sprintf("%v", claims["sub"])
if strings.ReplaceAll(claimString, "@", "_") != username {
return nil, fmt.Errorf("token supplied username %s but URL had %s",
claims["sub"], username)
}
} else {
if claims["sub"] != username {
return nil, fmt.Errorf("token supplied username %s but URL had %s",
claims["sub"], username)
return nil, fmt.Errorf("token supplied username %s but URL had %s", claims["sub"], username)
}
} else if claims["sub"] != username {
return nil, fmt.Errorf("token supplied username %s but URL had %s", claims["sub"], username)
}

return claims, nil
Expand Down

0 comments on commit eba5676

Please sign in to comment.