config: sha512 hash functions (#14100)

This commit is contained in:
Martins Sipenko 2017-05-03 02:35:23 +03:00 committed by Martin Atkins
parent 23f4e9fe08
commit c7fb9808ef
3 changed files with 70 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"crypto/md5" "crypto/md5"
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
"crypto/sha512"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@ -57,6 +58,7 @@ func Funcs() map[string]ast.Function {
"base64decode": interpolationFuncBase64Decode(), "base64decode": interpolationFuncBase64Decode(),
"base64encode": interpolationFuncBase64Encode(), "base64encode": interpolationFuncBase64Encode(),
"base64sha256": interpolationFuncBase64Sha256(), "base64sha256": interpolationFuncBase64Sha256(),
"base64sha512": interpolationFuncBase64Sha512(),
"ceil": interpolationFuncCeil(), "ceil": interpolationFuncCeil(),
"chomp": interpolationFuncChomp(), "chomp": interpolationFuncChomp(),
"cidrhost": interpolationFuncCidrHost(), "cidrhost": interpolationFuncCidrHost(),
@ -90,6 +92,7 @@ func Funcs() map[string]ast.Function {
"replace": interpolationFuncReplace(), "replace": interpolationFuncReplace(),
"sha1": interpolationFuncSha1(), "sha1": interpolationFuncSha1(),
"sha256": interpolationFuncSha256(), "sha256": interpolationFuncSha256(),
"sha512": interpolationFuncSha512(),
"signum": interpolationFuncSignum(), "signum": interpolationFuncSignum(),
"slice": interpolationFuncSlice(), "slice": interpolationFuncSlice(),
"sort": interpolationFuncSort(), "sort": interpolationFuncSort(),
@ -1240,6 +1243,20 @@ func interpolationFuncSha256() ast.Function {
} }
} }
func interpolationFuncSha512() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
s := args[0].(string)
h := sha512.New()
h.Write([]byte(s))
hash := hex.EncodeToString(h.Sum(nil))
return hash, nil
},
}
}
func interpolationFuncTrimSpace() ast.Function { func interpolationFuncTrimSpace() ast.Function {
return ast.Function{ return ast.Function{
ArgTypes: []ast.Type{ast.TypeString}, ArgTypes: []ast.Type{ast.TypeString},
@ -1266,6 +1283,21 @@ func interpolationFuncBase64Sha256() ast.Function {
} }
} }
func interpolationFuncBase64Sha512() ast.Function {
return ast.Function{
ArgTypes: []ast.Type{ast.TypeString},
ReturnType: ast.TypeString,
Callback: func(args []interface{}) (interface{}, error) {
s := args[0].(string)
h := sha512.New()
h.Write([]byte(s))
shaSum := h.Sum(nil)
encoded := base64.StdEncoding.EncodeToString(shaSum[:])
return encoded, nil
},
}
}
func interpolationFuncUUID() ast.Function { func interpolationFuncUUID() ast.Function {
return ast.Function{ return ast.Function{
ArgTypes: []ast.Type{}, ArgTypes: []ast.Type{},

View File

@ -2070,6 +2070,18 @@ func TestInterpolateFuncSha256(t *testing.T) {
}) })
} }
func TestInterpolateFuncSha512(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${sha512("test")}`,
"ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff",
false,
},
},
})
}
func TestInterpolateFuncTitle(t *testing.T) { func TestInterpolateFuncTitle(t *testing.T) {
testFunction(t, testFunctionConfig{ testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{ Cases: []testFunctionCase{
@ -2129,6 +2141,23 @@ func TestInterpolateFuncBase64Sha256(t *testing.T) {
}) })
} }
func TestInterpolateFuncBase64Sha512(t *testing.T) {
testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{
{
`${base64sha512("test")}`,
"7iaw3Ur350mqGo7jwQrpkj9hiYB3Lkc/iBml1JQODbJ6wYX4oOHV+E+IvIh/1nsUNzLDBMxfqa2Ob1f1ACio/w==",
false,
},
{ // This will differ because we're base64-encoding hex represantiation, not raw bytes
`${base64encode(sha512("test"))}`,
"ZWUyNmIwZGQ0YWY3ZTc0OWFhMWE4ZWUzYzEwYWU5OTIzZjYxODk4MDc3MmU0NzNmODgxOWE1ZDQ5NDBlMGRiMjdhYzE4NWY4YTBlMWQ1Zjg0Zjg4YmM4ODdmZDY3YjE0MzczMmMzMDRjYzVmYTlhZDhlNmY1N2Y1MDAyOGE4ZmY=",
false,
},
},
})
}
func TestInterpolateFuncMd5(t *testing.T) { func TestInterpolateFuncMd5(t *testing.T) {
testFunction(t, testFunctionConfig{ testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{ Cases: []testFunctionCase{

View File

@ -158,6 +158,11 @@ The supported built-in functions are:
**This is not equivalent** of `base64encode(sha256(string))` **This is not equivalent** of `base64encode(sha256(string))`
since `sha256()` returns hexadecimal representation. since `sha256()` returns hexadecimal representation.
* `base64sha512(string)` - Returns a base64-encoded representation of raw
SHA-512 sum of the given string.
**This is not equivalent** of `base64encode(sha512(string))`
since `sha512()` returns hexadecimal representation.
* `ceil(float)` - Returns the least integer value greater than or equal * `ceil(float)` - Returns the least integer value greater than or equal
to the argument. to the argument.
@ -321,6 +326,10 @@ The supported built-in functions are:
SHA-256 hash of the given string. SHA-256 hash of the given string.
Example: `"${sha256("${aws_vpc.default.tags.customer}-s3-bucket")}"` Example: `"${sha256("${aws_vpc.default.tags.customer}-s3-bucket")}"`
* `sha512(string)` - Returns a (conventional) hexadecimal representation of the
SHA-512 hash of the given string.
Example: `"${sha512("${aws_vpc.default.tags.customer}-s3-bucket")}"`
* `signum(int)` - Returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers. * `signum(int)` - Returns `-1` for negative numbers, `0` for `0` and `1` for positive numbers.
This function is useful when you need to set a value for the first resource and This function is useful when you need to set a value for the first resource and
a different value for the rest of the resources. a different value for the rest of the resources.