Skip to content

Commit

Permalink
Allow authentication using connection string when targeting Azure Blo…
Browse files Browse the repository at this point in the history
…b Storage (#383)

* Allow authentication using connection string when targeting Azure Blob Storage

* Bail on ambiguous configuration
  • Loading branch information
m90 authored Mar 8, 2024
1 parent e8562b1 commit baf34ec
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions cmd/backup/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type Config struct {
LockTimeout time.Duration `split_words:"true" default:"60m"`
AzureStorageAccountName string `split_words:"true"`
AzureStoragePrimaryAccountKey string `split_words:"true"`
AzureStorageConnectionString string `split_words:"true"`
AzureStorageContainerName string `split_words:"true"`
AzureStoragePath string `split_words:"true"`
AzureStorageEndpoint string `split_words:"true" default:"https://{{ .AccountName }}.blob.core.windows.net/"`
Expand Down
1 change: 1 addition & 0 deletions cmd/backup/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (s *script) init() error {
PrimaryAccountKey: s.c.AzureStoragePrimaryAccountKey,
Endpoint: s.c.AzureStorageEndpoint,
RemotePath: s.c.AzureStoragePath,
ConnectionString: s.c.AzureStorageConnectionString,
}
azureBackend, err := azure.NewStorageBackend(azureConfig, logFunc)
if err != nil {
Expand Down
9 changes: 8 additions & 1 deletion docs/reference/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,17 @@ You can populate below template according to your requirements and use it as you
# AZURE_STORAGE_ACCOUNT_NAME="account-name"
# The credential's primary account key when using Azure Blob Storage. If this
# is not given, the command tries to fall back to using a managed identity.
# is not given, the command tries to fall back to using a connection string
# (if given) or a managed identity (if nothing is given).
# AZURE_STORAGE_PRIMARY_ACCOUNT_KEY="<xxx>"
# A connection string for accessing Azure Blob Storage. If this
# is not given, the command tries to fall back to using a primary account key
# (if given) or a managed identity (if nothing is given).
# AZURE_STORAGE_CONNECTION_STRING="<xxx>"
# The container name when using Azure Blob Storage.
# AZURE_STORAGE_CONTAINER_NAME="container-name"
Expand Down
14 changes: 12 additions & 2 deletions internal/storage/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ type Config struct {
AccountName string
ContainerName string
PrimaryAccountKey string
ConnectionString string
Endpoint string
RemotePath string
}

// NewStorageBackend creates and initializes a new Azure Blob Storage backend.
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
if opts.PrimaryAccountKey != "" && opts.ConnectionString != "" {
return nil, errwrap.Wrap(nil, "using primary account key and connection string are mutually exclusive")
}

endpointTemplate, err := template.New("endpoint").Parse(opts.Endpoint)
if err != nil {
return nil, errwrap.Wrap(err, "error parsing endpoint template")
Expand All @@ -58,7 +63,12 @@ func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error

client, err = azblob.NewClientWithSharedKeyCredential(normalizedEndpoint, cred, nil)
if err != nil {
return nil, errwrap.Wrap(err, "error creating Azure client")
return nil, errwrap.Wrap(err, "error creating azure client from primary account key")
}
} else if opts.ConnectionString != "" {
client, err = azblob.NewClientFromConnectionString(opts.ConnectionString, nil)
if err != nil {
return nil, errwrap.Wrap(err, "error creating azure client from connection string")
}
} else {
cred, err := azidentity.NewManagedIdentityCredential(nil)
Expand All @@ -67,7 +77,7 @@ func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error
}
client, err = azblob.NewClient(normalizedEndpoint, cred, nil)
if err != nil {
return nil, errwrap.Wrap(err, "error creating Azure client")
return nil, errwrap.Wrap(err, "error creating azure client from managed identity")
}
}

Expand Down

0 comments on commit baf34ec

Please sign in to comment.