This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
client.go
412 lines (359 loc) · 12.6 KB
/
client.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright 2015 Square Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keysync
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
"time"
"github.com/jpillora/backoff"
pkgerr "github.com/pkg/errors"
"github.com/rcrowley/go-metrics"
"github.com/sirupsen/logrus"
sqmetrics "github.com/square/go-sq-metrics"
)
// Cipher suites enabled in the client. Since we also control the server, we can be fairly
// conservative here and only enable ECDHE / GCM suites.
var ciphers = []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
}
// Client represents an interface to a secrets storage backend.
type Client interface {
Secret(name string) (secret *Secret, err error)
SecretList() (map[string]Secret, error)
SecretListWithContents(secrets []string) (map[string]Secret, error)
Logger() *logrus.Entry
RebuildClient() error
}
// KeywhizHTTPClient is a client that reads from a Keywhiz server over HTTP (v2 API).
type KeywhizHTTPClient struct {
logger *logrus.Entry
httpClient *http.Client
url *url.URL
params httpClientParams
failCount metrics.Counter
lastSuccess metrics.Gauge
}
// httpClientParams are values necessary for constructing a TLS client.
type httpClientParams struct {
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
CaBundle string `json:"ca_bundle"`
timeout time.Duration
maxRetries int
minBackoff time.Duration
maxBackoff time.Duration
}
// SecretDeleted is returned as an error when the server 404s.
type SecretDeleted struct{}
func (e SecretDeleted) Error() string {
return "deleted"
}
func (c KeywhizHTTPClient) failCountInc() {
c.failCount.Inc(1)
}
func (c KeywhizHTTPClient) markSuccess() {
c.failCount.Clear()
c.lastSuccess.Update(time.Now().Unix())
}
// Logger returns the underlying logger for this client
func (c KeywhizHTTPClient) Logger() *logrus.Entry {
return c.logger
}
// NewClient produces a ready-to-use client struct given client config and
// CA file with the list of trusted certificate authorities.
func NewClient(cfg *ClientConfig, caFile string, serverURL *url.URL, logger *logrus.Entry, metricsHandle *sqmetrics.SquareMetrics) (client Client, err error) {
logger = logger.WithField("logger", "kwfs_client")
timeout, err := time.ParseDuration(cfg.Timeout)
if err != nil {
return &KeywhizHTTPClient{}, fmt.Errorf("bad timeout value '%s': %+v", cfg.Timeout, err)
}
minBackoff, err := time.ParseDuration(cfg.MinBackoff)
if err != nil {
return &KeywhizHTTPClient{}, fmt.Errorf("bad min backoff value '%s': %+v", cfg.MinBackoff, err)
}
maxBackoff, err := time.ParseDuration(cfg.MaxBackoff)
if err != nil {
return &KeywhizHTTPClient{}, fmt.Errorf("bad max backoff value '%s': %+v", cfg.MaxBackoff, err)
}
params := httpClientParams{
CertFile: cfg.Cert,
KeyFile: cfg.Key,
CaBundle: caFile,
timeout: timeout,
maxRetries: int(cfg.MaxRetries),
minBackoff: minBackoff,
maxBackoff: maxBackoff,
}
failCount := metrics.GetOrRegisterCounter("runtime.server.fails", metricsHandle.Registry)
lastSuccess := metrics.GetOrRegisterGauge("runtime.server.lastsuccess", metricsHandle.Registry)
initial, err := params.buildClient()
if err != nil {
return &KeywhizHTTPClient{}, err
}
return &KeywhizHTTPClient{logger, initial, serverURL, params, failCount, lastSuccess}, nil
}
// RebuildClient reloads certificates from disk. It should be called periodically to ensure up-to-date client
// certificates are used. This is important if you're using short-lived certificates that are routinely replaced.
func (c *KeywhizHTTPClient) RebuildClient() error {
client, err := c.params.buildClient()
if err != nil {
return err
}
c.httpClient = client
return nil
}
// ServerStatus returns raw JSON from the server's _status endpoint
func (c KeywhizHTTPClient) ServerStatus() (data []byte, err error) {
path := "_status"
logger := c.logger.WithField("logger", path)
now := time.Now()
resp, err := c.getWithRetry(path)
if err != nil {
logger.WithError(err).Warn("Error retrieving server status")
return nil, err
}
logger.Infof("GET /%s %d %v", path, resp.StatusCode, time.Since(now))
logger.WithFields(logrus.Fields{
"StatusCode": resp.StatusCode,
"duration": time.Since(now),
}).Infof("GET /%s", path)
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
logger.WithError(err).Warn("Error reading server status response")
return nil, err
}
return data, nil
}
// RawSecret returns raw JSON from requesting a secret.
func (c KeywhizHTTPClient) RawSecret(name string) ([]byte, error) {
// note: path.Join does not know how to properly escape for URLs!
pathname := path.Join("secret", name)
data, statusCode, err := c.queryKeywhizWithRetries(pathname, fmt.Sprintf("secret %s", name))
if err != nil {
c.logger.Errorf("Error querying Keywhiz for secret %v: %v", name, err)
c.failCountInc()
return nil, err
}
switch statusCode {
case 200:
c.markSuccess()
return data, nil
case 404:
c.logger.Warnf("Secret %v not found", name)
return nil, SecretDeleted{}
default:
msg := strings.Join(strings.Split(string(data), "\n"), " ")
c.logger.Errorf("Bad response code getting secret %v: (status=%v, msg='%s')", name, statusCode, msg)
c.failCountInc()
return nil, errors.New(msg)
}
}
// Secret returns an unmarshalled Secret struct after requesting a secret.
func (c KeywhizHTTPClient) Secret(name string) (secret *Secret, err error) {
data, err := c.RawSecret(name)
if err != nil {
return nil, err
}
secret, err = ParseSecret(data)
if err != nil {
return nil, fmt.Errorf("Error decoding retrieved secret %v: %v", name, err)
}
return secret, nil
}
// RawSecretList returns raw JSON from requesting a listing of secrets.
func (c KeywhizHTTPClient) RawSecretList() ([]byte, error) {
data, statusCode, err := c.queryKeywhizWithRetries("secrets", "secrets without contents")
if err != nil {
c.failCountInc()
return nil, fmt.Errorf("error querying Keywhiz for secrets without contents: %v", err)
} else if statusCode != 200 {
msg := strings.Join(strings.Split(string(data), "\n"), " ")
c.failCountInc()
return nil, fmt.Errorf("bad response code getting secrets: (status=%v, msg='%s')", statusCode, msg)
}
c.markSuccess()
return data, nil
}
// SecretList returns a map of unmarshalled Secret structs without their contents after requesting a listing of secrets.
// The map keys are the names of the secrets
func (c KeywhizHTTPClient) SecretList() (map[string]Secret, error) {
data, err := c.RawSecretList()
if err != nil {
return nil, err
}
return c.processSecretList(data)
}
// RawSecretListWithContents returns raw JSON from requesting a listing of secrets with their contents.
func (c KeywhizHTTPClient) RawSecretListWithContents(secrets []string) ([]byte, error) {
pathname := "batchsecret"
req, err := json.Marshal(map[string][]string{
"secrets": secrets,
})
if err != nil {
c.failCountInc()
c.logger.Errorf("Error creating request to retrieve secrets with contents: error %v, secrets %v", err, secrets)
return nil, err
}
now := time.Now()
resp, err := c.postWithRetry(pathname, "application/json", bytes.NewBuffer(req))
if err != nil {
c.failCountInc()
c.logger.Errorf("Error retrieving secrets with contents: %v", err)
return nil, err
}
defer resp.Body.Close()
c.logger.Infof("POST /%s %d %v", pathname, resp.StatusCode, time.Since(now))
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.failCountInc()
return nil, fmt.Errorf("error getting secrets with contents: %v", err)
} else if resp.StatusCode != 200 {
msg := strings.Join(strings.Split(string(data), "\n"), " ")
c.failCountInc()
return nil, fmt.Errorf("bad response code getting secrets with contents: (status=%v, msg='%s')", resp.StatusCode, msg)
}
c.markSuccess()
return data, nil
}
// SecretList returns a map of unmarshalled Secret structs, including their contents, associated with the
// given list of secrets. The map keys are the names of the secrets. All secrets must be accessible to this
// client, or the entire request will fail.
func (c KeywhizHTTPClient) SecretListWithContents(secrets []string) (map[string]Secret, error) {
data, err := c.RawSecretListWithContents(secrets)
if err != nil {
return nil, err
}
return c.processSecretList(data)
}
func (c KeywhizHTTPClient) processSecretList(data []byte) (map[string]Secret, error) {
secretList, err := ParseSecretList(data)
if err != nil {
return nil, fmt.Errorf("error decoding retrieved secrets: %v", err)
}
secretMap := map[string]Secret{}
for _, secret := range secretList {
filename, err := secret.Filename()
if err != nil {
return nil, pkgerr.Wrap(err, "unable to get secret's filename")
}
if duplicate, ok := secretMap[filename]; ok {
// This is not supported by Keysync. This stops syncing until the data inconsistency is fixed in the server.
return nil, fmt.Errorf("duplicate filename detected: %s on secrets %s and %s",
filename, duplicate.Name, secret.Name)
}
secretMap[filename] = secret
}
return secretMap, nil
}
func (c KeywhizHTTPClient) queryKeywhizWithRetries(pathname, goalForMsg string) (result []byte, status int, err error) {
now := time.Now()
resp, err := c.getWithRetry(pathname)
if err != nil {
c.logger.Errorf("Error retrieving %v: %v", goalForMsg, err)
return nil, -1, err
}
defer resp.Body.Close()
c.logger.Infof("GET /%s %d %v", pathname, resp.StatusCode, time.Since(now))
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.logger.Errorf("Error reading response body for %v: %v", goalForMsg, err)
return nil, resp.StatusCode, err
}
return data, resp.StatusCode, err
}
// buildClient constructs a new TLS client.
func (p httpClientParams) buildClient() (*http.Client, error) {
keyPair, err := tls.LoadX509KeyPair(p.CertFile, p.KeyFile)
if err != nil {
return nil, fmt.Errorf("Error loading Keypair '%s'/'%s': %v", p.CertFile, p.KeyFile, err)
}
caCert, err := ioutil.ReadFile(p.CaBundle)
if err != nil {
return nil, fmt.Errorf("Error loading CA file '%s': %v", p.CaBundle, err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
config := &tls.Config{
Certificates: []tls.Certificate{keyPair},
RootCAs: caCertPool,
MinVersion: tls.VersionTLS12, // TLSv1.2 and up is required
CipherSuites: ciphers,
}
config.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: config}
return &http.Client{Transport: transport, Timeout: p.timeout}, nil
}
// shouldRetry decides wether a request should be retried or not.
// e.g. 500 is an intermittent error, but 404 is most likely not.
func shouldRetry(resp *http.Response) bool {
return resp.StatusCode >= 500
}
// getWithRetry encapsulates the retry logic for requests that failed, because of
// intermittent issues
func (c *KeywhizHTTPClient) getWithRetry(url string) (resp *http.Response, err error) {
t := *c.url
t.Path = path.Join(c.url.Path, url)
b := &backoff.Backoff{
//These are the defaults
Min: c.params.minBackoff,
Max: c.params.maxBackoff,
Jitter: true,
}
for i := 0; i < c.params.maxRetries; i++ {
now := time.Now()
resp, err = c.httpClient.Get(t.String())
if err != nil || !shouldRetry(resp) {
return
}
sleep := b.Duration()
c.logger.Infof("GET /%s %d %v, attempt %d out of %d, retry in %v\n", url, resp.StatusCode, time.Since(now), i+1, c.params.maxRetries, sleep)
time.Sleep(sleep)
}
return
}
// postWithRetry encapsulates the retry logic for requests that failed, because of
// intermittent issues
func (c *KeywhizHTTPClient) postWithRetry(url, contentType string, body io.Reader) (resp *http.Response, err error) {
t := *c.url
t.Path = path.Join(c.url.Path, url)
b := &backoff.Backoff{
//These are the defaults
Min: c.params.minBackoff,
Max: c.params.maxBackoff,
Jitter: true,
}
for i := 0; i < c.params.maxRetries; i++ {
now := time.Now()
resp, err = c.httpClient.Post(t.String(), contentType, body)
if err != nil || !shouldRetry(resp) {
return
}
sleep := b.Duration()
c.logger.Infof("POST /%s %d %v, attempt %d out of %d, retry in %v\n", url, resp.StatusCode, time.Since(now), i+1, c.params.maxRetries, sleep)
time.Sleep(sleep)
}
return
}