From 02a8e461d435e53fe16f3aef9db0e436da176b49 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Mon, 25 Oct 2021 17:36:06 +0200 Subject: [PATCH] ability to use keys from envs --- go.mod | 1 + go.sum | 2 ++ internal/backend.go | 40 +++++++++++++++++++++++++++++++--------- internal/config.go | 9 ++++++++- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 9354fba..038ed53 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/blang/semver/v4 v4.0.0 github.com/buger/goterm v1.0.0 github.com/fatih/color v1.10.0 + github.com/joho/godotenv v1.4.0 github.com/mitchellh/go-homedir v1.1.0 github.com/robfig/cron v1.2.0 github.com/spf13/cobra v1.1.3 diff --git a/go.sum b/go.sum index 86b9403..d0c3565 100644 --- a/go.sum +++ b/go.sum @@ -101,6 +101,8 @@ github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2p github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc= github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= +github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= diff --git a/internal/backend.go b/internal/backend.go index 18b635e..a091357 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -58,7 +58,15 @@ func (b Backend) generateRepo() (string, error) { func (b Backend) getEnv() (map[string]string, error) { env := make(map[string]string) - env["RESTIC_PASSWORD"] = b.Key + if b.Key != "" { + env["RESTIC_PASSWORD"] = b.Key + } else { + key, err := b.getKey() + if err != nil { + return nil, err + } + env["RESTIC_PASSWORD"] = key + } repo, err := b.generateRepo() env["RESTIC_REPOSITORY"] = repo for key, value := range b.Env { @@ -77,6 +85,16 @@ func generateRandomKey() string { return key } +func (b Backend) getKey() (string, error) { + if b.Key != "" { + return b.Key, nil + } + if key, found := os.LookupEnv("AUTORESTIC_KEY_" + strings.ToUpper(b.name)); found { + return key, nil + } + return "", fmt.Errorf("no key found for backend \"%s\"", b.name) +} + func (b Backend) validate() error { if b.Type == "" { return fmt.Errorf(`Backend "%s" has no "type"`, b.name) @@ -85,14 +103,18 @@ func (b Backend) validate() error { return fmt.Errorf(`Backend "%s" has no "path"`, b.name) } if b.Key == "" { - key := generateRandomKey() - b.Key = key - c := GetConfig() - tmp := c.Backends[b.name] - tmp.Key = key - c.Backends[b.name] = tmp - if err := c.SaveConfig(); err != nil { - return err + // Check if key is set in environment + if _, err := b.getKey(); err != nil { + // If not generate a new one + key := generateRandomKey() + b.Key = key + c := GetConfig() + tmp := c.Backends[b.name] + tmp.Key = key + c.Backends[b.name] = tmp + if err := c.SaveConfig(); err != nil { + return err + } } } env, err := b.getEnv() diff --git a/internal/config.go b/internal/config.go index b4e353b..1c548cf 100644 --- a/internal/config.go +++ b/internal/config.go @@ -10,6 +10,7 @@ import ( "github.com/cupcakearmy/autorestic/internal/colors" "github.com/cupcakearmy/autorestic/internal/lock" + "github.com/joho/godotenv" "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -36,7 +37,13 @@ func GetConfig() *Config { if err := viper.ReadInConfig(); err == nil { if !CRON_LEAN { absConfig, _ := filepath.Abs(viper.ConfigFileUsed()) - colors.Faint.Println("Using config file:", absConfig) + colors.Faint.Println("Using config: \t", absConfig) + // Load env file + envFile := filepath.Join(filepath.Dir(absConfig), ".autorestic.env") + err = godotenv.Load(envFile) + if err == nil { + colors.Faint.Println("Using env:\t", envFile) + } } } else { return