conditions.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. package policy_engine
  2. import (
  3. "fmt"
  4. "net"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "sync"
  9. "time"
  10. "github.com/seaweedfs/seaweedfs/weed/glog"
  11. )
  12. // LRUNode represents a node in the doubly-linked list for efficient LRU operations
  13. type LRUNode struct {
  14. key string
  15. value []string
  16. prev *LRUNode
  17. next *LRUNode
  18. }
  19. // NormalizedValueCache provides size-limited caching for normalized values with efficient LRU eviction
  20. type NormalizedValueCache struct {
  21. mu sync.RWMutex
  22. cache map[string]*LRUNode
  23. maxSize int
  24. head *LRUNode // Most recently used
  25. tail *LRUNode // Least recently used
  26. }
  27. // NewNormalizedValueCache creates a new normalized value cache with configurable size
  28. func NewNormalizedValueCache(maxSize int) *NormalizedValueCache {
  29. if maxSize <= 0 {
  30. maxSize = 1000 // Default size
  31. }
  32. // Create dummy head and tail nodes for easier list manipulation
  33. head := &LRUNode{}
  34. tail := &LRUNode{}
  35. head.next = tail
  36. tail.prev = head
  37. return &NormalizedValueCache{
  38. cache: make(map[string]*LRUNode),
  39. maxSize: maxSize,
  40. head: head,
  41. tail: tail,
  42. }
  43. }
  44. // Get retrieves a cached value and updates access order in O(1) time
  45. func (c *NormalizedValueCache) Get(key string) ([]string, bool) {
  46. c.mu.Lock()
  47. defer c.mu.Unlock()
  48. if node, exists := c.cache[key]; exists {
  49. // Move to head (most recently used) - O(1) operation
  50. c.moveToHead(node)
  51. return node.value, true
  52. }
  53. return nil, false
  54. }
  55. // Set stores a value in the cache with size limit enforcement in O(1) time
  56. func (c *NormalizedValueCache) Set(key string, value []string) {
  57. c.mu.Lock()
  58. defer c.mu.Unlock()
  59. if node, exists := c.cache[key]; exists {
  60. // Update existing node and move to head
  61. node.value = value
  62. c.moveToHead(node)
  63. return
  64. }
  65. // Create new node
  66. newNode := &LRUNode{
  67. key: key,
  68. value: value,
  69. }
  70. // If at max size, evict least recently used
  71. if len(c.cache) >= c.maxSize {
  72. c.evictLeastRecentlyUsed()
  73. }
  74. // Add to cache and move to head
  75. c.cache[key] = newNode
  76. c.addToHead(newNode)
  77. }
  78. // moveToHead moves a node to the head of the list (most recently used) - O(1)
  79. func (c *NormalizedValueCache) moveToHead(node *LRUNode) {
  80. c.removeNode(node)
  81. c.addToHead(node)
  82. }
  83. // addToHead adds a node right after the head - O(1)
  84. func (c *NormalizedValueCache) addToHead(node *LRUNode) {
  85. node.prev = c.head
  86. node.next = c.head.next
  87. c.head.next.prev = node
  88. c.head.next = node
  89. }
  90. // removeNode removes a node from the list - O(1)
  91. func (c *NormalizedValueCache) removeNode(node *LRUNode) {
  92. node.prev.next = node.next
  93. node.next.prev = node.prev
  94. }
  95. // removeTail removes the last node before tail (least recently used) - O(1)
  96. func (c *NormalizedValueCache) removeTail() *LRUNode {
  97. lastNode := c.tail.prev
  98. c.removeNode(lastNode)
  99. return lastNode
  100. }
  101. // evictLeastRecentlyUsed removes the least recently used item in O(1) time
  102. func (c *NormalizedValueCache) evictLeastRecentlyUsed() {
  103. tail := c.removeTail()
  104. delete(c.cache, tail.key)
  105. }
  106. // Clear clears all cached values
  107. func (c *NormalizedValueCache) Clear() {
  108. c.mu.Lock()
  109. defer c.mu.Unlock()
  110. c.cache = make(map[string]*LRUNode)
  111. c.head.next = c.tail
  112. c.tail.prev = c.head
  113. }
  114. // GetStats returns cache statistics
  115. func (c *NormalizedValueCache) GetStats() (size int, maxSize int) {
  116. c.mu.RLock()
  117. defer c.mu.RUnlock()
  118. return len(c.cache), c.maxSize
  119. }
  120. // Global cache instance with size limit
  121. var normalizedValueCache = NewNormalizedValueCache(1000)
  122. // getCachedNormalizedValues returns cached normalized values or caches new ones
  123. func getCachedNormalizedValues(value interface{}) []string {
  124. // Create a string key for caching - more efficient than fmt.Sprintf
  125. typeStr := reflect.TypeOf(value).String()
  126. cacheKey := typeStr + ":" + fmt.Sprint(value)
  127. // Try to get from cache
  128. if cached, exists := normalizedValueCache.Get(cacheKey); exists {
  129. return cached
  130. }
  131. // Not in cache, normalize and store
  132. // Use the error-handling version for better error reporting
  133. normalized, err := normalizeToStringSliceWithError(value)
  134. if err != nil {
  135. glog.Warningf("Failed to normalize policy value %v: %v", value, err)
  136. // Fallback to string conversion for backward compatibility
  137. normalized = []string{fmt.Sprintf("%v", value)}
  138. }
  139. normalizedValueCache.Set(cacheKey, normalized)
  140. return normalized
  141. }
  142. // ConditionEvaluator evaluates policy conditions
  143. type ConditionEvaluator interface {
  144. Evaluate(conditionValue interface{}, contextValues []string) bool
  145. }
  146. // StringEqualsEvaluator evaluates StringEquals conditions
  147. type StringEqualsEvaluator struct{}
  148. func (e *StringEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  149. expectedValues := getCachedNormalizedValues(conditionValue)
  150. for _, expected := range expectedValues {
  151. for _, contextValue := range contextValues {
  152. if expected == contextValue {
  153. return true
  154. }
  155. }
  156. }
  157. return false
  158. }
  159. // StringNotEqualsEvaluator evaluates StringNotEquals conditions
  160. type StringNotEqualsEvaluator struct{}
  161. func (e *StringNotEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  162. expectedValues := getCachedNormalizedValues(conditionValue)
  163. for _, expected := range expectedValues {
  164. for _, contextValue := range contextValues {
  165. if expected == contextValue {
  166. return false
  167. }
  168. }
  169. }
  170. return true
  171. }
  172. // StringLikeEvaluator evaluates StringLike conditions (supports wildcards)
  173. type StringLikeEvaluator struct{}
  174. func (e *StringLikeEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  175. patterns := getCachedNormalizedValues(conditionValue)
  176. for _, pattern := range patterns {
  177. for _, contextValue := range contextValues {
  178. if MatchesWildcard(pattern, contextValue) {
  179. return true
  180. }
  181. }
  182. }
  183. return false
  184. }
  185. // StringNotLikeEvaluator evaluates StringNotLike conditions
  186. type StringNotLikeEvaluator struct{}
  187. func (e *StringNotLikeEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  188. patterns := getCachedNormalizedValues(conditionValue)
  189. for _, pattern := range patterns {
  190. for _, contextValue := range contextValues {
  191. if MatchesWildcard(pattern, contextValue) {
  192. return false
  193. }
  194. }
  195. }
  196. return true
  197. }
  198. // NumericEqualsEvaluator evaluates NumericEquals conditions
  199. type NumericEqualsEvaluator struct{}
  200. func (e *NumericEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  201. expectedValues := getCachedNormalizedValues(conditionValue)
  202. for _, expected := range expectedValues {
  203. expectedFloat, err := strconv.ParseFloat(expected, 64)
  204. if err != nil {
  205. continue
  206. }
  207. for _, contextValue := range contextValues {
  208. contextFloat, err := strconv.ParseFloat(contextValue, 64)
  209. if err != nil {
  210. continue
  211. }
  212. if expectedFloat == contextFloat {
  213. return true
  214. }
  215. }
  216. }
  217. return false
  218. }
  219. // NumericNotEqualsEvaluator evaluates NumericNotEquals conditions
  220. type NumericNotEqualsEvaluator struct{}
  221. func (e *NumericNotEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  222. expectedValues := getCachedNormalizedValues(conditionValue)
  223. for _, expected := range expectedValues {
  224. expectedFloat, err := strconv.ParseFloat(expected, 64)
  225. if err != nil {
  226. continue
  227. }
  228. for _, contextValue := range contextValues {
  229. contextFloat, err := strconv.ParseFloat(contextValue, 64)
  230. if err != nil {
  231. continue
  232. }
  233. if expectedFloat == contextFloat {
  234. return false
  235. }
  236. }
  237. }
  238. return true
  239. }
  240. // NumericLessThanEvaluator evaluates NumericLessThan conditions
  241. type NumericLessThanEvaluator struct{}
  242. func (e *NumericLessThanEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  243. expectedValues := getCachedNormalizedValues(conditionValue)
  244. for _, expected := range expectedValues {
  245. expectedFloat, err := strconv.ParseFloat(expected, 64)
  246. if err != nil {
  247. continue
  248. }
  249. for _, contextValue := range contextValues {
  250. contextFloat, err := strconv.ParseFloat(contextValue, 64)
  251. if err != nil {
  252. continue
  253. }
  254. if contextFloat < expectedFloat {
  255. return true
  256. }
  257. }
  258. }
  259. return false
  260. }
  261. // NumericLessThanEqualsEvaluator evaluates NumericLessThanEquals conditions
  262. type NumericLessThanEqualsEvaluator struct{}
  263. func (e *NumericLessThanEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  264. expectedValues := getCachedNormalizedValues(conditionValue)
  265. for _, expected := range expectedValues {
  266. expectedFloat, err := strconv.ParseFloat(expected, 64)
  267. if err != nil {
  268. continue
  269. }
  270. for _, contextValue := range contextValues {
  271. contextFloat, err := strconv.ParseFloat(contextValue, 64)
  272. if err != nil {
  273. continue
  274. }
  275. if contextFloat <= expectedFloat {
  276. return true
  277. }
  278. }
  279. }
  280. return false
  281. }
  282. // NumericGreaterThanEvaluator evaluates NumericGreaterThan conditions
  283. type NumericGreaterThanEvaluator struct{}
  284. func (e *NumericGreaterThanEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  285. expectedValues := getCachedNormalizedValues(conditionValue)
  286. for _, expected := range expectedValues {
  287. expectedFloat, err := strconv.ParseFloat(expected, 64)
  288. if err != nil {
  289. continue
  290. }
  291. for _, contextValue := range contextValues {
  292. contextFloat, err := strconv.ParseFloat(contextValue, 64)
  293. if err != nil {
  294. continue
  295. }
  296. if contextFloat > expectedFloat {
  297. return true
  298. }
  299. }
  300. }
  301. return false
  302. }
  303. // NumericGreaterThanEqualsEvaluator evaluates NumericGreaterThanEquals conditions
  304. type NumericGreaterThanEqualsEvaluator struct{}
  305. func (e *NumericGreaterThanEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  306. expectedValues := getCachedNormalizedValues(conditionValue)
  307. for _, expected := range expectedValues {
  308. expectedFloat, err := strconv.ParseFloat(expected, 64)
  309. if err != nil {
  310. continue
  311. }
  312. for _, contextValue := range contextValues {
  313. contextFloat, err := strconv.ParseFloat(contextValue, 64)
  314. if err != nil {
  315. continue
  316. }
  317. if contextFloat >= expectedFloat {
  318. return true
  319. }
  320. }
  321. }
  322. return false
  323. }
  324. // DateEqualsEvaluator evaluates DateEquals conditions
  325. type DateEqualsEvaluator struct{}
  326. func (e *DateEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  327. expectedValues := getCachedNormalizedValues(conditionValue)
  328. for _, expected := range expectedValues {
  329. expectedTime, err := time.Parse(time.RFC3339, expected)
  330. if err != nil {
  331. continue
  332. }
  333. for _, contextValue := range contextValues {
  334. contextTime, err := time.Parse(time.RFC3339, contextValue)
  335. if err != nil {
  336. continue
  337. }
  338. if expectedTime.Equal(contextTime) {
  339. return true
  340. }
  341. }
  342. }
  343. return false
  344. }
  345. // DateNotEqualsEvaluator evaluates DateNotEquals conditions
  346. type DateNotEqualsEvaluator struct{}
  347. func (e *DateNotEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  348. expectedValues := getCachedNormalizedValues(conditionValue)
  349. for _, expected := range expectedValues {
  350. expectedTime, err := time.Parse(time.RFC3339, expected)
  351. if err != nil {
  352. continue
  353. }
  354. for _, contextValue := range contextValues {
  355. contextTime, err := time.Parse(time.RFC3339, contextValue)
  356. if err != nil {
  357. continue
  358. }
  359. if expectedTime.Equal(contextTime) {
  360. return false
  361. }
  362. }
  363. }
  364. return true
  365. }
  366. // DateLessThanEvaluator evaluates DateLessThan conditions
  367. type DateLessThanEvaluator struct{}
  368. func (e *DateLessThanEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  369. expectedValues := getCachedNormalizedValues(conditionValue)
  370. for _, expected := range expectedValues {
  371. expectedTime, err := time.Parse(time.RFC3339, expected)
  372. if err != nil {
  373. continue
  374. }
  375. for _, contextValue := range contextValues {
  376. contextTime, err := time.Parse(time.RFC3339, contextValue)
  377. if err != nil {
  378. continue
  379. }
  380. if contextTime.Before(expectedTime) {
  381. return true
  382. }
  383. }
  384. }
  385. return false
  386. }
  387. // DateLessThanEqualsEvaluator evaluates DateLessThanEquals conditions
  388. type DateLessThanEqualsEvaluator struct{}
  389. func (e *DateLessThanEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  390. expectedValues := getCachedNormalizedValues(conditionValue)
  391. for _, expected := range expectedValues {
  392. expectedTime, err := time.Parse(time.RFC3339, expected)
  393. if err != nil {
  394. continue
  395. }
  396. for _, contextValue := range contextValues {
  397. contextTime, err := time.Parse(time.RFC3339, contextValue)
  398. if err != nil {
  399. continue
  400. }
  401. if contextTime.Before(expectedTime) || contextTime.Equal(expectedTime) {
  402. return true
  403. }
  404. }
  405. }
  406. return false
  407. }
  408. // DateGreaterThanEvaluator evaluates DateGreaterThan conditions
  409. type DateGreaterThanEvaluator struct{}
  410. func (e *DateGreaterThanEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  411. expectedValues := getCachedNormalizedValues(conditionValue)
  412. for _, expected := range expectedValues {
  413. expectedTime, err := time.Parse(time.RFC3339, expected)
  414. if err != nil {
  415. continue
  416. }
  417. for _, contextValue := range contextValues {
  418. contextTime, err := time.Parse(time.RFC3339, contextValue)
  419. if err != nil {
  420. continue
  421. }
  422. if contextTime.After(expectedTime) {
  423. return true
  424. }
  425. }
  426. }
  427. return false
  428. }
  429. // DateGreaterThanEqualsEvaluator evaluates DateGreaterThanEquals conditions
  430. type DateGreaterThanEqualsEvaluator struct{}
  431. func (e *DateGreaterThanEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  432. expectedValues := getCachedNormalizedValues(conditionValue)
  433. for _, expected := range expectedValues {
  434. expectedTime, err := time.Parse(time.RFC3339, expected)
  435. if err != nil {
  436. continue
  437. }
  438. for _, contextValue := range contextValues {
  439. contextTime, err := time.Parse(time.RFC3339, contextValue)
  440. if err != nil {
  441. continue
  442. }
  443. if contextTime.After(expectedTime) || contextTime.Equal(expectedTime) {
  444. return true
  445. }
  446. }
  447. }
  448. return false
  449. }
  450. // BoolEvaluator evaluates Bool conditions
  451. type BoolEvaluator struct{}
  452. func (e *BoolEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  453. expectedValues := getCachedNormalizedValues(conditionValue)
  454. for _, expected := range expectedValues {
  455. for _, contextValue := range contextValues {
  456. if strings.ToLower(expected) == strings.ToLower(contextValue) {
  457. return true
  458. }
  459. }
  460. }
  461. return false
  462. }
  463. // IpAddressEvaluator evaluates IpAddress conditions
  464. type IpAddressEvaluator struct{}
  465. func (e *IpAddressEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  466. expectedValues := getCachedNormalizedValues(conditionValue)
  467. for _, expected := range expectedValues {
  468. _, expectedNet, err := net.ParseCIDR(expected)
  469. if err != nil {
  470. // Try parsing as single IP
  471. expectedIP := net.ParseIP(expected)
  472. if expectedIP == nil {
  473. glog.V(3).Infof("Failed to parse expected IP address: %s", expected)
  474. continue
  475. }
  476. for _, contextValue := range contextValues {
  477. contextIP := net.ParseIP(contextValue)
  478. if contextIP == nil {
  479. glog.V(3).Infof("Failed to parse IP address: %s", contextValue)
  480. continue
  481. }
  482. if contextIP.Equal(expectedIP) {
  483. return true
  484. }
  485. }
  486. } else {
  487. // CIDR network
  488. for _, contextValue := range contextValues {
  489. contextIP := net.ParseIP(contextValue)
  490. if contextIP == nil {
  491. glog.V(3).Infof("Failed to parse IP address: %s", contextValue)
  492. continue
  493. }
  494. if expectedNet.Contains(contextIP) {
  495. return true
  496. }
  497. }
  498. }
  499. }
  500. return false
  501. }
  502. // NotIpAddressEvaluator evaluates NotIpAddress conditions
  503. type NotIpAddressEvaluator struct{}
  504. func (e *NotIpAddressEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  505. expectedValues := getCachedNormalizedValues(conditionValue)
  506. for _, expected := range expectedValues {
  507. _, expectedNet, err := net.ParseCIDR(expected)
  508. if err != nil {
  509. // Try parsing as single IP
  510. expectedIP := net.ParseIP(expected)
  511. if expectedIP == nil {
  512. glog.V(3).Infof("Failed to parse expected IP address: %s", expected)
  513. continue
  514. }
  515. for _, contextValue := range contextValues {
  516. contextIP := net.ParseIP(contextValue)
  517. if contextIP == nil {
  518. glog.V(3).Infof("Failed to parse IP address: %s", contextValue)
  519. continue
  520. }
  521. if contextIP.Equal(expectedIP) {
  522. return false
  523. }
  524. }
  525. } else {
  526. // CIDR network
  527. for _, contextValue := range contextValues {
  528. contextIP := net.ParseIP(contextValue)
  529. if contextIP == nil {
  530. glog.V(3).Infof("Failed to parse IP address: %s", contextValue)
  531. continue
  532. }
  533. if expectedNet.Contains(contextIP) {
  534. return false
  535. }
  536. }
  537. }
  538. }
  539. return true
  540. }
  541. // ArnEqualsEvaluator evaluates ArnEquals conditions
  542. type ArnEqualsEvaluator struct{}
  543. func (e *ArnEqualsEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  544. expectedValues := getCachedNormalizedValues(conditionValue)
  545. for _, expected := range expectedValues {
  546. for _, contextValue := range contextValues {
  547. if expected == contextValue {
  548. return true
  549. }
  550. }
  551. }
  552. return false
  553. }
  554. // ArnLikeEvaluator evaluates ArnLike conditions
  555. type ArnLikeEvaluator struct{}
  556. func (e *ArnLikeEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  557. patterns := getCachedNormalizedValues(conditionValue)
  558. for _, pattern := range patterns {
  559. for _, contextValue := range contextValues {
  560. if MatchesWildcard(pattern, contextValue) {
  561. return true
  562. }
  563. }
  564. }
  565. return false
  566. }
  567. // NullEvaluator evaluates Null conditions
  568. type NullEvaluator struct{}
  569. func (e *NullEvaluator) Evaluate(conditionValue interface{}, contextValues []string) bool {
  570. expectedValues := getCachedNormalizedValues(conditionValue)
  571. for _, expected := range expectedValues {
  572. expectedBool := strings.ToLower(expected) == "true"
  573. contextExists := len(contextValues) > 0
  574. if expectedBool && !contextExists {
  575. return true // Key should be null and it is
  576. }
  577. if !expectedBool && contextExists {
  578. return true // Key should not be null and it isn't
  579. }
  580. }
  581. return false
  582. }
  583. // GetConditionEvaluator returns the appropriate evaluator for a condition operator
  584. func GetConditionEvaluator(operator string) (ConditionEvaluator, error) {
  585. switch operator {
  586. case "StringEquals":
  587. return &StringEqualsEvaluator{}, nil
  588. case "StringNotEquals":
  589. return &StringNotEqualsEvaluator{}, nil
  590. case "StringLike":
  591. return &StringLikeEvaluator{}, nil
  592. case "StringNotLike":
  593. return &StringNotLikeEvaluator{}, nil
  594. case "NumericEquals":
  595. return &NumericEqualsEvaluator{}, nil
  596. case "NumericNotEquals":
  597. return &NumericNotEqualsEvaluator{}, nil
  598. case "NumericLessThan":
  599. return &NumericLessThanEvaluator{}, nil
  600. case "NumericLessThanEquals":
  601. return &NumericLessThanEqualsEvaluator{}, nil
  602. case "NumericGreaterThan":
  603. return &NumericGreaterThanEvaluator{}, nil
  604. case "NumericGreaterThanEquals":
  605. return &NumericGreaterThanEqualsEvaluator{}, nil
  606. case "DateEquals":
  607. return &DateEqualsEvaluator{}, nil
  608. case "DateNotEquals":
  609. return &DateNotEqualsEvaluator{}, nil
  610. case "DateLessThan":
  611. return &DateLessThanEvaluator{}, nil
  612. case "DateLessThanEquals":
  613. return &DateLessThanEqualsEvaluator{}, nil
  614. case "DateGreaterThan":
  615. return &DateGreaterThanEvaluator{}, nil
  616. case "DateGreaterThanEquals":
  617. return &DateGreaterThanEqualsEvaluator{}, nil
  618. case "Bool":
  619. return &BoolEvaluator{}, nil
  620. case "IpAddress":
  621. return &IpAddressEvaluator{}, nil
  622. case "NotIpAddress":
  623. return &NotIpAddressEvaluator{}, nil
  624. case "ArnEquals":
  625. return &ArnEqualsEvaluator{}, nil
  626. case "ArnLike":
  627. return &ArnLikeEvaluator{}, nil
  628. case "Null":
  629. return &NullEvaluator{}, nil
  630. default:
  631. return nil, fmt.Errorf("unsupported condition operator: %s", operator)
  632. }
  633. }
  634. // EvaluateConditions evaluates all conditions in a policy statement
  635. func EvaluateConditions(conditions PolicyConditions, contextValues map[string][]string) bool {
  636. if len(conditions) == 0 {
  637. return true // No conditions means always true
  638. }
  639. for operator, conditionMap := range conditions {
  640. conditionEvaluator, err := GetConditionEvaluator(operator)
  641. if err != nil {
  642. glog.Warningf("Unsupported condition operator: %s", operator)
  643. continue
  644. }
  645. for key, value := range conditionMap {
  646. contextVals, exists := contextValues[key]
  647. if !exists {
  648. contextVals = []string{}
  649. }
  650. if !conditionEvaluator.Evaluate(value.Strings(), contextVals) {
  651. return false // If any condition fails, the whole condition block fails
  652. }
  653. }
  654. }
  655. return true
  656. }
  657. // EvaluateConditionsLegacy evaluates conditions using the old interface{} format for backward compatibility
  658. func EvaluateConditionsLegacy(conditions map[string]interface{}, contextValues map[string][]string) bool {
  659. if len(conditions) == 0 {
  660. return true // No conditions means always true
  661. }
  662. for operator, conditionMap := range conditions {
  663. conditionEvaluator, err := GetConditionEvaluator(operator)
  664. if err != nil {
  665. glog.Warningf("Unsupported condition operator: %s", operator)
  666. continue
  667. }
  668. conditionMapTyped, ok := conditionMap.(map[string]interface{})
  669. if !ok {
  670. glog.Warningf("Invalid condition format for operator: %s", operator)
  671. continue
  672. }
  673. for key, value := range conditionMapTyped {
  674. contextVals, exists := contextValues[key]
  675. if !exists {
  676. contextVals = []string{}
  677. }
  678. if !conditionEvaluator.Evaluate(value, contextVals) {
  679. return false // If any condition fails, the whole condition block fails
  680. }
  681. }
  682. }
  683. return true
  684. }