mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
config: base64gzip interpolation function
Since Terraform's internals are not 8-bit clean (it assumes UTF-8 strings), we can't implement raw gzip directly. We're going to add support where it makes sense for passing data to attributes as base64 so that the result of this function can be used.
This commit is contained in:
parent
de561261ab
commit
a303817e03
@ -1,6 +1,8 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/md5"
|
||||
"crypto/sha1"
|
||||
"crypto/sha256"
|
||||
@ -58,6 +60,7 @@ func Funcs() map[string]ast.Function {
|
||||
"basename": interpolationFuncBasename(),
|
||||
"base64decode": interpolationFuncBase64Decode(),
|
||||
"base64encode": interpolationFuncBase64Encode(),
|
||||
"base64gzip": interpolationFuncBase64Gzip(),
|
||||
"base64sha256": interpolationFuncBase64Sha256(),
|
||||
"base64sha512": interpolationFuncBase64Sha512(),
|
||||
"bcrypt": interpolationFuncBcrypt(),
|
||||
@ -1205,6 +1208,32 @@ func interpolationFuncBase64Decode() ast.Function {
|
||||
}
|
||||
}
|
||||
|
||||
// interpolationFuncBase64Gzip implements the "gzip" function that allows gzip
|
||||
// compression encoding the result using base64
|
||||
func interpolationFuncBase64Gzip() ast.Function {
|
||||
return ast.Function{
|
||||
ArgTypes: []ast.Type{ast.TypeString},
|
||||
ReturnType: ast.TypeString,
|
||||
Callback: func(args []interface{}) (interface{}, error) {
|
||||
s := args[0].(string)
|
||||
|
||||
var b bytes.Buffer
|
||||
gz := gzip.NewWriter(&b)
|
||||
if _, err := gz.Write([]byte(s)); err != nil {
|
||||
return "", fmt.Errorf("failed to write gzip raw data: '%s'", s)
|
||||
}
|
||||
if err := gz.Flush(); err != nil {
|
||||
return "", fmt.Errorf("failed to flush gzip writer: '%s'", s)
|
||||
}
|
||||
if err := gz.Close(); err != nil {
|
||||
return "", fmt.Errorf("failed to close gzip writer: '%s'", s)
|
||||
}
|
||||
|
||||
return base64.StdEncoding.EncodeToString(b.Bytes()), nil
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// interpolationFuncLower implements the "lower" function that does
|
||||
// string lower casing.
|
||||
func interpolationFuncLower() ast.Function {
|
||||
|
@ -2249,6 +2249,18 @@ func TestInterpolateFuncTrimSpace(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncBase64Gzip(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
{
|
||||
`${base64gzip("test")}`,
|
||||
"H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA",
|
||||
false,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestInterpolateFuncBase64Sha256(t *testing.T) {
|
||||
testFunction(t, testFunctionConfig{
|
||||
Cases: []testFunctionCase{
|
||||
|
@ -153,6 +153,11 @@ The supported built-in functions are:
|
||||
* `base64encode(string)` - Returns a base64-encoded representation of the
|
||||
given string.
|
||||
|
||||
* `base64gzip(string)` - Compresses the given string with gzip and then
|
||||
encodes the result to base64. This can be used with certain resource
|
||||
arguments that allow binary data to be passed with base64 encoding, since
|
||||
Terraform strings are required to be valid UTF-8.
|
||||
|
||||
* `base64sha256(string)` - Returns a base64-encoded representation of raw
|
||||
SHA-256 sum of the given string.
|
||||
**This is not equivalent** of `base64encode(sha256(string))`
|
||||
|
Loading…
Reference in New Issue
Block a user