Skip to content

Commit

Permalink
s3 getter checks key prefixes properly
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanuber committed Jan 5, 2017
1 parent aaacc34 commit df80767
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
6 changes: 4 additions & 2 deletions get_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func (g *S3Getter) ClientMode(u *url.URL) (ClientMode, error) {
return 0, err
}

if len(resp.Contents) > 1 {
return ClientModeDir, nil
for _, o := range resp.Contents {
if strings.HasPrefix(*o.Key, path+"/") {
return ClientModeDir, nil
}
}
return ClientModeFile, nil
}
Expand Down
32 changes: 30 additions & 2 deletions get_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func init() {
// We do the string concat below to avoid AWS autodetection of a key. This
// key is locked down an IAM policy that is read-only so we're purposely
// exposing it.
os.Setenv("AWS_ACCESS_KEY", "AKIAJCTNQ" + "IOBWAYXKGZA")
os.Setenv("AWS_SECRET_KEY", "jcQOTYdXNzU5MO" + "5ExqbE1U995dIfKCKQtiVobMvr")
os.Setenv("AWS_ACCESS_KEY", "AKIAJCTNQ"+"IOBWAYXKGZA")
os.Setenv("AWS_SECRET_KEY", "jcQOTYdXNzU5MO"+"5ExqbE1U995dIfKCKQtiVobMvr")
}

func TestS3Getter_impl(t *testing.T) {
Expand Down Expand Up @@ -106,3 +106,31 @@ func TestS3Getter_GetFile_notfound(t *testing.T) {
t.Fatalf("expected error, got none")
}
}

func TestS3ClientMode_file(t *testing.T) {
g := new(S3Getter)

// Check client mode on a key prefix with only a single key.
mode, err := g.ClientMode(
testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder"))
if err != nil {
t.Fatalf("err: %s", err)
}
if mode != ClientModeDir {
t.Fatal("expect ClientModeDir")
}
}

func TestS3ClientMode_dir(t *testing.T) {
g := new(S3Getter)

// Check client mode on a key prefix which contains sub-keys.
mode, err := g.ClientMode(
testURL("https://s3.amazonaws.com/hc-oss-test/go-getter/folder/main.tf"))
if err != nil {
t.Fatalf("err: %s", err)
}
if mode != ClientModeFile {
t.Fatal("expect ClientModeFile")
}
}

0 comments on commit df80767

Please sign in to comment.