-
Notifications
You must be signed in to change notification settings - Fork 0
/
userbackend.go
225 lines (200 loc) · 5.22 KB
/
userbackend.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
package doorman
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"os/exec"
"sync"
"github.com/caddyserver/caddy/v2"
"go.uber.org/zap"
"gopkg.in/fsnotify.v1"
)
var (
_ userSearcher = (*userlistBackend)(nil)
_ userSearcher = (*userSearchCommand)(nil)
ErrNoUser = fmt.Errorf("no user found")
ErrNoConnection = fmt.Errorf("no connection")
valueCommandSearcher = "command"
valueListSearcher = "list"
valueLdapSearcher = "ldap"
valueFileSearcher = "file"
)
type UserEntry struct {
Name string `json:"name,omitempty"`
UID string `json:"uid"`
Mobile string `json:"mobile"`
Telephone string `json:"telephone"`
EMail string `json:"email"`
}
func (ue *UserEntry) SMSNumber() string {
if ue.Mobile != "" {
return ue.Mobile
}
return ue.Telephone
}
type userSearcher interface {
Search(log *zap.Logger, uid string) (*UserEntry, error)
}
type userlistBackend []UserEntry
func (ulb *userlistBackend) Search(log *zap.Logger, uid string) (*UserEntry, error) {
for _, u := range *ulb {
if u.UID == uid {
return &u, nil
}
}
return nil, fmt.Errorf("%w: %s", ErrNoUser, uid)
}
type userSearchCommand struct {
Command string `json:"command"`
Args []string `json:"args,omitempty"`
UseStdin bool `json:"use_stdin"`
}
func newUserSearchCommand(cfg userSearchCommand, rpl *caddy.Replacer) *userSearchCommand {
cfg.Command = rpl.ReplaceKnown(cfg.Command, "")
for i, a := range cfg.Args {
cfg.Args[i] = rpl.ReplaceKnown(a, "")
}
return &cfg
}
func (usc *userSearchCommand) Search(log *zap.Logger, uid string) (*UserEntry, error) {
args := append([]string{}, usc.Args...)
if !usc.UseStdin {
args = append(args, uid)
}
c := exec.Command(usc.Command, args...)
stdin, err := c.StdinPipe()
if err != nil {
return nil, err
}
go func() {
defer stdin.Close()
if usc.UseStdin {
_, _ = io.WriteString(stdin, uid)
}
}()
out, err := c.CombinedOutput()
if err != nil {
return nil, err
}
var res UserEntry
if err = json.Unmarshal(out, &res); err != nil {
return nil, fmt.Errorf("the command did not return the correct json format for a user entry (got: %s): %w", string(out), err)
}
return &res, nil
}
type userfileBackend struct {
lock sync.RWMutex
Path string `json:"path"`
Watch bool `json:"watch"`
data userlistBackend
}
func (ufb *userfileBackend) start(log *zap.Logger) error {
if err := ufb.load(); err != nil {
return err
}
if ufb.Watch {
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
err = watcher.Add(ufb.Path)
if err != nil {
return err
}
go func() {
defer watcher.Close()
log.Info("start watcher", zap.String("path", ufb.Path))
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Op&fsnotify.Write == fsnotify.Write {
log.Info("user file changed", zap.String("path", ufb.Path), zap.String("op", event.Op.String()))
err := ufb.load()
if err != nil {
log.Error("cannot reload user file", zap.Error(err))
}
}
case err, ok := <-watcher.Errors:
if !ok {
return
}
log.Error("watch error occurred", zap.Error(err))
}
}
}()
}
return nil
}
func (ufb *userfileBackend) Search(log *zap.Logger, uid string) (*UserEntry, error) {
ufb.lock.RLock()
defer ufb.lock.RUnlock()
return ufb.data.Search(log, uid)
}
func (ufb *userfileBackend) load() error {
f, err := os.Open(ufb.Path)
if err != nil {
return fmt.Errorf("cannot open file (%s) for reading: %w", ufb.Path, err)
}
defer f.Close()
ufb.lock.Lock()
defer ufb.lock.Unlock()
return json.NewDecoder(f).Decode(&ufb.data)
}
type userBackends struct {
searchers []userSearcher
}
func fromUserSpecs(log *zap.Logger, bks Plugins) (*userBackends, error) {
r := caddy.NewReplacer()
res := userBackends{}
for _, b := range bks {
switch b.Type {
case valueCommandSearcher:
var d userSearchCommand
if err := json.Unmarshal(b.Spec, &d); err != nil {
return nil, fmt.Errorf("cannot unmarshal search command: %w", err)
}
res.searchers = append(res.searchers, newUserSearchCommand(d, r))
case valueListSearcher:
var d userlistBackend
if err := json.Unmarshal(b.Spec, &d); err != nil {
return nil, fmt.Errorf("cannot unmarshal userlist: %w", err)
}
res.searchers = append(res.searchers, &d)
case valueFileSearcher:
var d userfileBackend
if err := json.Unmarshal(b.Spec, &d); err != nil {
return nil, fmt.Errorf("cannot unmarshal userlist: %w", err)
}
if err := d.start(log); err != nil {
return nil, fmt.Errorf("cannot start userfile backend: %w", err)
}
res.searchers = append(res.searchers, &d)
case valueLdapSearcher:
var d ldapConfiguration
if err := json.Unmarshal(b.Spec, &d); err != nil {
return nil, fmt.Errorf("cannot unmarshal ldap config: %w", err)
}
res.searchers = append(res.searchers, d.init(r))
default:
return nil, fmt.Errorf("unknown user backend: %q", b.Type)
}
}
return &res, nil
}
func (ulb *userBackends) Search(log *zap.Logger, uid string) (*UserEntry, error) {
for _, b := range ulb.searchers {
u, err := b.Search(log, uid)
if err == nil {
return u, nil
}
if !errors.Is(err, ErrNoUser) {
return nil, err
}
}
return nil, fmt.Errorf("%w: %s", ErrNoUser, uid)
}