From 37a3bdb9f1157e6a94c4bb93cbc7aab02c6ccde9 Mon Sep 17 00:00:00 2001 From: Joakim Bygdell Date: Sun, 6 Nov 2022 10:54:17 +0100 Subject: [PATCH] Convert if-else cases to switches --- proxy.go | 71 ++++++++++++++++++++++++++++++++++------------------- userauth.go | 19 ++++++-------- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/proxy.go b/proxy.go index 2f88bd2..b61f1ce 100644 --- a/proxy.go +++ b/proxy.go @@ -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 } } @@ -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) } @@ -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") diff --git a/userauth.go b/userauth.go index dee598d..8b51143 100644 --- a/userauth.go +++ b/userauth.go @@ -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) @@ -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) @@ -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