Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed key encoding when addressing S3 Access Points #1167

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}