allow_list.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package nebula
  2. import (
  3. "fmt"
  4. "net"
  5. "regexp"
  6. )
  7. type AllowList struct {
  8. // The values of this cidrTree are `bool`, signifying allow/deny
  9. cidrTree *CIDR6Tree
  10. }
  11. type RemoteAllowList struct {
  12. AllowList *AllowList
  13. // Inside Range Specific, keys of this tree are inside CIDRs and values
  14. // are *AllowList
  15. insideAllowLists *CIDR6Tree
  16. }
  17. type LocalAllowList struct {
  18. AllowList *AllowList
  19. // To avoid ambiguity, all rules must be true, or all rules must be false.
  20. nameRules []AllowListNameRule
  21. }
  22. type AllowListNameRule struct {
  23. Name *regexp.Regexp
  24. Allow bool
  25. }
  26. func (al *AllowList) Allow(ip net.IP) bool {
  27. if al == nil {
  28. return true
  29. }
  30. result := al.cidrTree.MostSpecificContains(ip)
  31. switch v := result.(type) {
  32. case bool:
  33. return v
  34. default:
  35. panic(fmt.Errorf("invalid state, allowlist returned: %T %v", result, result))
  36. }
  37. }
  38. func (al *AllowList) AllowIpV4(ip uint32) bool {
  39. if al == nil {
  40. return true
  41. }
  42. result := al.cidrTree.MostSpecificContainsIpV4(ip)
  43. switch v := result.(type) {
  44. case bool:
  45. return v
  46. default:
  47. panic(fmt.Errorf("invalid state, allowlist returned: %T %v", result, result))
  48. }
  49. }
  50. func (al *AllowList) AllowIpV6(hi, lo uint64) bool {
  51. if al == nil {
  52. return true
  53. }
  54. result := al.cidrTree.MostSpecificContainsIpV6(hi, lo)
  55. switch v := result.(type) {
  56. case bool:
  57. return v
  58. default:
  59. panic(fmt.Errorf("invalid state, allowlist returned: %T %v", result, result))
  60. }
  61. }
  62. func (al *LocalAllowList) Allow(ip net.IP) bool {
  63. if al == nil {
  64. return true
  65. }
  66. return al.AllowList.Allow(ip)
  67. }
  68. func (al *LocalAllowList) AllowName(name string) bool {
  69. if al == nil || len(al.nameRules) == 0 {
  70. return true
  71. }
  72. for _, rule := range al.nameRules {
  73. if rule.Name.MatchString(name) {
  74. return rule.Allow
  75. }
  76. }
  77. // If no rules match, return the default, which is the inverse of the rules
  78. return !al.nameRules[0].Allow
  79. }
  80. func (al *RemoteAllowList) AllowUnknownVpnIp(ip net.IP) bool {
  81. if al == nil {
  82. return true
  83. }
  84. return al.AllowList.Allow(ip)
  85. }
  86. func (al *RemoteAllowList) Allow(vpnIp uint32, ip net.IP) bool {
  87. if !al.getInsideAllowList(vpnIp).Allow(ip) {
  88. return false
  89. }
  90. return al.AllowList.Allow(ip)
  91. }
  92. func (al *RemoteAllowList) AllowIpV4(vpnIp uint32, ip uint32) bool {
  93. if al == nil {
  94. return true
  95. }
  96. if !al.getInsideAllowList(vpnIp).AllowIpV4(ip) {
  97. return false
  98. }
  99. return al.AllowList.AllowIpV4(ip)
  100. }
  101. func (al *RemoteAllowList) AllowIpV6(vpnIp uint32, hi, lo uint64) bool {
  102. if al == nil {
  103. return true
  104. }
  105. if !al.getInsideAllowList(vpnIp).AllowIpV6(hi, lo) {
  106. return false
  107. }
  108. return al.AllowList.AllowIpV6(hi, lo)
  109. }
  110. func (al *RemoteAllowList) getInsideAllowList(vpnIp uint32) *AllowList {
  111. if al.insideAllowLists != nil {
  112. inside := al.insideAllowLists.MostSpecificContainsIpV4(vpnIp)
  113. if inside != nil {
  114. return inside.(*AllowList)
  115. }
  116. }
  117. return nil
  118. }