From f1877e721e6c383082f219f973d5ee6dd0ca7c60 Mon Sep 17 00:00:00 2001 From: "Agatha V. Lovelace" Date: Tue, 26 Sep 2023 08:56:52 +0200 Subject: [PATCH] feat: support reading REST credentials from env --- internal/backend/rest/config.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/internal/backend/rest/config.go b/internal/backend/rest/config.go index ba42a0220..8458b0df2 100644 --- a/internal/backend/rest/config.go +++ b/internal/backend/rest/config.go @@ -2,10 +2,12 @@ package rest import ( "net/url" + "os" "strings" "github.com/restic/restic/internal/errors" "github.com/restic/restic/internal/options" + "github.com/restic/restic/internal/restic" ) // Config contains all configuration necessary to connect to a REST server. @@ -70,3 +72,19 @@ func prepareURL(s string) string { } return s } + +var _ restic.ApplyEnvironmenter = &Config{} + +// ApplyEnvironment saves values from the environment to the config. +func (cfg *Config) ApplyEnvironment(prefix string) { + username := cfg.URL.User.Username() + _, pwdSet := cfg.URL.User.Password() + + // Only apply env variable values if neither username nor password are provided. + if username == "" && !pwdSet { + envName := os.Getenv(prefix + "RESTIC_REST_USERNAME") + envPwd := os.Getenv(prefix + "RESTIC_REST_PASSWORD") + + cfg.URL.User = url.UserPassword(envName, envPwd) + } +}