Skip to content

Commit 85e25df

Browse files
committed
remove terrible filter code
Signed-off-by: Kristoffer Dalby <[email protected]>
1 parent 23ab6cf commit 85e25df

File tree

4 files changed

+3
-351
lines changed

4 files changed

+3
-351
lines changed

Diff for: acls.go

-72
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,6 @@ func (h *Headscale) UpdateACLRules() error {
136136
log.Trace().Interface("ACL", rules).Msg("ACL rules generated")
137137
h.aclRules = rules
138138

139-
// Precompute a map of which sources can reach each destination, this is
140-
// to provide quicker lookup when we calculate the peerlist for the map
141-
// response to nodes.
142-
// aclPeerCacheMap := generateACLPeerCacheMap(rules)
143-
// h.aclPeerCacheMapRW.Lock()
144-
// h.aclPeerCacheMap = aclPeerCacheMap
145-
// h.aclPeerCacheMapRW.Unlock()
146-
147139
if featureEnableSSH() {
148140
sshRules, err := h.generateSSHRules()
149141
if err != nil {
@@ -161,70 +153,6 @@ func (h *Headscale) UpdateACLRules() error {
161153
return nil
162154
}
163155

164-
// // generateACLPeerCacheMap takes a list of Tailscale filter rules and generates a map
165-
// // of which Sources ("*" and IPs) can access destinations. This is to speed up the
166-
// // process of generating MapResponses when deciding which Peers to inform nodes about.
167-
// func generateACLPeerCacheMap(rules []tailcfg.FilterRule) map[string][]string {
168-
// aclCachePeerMap := make(map[string][]string)
169-
// for _, rule := range rules {
170-
// for _, srcIP := range rule.SrcIPs {
171-
// for _, ip := range expandACLPeerAddr(srcIP) {
172-
// if data, ok := aclCachePeerMap[ip]; ok {
173-
// for _, dstPort := range rule.DstPorts {
174-
// data = append(data, dstPort.IP)
175-
// }
176-
// aclCachePeerMap[ip] = data
177-
// } else {
178-
// dstPortsMap := make([]string, 0)
179-
// for _, dstPort := range rule.DstPorts {
180-
// dstPortsMap = append(dstPortsMap, dstPort.IP)
181-
// }
182-
// aclCachePeerMap[ip] = dstPortsMap
183-
// }
184-
// }
185-
// }
186-
// }
187-
//
188-
// log.Trace().Interface("ACL Cache Map", aclCachePeerMap).Msg("ACL Peer Cache Map generated")
189-
//
190-
// return aclCachePeerMap
191-
// }
192-
//
193-
// // expandACLPeerAddr takes a "tailcfg.FilterRule" "IP" and expands it into
194-
// // something our cache logic can look up, which is "*" or single IP addresses.
195-
// // This is probably quite inefficient, but it is a result of
196-
// // "make it work, then make it fast", and a lot of the ACL stuff does not
197-
// // work, but people have tried to make it fast.
198-
// func expandACLPeerAddr(srcIP string) []string {
199-
// if ip, err := netip.ParseAddr(srcIP); err == nil {
200-
// return []string{ip.String()}
201-
// }
202-
//
203-
// if cidr, err := netip.ParsePrefix(srcIP); err == nil {
204-
// addrs := []string{}
205-
//
206-
// ipRange := netipx.RangeOfPrefix(cidr)
207-
//
208-
// from := ipRange.From()
209-
// too := ipRange.To()
210-
//
211-
// if from == too {
212-
// return []string{from.String()}
213-
// }
214-
//
215-
// for from != too && from.Less(too) {
216-
// addrs = append(addrs, from.String())
217-
// from = from.Next()
218-
// }
219-
// addrs = append(addrs, too.String()) // Add the last IP address in the range
220-
//
221-
// return addrs
222-
// }
223-
//
224-
// // probably "*" or other string based "IP"
225-
// return []string{srcIP}
226-
// }
227-
228156
// generateFilterRules takes a set of machines and an ACLPolicy and generates a
229157
// set of Tailscale compatible FilterRules used to allow traffic on clients.
230158
func (pol *ACLPolicy) generateFilterRules(

Diff for: acls_test.go

-135
Original file line numberDiff line numberDiff line change
@@ -1661,141 +1661,6 @@ func Test_excludeCorrectlyTaggedNodes(t *testing.T) {
16611661
}
16621662
}
16631663

1664-
// func Test_expandACLPeerAddr(t *testing.T) {
1665-
// type args struct {
1666-
// srcIP string
1667-
// }
1668-
// tests := []struct {
1669-
// name string
1670-
// args args
1671-
// want []string
1672-
// }{
1673-
// {
1674-
// name: "asterix",
1675-
// args: args{
1676-
// srcIP: "*",
1677-
// },
1678-
// want: []string{"*"},
1679-
// },
1680-
// {
1681-
// name: "ip",
1682-
// args: args{
1683-
// srcIP: "10.0.0.1",
1684-
// },
1685-
// want: []string{"10.0.0.1"},
1686-
// },
1687-
// {
1688-
// name: "ip/32",
1689-
// args: args{
1690-
// srcIP: "10.0.0.1/32",
1691-
// },
1692-
// want: []string{"10.0.0.1"},
1693-
// },
1694-
// {
1695-
// name: "ip/30",
1696-
// args: args{
1697-
// srcIP: "10.0.0.1/30",
1698-
// },
1699-
// want: []string{
1700-
// "10.0.0.0",
1701-
// "10.0.0.1",
1702-
// "10.0.0.2",
1703-
// "10.0.0.3",
1704-
// },
1705-
// },
1706-
// {
1707-
// name: "ip/28",
1708-
// args: args{
1709-
// srcIP: "192.168.0.128/28",
1710-
// },
1711-
// want: []string{
1712-
// "192.168.0.128", "192.168.0.129", "192.168.0.130",
1713-
// "192.168.0.131", "192.168.0.132", "192.168.0.133",
1714-
// "192.168.0.134", "192.168.0.135", "192.168.0.136",
1715-
// "192.168.0.137", "192.168.0.138", "192.168.0.139",
1716-
// "192.168.0.140", "192.168.0.141", "192.168.0.142",
1717-
// "192.168.0.143",
1718-
// },
1719-
// },
1720-
// }
1721-
// for _, tt := range tests {
1722-
// t.Run(tt.name, func(t *testing.T) {
1723-
// if got := expandACLPeerAddr(tt.args.srcIP); !reflect.DeepEqual(got, tt.want) {
1724-
// t.Errorf("expandACLPeerAddr() = %v, want %v", got, tt.want)
1725-
// }
1726-
// })
1727-
// }
1728-
// }
1729-
1730-
// func Test_expandACLPeerAddrV6(t *testing.T) {
1731-
// type args struct {
1732-
// srcIP string
1733-
// }
1734-
// tests := []struct {
1735-
// name string
1736-
// args args
1737-
// want []string
1738-
// }{
1739-
// {
1740-
// name: "asterix",
1741-
// args: args{
1742-
// srcIP: "*",
1743-
// },
1744-
// want: []string{"*"},
1745-
// },
1746-
// {
1747-
// name: "ipfull",
1748-
// args: args{
1749-
// srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:3166",
1750-
// },
1751-
// want: []string{"fd7a:115c:a1e0:ab12:4943:cd96:624c:3166"},
1752-
// },
1753-
// {
1754-
// name: "ipzerocompression",
1755-
// args: args{
1756-
// srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c::",
1757-
// },
1758-
// want: []string{"fd7a:115c:a1e0:ab12:4943:cd96:624c:0"},
1759-
// },
1760-
// {
1761-
// name: "ip/128",
1762-
// args: args{
1763-
// srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:3166/128",
1764-
// },
1765-
// want: []string{"fd7a:115c:a1e0:ab12:4943:cd96:624c:3166"},
1766-
// },
1767-
// {
1768-
// name: "ip/127",
1769-
// args: args{
1770-
// srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:0000/127",
1771-
// },
1772-
// want: []string{
1773-
// "fd7a:115c:a1e0:ab12:4943:cd96:624c:0",
1774-
// "fd7a:115c:a1e0:ab12:4943:cd96:624c:1",
1775-
// },
1776-
// },
1777-
// {
1778-
// name: "ip/126",
1779-
// args: args{
1780-
// srcIP: "fd7a:115c:a1e0:ab12:4943:cd96:624c:0000/126",
1781-
// },
1782-
// want: []string{
1783-
// "fd7a:115c:a1e0:ab12:4943:cd96:624c:0",
1784-
// "fd7a:115c:a1e0:ab12:4943:cd96:624c:1",
1785-
// "fd7a:115c:a1e0:ab12:4943:cd96:624c:2",
1786-
// "fd7a:115c:a1e0:ab12:4943:cd96:624c:3",
1787-
// },
1788-
// },
1789-
// }
1790-
// for _, tt := range tests {
1791-
// t.Run(tt.name, func(t *testing.T) {
1792-
// if got := expandACLPeerAddr(tt.args.srcIP); !reflect.DeepEqual(got, tt.want) {
1793-
// t.Errorf("expandACLPeerAddr() = %v, want %v", got, tt.want)
1794-
// }
1795-
// })
1796-
// }
1797-
// }
1798-
17991664
func TestACLPolicy_generateFilterRules(t *testing.T) {
18001665
type field struct {
18011666
pol ACLPolicy

Diff for: app.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,9 @@ type Headscale struct {
8484
DERPMap *tailcfg.DERPMap
8585
DERPServer *DERPServer
8686

87-
aclPolicy *ACLPolicy
88-
aclRules []tailcfg.FilterRule
89-
aclPeerCacheMapRW sync.RWMutex
90-
aclPeerCacheMap map[string][]string
91-
sshPolicy *tailcfg.SSHPolicy
87+
aclPolicy *ACLPolicy
88+
aclRules []tailcfg.FilterRule
89+
sshPolicy *tailcfg.SSHPolicy
9290

9391
lastStateChange *xsync.MapOf[string, time.Time]
9492

Diff for: machine.go

-139
Original file line numberDiff line numberDiff line change
@@ -204,145 +204,6 @@ func filterMachinesByACL(
204204
return result
205205
}
206206

207-
// filterMachinesByACL returns the list of peers authorized to be accessed from a given machine.
208-
// func filterMachinesByACL(
209-
// machine *Machine,
210-
// machines Machines,
211-
// lock *sync.RWMutex,
212-
// aclPeerCacheMap map[string][]string,
213-
// ) Machines {
214-
// log.Trace().
215-
// Caller().
216-
// Str("self", machine.Hostname).
217-
// Str("input", machines.String()).
218-
// Msg("Finding peers filtered by ACLs")
219-
//
220-
// peers := make(map[uint64]Machine)
221-
// // Aclfilter peers here. We are itering through machines in all users and search through the computed aclRules
222-
// // for match between rule SrcIPs and DstPorts. If the rule is a match we allow the machine to be viewable.
223-
// machineIPs := machine.IPAddresses.ToStringSlice()
224-
//
225-
// // TODO(kradalby): Remove this lock, I suspect its not a good idea, and might not be necessary,
226-
// // we only set this at startup atm (reading ACLs) and it might become a bottleneck.
227-
// lock.RLock()
228-
//
229-
// for _, peer := range machines {
230-
// if peer.ID == machine.ID {
231-
// continue
232-
// }
233-
// peerIPs := peer.IPAddresses.ToStringSlice()
234-
//
235-
// if dstMap, ok := aclPeerCacheMap["*"]; ok {
236-
// // match source and all destination
237-
//
238-
// for _, dst := range dstMap {
239-
// if dst == "*" {
240-
// peers[peer.ID] = peer
241-
//
242-
// continue
243-
// }
244-
// }
245-
//
246-
// // match source and all destination
247-
// for _, peerIP := range peerIPs {
248-
// for _, dst := range dstMap {
249-
// _, cdr, _ := net.ParseCIDR(dst)
250-
// ip := net.ParseIP(peerIP)
251-
// if dst == peerIP || (cdr != nil && ip != nil && cdr.Contains(ip)) {
252-
// peers[peer.ID] = peer
253-
//
254-
// continue
255-
// }
256-
// }
257-
// }
258-
//
259-
// // match all sources and source
260-
// for _, machineIP := range machineIPs {
261-
// for _, dst := range dstMap {
262-
// _, cdr, _ := net.ParseCIDR(dst)
263-
// ip := net.ParseIP(machineIP)
264-
// if dst == machineIP || (cdr != nil && ip != nil && cdr.Contains(ip)) {
265-
// peers[peer.ID] = peer
266-
//
267-
// continue
268-
// }
269-
// }
270-
// }
271-
// }
272-
//
273-
// for _, machineIP := range machineIPs {
274-
// if dstMap, ok := aclPeerCacheMap[machineIP]; ok {
275-
// // match source and all destination
276-
// for _, dst := range dstMap {
277-
// if dst == "*" {
278-
// peers[peer.ID] = peer
279-
//
280-
// continue
281-
// }
282-
// }
283-
//
284-
// // match source and destination
285-
// for _, peerIP := range peerIPs {
286-
// for _, dst := range dstMap {
287-
// _, cdr, _ := net.ParseCIDR(dst)
288-
// ip := net.ParseIP(peerIP)
289-
// if dst == peerIP || (cdr != nil && ip != nil && cdr.Contains(ip)) {
290-
// peers[peer.ID] = peer
291-
//
292-
// continue
293-
// }
294-
// }
295-
// }
296-
// }
297-
// }
298-
//
299-
// for _, peerIP := range peerIPs {
300-
// if dstMap, ok := aclPeerCacheMap[peerIP]; ok {
301-
// // match source and all destination
302-
// for _, dst := range dstMap {
303-
// if dst == "*" {
304-
// peers[peer.ID] = peer
305-
//
306-
// continue
307-
// }
308-
// }
309-
//
310-
// // match return path
311-
// for _, machineIP := range machineIPs {
312-
// for _, dst := range dstMap {
313-
// _, cdr, _ := net.ParseCIDR(dst)
314-
// ip := net.ParseIP(machineIP)
315-
// if dst == machineIP || (cdr != nil && ip != nil && cdr.Contains(ip)) {
316-
// peers[peer.ID] = peer
317-
//
318-
// continue
319-
// }
320-
// }
321-
// }
322-
// }
323-
// }
324-
// }
325-
//
326-
// lock.RUnlock()
327-
//
328-
// authorizedPeers := make(Machines, 0, len(peers))
329-
// for _, m := range peers {
330-
// authorizedPeers = append(authorizedPeers, m)
331-
// }
332-
// sort.Slice(
333-
// authorizedPeers,
334-
// func(i, j int) bool { return authorizedPeers[i].ID < authorizedPeers[j].ID },
335-
// )
336-
//
337-
// log.Trace().
338-
// Caller().
339-
// Str("self", machine.Hostname).
340-
// Str("peers", authorizedPeers.String()).
341-
// Msg("Authorized peers")
342-
//
343-
// return authorizedPeers
344-
// }
345-
346207
func (h *Headscale) ListPeers(machine *Machine) (Machines, error) {
347208
log.Trace().
348209
Caller().

0 commit comments

Comments
 (0)