Merge pull request #4450 from jkordish/jk-sha1-interpolation

implements sha1 hash interpolation func
This commit is contained in:
Paul Hinze 2016-01-06 15:57:01 -06:00
commit d103dd1dcb
3 changed files with 34 additions and 1 deletions

View File

@ -2,7 +2,9 @@ package config
import ( import (
"bytes" "bytes"
"crypto/sha1"
"encoding/base64" "encoding/base64"
"encoding/hex"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
@ -38,6 +40,7 @@ func init() {
"lower": interpolationFuncLower(), "lower": interpolationFuncLower(),
"replace": interpolationFuncReplace(), "replace": interpolationFuncReplace(),
"split": interpolationFuncSplit(), "split": interpolationFuncSplit(),
"sha1": interpolationFuncSha1(),
"base64encode": interpolationFuncBase64Encode(), "base64encode": interpolationFuncBase64Encode(),
"base64decode": interpolationFuncBase64Decode(), "base64decode": interpolationFuncBase64Decode(),
"upper": interpolationFuncUpper(), "upper": interpolationFuncUpper(),
@ -586,3 +589,17 @@ func interpolationFuncUpper() ast.Function {
}, },
} }
} }
func interpolationFuncSha1() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
s := args[0].(string)
h := sha1.New()
h.Write([]byte(s))
hash := hex.EncodeToString(h.Sum(nil))
return hash, nil
},
}
}

View File

@ -834,6 +834,18 @@ func TestInterpolateFuncUpper(t *testing.T) {
}) })
} }
func TestInterpolateFuncSha1(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${sha1("test")}`,
"a94a8fe5ccb19ba61c4c0873d391e987982fbbd3",
false,
},
},
})
}
type testFunctionConfig struct { type testFunctionConfig struct {
Cases []testFunctionCase Cases []testFunctionCase
Vars map[string]ast.Variable Vars map[string]ast.Variable

View File

@ -80,6 +80,10 @@ The supported built-in functions are:
* `base64encode(string)` - Returns a base64-encoded representation of the * `base64encode(string)` - Returns a base64-encoded representation of the
given string. given string.
* `sha1(string)` - Returns a sha1 hash representation of the
given string.
Example: `"${sha1(concat(aws_vpc.default.tags.customer, "-s3-bucket"))}"`
* `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation * `cidrhost(iprange, hostnum)` - Takes an IP address range in CIDR notation
and creates an IP address with the given host number. For example, and creates an IP address with the given host number. For example,
``cidrhost("10.0.0.0/8", 2)`` returns ``10.0.0.2``. ``cidrhost("10.0.0.0/8", 2)`` returns ``10.0.0.2``.
@ -95,7 +99,7 @@ The supported built-in functions are:
CIDR notation (like ``10.0.0.0/8``) and extends its prefix to include an CIDR notation (like ``10.0.0.0/8``) and extends its prefix to include an
additional subnet number. For example, additional subnet number. For example,
``cidrsubnet("10.0.0.0/8", 8, 2)`` returns ``10.2.0.0/16``. ``cidrsubnet("10.0.0.0/8", 8, 2)`` returns ``10.2.0.0/16``.
* `coalesce(string1, string2, ...)` - Returns the first non-empty value from * `coalesce(string1, string2, ...)` - Returns the first non-empty value from
the given arguments. At least two arguments must be provided. the given arguments. At least two arguments must be provided.