Skip to content

Commit

Permalink
Merge pull request #1 from yunify/base64
Browse files Browse the repository at this point in the history
add Base64 function
  • Loading branch information
Martin@qingcloud authored Oct 12, 2017
2 parents c901eaf + d1a58c9 commit 3e92c43
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 12 deletions.
10 changes: 5 additions & 5 deletions backends/redis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package redis
import (
"fmt"
"github.com/garyburd/redigo/redis"
"github.com/kelseyhightower/confd/log"
"os"
"strings"
"time"
"github.com/kelseyhightower/confd/log"
)

// Client is a wrapper around the redis client
Expand All @@ -28,7 +28,7 @@ func tryConnect(machines []string, password string) (redis.Conn, error) {
network = "unix"
}
log.Debug(fmt.Sprintf("Trying to connect to redis node %s", address))

dialops := []redis.DialOption{
redis.DialConnectTimeout(time.Second),
redis.DialReadTimeout(time.Second),
Expand Down Expand Up @@ -58,8 +58,8 @@ func (c *Client) connectedClient() (redis.Conn, error) {

resp, err := c.client.Do("PING")
if (err != nil && err == redis.ErrNil) || resp != "PONG" {
log.Error(fmt.Sprintf("Existing redis connection no longer usable. " +
"Will try to re-establish. Error: %s", err.Error()))
log.Error(fmt.Sprintf("Existing redis connection no longer usable. "+
"Will try to re-establish. Error: %s", err.Error()))
c.client = nil
}
}
Expand All @@ -80,7 +80,7 @@ func (c *Client) connectedClient() (redis.Conn, error) {
// It returns an error if a connection to the cluster cannot be made.
func NewRedisClient(machines []string, password string) (*Client, error) {
var err error
clientWrapper := &Client{ machines : machines, password: password, client: nil }
clientWrapper := &Client{machines: machines, password: password, client: nil}
clientWrapper.client, err = tryConnect(machines, password)
return clientWrapper, err
}
Expand Down
16 changes: 16 additions & 0 deletions docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,22 @@ hosts:{{$hosts := getvs "/test/data/*/id"}}
{{toYaml $hosts}}
```

### base64Encode

base64 encoding

```
{{getv "/test/data/password" |base64Encode }}
```

### base64Decode

base64 decoding

```
{{getv "/test/data/data" |base64Decode }}
```

## Example Usage

```Bash
Expand Down
2 changes: 2 additions & 0 deletions resource/template/template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func newFuncMap() map[string]interface{} {
m["filter"] = Filter
m["toJson"] = ToJson
m["toYaml"] = ToYaml
m["base64Encode"] = Base64Encode
m["base64Decode"] = Base64Decode
return m
}

Expand Down
30 changes: 30 additions & 0 deletions resource/template/template_funcs_ext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package template

import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -459,3 +460,32 @@ func ToYaml(v interface{}) (string, error) {
}
return string(b), nil
}

func Base64Encode(v interface{}) (string, error) {
var input []byte
s, ok := v.(string)
if ok {
input = []byte(s)
} else {
b, ok := v.([]byte)
if ok {
input = b
}
}
if input != nil {
return base64.StdEncoding.EncodeToString(input), nil
}
return "", fmt.Errorf("unsupported type %s", reflect.ValueOf(v).Kind().String())
}

func Base64Decode(v interface{}) (string, error) {
var input string
s, ok := v.(string)
if ok {
input = s
} else {
return "", fmt.Errorf("unsupported type %s", reflect.ValueOf(v).Kind().String())
}
r, err := base64.StdEncoding.DecodeString(input)
return string(r), err
}
97 changes: 93 additions & 4 deletions resource/template/template_funcs_ext_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package template

import (
"github.com/kelseyhightower/memkv"
"path"
"reflect"
"runtime"
"testing"
"time"

"github.com/kelseyhightower/memkv"
)

type tstCompareType int
Expand Down Expand Up @@ -60,6 +61,32 @@ val: 2
tr.store.Set("/test/key", "1")
},
},
templateTest{
desc: "add float test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/key",
]
`,
tmpl: `
{{with get "/test/key"}}
key: {{base .Key}}
val: {{add .Value 1}}
{{end}}
`,
expected: `
key: key
val: 2
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/key", "1.0")
},
},
templateTest{
desc: "sub test",
toml: `
Expand Down Expand Up @@ -665,8 +692,11 @@ func TestToJsonAndYaml(t *testing.T) {
{[]string{"a1", "b1"}, false, `["a1","b1"]`, "- a1\n- b1\n"},
{"a1", false, `"a1"`, "a1\n"},
{1, false, "1", "1\n"},
{struct {Name string}{Name:"test"}, false, `{"Name":"test"}`, "name: test\n"},
{struct {Name string; Addr []string}{Name:"test", Addr: []string{"a1", "a2"}}, false, `{"Name":"test","Addr":["a1","a2"]}`,
{struct{ Name string }{Name: "test"}, false, `{"Name":"test"}`, "name: test\n"},
{struct {
Name string
Addr []string
}{Name: "test", Addr: []string{"a1", "a2"}}, false, `{"Name":"test","Addr":["a1","a2"]}`,
`name: test
addr:
- a1
Expand Down Expand Up @@ -703,4 +733,63 @@ addr:
}
}
}
}
}

func TestBase64Encode(t *testing.T) {
for i, this := range []struct {
input interface{}
err bool
expect string
}{
{"", false, ""},
{"aaa", false, "YWFh"},
{[]byte("aaa"), false, "YWFh"},
{"user:123Abc", false, "dXNlcjoxMjNBYmM="},
{"user:\n123Abc", false, "dXNlcjoKMTIzQWJj"},
{struct{}{}, true, ""},
} {
result, err := Base64Encode(this.input)
if this.err {
if err == nil {
t.Errorf("[%d] Base64Encode didn't return an expected error", i)
}
} else {
if err != nil {
t.Errorf("[%d] failed: %s", i, err)
continue
}
if !reflect.DeepEqual(result, this.expect) {
t.Errorf("[%d] Base64Encode [%v] got %v but expected %v", i, this.input, result, this.expect)
}
}
}
}

func TestBase64Decode(t *testing.T) {
for i, this := range []struct {
input interface{}
err bool
expect string
}{
{"", false, ""},
{"YWFh", false, "aaa"},
{"dXNlcjoxMjNBYmM=", false, "user:123Abc"},
{"dXNlcjoKMTIzQWJj", false, "user:\n123Abc"},
{struct{}{}, true, ""},
} {
result, err := Base64Decode(this.input)
if this.err {
if err == nil {
t.Errorf("[%d] Base64Decode didn't return an expected error", i)
}
} else {
if err != nil {
t.Errorf("[%d] failed: %s", i, err)
continue
}
if !reflect.DeepEqual(result, this.expect) {
t.Errorf("[%d] Base64Decode [%v] got %v but expected %v", i, this.input, result, this.expect)
}
}
}
}
46 changes: 46 additions & 0 deletions resource/template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,52 @@ ids:
tr.store.Set("/test/data/ids", "b1 b2")
},
},
templateTest{
desc: "base64 test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data/username",
"/test/data/password",
]
`,
tmpl: `
{{$username := getv "/test/data/username"}}{{$password := getv "/test/data/password"}}{{$pair := printf "%s:%s" $username $password}}
base64:{{base64Encode $pair}}
`,
expected: `
base64:dXNlcjoxMjNBYmM=
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data/username", "user")
tr.store.Set("/test/data/password", "123Abc")
},
},
templateTest{
desc: "base64 test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data/encoded",
]
`,
tmpl: `
{{$data := getv "/test/data/encoded"}}
value:{{base64Decode $data}}
`,
expected: `
value:user:123Abc
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data/encoded", "dXNlcjoxMjNBYmM=")
},
},
}

// TestTemplates runs all tests in templateTests
Expand Down
4 changes: 2 additions & 2 deletions resource/template/util_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"fmt"
"io"
"os"
"syscall"
"os/exec"
"syscall"
)

// fileStat return a fileInfo describing the named file.
Expand All @@ -35,4 +35,4 @@ func fileStat(name string) (fi fileInfo, err error) {

func command(cmd string) *exec.Cmd {
return exec.Command("/bin/sh", "-c", cmd)
}
}
2 changes: 1 addition & 1 deletion resource/template/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ func fileStat(name string) (fi fileInfo, err error) {

func command(cmd string) *exec.Cmd {
return exec.Command("cmd", "/C", cmd)
}
}

0 comments on commit 3e92c43

Please sign in to comment.