github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/jobspec2/functions.go (about) 1 package jobspec2 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/go-cty-funcs/cidr" 7 "github.com/hashicorp/go-cty-funcs/crypto" 8 "github.com/hashicorp/go-cty-funcs/encoding" 9 "github.com/hashicorp/go-cty-funcs/filesystem" 10 "github.com/hashicorp/go-cty-funcs/uuid" 11 "github.com/hashicorp/hcl/v2/ext/tryfunc" 12 "github.com/hashicorp/hcl/v2/ext/typeexpr" 13 ctyyaml "github.com/zclconf/go-cty-yaml" 14 "github.com/zclconf/go-cty/cty" 15 "github.com/zclconf/go-cty/cty/function" 16 "github.com/zclconf/go-cty/cty/function/stdlib" 17 ) 18 19 // Functions returns the set of functions that should be used to when 20 // evaluating expressions in the receiving scope. 21 // 22 // basedir is used with file functions and allows a user to reference a file 23 // using local path. Usually basedir is the directory in which the config file 24 // is located 25 // 26 func Functions(basedir string, allowFS bool) map[string]function.Function { 27 funcs := map[string]function.Function{ 28 "abs": stdlib.AbsoluteFunc, 29 "base64decode": encoding.Base64DecodeFunc, 30 "base64encode": encoding.Base64EncodeFunc, 31 "bcrypt": crypto.BcryptFunc, 32 "can": tryfunc.CanFunc, 33 "ceil": stdlib.CeilFunc, 34 "chomp": stdlib.ChompFunc, 35 "chunklist": stdlib.ChunklistFunc, 36 "cidrhost": cidr.HostFunc, 37 "cidrnetmask": cidr.NetmaskFunc, 38 "cidrsubnet": cidr.SubnetFunc, 39 "cidrsubnets": cidr.SubnetsFunc, 40 "coalesce": stdlib.CoalesceFunc, 41 "coalescelist": stdlib.CoalesceListFunc, 42 "compact": stdlib.CompactFunc, 43 "concat": stdlib.ConcatFunc, 44 "contains": stdlib.ContainsFunc, 45 "convert": typeexpr.ConvertFunc, 46 "csvdecode": stdlib.CSVDecodeFunc, 47 "distinct": stdlib.DistinctFunc, 48 "element": stdlib.ElementFunc, 49 "flatten": stdlib.FlattenFunc, 50 "floor": stdlib.FloorFunc, 51 "format": stdlib.FormatFunc, 52 "formatdate": stdlib.FormatDateFunc, 53 "formatlist": stdlib.FormatListFunc, 54 "indent": stdlib.IndentFunc, 55 "index": stdlib.IndexFunc, 56 "join": stdlib.JoinFunc, 57 "jsondecode": stdlib.JSONDecodeFunc, 58 "jsonencode": stdlib.JSONEncodeFunc, 59 "keys": stdlib.KeysFunc, 60 "length": stdlib.LengthFunc, 61 "log": stdlib.LogFunc, 62 "lookup": stdlib.LookupFunc, 63 "lower": stdlib.LowerFunc, 64 "max": stdlib.MaxFunc, 65 "md5": crypto.Md5Func, 66 "merge": stdlib.MergeFunc, 67 "min": stdlib.MinFunc, 68 "parseint": stdlib.ParseIntFunc, 69 "pow": stdlib.PowFunc, 70 "range": stdlib.RangeFunc, 71 "reverse": stdlib.ReverseFunc, 72 "replace": stdlib.ReplaceFunc, 73 "regex_replace": stdlib.RegexReplaceFunc, 74 "rsadecrypt": crypto.RsaDecryptFunc, 75 "setintersection": stdlib.SetIntersectionFunc, 76 "setproduct": stdlib.SetProductFunc, 77 "setunion": stdlib.SetUnionFunc, 78 "sha1": crypto.Sha1Func, 79 "sha256": crypto.Sha256Func, 80 "sha512": crypto.Sha512Func, 81 "signum": stdlib.SignumFunc, 82 "slice": stdlib.SliceFunc, 83 "sort": stdlib.SortFunc, 84 "split": stdlib.SplitFunc, 85 "strrev": stdlib.ReverseFunc, 86 "substr": stdlib.SubstrFunc, 87 "timeadd": stdlib.TimeAddFunc, 88 "title": stdlib.TitleFunc, 89 "trim": stdlib.TrimFunc, 90 "trimprefix": stdlib.TrimPrefixFunc, 91 "trimspace": stdlib.TrimSpaceFunc, 92 "trimsuffix": stdlib.TrimSuffixFunc, 93 "try": tryfunc.TryFunc, 94 "upper": stdlib.UpperFunc, 95 "urlencode": encoding.URLEncodeFunc, 96 "uuidv4": uuid.V4Func, 97 "uuidv5": uuid.V5Func, 98 "values": stdlib.ValuesFunc, 99 "yamldecode": ctyyaml.YAMLDecodeFunc, 100 "yamlencode": ctyyaml.YAMLEncodeFunc, 101 "zipmap": stdlib.ZipmapFunc, 102 103 // filesystem calls 104 "abspath": guardFS(allowFS, filesystem.AbsPathFunc), 105 "basename": guardFS(allowFS, filesystem.BasenameFunc), 106 "dirname": guardFS(allowFS, filesystem.DirnameFunc), 107 "file": guardFS(allowFS, filesystem.MakeFileFunc(basedir, false)), 108 "fileexists": guardFS(allowFS, filesystem.MakeFileExistsFunc(basedir)), 109 "fileset": guardFS(allowFS, filesystem.MakeFileSetFunc(basedir)), 110 "pathexpand": guardFS(allowFS, filesystem.PathExpandFunc), 111 } 112 113 return funcs 114 } 115 116 func guardFS(allowFS bool, fn function.Function) function.Function { 117 if allowFS { 118 return fn 119 } 120 121 spec := &function.Spec{ 122 Params: fn.Params(), 123 VarParam: fn.VarParam(), 124 Type: func([]cty.Value) (cty.Type, error) { 125 return cty.DynamicPseudoType, fmt.Errorf("filesystem function disabled") 126 }, 127 Impl: func([]cty.Value, cty.Type) (cty.Value, error) { 128 return cty.DynamicVal, fmt.Errorf("filesystem functions disabled") 129 }, 130 } 131 132 return function.New(spec) 133 }