Merge pull request #4480 from AgathaSorceress/add-rest-auth-env

Support reading basic auth credentials for REST server from environment variables
This commit is contained in:
Michael Eischer
2023-10-21 17:41:08 +00:00
committed by GitHub
4 changed files with 53 additions and 14 deletions

View File

@@ -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)
}
}