helpers.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. package exprhelpers
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "fmt"
  6. "math"
  7. "net"
  8. "net/url"
  9. "os"
  10. "path/filepath"
  11. "reflect"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/antonmedv/expr"
  17. "github.com/bluele/gcache"
  18. "github.com/c-robinson/iplib"
  19. "github.com/cespare/xxhash/v2"
  20. "github.com/davecgh/go-spew/spew"
  21. "github.com/oschwald/geoip2-golang"
  22. "github.com/oschwald/maxminddb-golang"
  23. "github.com/prometheus/client_golang/prometheus"
  24. log "github.com/sirupsen/logrus"
  25. "github.com/umahmood/haversine"
  26. "github.com/wasilibs/go-re2"
  27. "github.com/crowdsecurity/go-cs-lib/ptr"
  28. "github.com/crowdsecurity/crowdsec/pkg/cache"
  29. "github.com/crowdsecurity/crowdsec/pkg/database"
  30. "github.com/crowdsecurity/crowdsec/pkg/fflag"
  31. "github.com/crowdsecurity/crowdsec/pkg/types"
  32. )
  33. var dataFile map[string][]string
  34. var dataFileRegex map[string][]*regexp.Regexp
  35. var dataFileRe2 map[string][]*re2.Regexp
  36. // This is used to (optionally) cache regexp results for RegexpInFile operations
  37. var dataFileRegexCache map[string]gcache.Cache = make(map[string]gcache.Cache)
  38. /*prometheus*/
  39. var RegexpCacheMetrics = prometheus.NewGaugeVec(
  40. prometheus.GaugeOpts{
  41. Name: "cs_regexp_cache_size",
  42. Help: "Entries per regexp cache.",
  43. },
  44. []string{"name"},
  45. )
  46. var dbClient *database.Client
  47. var exprFunctionOptions []expr.Option
  48. var keyValuePattern = regexp.MustCompile(`(?P<key>[^=\s]+)=(?:"(?P<quoted_value>[^"\\]*(?:\\.[^"\\]*)*)"|(?P<value>[^=\s]+)|\s*)`)
  49. var geoIPCityReader *geoip2.Reader
  50. var geoIPASNReader *geoip2.Reader
  51. var geoIPRangeReader *maxminddb.Reader
  52. func GetExprOptions(ctx map[string]interface{}) []expr.Option {
  53. if len(exprFunctionOptions) == 0 {
  54. exprFunctionOptions = []expr.Option{}
  55. for _, function := range exprFuncs {
  56. exprFunctionOptions = append(exprFunctionOptions,
  57. expr.Function(function.name,
  58. function.function,
  59. function.signature...,
  60. ))
  61. }
  62. }
  63. ret := []expr.Option{}
  64. ret = append(ret, exprFunctionOptions...)
  65. ret = append(ret, expr.Env(ctx))
  66. return ret
  67. }
  68. func GeoIPInit(datadir string) error {
  69. var err error
  70. geoIPCityReader, err = geoip2.Open(filepath.Join(datadir, "GeoLite2-City.mmdb"))
  71. if err != nil {
  72. log.Errorf("unable to open GeoLite2-City.mmdb : %s", err)
  73. return err
  74. }
  75. geoIPASNReader, err = geoip2.Open(filepath.Join(datadir, "GeoLite2-ASN.mmdb"))
  76. if err != nil {
  77. log.Errorf("unable to open GeoLite2-ASN.mmdb : %s", err)
  78. return err
  79. }
  80. geoIPRangeReader, err = maxminddb.Open(filepath.Join(datadir, "GeoLite2-ASN.mmdb"))
  81. if err != nil {
  82. log.Errorf("unable to open GeoLite2-ASN.mmdb : %s", err)
  83. return err
  84. }
  85. return nil
  86. }
  87. func Init(databaseClient *database.Client) error {
  88. dataFile = make(map[string][]string)
  89. dataFileRegex = make(map[string][]*regexp.Regexp)
  90. dataFileRe2 = make(map[string][]*re2.Regexp)
  91. dbClient = databaseClient
  92. return nil
  93. }
  94. func RegexpCacheInit(filename string, CacheCfg types.DataSource) error {
  95. //cache is explicitly disabled
  96. if CacheCfg.Cache != nil && !*CacheCfg.Cache {
  97. return nil
  98. }
  99. //cache is implicitly disabled if no cache config is provided
  100. if CacheCfg.Strategy == nil && CacheCfg.TTL == nil && CacheCfg.Size == nil {
  101. return nil
  102. }
  103. //cache is enabled
  104. if CacheCfg.Size == nil {
  105. CacheCfg.Size = ptr.Of(50)
  106. }
  107. gc := gcache.New(*CacheCfg.Size)
  108. if CacheCfg.Strategy == nil {
  109. CacheCfg.Strategy = ptr.Of("LRU")
  110. }
  111. switch *CacheCfg.Strategy {
  112. case "LRU":
  113. gc = gc.LRU()
  114. case "LFU":
  115. gc = gc.LFU()
  116. case "ARC":
  117. gc = gc.ARC()
  118. default:
  119. return fmt.Errorf("unknown cache strategy '%s'", *CacheCfg.Strategy)
  120. }
  121. if CacheCfg.TTL != nil {
  122. gc.Expiration(*CacheCfg.TTL)
  123. }
  124. cache := gc.Build()
  125. dataFileRegexCache[filename] = cache
  126. return nil
  127. }
  128. // UpdateCacheMetrics is called directly by the prom handler
  129. func UpdateRegexpCacheMetrics() {
  130. RegexpCacheMetrics.Reset()
  131. for name := range dataFileRegexCache {
  132. RegexpCacheMetrics.With(prometheus.Labels{"name": name}).Set(float64(dataFileRegexCache[name].Len(true)))
  133. }
  134. }
  135. func FileInit(fileFolder string, filename string, fileType string) error {
  136. log.Debugf("init (folder:%s) (file:%s) (type:%s)", fileFolder, filename, fileType)
  137. if fileType == "" {
  138. log.Debugf("ignored file %s%s because no type specified", fileFolder, filename)
  139. return nil
  140. }
  141. ok, err := existsInFileMaps(filename, fileType)
  142. if ok {
  143. log.Debugf("ignored file %s%s because already loaded", fileFolder, filename)
  144. return nil
  145. }
  146. if err != nil {
  147. return err
  148. }
  149. filepath := filepath.Join(fileFolder, filename)
  150. file, err := os.Open(filepath)
  151. if err != nil {
  152. return err
  153. }
  154. defer file.Close()
  155. scanner := bufio.NewScanner(file)
  156. for scanner.Scan() {
  157. if strings.HasPrefix(scanner.Text(), "#") { // allow comments
  158. continue
  159. }
  160. if len(scanner.Text()) == 0 { //skip empty lines
  161. continue
  162. }
  163. switch fileType {
  164. case "regex", "regexp":
  165. if fflag.Re2RegexpInfileSupport.IsEnabled() {
  166. dataFileRe2[filename] = append(dataFileRe2[filename], re2.MustCompile(scanner.Text()))
  167. continue
  168. }
  169. dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
  170. case "string":
  171. dataFile[filename] = append(dataFile[filename], scanner.Text())
  172. }
  173. }
  174. if err := scanner.Err(); err != nil {
  175. return err
  176. }
  177. return nil
  178. }
  179. // Expr helpers
  180. func Distinct(params ...any) (any, error) {
  181. if rt := reflect.TypeOf(params[0]).Kind(); rt != reflect.Slice && rt != reflect.Array {
  182. return nil, nil
  183. }
  184. array := params[0].([]interface{})
  185. if array == nil {
  186. return []interface{}{}, nil
  187. }
  188. var exists map[any]bool = make(map[any]bool)
  189. var ret []interface{} = make([]interface{}, 0)
  190. for _, val := range array {
  191. if _, ok := exists[val]; !ok {
  192. exists[val] = true
  193. ret = append(ret, val)
  194. }
  195. }
  196. return ret, nil
  197. }
  198. func FlattenDistinct(params ...any) (any, error) {
  199. return Distinct(flatten(nil, reflect.ValueOf(params))) //nolint:asasalint
  200. }
  201. func Flatten(params ...any) (any, error) {
  202. return flatten(nil, reflect.ValueOf(params)), nil
  203. }
  204. func flatten(args []interface{}, v reflect.Value) []interface{} {
  205. if v.Kind() == reflect.Interface {
  206. v = v.Elem()
  207. }
  208. if v.Kind() == reflect.Array || v.Kind() == reflect.Slice {
  209. for i := 0; i < v.Len(); i++ {
  210. args = flatten(args, v.Index(i))
  211. }
  212. } else {
  213. args = append(args, v.Interface())
  214. }
  215. return args
  216. }
  217. func existsInFileMaps(filename string, ftype string) (bool, error) {
  218. ok := false
  219. var err error
  220. switch ftype {
  221. case "regex", "regexp":
  222. if fflag.Re2RegexpInfileSupport.IsEnabled() {
  223. _, ok = dataFileRe2[filename]
  224. } else {
  225. _, ok = dataFileRegex[filename]
  226. }
  227. case "string":
  228. _, ok = dataFile[filename]
  229. default:
  230. err = fmt.Errorf("unknown data type '%s' for : '%s'", ftype, filename)
  231. }
  232. return ok, err
  233. }
  234. //Expr helpers
  235. // func Get(arr []string, index int) string {
  236. func Get(params ...any) (any, error) {
  237. arr := params[0].([]string)
  238. index := params[1].(int)
  239. if index >= len(arr) {
  240. return "", nil
  241. }
  242. return arr[index], nil
  243. }
  244. // func Atof(x string) float64 {
  245. func Atof(params ...any) (any, error) {
  246. x := params[0].(string)
  247. log.Debugf("debug atof %s", x)
  248. ret, err := strconv.ParseFloat(x, 64)
  249. if err != nil {
  250. log.Warningf("Atof : can't convert float '%s' : %v", x, err)
  251. }
  252. return ret, nil
  253. }
  254. // func Upper(s string) string {
  255. func Upper(params ...any) (any, error) {
  256. s := params[0].(string)
  257. return strings.ToUpper(s), nil
  258. }
  259. // func Lower(s string) string {
  260. func Lower(params ...any) (any, error) {
  261. s := params[0].(string)
  262. return strings.ToLower(s), nil
  263. }
  264. // func Distance(lat1 string, long1 string, lat2 string, long2 string) (float64, error) {
  265. func Distance(params ...any) (any, error) {
  266. lat1 := params[0].(string)
  267. long1 := params[1].(string)
  268. lat2 := params[2].(string)
  269. long2 := params[3].(string)
  270. lat1f, err := strconv.ParseFloat(lat1, 64)
  271. if err != nil {
  272. log.Warningf("lat1 is not a float : %v", err)
  273. return 0.0, fmt.Errorf("lat1 is not a float : %v", err)
  274. }
  275. long1f, err := strconv.ParseFloat(long1, 64)
  276. if err != nil {
  277. log.Warningf("long1 is not a float : %v", err)
  278. return 0.0, fmt.Errorf("long1 is not a float : %v", err)
  279. }
  280. lat2f, err := strconv.ParseFloat(lat2, 64)
  281. if err != nil {
  282. log.Warningf("lat2 is not a float : %v", err)
  283. return 0.0, fmt.Errorf("lat2 is not a float : %v", err)
  284. }
  285. long2f, err := strconv.ParseFloat(long2, 64)
  286. if err != nil {
  287. log.Warningf("long2 is not a float : %v", err)
  288. return 0.0, fmt.Errorf("long2 is not a float : %v", err)
  289. }
  290. //either set of coordinates is 0,0, return 0 to avoid FPs
  291. if (lat1f == 0.0 && long1f == 0.0) || (lat2f == 0.0 && long2f == 0.0) {
  292. log.Warningf("one of the coordinates is 0,0, returning 0")
  293. return 0.0, nil
  294. }
  295. first := haversine.Coord{Lat: lat1f, Lon: long1f}
  296. second := haversine.Coord{Lat: lat2f, Lon: long2f}
  297. _, km := haversine.Distance(first, second)
  298. return km, nil
  299. }
  300. // func QueryEscape(s string) string {
  301. func QueryEscape(params ...any) (any, error) {
  302. s := params[0].(string)
  303. return url.QueryEscape(s), nil
  304. }
  305. // func PathEscape(s string) string {
  306. func PathEscape(params ...any) (any, error) {
  307. s := params[0].(string)
  308. return url.PathEscape(s), nil
  309. }
  310. // func PathUnescape(s string) string {
  311. func PathUnescape(params ...any) (any, error) {
  312. s := params[0].(string)
  313. ret, err := url.PathUnescape(s)
  314. if err != nil {
  315. log.Debugf("unable to PathUnescape '%s': %+v", s, err)
  316. return s, nil
  317. }
  318. return ret, nil
  319. }
  320. // func QueryUnescape(s string) string {
  321. func QueryUnescape(params ...any) (any, error) {
  322. s := params[0].(string)
  323. ret, err := url.QueryUnescape(s)
  324. if err != nil {
  325. log.Debugf("unable to QueryUnescape '%s': %+v", s, err)
  326. return s, nil
  327. }
  328. return ret, nil
  329. }
  330. // func File(filename string) []string {
  331. func File(params ...any) (any, error) {
  332. filename := params[0].(string)
  333. if _, ok := dataFile[filename]; ok {
  334. return dataFile[filename], nil
  335. }
  336. log.Errorf("file '%s' (type:string) not found in expr library", filename)
  337. log.Errorf("expr library : %s", spew.Sdump(dataFile))
  338. return []string{}, nil
  339. }
  340. // func RegexpInFile(data string, filename string) bool {
  341. func RegexpInFile(params ...any) (any, error) {
  342. data := params[0].(string)
  343. filename := params[1].(string)
  344. var hash uint64
  345. hasCache := false
  346. matched := false
  347. if _, ok := dataFileRegexCache[filename]; ok {
  348. hasCache = true
  349. hash = xxhash.Sum64String(data)
  350. if val, err := dataFileRegexCache[filename].Get(hash); err == nil {
  351. return val.(bool), nil
  352. }
  353. }
  354. switch fflag.Re2RegexpInfileSupport.IsEnabled() {
  355. case true:
  356. if _, ok := dataFileRe2[filename]; ok {
  357. for _, re := range dataFileRe2[filename] {
  358. if re.MatchString(data) {
  359. matched = true
  360. break
  361. }
  362. }
  363. } else {
  364. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  365. log.Errorf("expr library : %s", spew.Sdump(dataFileRe2))
  366. }
  367. case false:
  368. if _, ok := dataFileRegex[filename]; ok {
  369. for _, re := range dataFileRegex[filename] {
  370. if re.MatchString(data) {
  371. matched = true
  372. break
  373. }
  374. }
  375. } else {
  376. log.Errorf("file '%s' (type:regexp) not found in expr library", filename)
  377. log.Errorf("expr library : %s", spew.Sdump(dataFileRegex))
  378. }
  379. }
  380. if hasCache {
  381. dataFileRegexCache[filename].Set(hash, matched)
  382. }
  383. return matched, nil
  384. }
  385. // func IpInRange(ip string, ipRange string) bool {
  386. func IpInRange(params ...any) (any, error) {
  387. var err error
  388. var ipParsed net.IP
  389. var ipRangeParsed *net.IPNet
  390. ip := params[0].(string)
  391. ipRange := params[1].(string)
  392. ipParsed = net.ParseIP(ip)
  393. if ipParsed == nil {
  394. log.Debugf("'%s' is not a valid IP", ip)
  395. return false, nil
  396. }
  397. if _, ipRangeParsed, err = net.ParseCIDR(ipRange); err != nil {
  398. log.Debugf("'%s' is not a valid IP Range", ipRange)
  399. return false, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  400. }
  401. if ipRangeParsed.Contains(ipParsed) {
  402. return true, nil
  403. }
  404. return false, nil
  405. }
  406. // func IsIPV6(ip string) bool {
  407. func IsIPV6(params ...any) (any, error) {
  408. ip := params[0].(string)
  409. ipParsed := net.ParseIP(ip)
  410. if ipParsed == nil {
  411. log.Debugf("'%s' is not a valid IP", ip)
  412. return false, nil
  413. }
  414. // If it's a valid IP and can't be converted to IPv4 then it is an IPv6
  415. return ipParsed.To4() == nil, nil
  416. }
  417. // func IsIPV4(ip string) bool {
  418. func IsIPV4(params ...any) (any, error) {
  419. ip := params[0].(string)
  420. ipParsed := net.ParseIP(ip)
  421. if ipParsed == nil {
  422. log.Debugf("'%s' is not a valid IP", ip)
  423. return false, nil
  424. }
  425. return ipParsed.To4() != nil, nil
  426. }
  427. // func IsIP(ip string) bool {
  428. func IsIP(params ...any) (any, error) {
  429. ip := params[0].(string)
  430. ipParsed := net.ParseIP(ip)
  431. if ipParsed == nil {
  432. log.Debugf("'%s' is not a valid IP", ip)
  433. return false, nil
  434. }
  435. return true, nil
  436. }
  437. // func IpToRange(ip string, cidr string) string {
  438. func IpToRange(params ...any) (any, error) {
  439. ip := params[0].(string)
  440. cidr := params[1].(string)
  441. cidr = strings.TrimPrefix(cidr, "/")
  442. mask, err := strconv.Atoi(cidr)
  443. if err != nil {
  444. log.Errorf("bad cidr '%s': %s", cidr, err)
  445. return "", nil
  446. }
  447. ipAddr := net.ParseIP(ip)
  448. if ipAddr == nil {
  449. log.Errorf("can't parse IP address '%s'", ip)
  450. return "", nil
  451. }
  452. ipRange := iplib.NewNet(ipAddr, mask)
  453. if ipRange.IP() == nil {
  454. log.Errorf("can't get cidr '%s' of '%s'", cidr, ip)
  455. return "", nil
  456. }
  457. return ipRange.String(), nil
  458. }
  459. // func TimeNow() string {
  460. func TimeNow(params ...any) (any, error) {
  461. return time.Now().UTC().Format(time.RFC3339), nil
  462. }
  463. // func ParseUri(uri string) map[string][]string {
  464. func ParseUri(params ...any) (any, error) {
  465. uri := params[0].(string)
  466. ret := make(map[string][]string)
  467. u, err := url.Parse(uri)
  468. if err != nil {
  469. log.Errorf("Could not parse URI: %s", err)
  470. return ret, nil
  471. }
  472. parsed, err := url.ParseQuery(u.RawQuery)
  473. if err != nil {
  474. log.Errorf("Could not parse query uri : %s", err)
  475. return ret, nil
  476. }
  477. for k, v := range parsed {
  478. ret[k] = v
  479. }
  480. return ret, nil
  481. }
  482. // func KeyExists(key string, dict map[string]interface{}) bool {
  483. func KeyExists(params ...any) (any, error) {
  484. key := params[0].(string)
  485. dict := params[1].(map[string]interface{})
  486. _, ok := dict[key]
  487. return ok, nil
  488. }
  489. // func GetDecisionsCount(value string) int {
  490. func GetDecisionsCount(params ...any) (any, error) {
  491. value := params[0].(string)
  492. if dbClient == nil {
  493. log.Error("No database config to call GetDecisionsCount()")
  494. return 0, nil
  495. }
  496. count, err := dbClient.CountDecisionsByValue(value)
  497. if err != nil {
  498. log.Errorf("Failed to get decisions count from value '%s'", value)
  499. return 0, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  500. }
  501. return count, nil
  502. }
  503. // func GetDecisionsSinceCount(value string, since string) int {
  504. func GetDecisionsSinceCount(params ...any) (any, error) {
  505. value := params[0].(string)
  506. since := params[1].(string)
  507. if dbClient == nil {
  508. log.Error("No database config to call GetDecisionsCount()")
  509. return 0, nil
  510. }
  511. sinceDuration, err := time.ParseDuration(since)
  512. if err != nil {
  513. log.Errorf("Failed to parse since parameter '%s' : %s", since, err)
  514. return 0, nil
  515. }
  516. sinceTime := time.Now().UTC().Add(-sinceDuration)
  517. count, err := dbClient.CountDecisionsSinceByValue(value, sinceTime)
  518. if err != nil {
  519. log.Errorf("Failed to get decisions count from value '%s'", value)
  520. return 0, nil //nolint:nilerr // This helper did not return an error before the move to expr.Function, we keep this behavior for backward compatibility
  521. }
  522. return count, nil
  523. }
  524. // func LookupHost(value string) []string {
  525. func LookupHost(params ...any) (any, error) {
  526. value := params[0].(string)
  527. addresses, err := net.LookupHost(value)
  528. if err != nil {
  529. log.Errorf("Failed to lookup host '%s' : %s", value, err)
  530. return []string{}, nil
  531. }
  532. return addresses, nil
  533. }
  534. // func ParseUnixTime(value string) (time.Time, error) {
  535. func ParseUnixTime(params ...any) (any, error) {
  536. value := params[0].(string)
  537. //Splitting string here as some unix timestamp may have milliseconds and break ParseInt
  538. i, err := strconv.ParseInt(strings.Split(value, ".")[0], 10, 64)
  539. if err != nil || i <= 0 {
  540. return time.Time{}, fmt.Errorf("unable to parse %s as unix timestamp", value)
  541. }
  542. return time.Unix(i, 0), nil
  543. }
  544. // func ParseUnix(value string) string {
  545. func ParseUnix(params ...any) (any, error) {
  546. value := params[0].(string)
  547. t, err := ParseUnixTime(value)
  548. if err != nil {
  549. log.Error(err)
  550. return "", nil
  551. }
  552. return t.(time.Time).Format(time.RFC3339), nil
  553. }
  554. // func ToString(value interface{}) string {
  555. func ToString(params ...any) (any, error) {
  556. value := params[0]
  557. s, ok := value.(string)
  558. if !ok {
  559. return "", nil
  560. }
  561. return s, nil
  562. }
  563. // func GetFromStash(cacheName string, key string) (string, error) {
  564. func GetFromStash(params ...any) (any, error) {
  565. cacheName := params[0].(string)
  566. key := params[1].(string)
  567. return cache.GetKey(cacheName, key)
  568. }
  569. // func SetInStash(cacheName string, key string, value string, expiration *time.Duration) any {
  570. func SetInStash(params ...any) (any, error) {
  571. cacheName := params[0].(string)
  572. key := params[1].(string)
  573. value := params[2].(string)
  574. expiration := params[3].(*time.Duration)
  575. return cache.SetKey(cacheName, key, value, expiration), nil
  576. }
  577. func Sprintf(params ...any) (any, error) {
  578. format := params[0].(string)
  579. return fmt.Sprintf(format, params[1:]...), nil
  580. }
  581. // func Match(pattern, name string) bool {
  582. func Match(params ...any) (any, error) {
  583. var matched bool
  584. pattern := params[0].(string)
  585. name := params[1].(string)
  586. if pattern == "" {
  587. return name == "", nil
  588. }
  589. if name == "" {
  590. if pattern == "*" || pattern == "" {
  591. return true, nil
  592. }
  593. return false, nil
  594. }
  595. if pattern[0] == '*' {
  596. for i := 0; i <= len(name); i++ {
  597. matched, _ := Match(pattern[1:], name[i:])
  598. if matched.(bool) {
  599. return matched, nil
  600. }
  601. }
  602. return matched, nil
  603. }
  604. if pattern[0] == '?' || pattern[0] == name[0] {
  605. return Match(pattern[1:], name[1:])
  606. }
  607. return matched, nil
  608. }
  609. func FloatApproxEqual(params ...any) (any, error) {
  610. float1 := params[0].(float64)
  611. float2 := params[1].(float64)
  612. if math.Abs(float1-float2) < 1e-6 {
  613. return true, nil
  614. }
  615. return false, nil
  616. }
  617. func B64Decode(params ...any) (any, error) {
  618. encoded := params[0].(string)
  619. decoded, err := base64.StdEncoding.DecodeString(encoded)
  620. if err != nil {
  621. return "", err
  622. }
  623. return string(decoded), nil
  624. }
  625. func ParseKV(params ...any) (any, error) {
  626. blob := params[0].(string)
  627. target := params[1].(map[string]interface{})
  628. prefix := params[2].(string)
  629. matches := keyValuePattern.FindAllStringSubmatch(blob, -1)
  630. if matches == nil {
  631. log.Errorf("could not find any key/value pair in line")
  632. return nil, fmt.Errorf("invalid input format")
  633. }
  634. if _, ok := target[prefix]; !ok {
  635. target[prefix] = make(map[string]string)
  636. } else {
  637. _, ok := target[prefix].(map[string]string)
  638. if !ok {
  639. log.Errorf("ParseKV: target is not a map[string]string")
  640. return nil, fmt.Errorf("target is not a map[string]string")
  641. }
  642. }
  643. for _, match := range matches {
  644. key := ""
  645. value := ""
  646. for i, name := range keyValuePattern.SubexpNames() {
  647. if name == "key" {
  648. key = match[i]
  649. } else if name == "quoted_value" && match[i] != "" {
  650. value = match[i]
  651. } else if name == "value" && match[i] != "" {
  652. value = match[i]
  653. }
  654. }
  655. target[prefix].(map[string]string)[key] = value
  656. }
  657. log.Tracef("unmarshaled KV: %+v", target[prefix])
  658. return nil, nil
  659. }
  660. func Hostname(params ...any) (any, error) {
  661. hostname, err := os.Hostname()
  662. if err != nil {
  663. return "", err
  664. }
  665. return hostname, nil
  666. }