1
0
Fork 0
mirror of https://github.com/Luzifer/korvike.git synced 2025-01-02 01:11:21 +00:00

Compare commits

...

2 commits

Author SHA1 Message Date
2cc2bee14a
prepare release v1.0.0 2024-02-29 18:05:49 +01:00
fd2a37ca97
Remove default values for file and vault
and add `must*` variants for them

Signed-off-by: Knut Ahlers <knut@ahlers.me>
2024-02-29 17:55:51 +01:00
4 changed files with 71 additions and 43 deletions

View file

@ -1,3 +1,19 @@
# 1.0.0 / 2024-02-29
* Breaking: Add sprig functions, replace some internal ones
* Replace old build-system
**Breaking changes:**
- Function `env` no longer takes a default, use `env "MYVAR" | default "..."`
- Function `file` no longer takes a default, use `file "[filename]" | default "..."`
- Function `now` returns `time.Time`, use `now | date "[format]"`
- Function `split` now has reversed parameters `split <sep> <str>`
- Function `vault` no longet takes a default, use `vault "key" "field" | default "..."`
- Removed function `b64decode`, use `b64dec`
- Removed function `b64encode`, use `b64enc`
- Removed function `hash`, use `sha1sum` / `sha256sum` / `sha512sum`
# 0.13.0 / 2022-03-30
* Add basic string manipulation `join` and `split`
@ -79,4 +95,4 @@
# 0.1.0 / 2016-07-31
* initial version
* initial version

View file

@ -4,7 +4,7 @@
# Luzifer / korvike
`korvike` is the finnish translation to the word "replacer" and that is what it does: It takes a Go template and executes it.
`korvike` is the Finnish translation to the word "replacer" and that is what it does: It takes a Go template and executes it.
## Available functions
@ -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

View file

@ -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
}

View file

@ -10,38 +10,44 @@ import (
)
func init() {
registerFunction("vault", func(name string, v ...string) (interface{}, error) {
if name == "" {
return nil, fmt.Errorf("path is not set")
}
if len(v) < 1 {
return nil, fmt.Errorf("key is not set")
}
registerFunction("mustVault", tplVaultKeyFetch)
client, err := vaultClientFromEnvOrFile()
registerFunction("vault", func(name string, key string) string {
v, err := tplVaultKeyFetch(name, key)
if err != nil {
return nil, err
return ""
}
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
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 "", 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) {
client, err := api.NewClient(&api.Config{
Address: os.Getenv(api.EnvVaultAddress),