-
Notifications
You must be signed in to change notification settings - Fork 8
/
redis.go
231 lines (188 loc) · 5.8 KB
/
redis.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package redis
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/hashicorp/errwrap"
hclog "github.com/hashicorp/go-hclog"
dbplugin "github.com/hashicorp/vault/sdk/database/dbplugin/v5"
"github.com/hashicorp/vault/sdk/database/helper/credsutil"
"github.com/mediocregopher/radix/v4"
"github.com/mediocregopher/radix/v4/resp/resp3"
)
const (
redisTypeName = "redis"
defaultRedisUserRule = `["~*", "+@read"]`
defaultTimeout = 20000 * time.Millisecond
maxKeyLength = 64
)
var _ dbplugin.Database = &RedisDB{}
// Type that combines the custom plugins Redis database connection configuration options and the Vault CredentialsProducer
// used for generating user information for the Redis database.
type RedisDB struct {
*redisDBConnectionProducer
credsutil.CredentialsProducer
}
// New implements builtinplugins.BuiltinFactory
func New() (interface{}, error) {
db := new()
// Wrap the plugin with middleware to sanitize errors
dbType := dbplugin.NewDatabaseErrorSanitizerMiddleware(db, db.secretValues)
return dbType, nil
}
func new() *RedisDB {
connProducer := &redisDBConnectionProducer{}
connProducer.Type = redisTypeName
db := &RedisDB{
redisDBConnectionProducer: connProducer,
}
return db
}
func (c *RedisDB) Initialize(ctx context.Context, req dbplugin.InitializeRequest) (dbplugin.InitializeResponse, error) {
err := c.redisDBConnectionProducer.Initialize(ctx, req.Config, req.VerifyConnection)
if err != nil {
return dbplugin.InitializeResponse{}, err
}
resp := dbplugin.InitializeResponse{
Config: req.Config,
}
return resp, nil
}
func (c *RedisDB) NewUser(ctx context.Context, req dbplugin.NewUserRequest) (dbplugin.NewUserResponse, error) {
// Grab the lock
c.Lock()
defer c.Unlock()
username, err := credsutil.GenerateUsername(
credsutil.DisplayName(req.UsernameConfig.DisplayName, maxKeyLength),
credsutil.RoleName(req.UsernameConfig.RoleName, maxKeyLength))
if err != nil {
return dbplugin.NewUserResponse{}, fmt.Errorf("failed to generate username: %w", err)
}
username = strings.ToUpper(username)
db, err := c.getConnection(ctx)
if err != nil {
return dbplugin.NewUserResponse{}, fmt.Errorf("failed to get connection: %w", err)
}
err = newUser(ctx, db, username, req)
if err != nil {
return dbplugin.NewUserResponse{}, err
}
resp := dbplugin.NewUserResponse{
Username: username,
}
return resp, nil
}
func (c *RedisDB) UpdateUser(ctx context.Context, req dbplugin.UpdateUserRequest) (dbplugin.UpdateUserResponse, error) {
if req.Password != nil {
err := c.changeUserPassword(ctx, req.Username, req.Password.NewPassword)
return dbplugin.UpdateUserResponse{}, err
}
return dbplugin.UpdateUserResponse{}, nil
}
func (c *RedisDB) DeleteUser(ctx context.Context, req dbplugin.DeleteUserRequest) (dbplugin.DeleteUserResponse, error) {
c.Lock()
defer c.Unlock()
db, err := c.getConnection(ctx)
if err != nil {
return dbplugin.DeleteUserResponse{}, fmt.Errorf("failed to make connection: %w", err)
}
// Close the database connection to ensure no new connections come in
defer func() {
if err := c.close(); err != nil {
logger := hclog.New(&hclog.LoggerOptions{})
logger.Error("defer close failed", "error", err)
}
}()
var response string
err = db.Do(ctx, radix.Cmd(&response, "ACL", "DELUSER", req.Username))
if err != nil {
return dbplugin.DeleteUserResponse{}, err
}
return dbplugin.DeleteUserResponse{}, nil
}
func newUser(ctx context.Context, db radix.Client, username string, req dbplugin.NewUserRequest) error {
statements := removeEmpty(req.Statements.Commands)
if len(statements) == 0 {
statements = append(statements, defaultRedisUserRule)
}
aclargs := []string{"SETUSER", username, "ON", ">" + req.Password}
var args []string
err := json.Unmarshal([]byte(statements[0]), &args)
if err != nil {
return errwrap.Wrapf("error unmarshalling REDIS rules in the creation statement JSON: {{err}}", err)
}
aclargs = append(aclargs, args...)
var response string
err = db.Do(ctx, radix.Cmd(&response, "ACL", aclargs...))
if err != nil {
return err
}
return nil
}
func (c *RedisDB) changeUserPassword(ctx context.Context, username, password string) error {
c.Lock()
defer c.Unlock()
db, err := c.getConnection(ctx)
if err != nil {
return err
}
// Close the database connection to ensure no new connections come in
defer func() {
if err := c.close(); err != nil {
logger := hclog.New(&hclog.LoggerOptions{})
logger.Error("defer close failed", "error", err)
}
}()
var response resp3.ArrayHeader
mn := radix.Maybe{Rcv: &response}
var redisErr resp3.SimpleError
err = db.Do(ctx, radix.Cmd(&mn, "ACL", "GETUSER", username))
if errors.As(err, &redisErr) {
return fmt.Errorf("redis error returned: %s", redisErr.Error())
}
if err != nil {
return fmt.Errorf("reset of passwords for user %s failed in changeUserPassword: %w", username, err)
}
if mn.Null {
return fmt.Errorf("changeUserPassword for user %s failed, user not found!", username)
}
var sresponse string
err = db.Do(ctx, radix.Cmd(&sresponse, "ACL", "SETUSER", username, "RESETPASS", ">"+password))
if err != nil {
return err
}
return nil
}
func removeEmpty(strs []string) []string {
var newStrs []string
for _, str := range strs {
str = strings.TrimSpace(str)
if str == "" {
continue
}
newStrs = append(newStrs, str)
}
return newStrs
}
func computeTimeout(ctx context.Context) (timeout time.Duration) {
deadline, ok := ctx.Deadline()
if ok {
return time.Until(deadline)
}
return defaultTimeout
}
func (c *RedisDB) getConnection(ctx context.Context) (radix.Client, error) {
db, err := c.Connection(ctx)
if err != nil {
return nil, err
}
return db.(radix.Client), nil
}
func (c *RedisDB) Type() (string, error) {
return redisTypeName, nil
}