feat: Add two template functions

- cat:        Allows reading a value from a file
- credential: Allows reading a credential passed by systemd
This commit is contained in:
Tom Hubrecht 2024-11-16 23:19:57 +01:00
parent c5f2a8f940
commit d42769ea84
No known key found for this signature in database

View file

@ -18,6 +18,7 @@ import (
"net"
"net/textproto"
"os"
"path"
"reflect"
"regexp"
"strconv"
@ -756,7 +757,11 @@ func (h *Hooks) LoadFromFile(path string, asTemplate bool) error {
}
if asTemplate {
funcMap := template.FuncMap{"getenv": getenv}
funcMap := template.FuncMap{
"cat": cat,
"credential": credential,
"getenv": getenv,
}
tmpl, err := template.New("hooks").Funcs(funcMap).Parse(string(file))
if err != nil {
@ -955,3 +960,27 @@ func compare(a, b string) bool {
func getenv(s string) string {
return os.Getenv(s)
}
// cat provides a template function to retrieve content of files
// Similarly to getenv, if no file is found, it returns the empty string
func cat(s string) string {
data, e := os.ReadFile(s)
if e != nil {
return ""
}
return string(data)
}
// credential provides a template function to retreive secrets using systemd's LoadCredential mechanism
func credential(s string) string {
dir := getenv("CREDENTIALS_DIRECTORY")
// If no credential directory is found, fallback to the env variable
if dir == "" {
return getenv(s)
}
return cat(path.Join(dir, s))
}