Fix #1351 Fail with useful error message when an invalid account name 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>
This commit is contained in:
Gehhilfe 2022-01-13 18:02:07 +01:00 committed by GitHub
parent 3af57ca5b7
commit 44199ca4dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 8 deletions

View File

@ -40,6 +40,7 @@ import (
"context"
b64 "encoding/base64"
"fmt"
"net"
"net/url"
"strings"
@ -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()

View File

@ -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) {