-
Notifications
You must be signed in to change notification settings - Fork 26
/
dao.go
223 lines (190 loc) · 5.14 KB
/
dao.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
package auth
import (
"context"
"encoding/json"
"fmt"
"os"
log "github.com/sirupsen/logrus"
"github.com/appbaseio/reactivesearch-api/model/credential"
"github.com/appbaseio/reactivesearch-api/model/permission"
"github.com/appbaseio/reactivesearch-api/model/user"
"github.com/appbaseio/reactivesearch-api/util"
)
type elasticsearch struct {
userIndex, userType string
permissionIndex, permissionType string
}
type publicKey struct {
PublicKey string `json:"public_key"`
RoleKey string `json:"role_key"`
}
func initPlugin(userIndex, permissionIndex string) (*elasticsearch, error) {
// auth only has to establish a connection to es, users, permissions
// plugin handles the creation of their respective meta indices
es := &elasticsearch{
userIndex, "_doc",
permissionIndex, "_doc",
}
return es, nil
}
func (es *elasticsearch) createIndex(indexName, mapping string) (bool, error) {
ctx := context.Background()
// Check if the index already exists
exists, err := util.GetClient7().IndexExists(indexName).
Do(ctx)
if err != nil {
return false, fmt.Errorf("%s: error while checking if index already exists: %v",
logTag, err)
}
if exists {
log.Println(logTag, ": index named", indexName, "already exists, skipping...")
return true, nil
}
replicas := util.GetReplicas()
settings := fmt.Sprintf(mapping, util.HiddenIndexSettings(), replicas)
// Meta index does not exists, create a new one
_, err = util.GetClient7().CreateIndex(indexName).
Body(settings).
Do(ctx)
if err != nil {
return false, fmt.Errorf("%s: error while creating index named %s: %v",
logTag, indexName, err)
}
log.Println(logTag, ": successfully created index named", indexName)
return true, nil
}
// Create or update the public key
func (es *elasticsearch) savePublicKey(ctx context.Context, indexName string, record publicKey) (interface{}, error) {
_, err := util.GetClient7().
Index().
Index(indexName).
BodyJson(record).
Id(publicKeyDocID).
Do(ctx)
if err != nil {
log.Errorln(logTag, ": error indexing public key record", err)
return false, err
}
return true, nil
}
// Get the public key
func (es *elasticsearch) getPublicKey(ctx context.Context) (publicKey, error) {
publicKeyIndex := os.Getenv(envPublicKeyEsIndex)
if publicKeyIndex == "" {
publicKeyIndex = defaultPublicKeyEsIndex
}
switch util.GetVersion() {
case 6:
return es.getPublicKeyEs6(ctx, publicKeyIndex, publicKeyDocID)
default:
return es.getPublicKeyEs7(ctx, publicKeyIndex, publicKeyDocID)
}
}
func (es *elasticsearch) getCredential(ctx context.Context, username string) (credential.AuthCredential, error) {
switch util.GetVersion() {
case 6:
return es.getCredentialEs6(ctx, username)
default:
return es.getCredentialEs7(ctx, username)
}
}
func (es *elasticsearch) putUser(ctx context.Context, u user.User) (bool, error) {
_, err := util.GetClient7().Index().
Index(es.userIndex).
Type(es.userType).
Id(u.Username).
BodyJson(u).
Do(ctx)
if err != nil {
return false, err
}
return true, nil
}
func (es *elasticsearch) getUser(ctx context.Context, username string) (*user.User, error) {
data, err := es.getRawUser(ctx, username)
if err != nil {
return nil, err
}
var u user.User
err = json.Unmarshal(data, &u)
if err != nil {
return nil, err
}
return &u, nil
}
func (es *elasticsearch) getRawUser(ctx context.Context, username string) ([]byte, error) {
data, err := util.GetClient7().Get().
Index(es.userIndex).
Type(es.userType).
Id(username).
FetchSource(true).
Do(ctx)
if err != nil {
return nil, err
}
src, err := data.Source.MarshalJSON()
if err != nil {
return nil, err
}
return src, nil
}
func (es *elasticsearch) putPermission(ctx context.Context, p permission.Permission) (bool, error) {
_, err := util.GetClient7().Index().
Index(es.permissionIndex).
Type(es.permissionType).
Id(p.Username).
BodyJson(p).
Do(ctx)
if err != nil {
return false, err
}
return true, nil
}
func (es *elasticsearch) getPermission(ctx context.Context, username string) (*permission.Permission, error) {
data, err := es.getRawPermission(ctx, username)
if err != nil {
return nil, err
}
var p permission.Permission
err = json.Unmarshal(data, &p)
if err != nil {
return nil, err
}
return &p, nil
}
func (es *elasticsearch) getRawPermission(ctx context.Context, username string) ([]byte, error) {
resp, err := util.GetClient7().Get().
Index(es.permissionIndex).
Type(es.permissionType).
Id(username).
FetchSource(true).
Do(ctx)
if err != nil {
return nil, err
}
src, err := resp.Source.MarshalJSON()
if err != nil {
return nil, err
}
return src, nil
}
func (es *elasticsearch) getRolePermission(ctx context.Context, role string) (*permission.Permission, error) {
data, err := es.getRawRolePermission(ctx, role)
if err != nil {
return nil, err
}
var p permission.Permission
err = json.Unmarshal(data, &p)
if err != nil {
return nil, err
}
return &p, nil
}
func (es *elasticsearch) getRawRolePermission(ctx context.Context, role string) ([]byte, error) {
switch util.GetVersion() {
case 6:
return es.getRawRolePermissionEs6(ctx, role)
default:
return es.getRawRolePermissionEs7(ctx, role)
}
}