Skip to content

Commit b04c087

Browse files
committed
Massive very unstable rewrite to event based architecture, wouldnt use this just yet
1 parent 94d016b commit b04c087

23 files changed

+2553
-1757
lines changed

commands/start.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,14 @@ func (g *start) Check() error {
9494
}
9595

9696
func (g *start) Run() error {
97-
defer data.TearDown()
9897

98+
var err error
99+
defer func() {
100+
data.TearDown()
101+
}()
99102
error := make(chan error)
100103

101-
err := router.Setup(error, !g.noIptables)
104+
err = router.Setup(error, !g.noIptables)
102105
if err != nil {
103106
return fmt.Errorf("unable to start router: %v", err)
104107
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ require (
6060
github.com/prometheus/client_model v0.2.0 // indirect
6161
github.com/prometheus/common v0.26.0 // indirect
6262
github.com/prometheus/procfs v0.6.0 // indirect
63+
github.com/r3labs/diff v1.1.0 // indirect
6364
github.com/sirupsen/logrus v1.9.3 // indirect
6465
github.com/soheilhy/cmux v0.1.5 // indirect
6566
github.com/spf13/pflag v1.0.5 // indirect

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
210210
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
211211
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
212212
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
213+
github.com/r3labs/diff v1.1.0 h1:V53xhrbTHrWFWq3gI4b94AjgEJOerO1+1l0xyHOBi8M=
214+
github.com/r3labs/diff v1.1.0/go.mod h1:7WjXasNzi0vJetRcB/RqNl5dlIsmXcTTLmF5IoH6Xig=
213215
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
214216
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
215217
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
@@ -229,6 +231,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
229231
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
230232
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
231233
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
234+
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
232235
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
233236
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
234237
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=

internal/acls/acls.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package acls
2+
3+
type Acl struct {
4+
Mfa []string `json:",omitempty"`
5+
Allow []string `json:",omitempty"`
6+
Deny []string `json:",omitempty"`
7+
}

internal/config/config.go

+1-38
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type Config struct {
9696
ListenAddresses []string
9797
Peers map[string][]string
9898
DatabaseLocation string
99+
ETCDLogLevel string
99100
}
100101

101102
Authenticators struct {
@@ -152,44 +153,6 @@ func Values() Config {
152153
return v
153154
}
154155

155-
func GetEffectiveAcl(username string) acls.Acl {
156-
valuesLock.RLock()
157-
defer valuesLock.RUnlock()
158-
159-
var resultingACLs acls.Acl
160-
//Add the server address by default
161-
resultingACLs.Allow = []string{values.Wireguard.ServerAddress.String() + "/32"}
162-
163-
// Add dns servers if defined
164-
// Make sure we resolve the dns servers in case someone added them as domains, so that clients dont get stuck trying to use the domain dns servers to look up the dns servers
165-
// Restrict dns servers to only having 53/any by default as per #49
166-
for _, server := range values.Wireguard.DNS {
167-
resultingACLs.Allow = append(resultingACLs.Allow, fmt.Sprintf("%s 53/any", server))
168-
}
169-
170-
if allPolicy, ok := values.Acls.Policies["*"]; ok {
171-
resultingACLs.Allow = append(resultingACLs.Allow, allPolicy.Allow...)
172-
resultingACLs.Mfa = append(resultingACLs.Mfa, allPolicy.Mfa...)
173-
}
174-
175-
//If the user has any user specific rules, add those
176-
if acl, ok := values.Acls.Policies[username]; ok {
177-
resultingACLs.Allow = append(resultingACLs.Allow, acl.Allow...)
178-
resultingACLs.Mfa = append(resultingACLs.Mfa, acl.Mfa...)
179-
}
180-
181-
//This may get expensive if the user belongs to a large number of
182-
for group := range values.Acls.rGroupLookup[username] {
183-
//If the user belongs to a series of groups, grab those, and add their rules
184-
if acl, ok := values.Acls.Policies[group]; ok {
185-
resultingACLs.Allow = append(resultingACLs.Allow, acl.Allow...)
186-
resultingACLs.Mfa = append(resultingACLs.Mfa, acl.Mfa...)
187-
}
188-
}
189-
190-
return resultingACLs
191-
}
192-
193156
// Used in authentication methods that can specify user groups directly (for the moment just oidc)
194157
// Adds groups to username, even if user does not exist in the config.json file, so GetEffectiveAcls works
195158
func AddVirtualUser(username string, groups []string) {

internal/data/acls.go

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package data
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
9+
"github.com/NHAS/wag/internal/acls"
10+
"github.com/NHAS/wag/internal/config"
11+
clientv3 "go.etcd.io/etcd/client/v3"
12+
)
13+
14+
func SetAcl(effects string, policy acls.Acl, overwrite bool) error {
15+
16+
response, err := etcd.Get(context.Background(), "wag-acls-"+effects)
17+
if err != nil {
18+
return err
19+
}
20+
21+
if len(response.Kvs) > 0 && !overwrite {
22+
return errors.New("acl already exists")
23+
}
24+
25+
policyJson, _ := json.Marshal(policy)
26+
27+
_, err = etcd.Put(context.Background(), "wag-acls-"+effects, string(policyJson))
28+
29+
return err
30+
}
31+
32+
func RemoveAcl(effects string) error {
33+
_, err := etcd.Delete(context.Background(), "wag-acls-"+effects)
34+
return err
35+
}
36+
37+
func GetEffectiveAcl(username string) acls.Acl {
38+
var resultingACLs acls.Acl
39+
//Add the server address by default
40+
resultingACLs.Allow = []string{config.Values().Wireguard.ServerAddress.String() + "/32"}
41+
42+
// Add dns servers if defined
43+
// Make sure we resolve the dns servers in case someone added them as domains, so that clients dont get stuck trying to use the domain dns servers to look up the dns servers
44+
// Restrict dns servers to only having 53/any by default as per #49
45+
for _, server := range config.Values().Wireguard.DNS {
46+
resultingACLs.Allow = append(resultingACLs.Allow, fmt.Sprintf("%s 53/any", server))
47+
}
48+
49+
txn := etcd.Txn(context.Background())
50+
txn.Then(clientv3.OpGet("wag-acls-*"), clientv3.OpGet("wag-acls-"+username), clientv3.OpGet("wag-membership"))
51+
resp, err := txn.Commit()
52+
if err != nil {
53+
return acls.Acl{}
54+
}
55+
56+
// the default policy contents
57+
if resp.Responses[0].GetResponseRange().GetCount() != 0 {
58+
var acl acls.Acl
59+
60+
err := json.Unmarshal(resp.Responses[0].GetResponseRange().Kvs[0].Value, &acl)
61+
if err == nil {
62+
resultingACLs.Allow = append(resultingACLs.Allow, acl.Allow...)
63+
resultingACLs.Mfa = append(resultingACLs.Mfa, acl.Mfa...)
64+
}
65+
}
66+
67+
// User specific acls
68+
if resp.Responses[1].GetResponseRange().GetCount() != 0 {
69+
var acl acls.Acl
70+
71+
err := json.Unmarshal(resp.Responses[1].GetResponseRange().Kvs[0].Value, &acl)
72+
if err == nil {
73+
resultingACLs.Allow = append(resultingACLs.Allow, acl.Allow...)
74+
resultingACLs.Mfa = append(resultingACLs.Mfa, acl.Mfa...)
75+
}
76+
}
77+
78+
// Membership map for finding all the other policies
79+
if resp.Responses[2].GetResponseRange().GetCount() != 0 {
80+
var rGroupLookup map[string]map[string]bool
81+
82+
err = json.Unmarshal(resp.Responses[2].GetResponseRange().Kvs[0].Value, &rGroupLookup)
83+
if err == nil {
84+
85+
txn := etcd.Txn(context.Background())
86+
87+
//If the user belongs to a series of groups, grab those, and add their rules
88+
var ops []clientv3.Op
89+
for group := range rGroupLookup[username] {
90+
ops = append(ops, clientv3.OpGet("wag-acls-"+group))
91+
}
92+
93+
resp, err := txn.Then(ops...).Commit()
94+
if err != nil {
95+
return acls.Acl{}
96+
}
97+
98+
for m := range resp.Responses {
99+
r := resp.Responses[m].GetResponseRange()
100+
if r.Count > 0 {
101+
102+
var acl acls.Acl
103+
104+
err := json.Unmarshal(r.Kvs[0].Value, &acl)
105+
if err != nil {
106+
continue
107+
}
108+
109+
resultingACLs.Allow = append(resultingACLs.Allow, acl.Allow...)
110+
resultingACLs.Mfa = append(resultingACLs.Mfa, acl.Mfa...)
111+
}
112+
}
113+
114+
}
115+
}
116+
117+
return resultingACLs
118+
}

internal/data/config.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package data
2+
3+
func SetHelpMail(helpMail string) error {
4+
return nil
5+
}
6+
7+
func SetExternalAddress(externalAddress string) error {
8+
return nil
9+
}
10+
11+
func SetDNS(dns []string) error {
12+
13+
return nil
14+
}
15+
16+
func SetSessionLifetimeMinutes(lifetimeMinutes int) error {
17+
18+
return nil
19+
}
20+
21+
func SetSessionInactivityTimeoutMinutes(InactivityTimeout int) error {
22+
23+
return nil
24+
}
25+
26+
func SetLockout(accountLockout int) error {
27+
28+
return nil
29+
}

0 commit comments

Comments
 (0)