mirror of
https://github.com/adnanh/webhook.git
synced 2025-06-26 14:28:31 +00:00
Merge 6ef8115050
into 7bb680821d
This commit is contained in:
commit
5e9f619cba
2 changed files with 24 additions and 2 deletions
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
In additional to the [built-in Go template functions and features][tt], `webhook` provides a `getenv` template function for inserting environment variables into a templated configuration file.
|
In additional to the [built-in Go template functions and features][tt], `webhook` provides a `getenv` template function for inserting environment variables into a templated configuration file.
|
||||||
|
|
||||||
|
`secret` template function provides access to docker secrets. `secret secret_name` will insert content of `/run/secrets/secret_name` file into a templated configuration file.
|
||||||
|
|
||||||
## Example Usage
|
## Example Usage
|
||||||
|
|
||||||
In the example JSON template file below (YAML is also supported), the `payload-hmac-sha1` matching rule looks up the HMAC secret from the environment using the `getenv` template function.
|
In the example JSON template file below (YAML is also supported), the `payload-hmac-sha1` matching rule looks up the HMAC secret from the environment using the `getenv` template function.
|
||||||
|
|
|
@ -19,8 +19,10 @@ import (
|
||||||
"net"
|
"net"
|
||||||
"net/textproto"
|
"net/textproto"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
@ -757,8 +759,10 @@ func (h *Hooks) LoadFromFile(path string, asTemplate bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if asTemplate {
|
if asTemplate {
|
||||||
funcMap := template.FuncMap{"getenv": getenv}
|
funcMap := template.FuncMap{
|
||||||
|
"getenv": getenv,
|
||||||
|
"secret": dockerSecret,
|
||||||
|
}
|
||||||
tmpl, err := template.New("hooks").Funcs(funcMap).Parse(string(file))
|
tmpl, err := template.New("hooks").Funcs(funcMap).Parse(string(file))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -956,3 +960,19 @@ func compare(a, b string) bool {
|
||||||
func getenv(s string) string {
|
func getenv(s string) string {
|
||||||
return os.Getenv(s)
|
return os.Getenv(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dockerSecret provides a template function to retrieve Docker secret.
|
||||||
|
func dockerSecret(name string) string {
|
||||||
|
_, file := filepath.Split(name)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
file = filepath.Join("C:\\ProgramData\\Docker\\secrets", file)
|
||||||
|
} else {
|
||||||
|
file = filepath.Join("/run/secrets", file)
|
||||||
|
}
|
||||||
|
b, err := ioutil.ReadFile(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error reading docker secret from %s %s", file, err)
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue