Skip to content

Commit

Permalink
Merge pull request #14725 from StackPointCloud/bcrypt
Browse files Browse the repository at this point in the history
`bcrypt` builtin function
  • Loading branch information
Jasmin Gacic committed May 30, 2017
2 parents ecc5bfb + bca1591 commit 5d33023
Show file tree
Hide file tree
Showing 9 changed files with 858 additions and 0 deletions.
36 changes: 36 additions & 0 deletions config/interpolate_funcs.go
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/bcrypt"
)

// stringSliceToVariableValue converts a string slice into the value
Expand Down Expand Up @@ -59,6 +60,7 @@ func Funcs() map[string]ast.Function {
"base64encode": interpolationFuncBase64Encode(),
"base64sha256": interpolationFuncBase64Sha256(),
"base64sha512": interpolationFuncBase64Sha512(),
"bcrypt": interpolationFuncBcrypt(),
"ceil": interpolationFuncCeil(),
"chomp": interpolationFuncChomp(),
"cidrhost": interpolationFuncCidrHost(),
Expand Down Expand Up @@ -1322,6 +1324,40 @@ func interpolationFuncBase64Sha512() ast.Function {
}
}

func interpolationFuncBcrypt() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
Variadic: true,
VariadicType: ast.TypeString,
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
defaultCost := 10

if len(args) > 1 {
costStr := args[1].(string)
cost, err := strconv.Atoi(costStr)
if err != nil {
return "", err
}

defaultCost = cost
}

if len(args) > 2 {
return "", fmt.Errorf("bcrypt() takes no more than two arguments")
}

input := args[0].(string)
out, err := bcrypt.GenerateFromPassword([]byte(input), defaultCost)
if err != nil {
return "", fmt.Errorf("error occured generating password %s", err.Error())
}

return string(out), nil
},
}
}

func interpolationFuncUUID() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{},
Expand Down
29 changes: 29 additions & 0 deletions config/interpolate_funcs_test.go
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/hil"
"github.com/hashicorp/hil/ast"
"github.com/mitchellh/go-homedir"
"golang.org/x/crypto/bcrypt"
)

func TestInterpolateFuncZipMap(t *testing.T) {
Expand Down Expand Up @@ -2437,3 +2438,31 @@ func TestInterpolateFuncSubstr(t *testing.T) {
},
})
}

func TestInterpolateFuncBcrypt(t *testing.T) {
node, err := hil.Parse(`${bcrypt("test")}`)
if err != nil {
t.Fatalf("err: %s", err)
}

result, err := hil.Eval(node, langEvalConfig(nil))
if err != nil {
t.Fatalf("err: %s", err)
}
err = bcrypt.CompareHashAndPassword([]byte(result.Value.(string)), []byte("test"))

if err != nil {
t.Fatalf("Error comparing hash and password: %s", err)
}

testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
//Negative test for more than two parameters
{
`${bcrypt("test", 15, 12)}`,
nil,
true,
},
},
})
}
35 changes: 35 additions & 0 deletions vendor/golang.org/x/crypto/bcrypt/base64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d33023

Please sign in to comment.