diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 3ae127221d3b..80b336c9509d 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" @@ -57,6 +59,7 @@ func Funcs() map[string]ast.Function { "basename": interpolationFuncBasename(), "base64decode": interpolationFuncBase64Decode(), "base64encode": interpolationFuncBase64Encode(), + "base64gzip": interpolationFuncBase64Gzip(), "base64sha256": interpolationFuncBase64Sha256(), "base64sha512": interpolationFuncBase64Sha512(), "ceil": interpolationFuncCeil(), @@ -1178,6 +1181,32 @@ func interpolationFuncBase64Decode() ast.Function { } } +// interpolationFuncBase64Gzip implements the "gzip" function that allows gzip +// compression encoding the result using base64 +func interpolationFuncBase64Gzip() 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 write gzip raw data: '%s'", s) + } + if err := gz.Flush(); err != nil { + return "", fmt.Errorf("failed to flush gzip writer: '%s'", s) + } + if err := gz.Close(); err != nil { + return "", fmt.Errorf("failed to close gzip writer: '%s'", s) + } + + return base64.StdEncoding.EncodeToString(b.Bytes()), nil + }, + } +} + // interpolationFuncLower implements the "lower" function that does // string lower casing. func interpolationFuncLower() ast.Function { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index fe68f38c485d..19abc55fa8c2 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -2215,6 +2215,18 @@ func TestInterpolateFuncTrimSpace(t *testing.T) { }) } +func TestInterpolateFuncBase64Gzip(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + { + `${base64gzip("test")}`, + "H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA", + false, + }, + }, + }) +} + func TestInterpolateFuncBase64Sha256(t *testing.T) { testFunction(t, testFunctionConfig{ Cases: []testFunctionCase{