-
Notifications
You must be signed in to change notification settings - Fork 1
/
elastic.go
402 lines (320 loc) · 8.8 KB
/
elastic.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
package main
import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"reflect"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/cenkalti/backoff"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esutil"
"github.com/tidwall/gjson"
log "github.com/sirupsen/logrus"
)
// ElasticConfig is a Struct that holds ElasticSearch config
type elasticConfig struct {
host string
port int
user string
password string
caCert string
batchSize int
filePrefix string
}
type esClient struct {
client *elasticsearch.Client
conf elasticConfig
}
func newElasticClient(config elasticConfig) (*esClient, error) {
retryBackoff := backoff.NewExponentialBackOff()
tr := transportConfigES(config)
URI := esURI(config)
c, err := elasticsearch.NewClient(elasticsearch.Config{
Addresses: []string{
URI,
},
Username: config.user,
Password: config.password,
RetryOnStatus: []int{502, 503, 504, 429},
RetryBackoff: func(i int) time.Duration {
if i == 1 {
retryBackoff.Reset()
}
return retryBackoff.NextBackOff()
},
MaxRetries: 5,
Transport: tr,
})
return &esClient{client: c, conf: config}, err
}
// transportConfigES is a helper method to setup TLS for the ES client.
func transportConfigES(config elasticConfig) http.RoundTripper {
cfg := new(tls.Config)
// Enforce TLS1.2 or higher
cfg.MinVersion = 2
// Read system CAs
var systemCAs, _ = x509.SystemCertPool()
if reflect.DeepEqual(systemCAs, x509.NewCertPool()) {
log.Debug("creating new CApool")
systemCAs = x509.NewCertPool()
}
cfg.RootCAs = systemCAs
if config.caCert != "" {
cacert, e := os.ReadFile(config.caCert)
if e != nil {
log.Fatalf("failed to append %q to RootCAs: %v", cacert, e)
}
if ok := cfg.RootCAs.AppendCertsFromPEM(cacert); !ok {
log.Debug("no certs appended, using system certs only")
}
}
var trConfig http.RoundTripper = &http.Transport{
TLSClientConfig: cfg,
ForceAttemptHTTP2: true}
return trConfig
}
func readResponse(r io.Reader) string {
var b bytes.Buffer
_, err := b.ReadFrom(r)
if err != nil { // Maybe propagate this error upwards?
log.Fatal(err)
}
return b.String()
}
func (es esClient) countDocuments(indexName string) error {
log.Infoln("Couting documents to fetch...")
log.Infoln(strings.Repeat("-", 80))
cr, err := es.client.Count(es.client.Count.WithIndex(indexName))
if err != nil {
log.Error(err)
}
json := readResponse(cr.Body)
cr.Body.Close()
count := int(gjson.Get(json, "count").Int())
log.Infof("Found %v documents", count)
return err
}
func findIndices(es esClient, indexGlob string) ([]string, error) {
log.Infoln("Finding indices to fetch...")
log.Infoln(strings.Repeat("-", 80))
catObj := es.client.Cat.Indices
cr, err := es.client.Cat.Indices(catObj.WithIndex(indexGlob), catObj.WithFormat("JSON"), catObj.WithH("index"))
if err != nil {
log.Error(err)
return nil, err
}
defer cr.Body.Close()
json := readResponse(cr.Body)
result := gjson.Get(json, "#.index")
var indices []string
for _, index := range result.Array() {
indices = append(indices, index.String())
}
if len(indices) == 0 {
return nil, fmt.Errorf("no indices found")
}
log.Debugf("Found indices: %v", indices)
return indices, err
}
func (es esClient) backupDocuments(sb *s3Backend, publicKeyPath, indexGlob string) error {
log.Infof("Backing up indexes that match glob: %s", indexGlob)
var (
batchNum int
scrollID string
)
batchsize := 50
filePrefix := ""
if es.conf.batchSize != 0 {
batchsize = es.conf.batchSize
}
if es.conf.filePrefix != "" {
filePrefix = es.conf.filePrefix
}
targetIndices, err := findIndices(es, indexGlob)
if err != nil {
return err
}
for _, index := range targetIndices {
wg := sync.WaitGroup{}
wr, err := sb.NewFileWriter(filePrefix+index+".bup", &wg)
if err != nil {
log.Fatalf("Could not open backup file for writing: %v", err)
}
log.Debug("Backup file ready for writing")
privateKey, publicKeyList, err := getKeys(publicKeyPath)
if err != nil {
return fmt.Errorf("could not retrieve public key or generate private key: %s", err)
}
log.Debug("Public key retrieved and private key successfully created")
e, err := newEncryptor(publicKeyList, privateKey, wr)
if err != nil {
return fmt.Errorf("could not initialize encryptor: %s", err)
}
c, err := newCompressor(e)
if err != nil {
return fmt.Errorf("could not initialize encryptor: %s", err)
}
_, err = es.client.Indices.Refresh(es.client.Indices.Refresh.WithIndex(index))
if err != nil {
return fmt.Errorf("could not refresh indexes: %s", err)
}
res, err := es.client.Search(
es.client.Search.WithIndex(index),
es.client.Search.WithSize(batchsize),
es.client.Search.WithSort("_doc"),
es.client.Search.WithScroll(time.Second*60),
)
if err != nil {
return err
}
json := readResponse(res.Body)
res.Body.Close()
hits := gjson.Get(json, "hits.hits")
_, err = c.Write([]byte(hits.Raw + "\n"))
if err != nil {
return fmt.Errorf("could not encrypt/write: %s", err)
}
log.Debug("Batch ", batchNum)
log.Trace("ScrollID", scrollID)
log.Trace("IDs ", gjson.Get(hits.Raw, "#._id"))
log.Trace(strings.Repeat("-", 80))
scrollID = gjson.Get(json, "_scroll_id").String()
for {
batchNum++
res, err := es.client.Scroll(es.client.Scroll.WithScrollID(scrollID), es.client.Scroll.WithScroll(time.Minute))
if err != nil {
return err
}
if res.IsError() {
return fmt.Errorf("error response: %s", err)
}
json = readResponse(res.Body)
res.Body.Close()
scrollID = gjson.Get(json, "_scroll_id").String()
hits := gjson.Get(json, "hits.hits")
log.Trace(hits)
if len(hits.Array()) < 1 {
log.Traceln("Finished scrolling")
break
}
_, err = c.Write([]byte(hits.Raw + "\n"))
if err != nil {
return fmt.Errorf("could not encrypt/write: %s", err)
}
log.Debug("Batch ", batchNum)
log.Trace("ScrollID", scrollID)
log.Trace("IDs ", gjson.Get(hits.Raw, "#._id"))
log.Trace(strings.Repeat("-", 80))
}
if err := c.Close(); err != nil {
log.Errorf("could not close compressor: %v", err)
}
if err := e.Close(); err != nil {
log.Errorf("could not close encryptor: %v", err)
}
if err := wr.Close(); err != nil {
log.Errorf("could not close destination file: %v", err)
}
wg.Wait()
}
return nil
}
func (es *esClient) restoreDocuments(sb *s3Backend, privateKeyPath, fileName, c4ghPassword string) error {
var countSuccessful uint64
err := es.countDocuments(fileName)
if err != nil {
return err
}
log.Infof("restoring index with name %s", fileName)
fr, err := sb.NewFileReader(fileName)
if err != nil {
return err
}
defer fr.Close()
privateKey, err := getPrivateKey(privateKeyPath, c4ghPassword)
if err != nil {
return fmt.Errorf("could not retrieve private key: %s", err)
}
log.Debug("Private key retrieved")
r, err := newDecryptor(privateKey, fr)
if err != nil {
return fmt.Errorf("could not initialise decryptor: %s", err)
}
d, err := newDecompressor(r)
if err != nil {
return fmt.Errorf("could not initialise decompressor: %s", err)
}
data, err := io.ReadAll(d)
if err != nil {
return fmt.Errorf("could not read all data: %s", err)
}
if err := d.Close(); err != nil {
log.Errorf("could not close decompressor: %v", err)
}
if err := r.Close(); err != nil {
log.Errorf("could not close decryptor: %v", err)
}
ud := string(data)
indexName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
bi, err := esutil.NewBulkIndexer(esutil.BulkIndexerConfig{
Index: indexName,
Client: es.client,
NumWorkers: 1,
FlushBytes: int(2048),
FlushInterval: 30 * time.Second,
})
if err != nil {
return fmt.Errorf("unexpected error: %s", err)
}
defer bi.Close(context.Background())
for _, docs := range strings.Split(ud, "\n") {
if docs == "" {
log.Debug("End of blob reached")
break
}
i := 0
for {
key := fmt.Sprintf("%v._source", i)
source := gjson.Get(docs, key).String()
if source == "" {
break
}
err = bi.Add(
context.Background(),
esutil.BulkIndexerItem{
Action: "index",
Body: strings.NewReader(source),
OnSuccess: func(context.Context, esutil.BulkIndexerItem, esutil.BulkIndexerResponseItem) {
atomic.AddUint64(&countSuccessful, 1)
},
OnFailure: func(_ context.Context, _ esutil.BulkIndexerItem, res esutil.BulkIndexerResponseItem, err error) {
if err != nil {
log.Errorf("Error: %s", err)
} else {
log.Errorf("Error: %s: %s", res.Error.Type, res.Error.Reason)
}
},
},
)
if err != nil {
return fmt.Errorf("unexpected error: %s", err)
}
i++
}
}
return nil
}
func esURI(c elasticConfig) string {
URI := c.host
URI = fmt.Sprintf(URI+":%d", c.port)
return URI
}