-
Notifications
You must be signed in to change notification settings - Fork 952
[Tables] connection string parser #15187
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
Changes from 5 commits
da7f308
4f2de5f
c94e1ce
bbe00cd
d967e57
c7da82f
4621f6c
ded6f8b
3842a4d
20ef734
0d28805
fdac8d6
95786a5
044733c
f405a90
99de9b0
f28e3f6
3f4cc57
2a3d5e5
48204fc
bb276fb
34871e6
cc7a769
7357bbf
1a40990
c88ae43
25fdd90
c41de6e
fbf1d9a
7c16a48
542360b
9a54e06
bb7978a
3e21e20
8194878
9c095bd
532c74e
caa14e8
ca09207
4125fd0
c125e2c
315a190
3b7e68d
8c9c9d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package aztable | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
| ) | ||
|
|
||
| var ErrConnectionString = errors.New("connection string is either blank or malformed") | ||
|
|
||
| func NewTableClientFromConnectionString(tableName string, connectionString string, options *TableClientOptions) (*TableClient, error) { | ||
| endpoint, credential, err := parseConnectionString(connectionString) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return NewTableClient(tableName, endpoint, *credential, options) | ||
| } | ||
|
|
||
| func NewTableServiceClientFrommConnectionString(connectionString string, options *TableClientOptions) (*TableServiceClient, error) { | ||
seankane-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| endpoint, credential, err := parseConnectionString(connectionString) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return NewTableServiceClient(endpoint, *credential, options) | ||
| } | ||
|
|
||
| func convertConnStrToMap(connStr string) (map[string]string, error) { | ||
| ret := make(map[string]string) | ||
| connStr = strings.TrimRight(connStr, ";") | ||
|
|
||
| splitString := strings.Split(connStr, ";") | ||
| if len(splitString) == 0 { | ||
| return ret, ErrConnectionString | ||
| } | ||
| for _, stringPart := range splitString { | ||
| parts := strings.Split(stringPart, "=") | ||
seankane-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if len(parts) != 2 { | ||
| return ret, ErrConnectionString | ||
| } | ||
| ret[parts[0]] = parts[1] | ||
| } | ||
| return ret, nil | ||
| } | ||
|
|
||
| func parseConnectionString(connStr string) (string, *azcore.Credential, error) { | ||
seankane-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var serviceURL string | ||
| var cred azcore.Credential | ||
|
|
||
| connStrMap, err := convertConnStrToMap(connStr) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| accountName, ok := connStrMap["AccountName"] | ||
| if !ok { | ||
| return "", nil, ErrConnectionString | ||
| } | ||
| accountKey, ok := connStrMap["AccountKey"] | ||
| if !ok { | ||
| return "", nil, ErrConnectionString | ||
| } | ||
|
|
||
| if accountName == "" || accountKey == "" { | ||
| // Try sharedaccesssignature | ||
| sharedAccessSignature, ok := connStrMap["sharedaccesssignature"] | ||
seankane-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if !ok { | ||
| return serviceURL, nil, ErrConnectionString | ||
| } | ||
| return sharedAccessSignature, nil, errors.New("there is not support for SharedAccessSignature yet") | ||
seankane-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // cred = azcore.SharedAccessSignature(sharedAccessSignature) | ||
| // TODO: fix this when shared access signatures are added. | ||
| } | ||
| defaultProtocol, ok := connStrMap["DefaultEndpointsProtocol"] | ||
| if !ok { | ||
| defaultProtocol = "https" | ||
| } | ||
|
|
||
| endpointSuffix, ok := connStrMap["EndpointSuffix"] | ||
| if !ok { | ||
| endpointSuffix = "core.windows.net" | ||
| } | ||
|
|
||
| tableEndpoint, ok := connStrMap["TableEndpoint"] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like you can combine some of this and just set 'tableEndpoint' to either their custom one or the serviceURL you formed below (and return only once). Unless there's something else interesting you have planned where they'll diverge more. |
||
| if ok { | ||
| cred, err = NewSharedKeyCredential(accountName, accountKey) | ||
| return tableEndpoint, &cred, err | ||
| } | ||
| serviceURL = fmt.Sprintf("%v://%v.table.%v", defaultProtocol, accountName, endpointSuffix) | ||
|
|
||
| cred, err = NewSharedKeyCredential(accountName, accountKey) | ||
| if err != nil { | ||
| return "", nil, err | ||
| } | ||
|
|
||
| return serviceURL, &cred, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need tests for azurite connection strings also, since they do some different things with the account name. |
||
| // Licensed under the MIT License. | ||
|
|
||
| package aztable | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConnectionStringParser(t *testing.T) { | ||
| require := require.New(t) | ||
|
|
||
| connStr := "DefaultEndpointsProtocol=https;AccountName=dummyaccount;AccountKey=secretkeykey;EndpointSuffix=core.windows.net" | ||
seankane-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| serviceURL, cred, err := parseConnectionString(connStr) | ||
| require.NoError(err) | ||
| require.Equal(serviceURL, "https://dummyaccount.table.core.windows.net") | ||
| require.NotNil(cred) | ||
| } | ||
|
|
||
| func TestConnectionStringParserHTTP(t *testing.T) { | ||
| require := require.New(t) | ||
|
|
||
| connStr := "DefaultEndpointsProtocol=http;AccountName=dummyaccount;AccountKey=secretkeykey;EndpointSuffix=core.windows.net" | ||
| serviceURL, cred, err := parseConnectionString(connStr) | ||
| require.NoError(err) | ||
| require.Equal(serviceURL, "http://dummyaccount.table.core.windows.net") | ||
| require.NotNil(cred) | ||
seankane-msft marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func TestConnectionStringParserBasic(t *testing.T) { | ||
| require := require.New(t) | ||
|
|
||
| connStr := "AccountName=dummyaccount;AccountKey=secretkeykey" | ||
| serviceURL, cred, err := parseConnectionString(connStr) | ||
| require.NoError(err) | ||
| require.Equal(serviceURL, "https://dummyaccount.table.core.windows.net") | ||
| require.NotNil(cred) | ||
| } | ||
|
|
||
| func TestConnectionStringParserCustomDomain(t *testing.T) { | ||
| require := require.New(t) | ||
|
|
||
| connStr := "AccountName=dummyaccount;AccountKey=secretkeykey;TableEndpoint=www.mydomain.com;" | ||
seankane-msft marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| serviceURL, cred, err := parseConnectionString(connStr) | ||
| require.NoError(err) | ||
| require.Equal(serviceURL, "www.mydomain.com") | ||
| require.NotNil(cred) | ||
| } | ||
|
|
||
| func TestConnectionStringParserInvalid(t *testing.T) { | ||
| badConnectionStrings := []string{ | ||
| "", | ||
| "foobar", | ||
| "foo;bar;baz", | ||
| "foo=;bar=;", | ||
| "=", | ||
| ";", | ||
| "=;==", | ||
| "foobar=baz=foo" | ||
| } | ||
| require := require.New(t) | ||
|
|
||
| for _, badConnStr := range badConnectionStrings { | ||
| _, _, err := parseConnectionString(badConnStr) | ||
| require.Error(err) | ||
| require.Contains(err.Error(), ErrConnectionString.Error()) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.