2017-07-08 13:38:48 +00:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2023-04-15 08:25:45 +00:00
|
|
|
"os"
|
2017-07-08 13:38:48 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
"github.com/restic/restic/internal/backend"
|
2017-08-05 19:46:15 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2017-07-08 13:38:48 +00:00
|
|
|
"github.com/restic/restic/internal/options"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains all configuration necessary to connect to an azure compatible
|
|
|
|
// server.
|
|
|
|
type Config struct {
|
2023-06-25 00:06:54 +00:00
|
|
|
AccountName string
|
|
|
|
AccountSAS options.SecretString
|
|
|
|
AccountKey options.SecretString
|
|
|
|
EndpointSuffix string
|
|
|
|
Container string
|
|
|
|
Prefix string
|
2017-07-08 13:38:48 +00:00
|
|
|
|
2021-05-15 21:08:51 +00:00
|
|
|
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
|
2017-07-08 13:38:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig returns a new Config with the default values filled in.
|
|
|
|
func NewConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Connections: 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
options.Register("azure", Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the string s and extracts the azure config. The
|
|
|
|
// configuration format is azure:containerName:/[prefix].
|
2023-04-21 19:35:34 +00:00
|
|
|
func ParseConfig(s string) (*Config, error) {
|
2017-08-06 18:13:45 +00:00
|
|
|
if !strings.HasPrefix(s, "azure:") {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.New("azure: invalid format")
|
2017-07-08 13:38:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-06 18:13:45 +00:00
|
|
|
// strip prefix "azure:"
|
|
|
|
s = s[6:]
|
|
|
|
|
|
|
|
// use the first entry of the path as the bucket name and the
|
|
|
|
// remainder as prefix
|
2022-11-27 17:09:59 +00:00
|
|
|
container, prefix, colon := strings.Cut(s, ":")
|
|
|
|
if !colon {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.New("azure: invalid format: bucket name or path not found")
|
2017-07-08 13:38:48 +00:00
|
|
|
}
|
2022-11-27 17:09:59 +00:00
|
|
|
prefix = strings.TrimPrefix(path.Clean(prefix), "/")
|
2017-08-06 18:13:45 +00:00
|
|
|
cfg := NewConfig()
|
|
|
|
cfg.Container = container
|
2022-11-27 17:09:59 +00:00
|
|
|
cfg.Prefix = prefix
|
2023-04-21 19:35:34 +00:00
|
|
|
return &cfg, nil
|
2017-07-08 13:38:48 +00:00
|
|
|
}
|
2023-04-15 08:25:45 +00:00
|
|
|
|
2023-10-01 09:40:12 +00:00
|
|
|
var _ backend.ApplyEnvironmenter = &Config{}
|
2023-04-21 19:51:58 +00:00
|
|
|
|
2023-04-15 08:25:45 +00:00
|
|
|
// ApplyEnvironment saves values from the environment to the config.
|
2023-06-08 13:28:07 +00:00
|
|
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
2023-04-15 08:25:45 +00:00
|
|
|
if cfg.AccountName == "" {
|
2023-04-21 19:51:58 +00:00
|
|
|
cfg.AccountName = os.Getenv(prefix + "AZURE_ACCOUNT_NAME")
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.AccountKey.String() == "" {
|
2023-04-21 19:51:58 +00:00
|
|
|
cfg.AccountKey = options.NewSecretString(os.Getenv(prefix + "AZURE_ACCOUNT_KEY"))
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.AccountSAS.String() == "" {
|
2023-04-21 19:51:58 +00:00
|
|
|
cfg.AccountSAS = options.NewSecretString(os.Getenv(prefix + "AZURE_ACCOUNT_SAS"))
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|
2023-07-07 21:09:44 +00:00
|
|
|
|
2023-06-25 00:06:54 +00:00
|
|
|
if cfg.EndpointSuffix == "" {
|
2023-07-07 21:09:44 +00:00
|
|
|
cfg.EndpointSuffix = os.Getenv(prefix + "AZURE_ENDPOINT_SUFFIX")
|
2023-06-25 00:06:54 +00:00
|
|
|
}
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|