-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
middleware_user_logging.go
166 lines (149 loc) · 5.11 KB
/
middleware_user_logging.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
package tenant
import (
"context"
"fmt"
"time"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/kit/platform"
"go.uber.org/zap"
)
var _ influxdb.UserService = (*UserLogger)(nil)
var _ influxdb.PasswordsService = (*PasswordLogger)(nil)
type UserLogger struct {
logger *zap.Logger
userService influxdb.UserService
}
// NewUserLogger returns a logging service middleware for the User Service.
func NewUserLogger(log *zap.Logger, s influxdb.UserService) *UserLogger {
return &UserLogger{
logger: log,
userService: s,
}
}
func (l *UserLogger) CreateUser(ctx context.Context, u *influxdb.User) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to create user", zap.Error(err), dur)
return
}
l.logger.Debug("user create", dur)
}(time.Now())
return l.userService.CreateUser(ctx, u)
}
func (l *UserLogger) FindUserByID(ctx context.Context, id platform.ID) (u *influxdb.User, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
msg := fmt.Sprintf("failed to find user with ID %v", id)
l.logger.Debug(msg, zap.Error(err), dur)
return
}
l.logger.Debug("user find by ID", dur)
}(time.Now())
return l.userService.FindUserByID(ctx, id)
}
func (l *UserLogger) FindUser(ctx context.Context, filter influxdb.UserFilter) (u *influxdb.User, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to find user matching the given filter", zap.Error(err), dur)
return
}
l.logger.Debug("user find", dur)
}(time.Now())
return l.userService.FindUser(ctx, filter)
}
func (l *UserLogger) FindUsers(ctx context.Context, filter influxdb.UserFilter, opt ...influxdb.FindOptions) (users []*influxdb.User, n int, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to find users matching the given filter", zap.Error(err), dur)
return
}
l.logger.Debug("users find", dur)
}(time.Now())
return l.userService.FindUsers(ctx, filter, opt...)
}
func (l *UserLogger) UpdateUser(ctx context.Context, id platform.ID, upd influxdb.UserUpdate) (u *influxdb.User, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
l.logger.Debug("failed to update user", zap.Error(err), dur)
return
}
l.logger.Debug("user update", dur)
}(time.Now())
return l.userService.UpdateUser(ctx, id, upd)
}
func (l *UserLogger) DeleteUser(ctx context.Context, id platform.ID) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
msg := fmt.Sprintf("failed to delete user with ID %v", id)
l.logger.Debug(msg, zap.Error(err), dur)
return
}
l.logger.Debug("user create", dur)
}(time.Now())
return l.userService.DeleteUser(ctx, id)
}
func (l *UserLogger) FindPermissionForUser(ctx context.Context, id platform.ID) (ps influxdb.PermissionSet, err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
msg := fmt.Sprintf("failed to delete user with ID %v", id)
l.logger.Debug(msg, zap.Error(err), dur)
return
}
l.logger.Debug("find permission for user", dur)
}(time.Now())
return l.userService.FindPermissionForUser(ctx, id)
}
type PasswordLogger struct {
logger *zap.Logger
pwdService influxdb.PasswordsService
}
// NewPasswordLogger returns a logging service middleware for the Password Service.
func NewPasswordLogger(log *zap.Logger, s influxdb.PasswordsService) *PasswordLogger {
return &PasswordLogger{
logger: log,
pwdService: s,
}
}
func (l *PasswordLogger) SetPassword(ctx context.Context, userID platform.ID, password string) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
msg := fmt.Sprintf("failed to set password for user with ID %v", userID)
l.logger.Debug(msg, zap.Error(err), dur)
return
}
l.logger.Debug("set password", dur)
}(time.Now())
return l.pwdService.SetPassword(ctx, userID, password)
}
func (l *PasswordLogger) ComparePassword(ctx context.Context, userID platform.ID, password string) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
msg := fmt.Sprintf("failed to compare password for user with ID %v", userID)
l.logger.Debug(msg, zap.Error(err), dur)
return
}
l.logger.Debug("compare password", dur)
}(time.Now())
return l.pwdService.ComparePassword(ctx, userID, password)
}
func (l *PasswordLogger) CompareAndSetPassword(ctx context.Context, userID platform.ID, old, new string) (err error) {
defer func(start time.Time) {
dur := zap.Duration("took", time.Since(start))
if err != nil {
msg := fmt.Sprintf("failed to compare and set password for user with ID %v", userID)
l.logger.Debug(msg, zap.Error(err), dur)
return
}
l.logger.Debug("compare and set password", dur)
}(time.Now())
return l.pwdService.CompareAndSetPassword(ctx, userID, old, new)
}