Skip to content

Commit

Permalink
Support using connection string authentication for azure storage
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Holik <nholik@gmail.com>
  • Loading branch information
nholik committed Apr 28, 2023
1 parent d1711f1 commit af059d5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re
- [#41](https://github.com/thanos-io/objstore/pull/41) S3: Support S3 session token.
- [#43](https://github.com/thanos-io/objstore/pull/43) filesystem: abort filesystem bucket operations if the context has been cancelled
- [#44](https://github.com/thanos-io/objstore/pull/44) Add new metric to count total number of fetched bytes from bucket
- [#51](https://github.com/thanos-io/objstore/pull/51) azure: Support using connection string authentication.

### Changed
- [#38](https://github.com/thanos-io/objstore/pull/38) *: Upgrade minio-go version to `v7.0.45`.
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Bucket interface {
Upload(ctx context.Context, name string, r io.Reader) error

// Delete removes the object with the given name.
// If object does not exists in the moment of deletion, Delete should throw error.
// If object does not exist in the moment of deletion, Delete should throw error.
Delete(ctx context.Context, name string) error

```
Expand Down Expand Up @@ -152,6 +152,7 @@ config:
insecure: false
signature_version2: false
secret_key: ""
session_token: ""
put_user_metadata: {}
http_config:
idle_conn_timeout: 1m30s
Expand Down Expand Up @@ -416,6 +417,7 @@ type: AZURE
config:
storage_account: ""
storage_account_key: ""
storage_connection_string: ""
container: ""
endpoint: ""
user_assigned_id: ""
Expand Down Expand Up @@ -451,6 +453,8 @@ If `msi_resource` is used, authentication is done via system-assigned managed id

If `user_assigned_id` is used, authentication is done via user-assigned managed identity. When using `user_assigned_id` the `msi_resource` defaults to `https://<storage_account>.<endpoint>`

If `storage_connection_string` is set, the values of `storage_account` and `endpoint` values will not be used. Use this method over `storage_account_key` if you need to authenticate via a SAS token.

The generic `max_retries` will be used as value for the `pipeline_config`'s `max_tries` and `reader_config`'s `max_retry_requests`. For more control, `max_retries` could be ignored (0) and one could set specific retry values.

##### OpenStack Swift
Expand Down
27 changes: 18 additions & 9 deletions providers/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ var DefaultConfig = Config{

// Config Azure storage configuration.
type Config struct {
StorageAccountName string `yaml:"storage_account"`
StorageAccountKey string `yaml:"storage_account_key"`
ContainerName string `yaml:"container"`
Endpoint string `yaml:"endpoint"`
UserAssignedID string `yaml:"user_assigned_id"`
MaxRetries int `yaml:"max_retries"`
ReaderConfig ReaderConfig `yaml:"reader_config"`
PipelineConfig PipelineConfig `yaml:"pipeline_config"`
HTTPConfig exthttp.HTTPConfig `yaml:"http_config"`
StorageAccountName string `yaml:"storage_account"`
StorageAccountKey string `yaml:"storage_account_key"`
StorageConnectionString string `yaml:"storage_connection_string"`
ContainerName string `yaml:"container"`
Endpoint string `yaml:"endpoint"`
UserAssignedID string `yaml:"user_assigned_id"`
MaxRetries int `yaml:"max_retries"`
ReaderConfig ReaderConfig `yaml:"reader_config"`
PipelineConfig PipelineConfig `yaml:"pipeline_config"`
HTTPConfig exthttp.HTTPConfig `yaml:"http_config"`

// Deprecated: Is automatically set by the Azure SDK.
MSIResource string `yaml:"msi_resource"`
Expand All @@ -76,6 +77,14 @@ func (conf *Config) validate() error {
errMsg = append(errMsg, "user_assigned_id cannot be set when using storage_account_key authentication")
}

if conf.UserAssignedID != "" && conf.StorageConnectionString != "" {
errMsg = append(errMsg, "user_assigned_id cannot be set when using storage_connection_string authentication")
}

if conf.StorageAccountKey != "" && conf.StorageConnectionString != "" {
errMsg = append(errMsg, "storage_account_key and storage_connection_string cannot both be set")
}

if conf.StorageAccountName == "" {
errMsg = append(errMsg, "storage_account_name is required but not configured")
}
Expand Down
10 changes: 10 additions & 0 deletions providers/azure/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ container: "MyContainer"`),
wantFailParse: false,
wantFailValidate: false,
},
{
name: "Valid User Assigned and Connection String set",
config: []byte(`storage_account: "myAccount"
storage_account_key: ""
user_assigned_id: "1234-56578678-655"
storage_connection_string: "myConnectionString"
container: "MyContainer"`),
wantFailParse: false,
wantFailValidate: true,
},
}

func TestConfig_validate(t *testing.T) {
Expand Down
10 changes: 10 additions & 0 deletions providers/azure/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ func getContainerClient(conf Config) (*container.Client, error) {
Transport: &http.Client{Transport: dt},
},
}

// Use connection string if set
if conf.StorageConnectionString != "" {
containerClient, err := container.NewClientFromConnectionString(conf.StorageConnectionString, conf.ContainerName, opt)
if err != nil {
return nil, err
}
return containerClient, nil
}

containerURL := fmt.Sprintf("https://%s.%s/%s", conf.StorageAccountName, conf.Endpoint, conf.ContainerName)

// Use shared keys if set
Expand Down

0 comments on commit af059d5

Please sign in to comment.