Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements base64{enc,dec} interpolation funcs #3325

Merged
merged 1 commit into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -29,6 +30,8 @@ func init() {
"length": interpolationFuncLength(),
"replace": interpolationFuncReplace(),
"split": interpolationFuncSplit(),
"base64enc": interpolationFuncBase64Encode(),
"base64dec": interpolationFuncBase64Decode(),
}
}

Expand Down Expand Up @@ -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
},
}
}
33 changes: 33 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down