From 70522fb7708a0fec078098ab33f799cf0afcc126 Mon Sep 17 00:00:00 2001 From: Gorka Lerchundi Osa Date: Fri, 25 Sep 2015 09:23:36 +0200 Subject: [PATCH 1/2] implements base64{enc,dec} interpolation funcs fixes #3320 Signed-off-by: Gorka Lerchundi Osa --- config/interpolate_funcs.go | 33 ++++++++++++++++++++++++++++++++ config/interpolate_funcs_test.go | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index bbe2b8434..4778f1ec6 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -2,6 +2,7 @@ package config import ( "bytes" + "encoding/base64" "errors" "fmt" "io/ioutil" @@ -29,6 +30,8 @@ func init() { "length": interpolationFuncLength(), "replace": interpolationFuncReplace(), "split": interpolationFuncSplit(), + "base64enc": interpolationFuncBase64Encode(), + "base64dec": interpolationFuncBase64Decode(), } } @@ -392,3 +395,33 @@ func interpolationFuncValues(vs map[string]ast.Variable) ast.Function { }, } } + +// interpolationFuncBase64Encode implements the "base64enc" function that allows +// Base64 encoding. +func interpolationFuncBase64Encode() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + s := args[0].(string) + return base64.StdEncoding.EncodeToString([]byte(s)), nil + }, + } +} + +// interpolationFuncBase64Decode implements the "base64dec" function that allows +// Base64 decoding. +func interpolationFuncBase64Decode() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeString, + Callback: func(args []interface{}) (interface{}, error) { + s := args[0].(string) + sDec, err := base64.StdEncoding.DecodeString(s) + if err != nil { + return "", fmt.Errorf("failed to decode base64 data '%s'", s) + } + return string(sDec), nil + }, + } +} diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 05f84c201..773f92c99 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -584,6 +584,39 @@ func TestInterpolateFuncElement(t *testing.T) { }) } +func TestInterpolateFuncBase64Encode(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + // Regular base64 encoding + { + `${base64enc("abc123!?$*&()'-=@~")}`, + "YWJjMTIzIT8kKiYoKSctPUB+", + false, + }, + }, + }) +} + +func TestInterpolateFuncBase64Decode(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + // Regular base64 decoding + { + `${base64dec("YWJjMTIzIT8kKiYoKSctPUB+")}`, + "abc123!?$*&()'-=@~", + false, + }, + + // Invalid base64 data decoding + { + `${base64dec("this-is-an-invalid-base64-data")}`, + nil, + true, + }, + }, + }) +} + type testFunctionConfig struct { Cases []testFunctionCase Vars map[string]ast.Variable From c3d15a2336d783e4f73c4c81c20bf076cb5bd848 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Sat, 3 Oct 2015 14:49:50 -0700 Subject: [PATCH 2/2] Docs for base64 interpolation functions. --- website/source/docs/configuration/interpolation.html.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index f4730acd6..ff47202d8 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -74,6 +74,12 @@ are documented below. The supported built-in functions are: + * `base64dec(string)` - Given a base64-encoded string, decodes it and + returns the original string. + + * `base64enc(string)` - Returns a base64-encoded representation of the + given string. + * `concat(list1, list2)` - Combines two or more lists into a single list. Example: `concat(aws_instance.db.*.tags.Name, aws_instance.web.*.tags.Name)`