github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/lang/funcs/sensitive.go (about) 1 package funcs 2 3 import ( 4 "github.com/zclconf/go-cty/cty" 5 "github.com/zclconf/go-cty/cty/function" 6 ) 7 8 // SensitiveFunc returns a value identical to its argument except that 9 // Terraform will consider it to be sensitive. 10 var SensitiveFunc = function.New(&function.Spec{ 11 Params: []function.Parameter{ 12 { 13 Name: "value", 14 Type: cty.DynamicPseudoType, 15 AllowUnknown: true, 16 AllowNull: true, 17 AllowMarked: true, 18 AllowDynamicType: true, 19 }, 20 }, 21 Type: func(args []cty.Value) (cty.Type, error) { 22 // This function only affects the value's marks, so the result 23 // type is always the same as the argument type. 24 return args[0].Type(), nil 25 }, 26 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { 27 val, _ := args[0].Unmark() 28 return val.Mark("sensitive"), nil 29 }, 30 }) 31 32 // NonsensitiveFunc takes a sensitive value and returns the same value without 33 // the sensitive marking, effectively exposing the value. 34 var NonsensitiveFunc = function.New(&function.Spec{ 35 Params: []function.Parameter{ 36 { 37 Name: "value", 38 Type: cty.DynamicPseudoType, 39 AllowUnknown: true, 40 AllowNull: true, 41 AllowMarked: true, 42 AllowDynamicType: true, 43 }, 44 }, 45 Type: func(args []cty.Value) (cty.Type, error) { 46 // This function only affects the value's marks, so the result 47 // type is always the same as the argument type. 48 return args[0].Type(), nil 49 }, 50 Impl: func(args []cty.Value, retType cty.Type) (ret cty.Value, err error) { 51 if args[0].IsKnown() && !args[0].HasMark("sensitive") { 52 return cty.DynamicVal, function.NewArgErrorf(0, "the given value is not sensitive, so this call is redundant") 53 } 54 v, marks := args[0].Unmark() 55 delete(marks, "sensitive") // remove the sensitive marking 56 return v.WithMarks(marks), nil 57 }, 58 }) 59 60 func Sensitive(v cty.Value) (cty.Value, error) { 61 return SensitiveFunc.Call([]cty.Value{v}) 62 } 63 64 func Nonsensitive(v cty.Value) (cty.Value, error) { 65 return NonsensitiveFunc.Call([]cty.Value{v}) 66 }