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"
|
|
|
|
|
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"
|
2023-04-21 19:51:58 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-07-08 13:38:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains all configuration necessary to connect to an azure compatible
|
|
|
|
// server.
|
|
|
|
type Config struct {
|
|
|
|
AccountName string
|
2022-03-05 18:16:13 +00:00
|
|
|
AccountSAS options.SecretString
|
2021-08-04 20:56:18 +00:00
|
|
|
AccountKey options.SecretString
|
2017-07-08 13:38:48 +00:00
|
|
|
Container string
|
|
|
|
Prefix string
|
|
|
|
|
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-04-21 19:51:58 +00:00
|
|
|
var _ restic.ApplyEnvironmenter = &Config{}
|
|
|
|
|
2023-04-15 08:25:45 +00:00
|
|
|
// ApplyEnvironment saves values from the environment to the config.
|
2023-04-21 19:51:58 +00:00
|
|
|
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
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
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|