diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 3ae127221d3b..fcc776adced6 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -1,6 +1,8 @@ package config import ( + "bytes" + "compress/gzip" "crypto/md5" "crypto/sha1" "crypto/sha256" @@ -72,6 +74,7 @@ func Funcs() map[string]ast.Function { "distinct": interpolationFuncDistinct(), "element": interpolationFuncElement(), "file": interpolationFuncFile(), + "gzipbase64": interpolationFuncGzipBase64(), "matchkeys": interpolationFuncMatchKeys(), "floor": interpolationFuncFloor(), "format": interpolationFuncFormat(), @@ -1322,6 +1325,28 @@ func interpolationFuncBase64Sha512() ast.Function { } } +func interpolationFuncGzipBase64() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + s := args[0].(string) + var b bytes.Buffer + gz := gzip.NewWriter(&b) + if _, err := gz.Write([]byte(s)); err != nil { + return "", fmt.Errorf("failed to gzip raw data: '%s'", s) + } + if err := gz.Flush(); err != nil { + return "", fmt.Errorf("failed to gzip raw data: '%s'", s) + } + if err := gz.Close(); err != nil { + return "", fmt.Errorf("failed to gzip raw data: '%s'", s) + } + return base64.StdEncoding.EncodeToString(b.Bytes()), nil + }, + } +} + func interpolationFuncUUID() ast.Function { return ast.Function{ ArgTypes: []ast.Type{}, diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index fe68f38c485d..4ea72b0a4160 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -2249,6 +2249,18 @@ func TestInterpolateFuncBase64Sha512(t *testing.T) { }) } +func TestInterpolateFuncGzip(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${gzipbase64("test")}`, + "H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA", + false, + }, + }, + }) +} + func TestInterpolateFuncMd5(t *testing.T) { testFunction(t, testFunctionConfig{ Cases: []testFunctionCase{