/
libstorage_driver.go
201 lines (161 loc) · 5.04 KB
/
libstorage_driver.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
package libstorage
import (
"crypto/tls"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
log "github.com/sirupsen/logrus"
gofig "github.com/akutz/gofig/types"
"github.com/akutz/gotil"
apiclient "github.com/rexray/rexray/libstorage/api/client"
"github.com/rexray/rexray/libstorage/api/context"
"github.com/rexray/rexray/libstorage/api/types"
"github.com/rexray/rexray/libstorage/api/utils"
)
var (
// EnableInstanceIDHeaders is a flag indicating whether or not the
// client will automatically send the instance ID header(s) along with
// storage-related API requests. The default is enabled.
EnableInstanceIDHeaders = true
// EnableLocalDevicesHeaders is a flag indicating whether or not the
// client will automatically send the local devices header(s) along with
// storage-related API requests. The default is enabled.
EnableLocalDevicesHeaders = true
)
type driver struct {
client
}
func newDriver() types.StorageDriver {
return &driver{}
}
func (d *driver) Init(ctx types.Context, config gofig.Config) error {
logFields := log.Fields{}
addr := config.GetString(types.ConfigHost)
d.ctx = ctx.WithValue(context.HostKey, addr)
d.ctx.Debug("got configured host address")
if tok := config.GetString(types.ConfigClientAuthToken); len(tok) > 0 {
if gotil.FileExists(tok) {
d.ctx.WithField("tokenFilePath", tok).Debug(
"reading client token file")
buf, err := ioutil.ReadFile(tok)
if err != nil {
d.ctx.WithField("tokenFilePath", tok).WithError(err).Error(
"error reading client token file")
return err
}
tok = string(buf)
}
d.ctx = d.ctx.WithValue(context.EncodedAuthTokenKey, tok)
d.ctx.WithField("encodedToken", tok).Debug("got configured auth token")
logFields["encodedToken"] = tok
}
proto, lAddr, err := gotil.ParseAddress(addr)
if err != nil {
return err
}
tlsConfig, err := utils.ParseTLSConfig(
d.ctx, config, proto, logFields, types.ConfigClient)
if err != nil {
return err
}
host := getHost(d.ctx, proto, lAddr, tlsConfig)
lsxPath := config.GetString(types.ConfigExecutorPath)
cliType := types.ParseClientType(config.GetString(types.ConfigClientType))
disableKeepAlive := config.GetBool(types.ConfigHTTPDisableKeepAlive)
logFields["lAddr"] = host
logFields["lsxPath"] = lsxPath
logFields["clientType"] = cliType
logFields["disableKeepAlive"] = disableKeepAlive
httpTransport := &http.Transport{
Dial: func(string, string) (net.Conn, error) {
if tlsConfig == nil {
conn, err := net.Dial(proto, lAddr)
if err != nil {
return nil, err
}
d.ctx.Debug("successful connection")
return conn, nil
}
conn, err := tls.Dial(proto, lAddr, &tlsConfig.Config)
if err != nil {
return nil, err
}
if !tlsConfig.VerifyPeers {
d.ctx.Debug("successful tls connection; not verifying peers")
return conn, nil
}
const errMatch = "error matching peer fingerprint"
// get the fqdn/IP of the endpoint to which the connection
// is being made in case an ErrKnownHost error occurs
hostSansPort := lAddr
if hostParts := strings.Split(lAddr, ":"); len(hostParts) > 1 {
hostSansPort = hostParts[0]
}
peerCerts := conn.ConnectionState().PeerCertificates
if ok, err := verifyKnownHost(
d.ctx,
hostSansPort,
peerCerts,
tlsConfig.KnownHost); ok {
return conn, nil
} else if err != nil {
d.ctx.WithError(err).Error(errMatch)
return nil, err
}
if ok, err := verifyKnownHostFiles(
d.ctx,
hostSansPort,
peerCerts,
tlsConfig.UsrKnownHosts,
tlsConfig.SysKnownHosts); ok {
return conn, nil
} else if err != nil {
d.ctx.WithError(err).Error(errMatch)
return nil, err
}
return nil, newErrKnownHost(hostSansPort, peerCerts)
},
DisableKeepAlives: disableKeepAlive,
}
apiClient := apiclient.New(host, httpTransport)
logReq := config.GetBool(types.ConfigLogHTTPRequests)
logRes := config.GetBool(types.ConfigLogHTTPResponses)
apiClient.LogRequests(logReq)
apiClient.LogResponses(logRes)
logFields["enableInstanceIDHeaders"] = EnableInstanceIDHeaders
logFields["enableLocalDevicesHeaders"] = EnableLocalDevicesHeaders
logFields["logRequests"] = logReq
logFields["logResponses"] = logRes
pathConfig := context.MustPathConfig(d.ctx)
d.client = client{
APIClient: apiClient,
ctx: d.ctx,
config: config,
tlsConfig: tlsConfig,
pathConfig: pathConfig,
clientType: cliType,
serviceCache: &lss{Store: utils.NewStore()},
}
if d.clientType == types.IntegrationClient {
newIIDCache := utils.NewStore
dur, err := time.ParseDuration(
config.GetString(types.ConfigClientCacheInstanceID))
if err != nil {
logFields["iidCacheDuration"] = dur.String()
newIIDCache = func() types.Store {
return utils.NewTTLStore(dur, true)
}
}
d.lsxCache = &lss{Store: utils.NewStore()}
d.supportedCache = &lss{Store: utils.NewStore()}
d.instanceIDCache = newIIDCache()
}
d.ctx.WithFields(logFields).Info("created libStorage client")
if err := d.dial(d.ctx); err != nil {
return err
}
d.ctx.Info("successefully dialed libStorage server")
return nil
}