-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhost.go
266 lines (243 loc) · 7.27 KB
/
host.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package backend
import (
"fmt"
"strings"
"time"
)
const AUTHFILE = ".ssh/authorized_keys"
// Host holds and manages a host entry in the config
type Host struct {
Host string `json:"host"`
User string `json:"user"`
Key string `json:"key"`
Protected map[string]struct{} `json:"protected"`
Alias string `json:"alias"`
Config *Storage `json:"-"`
Users []*User `json:"userlist"`
Groups []string `json:"groups"`
LastUpdated time.Time `json:"last_updated"`
Checksum string `json:"checksum"`
Modified bool `json:"modified"`
}
// ReadUsers reads ssh entries from the host's authorized_keys file,
// returning user list, checksum and error
func (h *Host) ReadUsers() ([]*User, string, error) {
lines, err := h.read()
sum := checksum(strings.Join(lines, "\n"))
if err != nil {
return nil, sum, err
}
var userlist []*User
for _, line := range lines {
parts := strings.Split(line, " ")
if len(parts) == 3 {
lsum := checksum(parts[1])
email := parts[2] + "@" + h.Alias
user := h.Config.GetUserByKey(lsum)
if user == nil {
user = NewUser(email, parts[0], parts[1], parts[2])
user.Config = h.Config
}
userlist = append(userlist, user)
}
}
return userlist, sum, nil
}
// UpdateUsersList sets the host's user list
func (h *Host) UpdateUsersList(userlist []*User) error {
if h.Modified {
return fmt.Errorf("host has been modified since last update, please refresh host first")
}
if len(userlist) > 0 && len(h.Protected) == 0 {
// right after adding to host, all users should be protected (then we can untick them as we please)
for _, u := range userlist {
h.Protected = map[string]struct{}{u.Key: {}}
}
}
uList := []*User{}
for _, user := range userlist {
if !h.Config.FromGroup(h, user.Email) {
uList = append(uList, user)
}
}
h.Users = uList
h.LastUpdated = time.Now()
h.Config.Write()
return nil
}
// connects to host via ssh, downloads authorized_keys file content, updates Checksum field
func (h *Host) read() ([]string, error) {
if h.Config == nil {
return nil, fmt.Errorf("host is nil")
}
err := h.Config.Conn.Connect(h.Key, h.Host, h.User)
if err != nil {
return nil, fmt.Errorf("error connecting %s: %v", h.Alias, err)
}
defer h.Config.Conn.Close()
b, err := h.Config.Conn.Read(AUTHFILE)
if err != nil {
return nil, fmt.Errorf("error reading authorized keys on %s: %v", h.Alias, err)
}
lines := deleteEmpty(strings.Split(string(b), "\n"))
h.Checksum = checksum(strings.Join(lines, "\n"))
return lines, nil
}
// connects to host via ssh, uploads new authorized_keys file, updates Modified, LastUpdated and Checksum field
func (h *Host) write(lines []string) error {
ls, err := h.read()
if err != nil {
return fmt.Errorf("error connecting %s: %v", h.Alias, err)
}
if checksum(strings.Join(ls, "\n")) != h.Checksum {
return fmt.Errorf("host's authorized_keys file was modified since last update, please refresh hosts first")
}
if len(lines) == 0 {
return fmt.Errorf("no keys in new file for host '%s', host would be inaccessible", h.Alias)
}
err = h.Config.Conn.Connect(h.Key, h.Host, h.User)
if err != nil {
return fmt.Errorf("error connecting %s: %v", h.Alias, err)
}
defer h.Config.Conn.Close()
err = h.Config.Conn.Write(AUTHFILE, strings.Join(lines, "\n")+"\n")
if err != nil {
return err
}
h.LastUpdated = time.Now()
h.Modified = false
h.Checksum = checksum(strings.Join(lines, "\n"))
return nil
}
// GetUsers is a getter for host's Users
func (h *Host) GetUsers() []*User {
return h.Users
}
// GetGroups is a getter for host's Groups
func (h *Host) GetGroups() []string {
return h.Groups
}
// SetGroups is a setter for host's Groups (overwrite all groups at once)
func (h *Host) SetGroups(groups []string) {
h.Groups = groups
}
// HasMatchingGroups checks if the host has any matching groups with the user
func (h *Host) HasMatchingGroups(user *User) bool {
return match(h.GetGroups(), user.GetGroups())
}
// HasUser checks if the host has a user with the given email
func (h *Host) HasUser(email string) bool {
for _, u := range h.Users {
if u.Email == email {
return true
}
}
return false
}
// AddUser adds a user to the host's authorized_keys file
func (h *Host) AddUser(u *User) error {
if h.LastUpdated.IsZero() {
return fmt.Errorf("host was never read, can't update, please refresh hosts first")
}
h.Users = append(h.Users, u)
return h.Upload()
}
// Upload new authorized_keys file content
func (h *Host) Upload() error {
var lines []string
for _, user := range h.Users {
// double-check old user entries
lines = append(lines, user.KeyType+" "+user.Key+" "+user.Name)
}
lines = deleteEmpty(lines)
h.Config.Log().Infof("updating %s", h.Alias)
// return nil
return h.write(lines)
}
// RemoveUser removes a user from the host's authorized_keys file
func (h *Host) RemoveUser(u *User) error {
if h.LastUpdated.IsZero() {
return fmt.Errorf("host was never read, can't update, please refresh hosts first")
}
if u == nil {
return fmt.Errorf("user is nil")
}
userlist := []*User{}
for _, user := range h.Users {
if user.Key == u.Key {
if _, protected := h.Protected[user.Key]; protected {
return fmt.Errorf("user is protected, please remove protection first")
}
h.Config.Log().Infof("removing %s from %s", u.Email, h.Alias)
h.Modified = true
continue
}
userlist = append(userlist, user)
}
h.Users = userlist
if h.Modified {
return h.Upload()
}
return nil
}
// DueGroup answers the question if user is on host due to group membership
func (h *Host) DueGroup(u *User) bool {
diff := Difference(u.Groups, h.Groups)
if len(diff[0]) < len(u.Groups) {
return true
}
if len(diff[1]) < len(h.Groups) {
return true
}
return false
}
// UpdateGroups updates the host's groups based on old groups
func (h *Host) UpdateGroups(cfg *Storage, oldgroups []string) bool {
added, removed := splitUpdates(oldgroups, h.Groups)
h.Config.Log().Infof("added: %v removed: %v", added, removed)
success := processHostAdded(added, cfg, h)
// are there other groups that keep user on host
success = processHostRemoved(removed, cfg, h, success)
cfg.SetHost(h.Alias, h)
cfg.Write()
return success
}
func processHostRemoved(removed []string, cfg Config, h *Host, success bool) bool {
for _, group := range removed {
users := cfg.GetUsers(group)
for _, u := range users {
if h.HasMatchingGroups(u) {
continue
}
if h.HasUser(u.Email) {
err := h.RemoveUser(u)
if err != nil {
cfg.Log().Errorf("error removing %s from %s", u.Email, h.Alias)
success = false
continue
}
cfg.Log().Infof("removed %s from %s", u.Email, h.Alias)
}
}
}
return success
}
func processHostAdded(added []string, cfg *Storage, h *Host) bool {
success := true
for _, group := range added {
users := cfg.GetUsers(group)
for _, u := range users {
if !h.HasUser(u.Email) {
h.Config.Log().Infof("Adding %s (group %s) to %s", u.Email, group, h.Alias)
err := h.AddUser(u)
if err != nil {
h.Config.Log().Errorf("error adding %s to %s: %v", u.Email, h.Alias, err)
success = false
continue
}
h.Config.Log().Infof("Added %s to %s (host groups %v)", u.Email, h.Alias, h.Groups)
}
}
}
return success
}