Skip to content

Commit

Permalink
Fixed key encoding when addressing S3 Access Points (#1167)
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail authored Mar 12, 2021
1 parent 76f2f55 commit 60c5cdf
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ID": "service.s3-bugfix-1615508971377612000",
"SchemaVersion": 1,
"Module": "service/s3",
"Type": "bugfix",
"Description": "Fixed key encoding when addressing S3 Access Points",
"MinVersion": "",
"AffectedModules": null
}
13 changes: 9 additions & 4 deletions service/s3/internal/customizations/update_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package customizations
import (
"context"
"fmt"
"github.com/aws/smithy-go/encoding/httpbinding"
"log"
"net/url"
"strings"
Expand Down Expand Up @@ -229,14 +230,18 @@ func moveBucketNameToHost(u *url.URL, bucket string) {

// remove bucket from url
func removeBucketFromPath(u *url.URL, bucket string) {
// modify url path
u.Path = strings.Replace(u.Path, "/"+bucket, "", -1)
if strings.HasPrefix(u.Path, "/"+bucket) {
// modify url path
u.Path = strings.Replace(u.Path, "/"+bucket, "", 1)

// modify url raw path
u.RawPath = strings.Replace(u.RawPath, "/"+httpbinding.EscapePath(bucket, true), "", 1)
}

if u.Path == "" {
u.Path = "/"
}

// modify url raw path
u.RawPath = strings.Replace(u.RawPath, "/"+bucket, "", -1)
if u.RawPath == "" {
u.RawPath = "/"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package customizations

import (
"net/url"
"strconv"
"testing"
)

func TestRemoveBucketFromPath(t *testing.T) {
cases := []struct {
url url.URL
bucket string
expected string
}{
{
url: url.URL{
Scheme: "https",
Host: "amazonaws.com",
Path: "/bucket-name/key/path",
RawPath: "/bucket-name/key/path",
},
bucket: "bucket-name",
expected: "https://amazonaws.com/key/path",
},
{
url: url.URL{
Scheme: "https",
Host: "amazonaws.com",
Path: "/bucket-name/key/path/with/bucket-name",
RawPath: "/bucket-name/key/path/with/bucket-name",
},
bucket: "bucket-name",
expected: "https://amazonaws.com/key/path/with/bucket-name",
},
{
url: url.URL{
Scheme: "https",
Host: "amazonaws.com",
Path: "/arn:aws:s3:us-east-1:012345678901:accesspoint:myap/key/path?isEscaped=true",
RawPath: "/arn%3Aaws%3As3%3Aus-east-1%3A012345678901%3Aaccesspoint%3Amyap/key/path%3FisEscaped%3Dtrue",
},
bucket: "arn:aws:s3:us-east-1:012345678901:accesspoint:myap",
expected: "https://amazonaws.com/key/path%3FisEscaped%3Dtrue",
},
{
url: url.URL{
Scheme: "https",
Host: "amazonaws.com",
Path: "/path/to/key",
RawPath: "/path/to/key",
},
bucket: "not-a-match",
expected: "https://amazonaws.com/path/to/key",
},
{
url: url.URL{
Scheme: "https",
Host: "amazonaws.com",
Path: "",
RawPath: "",
},
bucket: "not-a-match",
expected: "https://amazonaws.com/",
},
}

for i, tt := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
removeBucketFromPath(&tt.url, tt.bucket)

if e, a := tt.expected, tt.url.String(); e != a {
t.Errorf("expect %v, got %v", e, a)
}
})
}
}

0 comments on commit 60c5cdf

Please sign in to comment.