diff --git a/CHANGELOG.md b/CHANGELOG.md index d5bf617..1cea8da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.1.9] - 2024-04-28 + +## Added + +-- Vault cache token lookup + ## [0.1.8] - 2024-04-28 ## Fixed diff --git a/docs/usage.md b/docs/usage.md index f04de47..f13a9ec 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -67,6 +67,13 @@ Example: [`~/myuser/.vaul7y.yaml`](./examples/vaul7y.yaml) Or alternatively pass a config file as an argument using `-c ` Example: `vaul7y -c ./new-env.yml` +#### Authentication and variables priority +Variables will be loaded in the following order, with the next superseding the previous ones: + +1. Will check for vault [token cache](https://developer.hashicorp.com/vault/docs/commands#authenticating-to-vault) +2. Read from env variables +3. Config file + ### Features Currently the capabilities are limited. diff --git a/internal/config/configs.go b/internal/config/configs.go index b30fa92..330499d 100644 --- a/internal/config/configs.go +++ b/internal/config/configs.go @@ -36,11 +36,14 @@ func LoadConfig(cfgFile string) Config { var data []byte if cfgFile == "" { - fmt.Println("No config file specified") yamlFilePath := filepath.Join(home, ".vaul7y.yaml") - data, err = os.ReadFile(yamlFilePath) - if err != nil { - fmt.Printf("Error reading YAML file: %v\n", err) + if _, err := os.Stat(yamlFilePath); os.IsNotExist(err) { + fmt.Printf("Config file does not exist: %s\n", yamlFilePath) + } else { + data, err = os.ReadFile(yamlFilePath) + if err != nil { + fmt.Printf("Error reading YAML file: %v\n", err) + } } } else { fmt.Println("Using config file: ", cfgFile) @@ -50,9 +53,29 @@ func LoadConfig(cfgFile string) Config { } } - err = yaml.Unmarshal(data, &config) + if data != nil { + err = yaml.Unmarshal(data, &config) + if err != nil { + fmt.Printf("Error parsing YAML file: %v\n", err) + } + } + + // Check for vault cache + home, err = os.UserHomeDir() if err != nil { - fmt.Printf("Error parsing YAML file: %v\n", err) + fmt.Println("Error getting user home directory") + } else { + vaultTokenPath := filepath.Join(home, ".vault-token") + if _, err := os.Stat(vaultTokenPath); os.IsNotExist(err) { + fmt.Printf("Vault token file does not exist: %s\n", vaultTokenPath) + } else { + data, err := os.ReadFile(vaultTokenPath) + if err != nil { + fmt.Printf("Error reading vault token file: %v\n", err) + } else { + config.VaultToken = string(data) + } + } } // Overwrite with environment variables if they are set @@ -89,6 +112,23 @@ func LoadConfig(cfgFile string) Config { } } + if config.VaultToken == "" { + home, err := os.UserHomeDir() + if err != nil { + fmt.Println("Error getting user home directory") + } else { + vaultTokenPath := filepath.Join(home, ".vault-token") + if _, err := os.Stat(vaultTokenPath); err == nil { + data, err := os.ReadFile(vaultTokenPath) + if err != nil { + fmt.Printf("Error reading vault token file: %v\n", err) + } else { + config.VaultToken = string(data) + } + } + } + } + if config.VaultAddr == "" { fmt.Println("VAULT_ADDR is not set. Please set it and try again.") os.Exit(1)