Skip to content

Commit

Permalink
Fix #1351 Fail with useful error message when an invalid account name…
Browse files Browse the repository at this point in the history
… is used (#1403)

* state azure blobstorage: fail for invalid account name

Signed-off-by: Tim Burkert <burkert.tim@gmail.com>

* state azure blobstorage: Remove duplicated code and add error check for common entpoint

Signed-off-by: Tim Burkert <burkert.tim@gmail.com>

Co-authored-by: Looong Dai <long.dai@intel.com>
Co-authored-by: Yaron Schneider <schneider.yaron@live.com>
  • Loading branch information
3 people committed Jan 13, 2022
1 parent 3af57ca commit 44199ca
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
21 changes: 13 additions & 8 deletions state/azure/blobstorage/blobstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"context"
b64 "encoding/base64"
"fmt"
"net"
"net/url"
"strings"

Expand Down Expand Up @@ -98,17 +99,21 @@ func (r *StateStore) Init(metadata state.Metadata) error {
}
p := azblob.NewPipeline(credential, options)

var containerURL azblob.ContainerURL
var URL *url.URL
customEndpoint, ok := metadata.Properties[endpointKey]
if ok && customEndpoint != "" {
URL, parseErr := url.Parse(fmt.Sprintf("%s/%s/%s", customEndpoint, meta.accountName, meta.containerName))
if parseErr != nil {
return parseErr
}
containerURL = azblob.NewContainerURL(*URL, p)
URL, err = url.Parse(fmt.Sprintf("%s/%s/%s", customEndpoint, meta.accountName, meta.containerName))
} else {
URL, _ := url.Parse(fmt.Sprintf("https://%s.blob.%s/%s", meta.accountName, env.StorageEndpointSuffix, meta.containerName))
containerURL = azblob.NewContainerURL(*URL, p)
URL, err = url.Parse(fmt.Sprintf("https://%s.blob.%s/%s", meta.accountName, env.StorageEndpointSuffix, meta.containerName))
}
if err != nil {
return err
}
containerURL := azblob.NewContainerURL(*URL, p)

_, err = net.LookupHost(URL.Hostname())
if err != nil {
return err
}

ctx := context.Background()
Expand Down
10 changes: 10 additions & 0 deletions state/azure/blobstorage/blobstorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ func TestInit(t *testing.T) {
assert.NotNil(t, err)
assert.Equal(t, err, fmt.Errorf("missing or empty accountName field from metadata"))
})

t.Run("Init with invalid account name", func(t *testing.T) {
m.Properties = map[string]string{
"accountName": "invalid-account",
"accountKey": "e+Dnvl8EOxYxV94nurVaRQ==",
"containerName": "dapr",
}
err := s.Init(m)
assert.NotNil(t, err)
})
}

func TestGetBlobStorageMetaData(t *testing.T) {
Expand Down

0 comments on commit 44199ca

Please sign in to comment.