Skip to content

Commit

Permalink
feat: allow for vault tocken cache (#21)
Browse files Browse the repository at this point in the history
* feat: allow for vault tocken cache

* feat: documentation update

* feat: changelog update
  • Loading branch information
dkyanakiev authored Dec 24, 2024
1 parent 902d1e6 commit 1610986
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## [0.1.9] - 2024-04-28

## Added

-- Vault cache token lookup

## [0.1.8] - 2024-04-28

## Fixed
Expand Down
7 changes: 7 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ Example: [`~/myuser/.vaul7y.yaml`](./examples/vaul7y.yaml)
Or alternatively pass a config file as an argument using `-c <path/file.yaml>`
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.
Expand Down
52 changes: 46 additions & 6 deletions internal/config/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1610986

Please sign in to comment.