-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconnection_producer.go
170 lines (142 loc) · 3.74 KB
/
connection_producer.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package redis
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"strconv"
"sync"
"github.com/hashicorp/vault/sdk/database/helper/connutil"
"github.com/mediocregopher/radix/v4"
"github.com/mitchellh/mapstructure"
)
type redisDBConnectionProducer struct {
Host string `json:"host"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
TLS bool `json:"tls"`
InsecureTLS bool `json:"insecure_tls"`
CACert string `json:"ca_cert"`
Initialized bool
rawConfig map[string]interface{}
Type string
client radix.Client
Addr string
sync.Mutex
}
func (c *redisDBConnectionProducer) secretValues() map[string]string {
return map[string]string{
c.Password: "[password]",
c.Username: "[username]",
}
}
func (c *redisDBConnectionProducer) Init(ctx context.Context, initConfig map[string]interface{}, verifyConnection bool) (saveConfig map[string]interface{}, err error) {
c.Lock()
defer c.Unlock()
c.rawConfig = initConfig
decoderConfig := &mapstructure.DecoderConfig{
Result: c,
WeaklyTypedInput: true,
TagName: "json",
}
decoder, err := mapstructure.NewDecoder(decoderConfig)
if err != nil {
return nil, err
}
err = decoder.Decode(initConfig)
if err != nil {
return nil, err
}
switch {
case len(c.Host) == 0:
return nil, fmt.Errorf("host cannot be empty")
case c.Port == 0:
return nil, fmt.Errorf("port cannot be empty")
case len(c.Username) == 0:
return nil, fmt.Errorf("username cannot be empty")
case len(c.Password) == 0:
return nil, fmt.Errorf("password cannot be empty")
}
c.Addr = net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
if c.TLS {
if len(c.CACert) == 0 {
return nil, fmt.Errorf("ca_cert cannot be empty")
}
}
c.Initialized = true
if verifyConnection {
if _, err := c.Connection(ctx); err != nil {
c.close()
return nil, fmt.Errorf("error verifying connection: %w", err)
}
}
return initConfig, nil
}
func (c *redisDBConnectionProducer) Initialize(ctx context.Context, config map[string]interface{}, verifyConnection bool) error {
_, err := c.Init(ctx, config, verifyConnection)
return err
}
func (c *redisDBConnectionProducer) Connection(ctx context.Context) (interface{}, error) {
// This is intentionally not grabbing the lock since the calling functions (e.g. CreateUser)
// are claiming it. (The locking patterns could be refactored to be more consistent/clear.)
if !c.Initialized {
return nil, connutil.ErrNotInitialized
}
if c.client != nil {
return c.client, nil
}
var err error
var poolConfig radix.PoolConfig
if c.TLS {
rootCAs := x509.NewCertPool()
ok := rootCAs.AppendCertsFromPEM([]byte(c.CACert))
if !ok {
return nil, fmt.Errorf("failed to parse root certificate")
}
poolConfig = radix.PoolConfig{
Dialer: radix.Dialer{
AuthUser: c.Username,
AuthPass: c.Password,
NetDialer: &tls.Dialer{
Config: &tls.Config{
RootCAs: rootCAs,
InsecureSkipVerify: c.InsecureTLS,
},
},
},
}
} else {
poolConfig = radix.PoolConfig{
Dialer: radix.Dialer{
AuthUser: c.Username,
AuthPass: c.Password,
},
}
}
client, err := poolConfig.New(ctx, "tcp", c.Addr)
if err != nil {
return nil, err
}
c.client = client
return c.client, nil
}
// close terminates the database connection without locking
func (c *redisDBConnectionProducer) close() error {
if c.client != nil {
if err := c.client.Close(); err != nil {
return err
}
}
c.client = nil
return nil
}
// Close terminates the database connection with locking
func (c *redisDBConnectionProducer) Close() error {
c.Lock()
defer c.Unlock()
return c.close()
}