/
datastore.go
147 lines (125 loc) · 3.48 KB
/
datastore.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
package datastore
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/azer/logger"
"github.com/gobuffalo/packr/v2"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gopkg.in/olivere/elastic.v6"
"github.com/oipwg/oip/config"
)
var client *elastic.Client
var AutoBulk BulkIndexer
var mappings = make(map[string]string)
var mapBox = packr.New("mappings", "./mappings")
func Setup(ctx context.Context) error {
var err error
httpClient, err := getHttpClient()
if err != nil {
log.Error("couldn't create httpClient", logger.Attrs{"err": err})
return err
}
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetHttpClient(httpClient),
elastic.SetURL(viper.GetString("elastic.host")))
if err != nil {
log.Error("unable to connect to elasticsearch", logger.Attrs{"err": err})
return errors.Wrap(err, "datastore.setup.newClient")
}
for index, mapping := range mappings {
err := createIndex(ctx, index, mapping)
if err != nil {
return errors.Wrap(err, fmt.Sprint("datastore.setup.createIndex", index))
}
}
AutoBulk = BeginBulkIndexer()
return nil
}
func getHttpClient() (*http.Client, error) {
var httpClient *http.Client
useCert := viper.GetBool("elastic.useCert")
if useCert {
certFile := config.GetFilePath("elastic.certFile")
certKey := config.GetFilePath("elastic.certKey")
rootCertPath := config.GetFilePath("elastic.certRoot")
// ToDo: add encrypted key support - potentially via x509.DecryptPEMBloc & tls.ParsePKCS1PrivateKey
cert, err := tls.LoadX509KeyPair(certFile, certKey)
if err != nil {
log.Error("couldn't LoadX509KeyPair", logger.Attrs{"err": err})
return nil, err
}
caCert, err := ioutil.ReadFile(rootCertPath)
if err != nil {
log.Error("couldn't read root certificate", logger.Attrs{"err": err})
return nil, err
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
// Setup HTTPS client
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: caCertPool,
InsecureSkipVerify: true,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{
TLSClientConfig: tlsConfig,
}
httpClient = &http.Client{
Transport: transport,
}
} else {
httpClient = http.DefaultClient
}
return httpClient, nil
}
func RegisterMapping(index, fileName string) {
index = Index(index) // apply proper prefix
mapping, err := mapBox.FindString(fileName)
if err != nil {
panic(fmt.Sprintf("Unable to find mapping %s for index %s", fileName, index))
}
mappings[index] = mapping
if client != nil {
err := createIndex(context.TODO(), index, mapping)
if err != nil {
panic(fmt.Sprintf("unable to create index %s - %s", index, err))
}
}
}
func createIndex(ctx context.Context, index, mapping string) error {
exists, err := client.IndexExists(Index(index)).Do(ctx)
if err != nil {
return errors.Wrap(err, "index existence check failure")
}
if !exists {
createIndex, err := client.CreateIndex(Index(index)).BodyString(mapping).Do(ctx)
if err != nil {
return errors.Wrap(err, "create index failed")
}
if !createIndex.Acknowledged {
return errors.New("create index not acknowledged")
}
}
return nil
}
func Client() *elastic.Client {
return client
}
func Index(index string) string {
if config.IsTestnet() {
if strings.HasPrefix(index, "testnet-") {
return index
}
return "testnet-" + index
}
if strings.HasPrefix(index, "mainnet-") {
return index
}
return "mainnet-" + index
}