-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_handler.go
172 lines (152 loc) · 5.64 KB
/
event_handler.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
package updater
import (
"fmt"
"net/http"
"github.com/fsnotify/fsnotify"
"github.com/go-logr/logr"
rabbithole "github.com/michaelklishin/rabbit-hole/v2"
"gopkg.in/ini.v1"
)
type PasswordUpdater struct {
DefaultUserFile string
AdminFile string
Watcher *fsnotify.Watcher
WatchDir string
Done chan<- bool
Log logr.Logger
Rmqc RabbitClient
}
type RabbitClient interface {
// RabbitMQ Management API functions
GetUser(string) (*rabbithole.UserInfo, error)
PutUser(string, rabbithole.UserSettings) (*http.Response, error)
Whoami() (*rabbithole.WhoamiInfo, error)
// Field getters and setters
GetUsername() string
SetUsername(string)
SetPassword(string)
}
func (u *PasswordUpdater) HandleEvents() {
for {
select {
case event, ok := <-u.Watcher.Events:
if !ok {
u.Log.V(0).Info("watcher events channel is closed, exiting...", "directory", u.WatchDir)
u.Done <- true
return
}
if fileChanged(u.DefaultUserFile, event) {
u.Log.V(2).Info("file system event", "file", u.DefaultUserFile, "operation", event.Op.String())
// read default user username and (new) password
defaultUserCfg, err := ini.Load(u.DefaultUserFile)
if err != nil {
u.Log.Error(err, "failed to load INI data source", "file", u.DefaultUserFile)
u.Done <- true
return
}
defaultUser := defaultUserCfg.Section("").Key("default_user").String()
newPasswd := defaultUserCfg.Section("").Key("default_pass").String()
// read admin username and (old) password
adminCfg, err := ini.Load(u.AdminFile)
if err != nil {
u.Log.Error(err, "failed to load INI data source", "file", u.AdminFile)
u.Done <- true
return
}
adminSection := adminCfg.Section("default")
adminUser := adminSection.Key("username").String()
oldPasswd := adminSection.Key("password").String()
if defaultUser != adminUser {
u.Log.V(0).Info("exiting because usernames do not match",
"default-user", defaultUser, "default-user-file", u.DefaultUserFile,
"admin-user", adminUser, "admin-file", u.AdminFile)
u.Done <- true
return
}
if newPasswd == oldPasswd {
u.Log.V(2).Info("passwords already match, nothing to do", "username", defaultUser)
break
}
u.Rmqc.SetUsername(adminUser)
u.Rmqc.SetPassword(oldPasswd)
if err := u.updateInRabbitMQ(adminUser, newPasswd); err != nil {
break
}
u.Log.V(4).Info("copying new password...", "source", u.DefaultUserFile, "target", u.AdminFile)
adminSection.Key("password").SetValue(newPasswd)
if err := adminCfg.SaveTo(u.AdminFile); err != nil {
u.Log.Error(err, "failed to write new password", "file", u.AdminFile)
u.Done <- true
return
}
u.Log.V(2).Info("copied new password", "source", u.DefaultUserFile, "target", u.AdminFile)
} else {
u.Log.V(4).Info("file system event", "file", event.Name, "operation", event.Op.String())
}
case err, ok := <-u.Watcher.Errors:
if !ok {
u.Log.V(0).Info("watcher errors channel is closed, exiting...")
u.Done <- true
return
}
u.Log.Error(err, "failed to watch", "directory", u.WatchDir)
}
}
}
func fileChanged(defaultUserFile string, event fsnotify.Event) bool {
return event.Name == defaultUserFile &&
(event.Op&fsnotify.Create == fsnotify.Create ||
event.Op&fsnotify.Write == fsnotify.Write)
}
// updateInRabbitMQ sets newPasswd for existingUser in the RabbitMQ server.
// It returns an error if password cannot be updated.
func (u *PasswordUpdater) updateInRabbitMQ(existingUser, newPasswd string) error {
pathUsers := "/api/users/" + existingUser
user, err := u.Rmqc.GetUser(existingUser)
if err != nil {
return u.handleHTTPError(err, http.MethodGet, pathUsers, newPasswd)
}
// We succeeded to fetch user tags, continue to update user password.
newUserSettings := rabbithole.UserSettings{
Name: existingUser,
Tags: user.Tags,
Password: newPasswd,
HashingAlgorithm: user.HashingAlgorithm,
}
resp, err := u.Rmqc.PutUser(existingUser, newUserSettings)
if err != nil {
return u.handleHTTPError(err, http.MethodPut, pathUsers, newPasswd)
}
u.Log.V(3).Info("HTTP response", "method", http.MethodPut, "path", pathUsers, "status", resp.Status)
u.Log.V(2).Info("updated password on RabbitMQ server", "user", existingUser)
return nil
}
func (u *PasswordUpdater) handleHTTPError(err error, httpMethod, pathUsers, newPasswd string) error {
// as returned in
// https://github.com/michaelklishin/rabbit-hole/blob/1de83b96b8ba1e29afd003143a9d8a8234d4e913/client.go#L153
if err.Error() == "Error: API responded with a 401 Unauthorized" {
// Only one node in a multi node RabbitMQ cluster will update the password.
// All other nodes are expected to run into this branch.
u.Log.V(2).Info(
"HTTP request with old password returned 401 Unauthorized, therefore trying to authenticate with new password...",
"method", httpMethod, "path", pathUsers)
u.Rmqc.SetPassword(newPasswd)
return u.authenticate()
}
u.Log.Error(err, "HTTP request failed", "method", httpMethod, "path", pathUsers)
return err
}
// authenticate checks whether authentication succeeds.
// It queries /api/whoami (although it could query any other endpoint requiring basic auth).
// Returns an error if authentication fails.
func (u *PasswordUpdater) authenticate() error {
const pathWhoAmI = "/api/whoami"
_, err := u.Rmqc.Whoami()
if err != nil {
u.Log.Error(err, fmt.Sprintf("failed to GET %s with new password", pathWhoAmI))
return err
}
u.Log.V(2).Info(fmt.Sprintf(
"GET %s with new password succeeded, therefore skipping PUT %s...", pathWhoAmI, "/api/users/"+u.Rmqc.GetUsername()))
return nil
}