mirror of
https://github.com/octoleo/restic.git
synced 2024-11-25 22:27:35 +00:00
b2/s3: Move config validation from ApplyEnvironment to Open/Create
Conceptually the backend configuration should be validated when creating or opening the backend, but not when filling in information from environment variables into the configuration.
This commit is contained in:
parent
19ac12d95b
commit
3a3cf608f5
@ -550,9 +550,7 @@ func OpenRepository(ctx context.Context, opts GlobalOptions) (*repository.Reposi
|
|||||||
func parseConfig(loc location.Location, opts options.Options) (interface{}, error) {
|
func parseConfig(loc location.Location, opts options.Options) (interface{}, error) {
|
||||||
cfg := loc.Config
|
cfg := loc.Config
|
||||||
if cfg, ok := cfg.(restic.ApplyEnvironmenter); ok {
|
if cfg, ok := cfg.(restic.ApplyEnvironmenter); ok {
|
||||||
if err := cfg.ApplyEnvironment(""); err != nil {
|
cfg.ApplyEnvironment("")
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// only apply options for a particular backend here
|
// only apply options for a particular backend here
|
||||||
|
@ -35,11 +35,7 @@ func newAzureTestSuite(t testing.TB) *test.Suite[azure.Config] {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cfg.ApplyEnvironment("RESTIC_TEST_")
|
cfg.ApplyEnvironment("RESTIC_TEST_")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
|
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
},
|
},
|
||||||
|
@ -59,7 +59,7 @@ func ParseConfig(s string) (*Config, error) {
|
|||||||
var _ restic.ApplyEnvironmenter = &Config{}
|
var _ restic.ApplyEnvironmenter = &Config{}
|
||||||
|
|
||||||
// ApplyEnvironment saves values from the environment to the config.
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
||||||
if cfg.AccountName == "" {
|
if cfg.AccountName == "" {
|
||||||
cfg.AccountName = os.Getenv(prefix + "AZURE_ACCOUNT_NAME")
|
cfg.AccountName = os.Getenv(prefix + "AZURE_ACCOUNT_NAME")
|
||||||
}
|
}
|
||||||
@ -71,5 +71,4 @@ func (cfg *Config) ApplyEnvironment(prefix string) error {
|
|||||||
if cfg.AccountSAS.String() == "" {
|
if cfg.AccountSAS.String() == "" {
|
||||||
cfg.AccountSAS = options.NewSecretString(os.Getenv(prefix + "AZURE_ACCOUNT_SAS"))
|
cfg.AccountSAS = options.NewSecretString(os.Getenv(prefix + "AZURE_ACCOUNT_SAS"))
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,13 @@ func (s *sniffingRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newClient(ctx context.Context, cfg Config, rt http.RoundTripper) (*b2.Client, error) {
|
func newClient(ctx context.Context, cfg Config, rt http.RoundTripper) (*b2.Client, error) {
|
||||||
|
if cfg.AccountID == "" {
|
||||||
|
return nil, errors.Fatalf("unable to open B2 backend: Account ID ($B2_ACCOUNT_ID) is empty")
|
||||||
|
}
|
||||||
|
if cfg.Key.String() == "" {
|
||||||
|
return nil, errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty")
|
||||||
|
}
|
||||||
|
|
||||||
sniffer := &sniffingRoundTripper{RoundTripper: rt}
|
sniffer := &sniffingRoundTripper{RoundTripper: rt}
|
||||||
opts := []b2.ClientOption{b2.Transport(sniffer)}
|
opts := []b2.ClientOption{b2.Transport(sniffer)}
|
||||||
|
|
||||||
|
@ -35,11 +35,7 @@ func newB2TestSuite(t testing.TB) *test.Suite[b2.Config] {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cfg.ApplyEnvironment("RESTIC_TEST_")
|
cfg.ApplyEnvironment("RESTIC_TEST_")
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
|
cfg.Prefix = fmt.Sprintf("test-%d", time.Now().UnixNano())
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
},
|
},
|
||||||
|
@ -85,21 +85,11 @@ func ParseConfig(s string) (*Config, error) {
|
|||||||
var _ restic.ApplyEnvironmenter = &Config{}
|
var _ restic.ApplyEnvironmenter = &Config{}
|
||||||
|
|
||||||
// ApplyEnvironment saves values from the environment to the config.
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
||||||
if cfg.AccountID == "" {
|
if cfg.AccountID == "" {
|
||||||
cfg.AccountID = os.Getenv(prefix + "B2_ACCOUNT_ID")
|
cfg.AccountID = os.Getenv(prefix + "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() == "" {
|
if cfg.Key.String() == "" {
|
||||||
cfg.Key = options.NewSecretString(os.Getenv(prefix + "B2_ACCOUNT_KEY"))
|
cfg.Key = options.NewSecretString(os.Getenv(prefix + "B2_ACCOUNT_KEY"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Key.String() == "" {
|
|
||||||
return errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -62,9 +62,8 @@ func ParseConfig(s string) (*Config, error) {
|
|||||||
var _ restic.ApplyEnvironmenter = &Config{}
|
var _ restic.ApplyEnvironmenter = &Config{}
|
||||||
|
|
||||||
// ApplyEnvironment saves values from the environment to the config.
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
||||||
if cfg.ProjectID == "" {
|
if cfg.ProjectID == "" {
|
||||||
cfg.ProjectID = os.Getenv(prefix + "GOOGLE_PROJECT_ID")
|
cfg.ProjectID = os.Getenv(prefix + "GOOGLE_PROJECT_ID")
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -97,24 +97,14 @@ func createConfig(endpoint, bucket, prefix string, useHTTP bool) (*Config, error
|
|||||||
var _ restic.ApplyEnvironmenter = &Config{}
|
var _ restic.ApplyEnvironmenter = &Config{}
|
||||||
|
|
||||||
// ApplyEnvironment saves values from the environment to the config.
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
||||||
if cfg.KeyID == "" {
|
if cfg.KeyID == "" {
|
||||||
cfg.KeyID = os.Getenv(prefix + "AWS_ACCESS_KEY_ID")
|
cfg.KeyID = os.Getenv(prefix + "AWS_ACCESS_KEY_ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Secret.String() == "" {
|
if cfg.Secret.String() == "" {
|
||||||
cfg.Secret = options.NewSecretString(os.Getenv(prefix + "AWS_SECRET_ACCESS_KEY"))
|
cfg.Secret = options.NewSecretString(os.Getenv(prefix + "AWS_SECRET_ACCESS_KEY"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.KeyID == "" && cfg.Secret.String() != "" {
|
|
||||||
return errors.Fatalf("unable to open S3 backend: Key ID ($AWS_ACCESS_KEY_ID) is empty")
|
|
||||||
} else if cfg.KeyID != "" && cfg.Secret.String() == "" {
|
|
||||||
return errors.Fatalf("unable to open S3 backend: Secret ($AWS_SECRET_ACCESS_KEY) is empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
if cfg.Region == "" {
|
if cfg.Region == "" {
|
||||||
cfg.Region = os.Getenv(prefix + "AWS_DEFAULT_REGION")
|
cfg.Region = os.Getenv(prefix + "AWS_DEFAULT_REGION")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,12 @@ const defaultLayout = "default"
|
|||||||
func open(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
|
func open(ctx context.Context, cfg Config, rt http.RoundTripper) (*Backend, error) {
|
||||||
debug.Log("open, config %#v", cfg)
|
debug.Log("open, config %#v", cfg)
|
||||||
|
|
||||||
|
if cfg.KeyID == "" && cfg.Secret.String() != "" {
|
||||||
|
return nil, errors.Fatalf("unable to open S3 backend: Key ID ($AWS_ACCESS_KEY_ID) is empty")
|
||||||
|
} else if cfg.KeyID != "" && cfg.Secret.String() == "" {
|
||||||
|
return nil, errors.Fatalf("unable to open S3 backend: Secret ($AWS_SECRET_ACCESS_KEY) is empty")
|
||||||
|
}
|
||||||
|
|
||||||
if cfg.MaxRetries > 0 {
|
if cfg.MaxRetries > 0 {
|
||||||
minio.MaxRetry = int(cfg.MaxRetries)
|
minio.MaxRetry = int(cfg.MaxRetries)
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ func ParseConfig(s string) (*Config, error) {
|
|||||||
var _ restic.ApplyEnvironmenter = &Config{}
|
var _ restic.ApplyEnvironmenter = &Config{}
|
||||||
|
|
||||||
// ApplyEnvironment saves values from the environment to the config.
|
// ApplyEnvironment saves values from the environment to the config.
|
||||||
func (cfg *Config) ApplyEnvironment(prefix string) error {
|
func (cfg *Config) ApplyEnvironment(prefix string) {
|
||||||
for _, val := range []struct {
|
for _, val := range []struct {
|
||||||
s *string
|
s *string
|
||||||
env string
|
env string
|
||||||
@ -130,5 +130,4 @@ func (cfg *Config) ApplyEnvironment(prefix string) error {
|
|||||||
*val.s = options.NewSecretString(os.Getenv(val.env))
|
*val.s = options.NewSecretString(os.Getenv(val.env))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
@ -48,9 +48,7 @@ func newSwiftTestSuite(t testing.TB) *test.Suite[swift.Config] {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cfg.ApplyEnvironment("RESTIC_TEST_"); err != nil {
|
cfg.ApplyEnvironment("RESTIC_TEST_")
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano())
|
cfg.Prefix += fmt.Sprintf("/test-%d", time.Now().UnixNano())
|
||||||
t.Logf("using prefix %v", cfg.Prefix)
|
t.Logf("using prefix %v", cfg.Prefix)
|
||||||
return cfg, nil
|
return cfg, nil
|
||||||
|
@ -83,5 +83,5 @@ type FileInfo struct {
|
|||||||
|
|
||||||
// ApplyEnvironmenter fills in a backend configuration from the environment
|
// ApplyEnvironmenter fills in a backend configuration from the environment
|
||||||
type ApplyEnvironmenter interface {
|
type ApplyEnvironmenter interface {
|
||||||
ApplyEnvironment(prefix string) error
|
ApplyEnvironment(prefix string)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user