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"
|
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-20 20:40:21 +00:00
|
|
|
func ParseConfig(s string) (Config, error) {
|
2017-05-28 08:19:01 +00:00
|
|
|
if !strings.HasPrefix(s, "b2:") {
|
2023-04-20 20:40:21 +00:00
|
|
|
return Config{}, 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-20 20:40:21 +00:00
|
|
|
return Config{}, 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
|
|
|
|
|
2017-05-28 08:19:01 +00:00
|
|
|
return cfg, nil
|
|
|
|
}
|
2023-04-15 08:25:45 +00:00
|
|
|
|
|
|
|
// ApplyEnvironment saves values from the environment to the config.
|
|
|
|
func ApplyEnvironment(cfgRaw interface{}) error {
|
|
|
|
cfg := cfgRaw.(*Config)
|
|
|
|
if cfg.AccountID == "" {
|
|
|
|
cfg.AccountID = os.Getenv("B2_ACCOUNT_ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.AccountID == "" {
|
|
|
|
return errors.Fatalf("unable to open B2 backend: Account ID ($B2_ACCOUNT_ID) is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Key.String() == "" {
|
|
|
|
cfg.Key = options.NewSecretString(os.Getenv("B2_ACCOUNT_KEY"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.Key.String() == "" {
|
|
|
|
return errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|