Debian Codegraph @

allow_list.go

raw
1 package nebula
2
3 import (
4 "fmt"
5 "net/netip"
6 "regexp"
7
8 "github.com/gaissmai/bart"
9 "github.com/slackhq/nebula/config"
10 )
11
12 type AllowList struct {
13 // The values of this cidrTree are `bool`, signifying allow/deny
14 cidrTree *bart.Table[bool]
15 }
16
17 type RemoteAllowList struct {
18 AllowList *AllowList
19
20 // Inside Range Specific, keys of this tree are inside CIDRs and values
21 // are *AllowList
22 insideAllowLists *bart.Table[*AllowList]
23 }
24
25 type LocalAllowList struct {
26 AllowList *AllowList
27
28 // To avoid ambiguity, all rules must be true, or all rules must be false.
29 nameRules []AllowListNameRule
30 }
31
32 type AllowListNameRule struct {
33 Name *regexp.Regexp
34 Allow bool
35 }
36
37 func NewLocalAllowListFromConfig(c *config.C, k string) (*LocalAllowList, error) {
38 var nameRules []AllowListNameRule
39 handleKey := func(key string, value any) (bool, error) {
40 if key == "interfaces" {
41 var err error
42 nameRules, err = getAllowListInterfaces(k, value)
43 if err != nil {
44 return false, err
45 }
46
47 return true, nil
48 }
49 return false, nil
50 }
51
52 al, err := newAllowListFromConfig(c, k, handleKey)
53 if err != nil {
54 return nil, err
55 }
56 return &LocalAllowList{AllowList: al, nameRules: nameRules}, nil
57 }
58
59 func NewRemoteAllowListFromConfig(c *config.C, k, rangesKey string) (*RemoteAllowList, error) {
60 al, err := newAllowListFromConfig(c, k, nil)
61 if err != nil {
62 return nil, err
63 }
64 remoteAllowRanges, err := getRemoteAllowRanges(c, rangesKey)
65 if err != nil {
66 return nil, err
67 }
68 return &RemoteAllowList{AllowList: al, insideAllowLists: remoteAllowRanges}, nil
69 }
70
71 // If the handleKey func returns true, the rest of the parsing is skipped
72 // for this key. This allows parsing of special values like `interfaces`.
73 func newAllowListFromConfig(c *config.C, k string, handleKey func(key string, value any) (bool, error)) (*AllowList, error) {
74 r := c.Get(k)
75 if r == nil {
76 return nil, nil
77 }
78
79 return newAllowList(k, r, handleKey)
80 }
81
82 // If the handleKey func returns true, the rest of the parsing is skipped
83 // for this key. This allows parsing of special values like `interfaces`.
84 func newAllowList(k string, raw any, handleKey func(key string, value any) (bool, error)) (*AllowList, error) {
85 rawMap, ok := raw.(map[string]any)
86 if !ok {
87 return nil, fmt.Errorf("config `%s` has invalid type: %T", k, raw)
88 }
89
90 tree := new(bart.Table[bool])
91
92 // Keep track of the rules we have added for both ipv4 and ipv6
93 type allowListRules struct {
94 firstValue bool
95 allValuesMatch bool
96 defaultSet bool
97 allValues bool
98 }
99
100 rules4 := allowListRules{firstValue: true, allValuesMatch: true, defaultSet: false}
101 rules6 := allowListRules{firstValue: true, allValuesMatch: true, defaultSet: false}
102
103 for rawCIDR, rawValue := range rawMap {
104 if handleKey != nil {
105 handled, err := handleKey(rawCIDR, rawValue)
106 if err != nil {
107 return nil, err
108 }
109 if handled {
110 continue
111 }
112 }
113
114 value, ok := config.AsBool(rawValue)
115 if !ok {
116 return nil, fmt.Errorf("config `%s` has invalid value (type %T): %v", k, rawValue, rawValue)
117 }
118
119 ipNet, err := netip.ParsePrefix(rawCIDR)
120 if err != nil {
121 return nil, fmt.Errorf("config `%s` has invalid CIDR: %s. %w", k, rawCIDR, err)
122 }
123
124 ipNet = netip.PrefixFrom(ipNet.Addr().Unmap(), ipNet.Bits())
125
126 tree.Insert(ipNet, value)
127
128 maskBits := ipNet.Bits()
129
130 var rules *allowListRules
131 if ipNet.Addr().Is4() {
132 rules = &rules4
133 } else {
134 rules = &rules6
135 }
136
137 if rules.firstValue {
138 rules.allValues = value
139 rules.firstValue = false
140 } else {
141 if value != rules.allValues {
142 rules.allValuesMatch = false
143 }
144 }
145
146 // Check if this is 0.0.0.0/0 or ::/0
147 if maskBits == 0 {
148 rules.defaultSet = true
149 }
150 }
151
152 if !rules4.defaultSet {
153 if rules4.allValuesMatch {
154 tree.Insert(netip.PrefixFrom(netip.IPv4Unspecified(), 0), !rules4.allValues)
155 } else {
156 return nil, fmt.Errorf("config `%s` contains both true and false rules, but no default set for 0.0.0.0/0", k)
157 }
158 }
159
160 if !rules6.defaultSet {
161 if rules6.allValuesMatch {
162 tree.Insert(netip.PrefixFrom(netip.IPv6Unspecified(), 0), !rules6.allValues)
163 } else {
164 return nil, fmt.Errorf("config `%s` contains both true and false rules, but no default set for ::/0", k)
165 }
166 }
167
168 return &AllowList{cidrTree: tree}, nil
169 }
170
171 func getAllowListInterfaces(k string, v any) ([]AllowListNameRule, error) {
172 var nameRules []AllowListNameRule
173
174 rawRules, ok := v.(map[string]any)
175 if !ok {
176 return nil, fmt.Errorf("config `%s.interfaces` is invalid (type %T): %v", k, v, v)
177 }
178
179 firstEntry := true
180 var allValues bool
181 for name, rawAllow := range rawRules {
182 allow, ok := config.AsBool(rawAllow)
183 if !ok {
184 return nil, fmt.Errorf("config `%s.interfaces` has invalid value (type %T): %v", k, rawAllow, rawAllow)
185 }
186
187 nameRE, err := regexp.Compile("^" + name + "$")
188 if err != nil {
189 return nil, fmt.Errorf("config `%s.interfaces` has invalid key: %s: %v", k, name, err)
190 }
191
192 nameRules = append(nameRules, AllowListNameRule{
193 Name: nameRE,
194 Allow: allow,
195 })
196
197 if firstEntry {
198 allValues = allow
199 firstEntry = false
200 } else {
201 if allow != allValues {
202 return nil, fmt.Errorf("config `%s.interfaces` values must all be the same true/false value", k)
203 }
204 }
205 }
206
207 return nameRules, nil
208 }
209
210 func getRemoteAllowRanges(c *config.C, k string) (*bart.Table[*AllowList], error) {
211 value := c.Get(k)
212 if value == nil {
213 return nil, nil
214 }
215
216 remoteAllowRanges := new(bart.Table[*AllowList])
217
218 rawMap, ok := value.(map[string]any)
219 if !ok {
220 return nil, fmt.Errorf("config `%s` has invalid type: %T", k, value)
221 }
222 for rawCIDR, rawValue := range rawMap {
223 allowList, err := newAllowList(fmt.Sprintf("%s.%s", k, rawCIDR), rawValue, nil)
224 if err != nil {
225 return nil, err
226 }
227
228 ipNet, err := netip.ParsePrefix(rawCIDR)
229 if err != nil {
230 return nil, fmt.Errorf("config `%s` has invalid CIDR: %s. %w", k, rawCIDR, err)
231 }
232
233 remoteAllowRanges.Insert(netip.PrefixFrom(ipNet.Addr().Unmap(), ipNet.Bits()), allowList)
234 }
235
236 return remoteAllowRanges, nil
237 }
238
239 func (al *AllowList) Allow(addr netip.Addr) bool {
240 if al == nil {
241 return true
242 }
243
244 result, _ := al.cidrTree.Lookup(addr)
245 return result
246 }
247
248 func (al *LocalAllowList) Allow(udpAddr netip.Addr) bool {
249 if al == nil {
250 return true
251 }
252 return al.AllowList.Allow(udpAddr)
253 }
254
255 func (al *LocalAllowList) AllowName(name string) bool {
256 if al == nil || len(al.nameRules) == 0 {
257 return true
258 }
259
260 for _, rule := range al.nameRules {
261 if rule.Name.MatchString(name) {
262 return rule.Allow
263 }
264 }
265
266 // If no rules match, return the default, which is the inverse of the rules
267 return !al.nameRules[0].Allow
268 }
269
270 func (al *RemoteAllowList) AllowUnknownVpnAddr(vpnAddr netip.Addr) bool {
271 if al == nil {
272 return true
273 }
274 return al.AllowList.Allow(vpnAddr)
275 }
276
277 func (al *RemoteAllowList) Allow(vpnAddr netip.Addr, udpAddr netip.Addr) bool {
278 if !al.getInsideAllowList(vpnAddr).Allow(udpAddr) {
279 return false
280 }
281 return al.AllowList.Allow(udpAddr)
282 }
283
284 func (al *RemoteAllowList) AllowAll(vpnAddrs []netip.Addr, udpAddr netip.Addr) bool {
285 if !al.AllowList.Allow(udpAddr) {
286 return false
287 }
288
289 for _, vpnAddr := range vpnAddrs {
290 if !al.getInsideAllowList(vpnAddr).Allow(udpAddr) {
291 return false
292 }
293 }
294
295 return true
296 }
297
298 func (al *RemoteAllowList) getInsideAllowList(vpnAddr netip.Addr) *AllowList {
299 if al.insideAllowLists != nil {
300 inside, ok := al.insideAllowLists.Lookup(vpnAddr)
301 if ok {
302 return inside
303 }
304 }
305 return nil
306 }