ability to use keys from envs

This commit is contained in:
cupcakearmy 2021-10-25 17:36:06 +02:00
parent a1abe13a39
commit 02a8e461d4
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
4 changed files with 42 additions and 10 deletions

1
go.mod
View File

@ -6,6 +6,7 @@ require (
github.com/blang/semver/v4 v4.0.0 github.com/blang/semver/v4 v4.0.0
github.com/buger/goterm v1.0.0 github.com/buger/goterm v1.0.0
github.com/fatih/color v1.10.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/mitchellh/go-homedir v1.1.0
github.com/robfig/cron v1.2.0 github.com/robfig/cron v1.2.0
github.com/spf13/cobra v1.1.3 github.com/spf13/cobra v1.1.3

2
go.sum
View File

@ -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/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 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= 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/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= 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= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=

View File

@ -58,7 +58,15 @@ func (b Backend) generateRepo() (string, error) {
func (b Backend) getEnv() (map[string]string, error) { func (b Backend) getEnv() (map[string]string, error) {
env := make(map[string]string) env := make(map[string]string)
if b.Key != "" {
env["RESTIC_PASSWORD"] = 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() repo, err := b.generateRepo()
env["RESTIC_REPOSITORY"] = repo env["RESTIC_REPOSITORY"] = repo
for key, value := range b.Env { for key, value := range b.Env {
@ -77,6 +85,16 @@ func generateRandomKey() string {
return key 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 { func (b Backend) validate() error {
if b.Type == "" { if b.Type == "" {
return fmt.Errorf(`Backend "%s" has no "type"`, b.name) return fmt.Errorf(`Backend "%s" has no "type"`, b.name)
@ -85,6 +103,9 @@ func (b Backend) validate() error {
return fmt.Errorf(`Backend "%s" has no "path"`, b.name) return fmt.Errorf(`Backend "%s" has no "path"`, b.name)
} }
if b.Key == "" { if b.Key == "" {
// Check if key is set in environment
if _, err := b.getKey(); err != nil {
// If not generate a new one
key := generateRandomKey() key := generateRandomKey()
b.Key = key b.Key = key
c := GetConfig() c := GetConfig()
@ -95,6 +116,7 @@ func (b Backend) validate() error {
return err return err
} }
} }
}
env, err := b.getEnv() env, err := b.getEnv()
if err != nil { if err != nil {
return err return err

View File

@ -10,6 +10,7 @@ import (
"github.com/cupcakearmy/autorestic/internal/colors" "github.com/cupcakearmy/autorestic/internal/colors"
"github.com/cupcakearmy/autorestic/internal/lock" "github.com/cupcakearmy/autorestic/internal/lock"
"github.com/joho/godotenv"
"github.com/mitchellh/go-homedir" "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -36,7 +37,13 @@ func GetConfig() *Config {
if err := viper.ReadInConfig(); err == nil { if err := viper.ReadInConfig(); err == nil {
if !CRON_LEAN { if !CRON_LEAN {
absConfig, _ := filepath.Abs(viper.ConfigFileUsed()) 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 { } else {
return return