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:
parent
3354a29311
commit
fd2a37ca97
3 changed files with 53 additions and 41 deletions
|
@ -16,8 +16,8 @@ Starting with `v1.0.0` Korvike is based on the [sprig functions collection](http
|
|||
$ echo "{{ .foo }}" | korvike -v foo=bar
|
||||
bar
|
||||
```
|
||||
- `{{ file <file name> [default value] }}`
|
||||
Read a file and place it inside the template
|
||||
- `{{ file <file name> }}` / `{{ mustFile <file name> }}`
|
||||
Read a file and place it inside the template, `file` returns an empty string on error, `mustFile` an error
|
||||
```console
|
||||
$ echo "Hello World" > hello
|
||||
$ 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
|
||||
Hell%C3%B6+W%C3%B6rld%40Golang
|
||||
```
|
||||
- `{{ vault <path> <key> [default value] }}`
|
||||
Read a key from Vault using `VAULT_ADDR` and `VAULT_TOKEN` environment variables (or `~/.vault-token` file) for authentication.
|
||||
- `{{ 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. `vault` returns an empty string on error, `mustVault` an error
|
||||
```console
|
||||
$ vault write secret/test foo=bar
|
||||
$ echo '{{ vault "secret/test" "foo" }}' | korvike
|
||||
|
|
|
@ -1,21 +1,27 @@
|
|||
package functions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registerFunction("file", func(name string, v ...string) string {
|
||||
defaultValue := ""
|
||||
if len(v) > 0 {
|
||||
defaultValue = v[0]
|
||||
registerFunction("file", func(name string) string {
|
||||
fc, err := tplReadFile(name)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if _, err := os.Stat(name); err == nil {
|
||||
//#nosec:G304 // Intended to load custom file
|
||||
if rawValue, err := os.ReadFile(name); err == nil {
|
||||
return string(rawValue)
|
||||
}
|
||||
}
|
||||
return defaultValue
|
||||
return fc
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -10,36 +10,42 @@ import (
|
|||
)
|
||||
|
||||
func init() {
|
||||
registerFunction("vault", func(name string, v ...string) (interface{}, error) {
|
||||
if name == "" {
|
||||
return nil, fmt.Errorf("path is not set")
|
||||
registerFunction("mustVault", tplVaultKeyFetch)
|
||||
|
||||
registerFunction("vault", func(name string, key string) string {
|
||||
v, err := tplVaultKeyFetch(name, key)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if len(v) < 1 {
|
||||
return nil, fmt.Errorf("key is not set")
|
||||
return v
|
||||
})
|
||||
}
|
||||
|
||||
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 nil, err
|
||||
return "", err
|
||||
}
|
||||
|
||||
secret, err := client.Logical().Read(name)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("reading secret: %s", err)
|
||||
return "", fmt.Errorf("reading secret: %s", err)
|
||||
}
|
||||
|
||||
if secret != nil && secret.Data != nil {
|
||||
if val, ok := secret.Data[v[0]]; ok {
|
||||
if val, ok := secret.Data[key].(string); 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
|
||||
})
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue