2017-05-28 08:19:01 +00:00
|
|
|
package b2
|
|
|
|
|
|
|
|
import (
|
2023-04-15 08:25:45 +00:00
|
|
|
"os"
|
2017-05-28 08:19:01 +00:00
|
|
|
"path"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
"github.com/restic/restic/internal/options"
|
2023-04-21 19:51:58 +00:00
|
|
|
"github.com/restic/restic/internal/restic"
|
2017-05-28 08:19:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains all configuration necessary to connect to an b2 compatible
|
|
|
|
// server.
|
|
|
|
type Config struct {
|
|
|
|
AccountID string
|
2021-08-04 20:56:18 +00:00
|
|
|
Key options.SecretString
|
2017-05-28 08:19:01 +00:00
|
|
|
Bucket string
|
|
|
|
Prefix string
|
|
|
|
|
2017-06-05 22:17:21 +00:00
|
|
|
Connections uint `option:"connections" help:"set a limit for the number of concurrent connections (default: 5)"`
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig returns a new config with default options applied.
|
|
|
|
func NewConfig() Config {
|
|
|
|
return Config{
|
|
|
|
Connections: 5,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
options.Register("b2", Config{})
|
|
|
|
}
|
|
|
|
|
|
|
|
var bucketName = regexp.MustCompile("^[a-zA-Z0-9-]+$")
|
|
|
|
|
|
|
|
// checkBucketName tests the bucket name against the rules at
|
|
|
|
// https://help.backblaze.com/hc/en-us/articles/217666908-What-you-need-to-know-about-B2-Bucket-names
|
|
|
|
func checkBucketName(name string) error {
|
|
|
|
if name == "" {
|
2022-11-27 17:09:59 +00:00
|
|
|
return errors.New("bucket name not found")
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) < 6 {
|
|
|
|
return errors.New("bucket name is too short")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(name) > 50 {
|
|
|
|
return errors.New("bucket name is too long")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bucketName.MatchString(name) {
|
|
|
|
return errors.New("bucket name contains invalid characters, allowed are: a-z, 0-9, dash (-)")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseConfig parses the string s and extracts the b2 config. The supported
|
|
|
|
// configuration format is b2:bucketname/prefix. If no prefix is given the
|
|
|
|
// prefix "restic" will be used.
|
2023-04-21 19:35:34 +00:00
|
|
|
func ParseConfig(s string) (*Config, error) {
|
2017-05-28 08:19:01 +00:00
|
|
|
if !strings.HasPrefix(s, "b2:") {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, errors.New("invalid format, want: b2:bucket-name[:path]")
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = s[3:]
|
2022-11-27 17:09:59 +00:00
|
|
|
bucket, prefix, _ := strings.Cut(s, ":")
|
|
|
|
if err := checkBucketName(bucket); err != nil {
|
2023-04-21 19:35:34 +00:00
|
|
|
return nil, err
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 17:09:59 +00:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
prefix = strings.TrimPrefix(path.Clean(prefix), "/")
|
2017-05-28 08:19:01 +00:00
|
|
|
}
|
|
|
|
|
2022-11-27 17:09:59 +00:00
|
|
|
cfg := NewConfig()
|
|
|
|
cfg.Bucket = bucket
|
|
|
|
cfg.Prefix = prefix
|
|
|
|
|
2023-04-21 19:35:34 +00:00
|
|
|
return &cfg, nil
|
2017-05-28 08:19:01 +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-06-08 13:28:07 +00:00
|
|
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
2023-04-15 08:25:45 +00:00
|
|
|
if cfg.AccountID == "" {
|
2023-04-21 19:51:58 +00:00
|
|
|
cfg.AccountID = os.Getenv(prefix + "B2_ACCOUNT_ID")
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|
|
|
|
if cfg.Key.String() == "" {
|
2023-04-21 19:51:58 +00:00
|
|
|
cfg.Key = options.NewSecretString(os.Getenv(prefix + "B2_ACCOUNT_KEY"))
|
2023-04-15 08:25:45 +00:00
|
|
|
}
|
|
|
|
}
|