Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Handle regions returned in error responses (#1117)
  • Loading branch information
harshavardhana authored and kannappanr committed Jun 5, 2019
1 parent e1cd915 commit 0c7b691
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
17 changes: 0 additions & 17 deletions api-list.go
Expand Up @@ -41,23 +41,6 @@ import (
func (c Client) ListBuckets() ([]BucketInfo, error) {
// Execute GET on service.
resp, err := c.executeMethod(context.Background(), "GET", requestMetadata{contentSHA256Hex: emptySHA256Hex})

if err != nil {
closeResponse(resp)
return nil, err
}
if resp != nil {
if resp.StatusCode != http.StatusOK {
errResponse := ToErrorResponse(httpRespToErrorResponse(resp, "", ""))
closeResponse(resp)
if errResponse.Code != "AuthorizationHeaderMalformed" && errResponse.Code != "InvalidRegion" {
return nil, httpRespToErrorResponse(resp, "", "")
}
// Retry with the region returned by the server.
resp, err = c.executeMethod(context.Background(), "GET", requestMetadata{contentSHA256Hex: emptySHA256Hex, bucketLocation: errResponse.Region})
}
}

defer closeResponse(resp)
if err != nil {
return nil, err
Expand Down
20 changes: 12 additions & 8 deletions api.go
Expand Up @@ -653,14 +653,23 @@ func (c Client) executeMethod(ctx context.Context, method string, metadata reque
//
// Additionally we should only retry if bucketLocation and custom
// region is empty.
if metadata.bucketLocation == "" && c.region == "" {
if errResponse.Code == "AuthorizationHeaderMalformed" || errResponse.Code == "InvalidRegion" {
if c.region == "" {
switch errResponse.Code {
case "AuthorizationHeaderMalformed":
fallthrough
case "InvalidRegion":
fallthrough
case "AccessDenied":
if metadata.bucketName != "" && errResponse.Region != "" {
// Gather Cached location only if bucketName is present.
if _, cachedOk := c.bucketLocCache.Get(metadata.bucketName); cachedOk {
c.bucketLocCache.Set(metadata.bucketName, errResponse.Region)
continue // Retry.
}
} else {
// Most probably for ListBuckets()
metadata.bucketLocation = errResponse.Region
continue // Retry
}
}
}
Expand Down Expand Up @@ -694,13 +703,8 @@ func (c Client) newRequest(method string, metadata requestMetadata) (req *http.R
// Gather location only if bucketName is present.
location, err = c.getBucketLocation(metadata.bucketName)
if err != nil {
if ToErrorResponse(err).Code != "AccessDenied" {
return nil, err
}
return nil, err
}
// Upon AccessDenied error on fetching bucket location, default
// to possible locations based on endpoint URL. This can usually
// happen when GetBucketLocation() is disabled using IAM policies.
}
if location == "" {
location = getDefaultLocation(*c.endpointURL, c.region)
Expand Down
12 changes: 10 additions & 2 deletions bucket-cache.go
Expand Up @@ -124,8 +124,16 @@ func processBucketLocationResponse(resp *http.Response, bucketName string) (buck
// For access denied error, it could be an anonymous
// request. Move forward and let the top level callers
// succeed if possible based on their policy.
if errResp.Code == "AccessDenied" {
return "us-east-1", nil
switch errResp.Code {
case "AuthorizationHeaderMalformed":
fallthrough
case "InvalidRegion":
fallthrough
case "AccessDenied":
if errResp.Region == "" {
return "us-east-1", nil
}
return errResp.Region, nil
}
return "", err
}
Expand Down

0 comments on commit 0c7b691

Please sign in to comment.