From 2f7b4ceae1fee236e961a392b170f4289633151e Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Sat, 15 Apr 2023 10:25:45 +0200 Subject: [PATCH] backend: Move environment based configuration into backend --- cmd/restic/global.go | 53 ++++++-------------------------- internal/backend/azure/config.go | 18 +++++++++++ internal/backend/b2/config.go | 22 +++++++++++++ internal/backend/gs/config.go | 10 ++++++ internal/backend/s3/config.go | 25 +++++++++++++++ 5 files changed, 84 insertions(+), 44 deletions(-) diff --git a/cmd/restic/global.go b/cmd/restic/global.go index 981f18bb2..bb0f8a570 100644 --- a/cmd/restic/global.go +++ b/cmd/restic/global.go @@ -559,24 +559,10 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro case "s3": cfg := loc.Config.(s3.Config) - if cfg.KeyID == "" { - cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID") - } - if cfg.Secret.String() == "" { - cfg.Secret = options.NewSecretString(os.Getenv("AWS_SECRET_ACCESS_KEY")) + if err := s3.ApplyEnvironment(&cfg); err != nil { + return nil, err } - - 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.Region == "" { - cfg.Region = os.Getenv("AWS_DEFAULT_REGION") - } - if err := opts.Apply(loc.Scheme, &cfg); err != nil { return nil, err } @@ -586,10 +572,10 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro case "gs": cfg := loc.Config.(gs.Config) - if cfg.ProjectID == "" { - cfg.ProjectID = os.Getenv("GOOGLE_PROJECT_ID") - } + if err := gs.ApplyEnvironment(&cfg); err != nil { + return nil, err + } if err := opts.Apply(loc.Scheme, &cfg); err != nil { return nil, err } @@ -599,18 +585,10 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro case "azure": cfg := loc.Config.(azure.Config) - if cfg.AccountName == "" { - cfg.AccountName = os.Getenv("AZURE_ACCOUNT_NAME") - } - if cfg.AccountKey.String() == "" { - cfg.AccountKey = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_KEY")) + if err := azure.ApplyEnvironment(&cfg); err != nil { + return nil, err } - - if cfg.AccountSAS.String() == "" { - cfg.AccountSAS = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_SAS")) - } - if err := opts.Apply(loc.Scheme, &cfg); err != nil { return nil, err } @@ -635,22 +613,9 @@ func parseConfig(loc location.Location, opts options.Options) (interface{}, erro case "b2": cfg := loc.Config.(b2.Config) - if cfg.AccountID == "" { - cfg.AccountID = os.Getenv("B2_ACCOUNT_ID") + if err := b2.ApplyEnvironment(&cfg); err != nil { + return nil, err } - - if cfg.AccountID == "" { - return nil, 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 nil, errors.Fatalf("unable to open B2 backend: Key ($B2_ACCOUNT_KEY) is empty") - } - if err := opts.Apply(loc.Scheme, &cfg); err != nil { return nil, err } diff --git a/internal/backend/azure/config.go b/internal/backend/azure/config.go index 55b26d4f1..771fb6c3c 100644 --- a/internal/backend/azure/config.go +++ b/internal/backend/azure/config.go @@ -1,6 +1,7 @@ package azure import ( + "os" "path" "strings" @@ -53,3 +54,20 @@ func ParseConfig(s string) (interface{}, error) { cfg.Prefix = prefix return cfg, nil } + +// ApplyEnvironment saves values from the environment to the config. +func ApplyEnvironment(cfgRaw interface{}) error { + cfg := cfgRaw.(*Config) + if cfg.AccountName == "" { + cfg.AccountName = os.Getenv("AZURE_ACCOUNT_NAME") + } + + if cfg.AccountKey.String() == "" { + cfg.AccountKey = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_KEY")) + } + + if cfg.AccountSAS.String() == "" { + cfg.AccountSAS = options.NewSecretString(os.Getenv("AZURE_ACCOUNT_SAS")) + } + return nil +} diff --git a/internal/backend/b2/config.go b/internal/backend/b2/config.go index ba5141834..6572b7dd0 100644 --- a/internal/backend/b2/config.go +++ b/internal/backend/b2/config.go @@ -1,6 +1,7 @@ package b2 import ( + "os" "path" "regexp" "strings" @@ -79,3 +80,24 @@ func ParseConfig(s string) (interface{}, error) { return cfg, nil } + +// 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 +} diff --git a/internal/backend/gs/config.go b/internal/backend/gs/config.go index 8bb2bddea..a81ae565c 100644 --- a/internal/backend/gs/config.go +++ b/internal/backend/gs/config.go @@ -1,6 +1,7 @@ package gs import ( + "os" "path" "strings" @@ -56,3 +57,12 @@ func ParseConfig(s string) (interface{}, error) { cfg.Prefix = prefix return cfg, nil } + +// ApplyEnvironment saves values from the environment to the config. +func ApplyEnvironment(cfgRaw interface{}) error { + cfg := cfgRaw.(*Config) + if cfg.ProjectID == "" { + cfg.ProjectID = os.Getenv("GOOGLE_PROJECT_ID") + } + return nil +} diff --git a/internal/backend/s3/config.go b/internal/backend/s3/config.go index 9050e20f4..681065b60 100644 --- a/internal/backend/s3/config.go +++ b/internal/backend/s3/config.go @@ -2,6 +2,7 @@ package s3 import ( "net/url" + "os" "path" "strings" @@ -91,3 +92,27 @@ func createConfig(endpoint, bucket, prefix string, useHTTP bool) (interface{}, e cfg.Prefix = prefix return cfg, nil } + +// ApplyEnvironment saves values from the environment to the config. +func ApplyEnvironment(cfgRaw interface{}) error { + cfg := cfgRaw.(*Config) + if cfg.KeyID == "" { + cfg.KeyID = os.Getenv("AWS_ACCESS_KEY_ID") + } + + if cfg.Secret.String() == "" { + cfg.Secret = options.NewSecretString(os.Getenv("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 == "" { + cfg.Region = os.Getenv("AWS_DEFAULT_REGION") + } + + return nil +}