forked from cogolabs/beyond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacl.go
134 lines (118 loc) · 2.62 KB
/
acl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package beyond
import (
"encoding/json"
"flag"
"net/http"
"path"
"sync"
)
var (
fenceURL = flag.String("fence-url", "", "URL to user fencing config (eg. https://github.com/myorg/beyond-config/main/raw/fence.json)")
sitesURL = flag.String("sites-url", "", "URL to allowed sites config (eg. https://github.com/myorg/beyond-config/main/raw/sites.json)")
allowlistURL = flag.String("allowlist-url", "", "URL to site allowlist (eg. https://github.com/myorg/beyond-config/main/raw/allowlist.json)")
fence = concurrentMapMapBool{m: map[string]map[string]bool{}}
sites = concurrentMapMapBool{m: map[string]map[string]bool{}}
allowlist = concurrentMapMapBool{m: map[string]map[string]bool{}}
httpACL = &http.Client{}
)
type concurrentMapMapBool struct {
sync.RWMutex
m map[string]map[string]bool
}
func refreshFence() error {
if *fenceURL == "" {
return nil
}
resp, err := httpACL.Get(*fenceURL)
if err != nil {
return err
}
defer resp.Body.Close()
d := map[string][]string{}
err = json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
return err
}
for k, v := range d {
if _, ok := fence.m[k]; !ok {
fence.m[k] = map[string]bool{}
}
for _, v := range v {
fence.m[k][v] = true
}
}
return nil
}
func refreshSites() error {
if *sitesURL == "" {
return nil
}
resp, err := httpACL.Get(*sitesURL)
if err != nil {
return err
}
defer resp.Body.Close()
d := map[string][]string{}
err = json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
return err
}
sites.Lock()
defer sites.Unlock()
for k, v := range d {
if _, ok := sites.m[k]; !ok {
sites.m[k] = map[string]bool{}
}
for _, v := range v {
sites.m[k][v] = true
}
}
return nil
}
func refreshAllowlist() error {
if *allowlistURL == "" {
return nil
}
resp, err := httpACL.Get(*allowlistURL)
if err != nil {
return err
}
defer resp.Body.Close()
allowlist.Lock()
defer allowlist.Unlock()
return json.NewDecoder(resp.Body).Decode(&allowlist.m)
}
func allowlisted(r *http.Request) bool {
allowlist.RLock()
allow := allowlist.m["host"][r.Host]
hostM := allowlist.m["host:method"][r.Host+":"+r.Method]
paths := allowlist.m["path"]
allowlist.RUnlock()
if allow || hostM {
return true
}
p := path.Clean(r.URL.Path)
for ; p != "/"; p = path.Dir(p) {
if paths[p] {
allow = true
}
}
return allow
}
func deny(r *http.Request, user string) bool {
fence.RLock()
zones, ok := fence.m[user]
fence.RUnlock()
if !ok || len(zones) < 1 {
return false
}
sites.RLock()
m := sites.m
sites.RUnlock()
for k := range zones {
if m[k]["https://"+r.Host] || m[k]["http://"+r.Host] {
return false
}
}
return true
}