-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrbac.go
231 lines (206 loc) · 4.47 KB
/
rbac.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package main
import (
"net"
"net/http"
"path/filepath"
"strings"
"sync"
"time"
"github.com/BurntSushi/toml"
"github.com/gnur/beyondauth/jwt"
log "github.com/sirupsen/logrus"
fsnotify "gopkg.in/fsnotify/fsnotify.v1"
)
// Conf is the basic config struct
type Conf struct {
sync.RWMutex
DefaultPublic bool
DisableHTTPS bool
OAuth oauthConf
Fqdn string
CookieScope string
Verbose bool
MaxTokenAge duration
Groups map[string]group
Hosts map[string]host
}
type oauthConf struct {
ClientID string
ClientSecret string
ProviderDomain string
Nonce string
}
type group struct {
Subnets []cidr
Domains []string
Users []string
}
type host struct {
Public bool
MatchSubDomains bool
AllowedGroups []string
}
type cidr struct {
net.IPNet
}
type duration struct {
time.Duration
}
func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = time.ParseDuration(string(text))
return err
}
func (c *cidr) UnmarshalText(text []byte) error {
_, subnet, err := net.ParseCIDR(string(text))
c.IPNet = *subnet
return err
}
func watchConfig(conf *Conf, path string) {
log.Debug("Starting conf watcher")
watcher, err := fsnotify.NewWatcher()
if err != nil {
log.Fatal(err)
}
log.Debug("starting watcher loop")
go func() {
for {
select {
case event := <-watcher.Events:
if event.Op&fsnotify.Create == fsnotify.Create && filepath.Base(event.Name) == "..data" {
//kubernetes configmaps get updated like this
loadConfig(conf, path, false)
} else if event.Op&fsnotify.Write == fsnotify.Write && event.Name == path {
//regular files get updated like this
loadConfig(conf, path, false)
}
case err := <-watcher.Errors:
log.Println("error:", err)
return
}
}
}()
log.WithField("path", path).Debug("adding watcher")
err = watcher.Add(filepath.Dir(path))
if err != nil {
log.Fatal(err)
}
}
func loadConfig(conf *Conf, path string, init bool) error {
log.WithField("path", path).Info("Loading config")
var c Conf
_, err := toml.DecodeFile(path, &c)
if err != nil {
log.WithField("err", err).Error("invalid config")
return err
}
if init {
*conf = c
} else {
conf.Lock()
conf.Groups = c.Groups
conf.Hosts = c.Hosts
conf.Unlock()
}
return nil
}
func (rules *Conf) requestAllowed(r *http.Request) (allowed bool, user string) {
var h host
var ok bool
var hostWithoutSub string
host := r.Header.Get("x-forwarded-host")
s := strings.SplitAfterN(host, ".", 2)
if len(s) > 1 {
hostWithoutSub = s[1]
} else {
hostWithoutSub = host
}
if h, ok = rules.Hosts[host]; !ok {
if h, ok = rules.Hosts[hostWithoutSub]; !ok || !h.MatchSubDomains {
log.WithFields(log.Fields{
"host": host,
"allowed": false,
}).Debug("host not found")
return false, ""
}
}
if h.Public {
return true, ""
}
ipHeader := r.Header.Get("x-forwarded-for")
ip := net.ParseIP(ipHeader)
if ip == nil {
log.WithFields(log.Fields{
"header": ipHeader,
"allowed": false,
}).Debug("invalid ip in header")
return false, ""
}
c, err := r.Cookie("x-beyond-auth")
if err == nil {
user, err = jwt.ValidateToken(c.Value)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"allowed": false,
}).Debug("could not validate cookie JWT")
}
}
groups := getMatchedGroups(rules.Groups, ip, user)
if hasMatch(h.AllowedGroups, groups) {
log.WithFields(log.Fields{
"allowed": true,
}).Debug("user in valid group")
return true, user
}
return false, ""
}
func hasMatch(a, b []string) bool {
for _, i := range a {
for _, a := range b {
if i == a {
return true
}
}
}
return false
}
func getMatchedGroups(matchGroups map[string]group, ip net.IP, user string) []string {
l := log.WithFields(log.Fields{
"ip": ip,
"user": user,
})
var groups []string
for name, group := range matchGroups {
if validUser(user, group.Users) || validDomain(user, group.Domains) || validIP(ip, group.Subnets) {
l.WithField("group", name).Debug("adding to group")
groups = append(groups, name)
continue
}
}
return groups
}
func validIP(ip net.IP, list []cidr) bool {
for _, b := range list {
if b.Contains(ip) {
return true
}
}
return false
}
func validDomain(a string, list []string) bool {
for _, b := range list {
if strings.HasSuffix(a, b) {
return true
}
}
return false
}
func validUser(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}