add Hostname helper in expr and templating (#2193)

This commit is contained in:
blotus 2023-05-11 14:25:04 +02:00 committed by GitHub
parent 71b7a594bd
commit 4ae41a363d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 0 deletions

View file

@ -1,6 +1,7 @@
package csplugin package csplugin
import ( import (
"os"
"text/template" "text/template"
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers" "github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
@ -20,6 +21,7 @@ var helpers = template.FuncMap{
return metaValues return metaValues
}, },
"CrowdsecCTI": exprhelpers.CrowdsecCTI, "CrowdsecCTI": exprhelpers.CrowdsecCTI,
"Hostname": os.Hostname,
} }
func funcMap() template.FuncMap { func funcMap() template.FuncMap {

View file

@ -398,6 +398,13 @@ var exprFuncs = []exprCustomFunc{
new(func(string) string), new(func(string) string),
}, },
}, },
{
name: "Hostname",
function: Hostname,
signature: []interface{}{
new(func() (string, error)),
},
},
} }
//go 1.20 "CutPrefix": strings.CutPrefix, //go 1.20 "CutPrefix": strings.CutPrefix,

View file

@ -595,3 +595,11 @@ func B64Decode(params ...any) (any, error) {
} }
return string(decoded), nil return string(decoded), nil
} }
func Hostname(params ...any) (any, error) {
hostname, err := os.Hostname()
if err != nil {
return "", err
}
return hostname, nil
}