1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2024-12-20 11:11:19 +00:00

Remove default values for file and vault

and add `must*` variants for them

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-02-29 17:55:20 +01:00
parent 3354a29311
commit fd2a37ca97
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
3 changed files with 53 additions and 41 deletions

View file

@ -16,8 +16,8 @@ Starting with `v1.0.0` Korvike is based on the [sprig functions collection](http
$ echo "{{ .foo }}" | korvike -v foo=bar $ echo "{{ .foo }}" | korvike -v foo=bar
bar bar
``` ```
- `{{ file <file name> [default value] }}` - `{{ file <file name> }}` / `{{ mustFile <file name> }}`
Read a file and place it inside the template Read a file and place it inside the template, `file` returns an empty string on error, `mustFile` an error
```console ```console
$ echo "Hello World" > hello $ echo "Hello World" > hello
$ echo '{{ file "hello" }}' | korvike $ echo '{{ file "hello" }}' | korvike
@ -43,8 +43,8 @@ Starting with `v1.0.0` Korvike is based on the [sprig functions collection](http
$ echo '{{ urlescape "Hellö Wörld@Golang" }}' | korvike $ echo '{{ urlescape "Hellö Wörld@Golang" }}' | korvike
Hell%C3%B6+W%C3%B6rld%40Golang Hell%C3%B6+W%C3%B6rld%40Golang
``` ```
- `{{ vault <path> <key> [default value] }}` - `{{ vault <path> <key> }}` / `{{ mustVault <path> <key> }}`
Read a key from Vault using `VAULT_ADDR` and `VAULT_TOKEN` environment variables (or `~/.vault-token` file) for authentication. Read a key from Vault using `VAULT_ADDR` and `VAULT_TOKEN` environment variables (or `~/.vault-token` file) for authentication. `vault` returns an empty string on error, `mustVault` an error
```console ```console
$ vault write secret/test foo=bar $ vault write secret/test foo=bar
$ echo '{{ vault "secret/test" "foo" }}' | korvike $ echo '{{ vault "secret/test" "foo" }}' | korvike

View file

@ -1,21 +1,27 @@
package functions package functions
import ( import (
"fmt"
"os" "os"
) )
func init() { func init() {
registerFunction("file", func(name string, v ...string) string { registerFunction("file", func(name string) string {
defaultValue := "" fc, err := tplReadFile(name)
if len(v) > 0 { if err != nil {
defaultValue = v[0] return ""
} }
if _, err := os.Stat(name); err == nil { return fc
//#nosec:G304 // Intended to load custom file
if rawValue, err := os.ReadFile(name); err == nil {
return string(rawValue)
}
}
return defaultValue
}) })
registerFunction("mustFile", tplReadFile)
}
func tplReadFile(name string) (string, error) {
rawValue, err := os.ReadFile(name) //#nosec:G304 // Intended to load custom file
if err != nil {
return "", fmt.Errorf("reading file: %w", err)
}
return string(rawValue), nil
} }

View file

@ -10,38 +10,44 @@ import (
) )
func init() { func init() {
registerFunction("vault", func(name string, v ...string) (interface{}, error) { registerFunction("mustVault", tplVaultKeyFetch)
if name == "" {
return nil, fmt.Errorf("path is not set")
}
if len(v) < 1 {
return nil, fmt.Errorf("key is not set")
}
client, err := vaultClientFromEnvOrFile() registerFunction("vault", func(name string, key string) string {
v, err := tplVaultKeyFetch(name, key)
if err != nil { if err != nil {
return nil, err return ""
} }
return v
secret, err := client.Logical().Read(name)
if err != nil {
return nil, fmt.Errorf("reading secret: %s", err)
}
if secret != nil && secret.Data != nil {
if val, ok := secret.Data[v[0]]; ok {
return val, nil
}
}
if len(v) < 2 { //nolint:gomnd
return nil, fmt.Errorf("requested value %q in key %q was not found in Vault and no default was set", v[0], name)
}
return v[1], nil
}) })
} }
func tplVaultKeyFetch(name string, key string) (string, error) {
if name == "" {
return "", fmt.Errorf("path is not set")
}
if key == "" {
return "", fmt.Errorf("key is not set")
}
client, err := vaultClientFromEnvOrFile()
if err != nil {
return "", err
}
secret, err := client.Logical().Read(name)
if err != nil {
return "", fmt.Errorf("reading secret: %s", err)
}
if secret != nil && secret.Data != nil {
if val, ok := secret.Data[key].(string); ok {
return val, nil
}
}
return "", fmt.Errorf("requested value %q in key %q was not found in Vault and no default was set", key, name)
}
func vaultClientFromEnvOrFile() (*api.Client, error) { func vaultClientFromEnvOrFile() (*api.Client, error) {
client, err := api.NewClient(&api.Config{ client, err := api.NewClient(&api.Config{
Address: os.Getenv(api.EnvVaultAddress), Address: os.Getenv(api.EnvVaultAddress),