-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathnamespace.go
More file actions
440 lines (378 loc) · 12 KB
/
namespace.go
File metadata and controls
440 lines (378 loc) · 12 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/*
* SPDX-FileCopyrightText: © Hypermode Inc. <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
package edgraph
import (
"context"
"encoding/json"
"fmt"
"math"
"strconv"
"strings"
"unicode"
"github.com/golang/glog"
"github.com/pkg/errors"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"github.com/dgraph-io/dgo/v250/protos/api"
apiv2 "github.com/dgraph-io/dgo/v250/protos/api.v2"
"github.com/hypermodeinc/dgraph/v25/schema"
"github.com/hypermodeinc/dgraph/v25/x"
)
const (
queryAllNamespaces = `{
namespaces(func: type(dgraph.namespace)) {
dgraph.namespace.name
dgraph.namespace.id
}
}`
queryNamespaceByName = `{
namespaces(func: eq(dgraph.namespace.name, "%v")) {
dgraph.namespace.id
}
}`
queryNamespaceByID = `{
namespaces(func: eq(dgraph.namespace.id, "%v")) {
dgraph.namespace.name
}
}`
queryDeleteNamespaceByName = `{
namespaces(func: eq(dgraph.namespace.name, "%v")) {
nsUID as uid
dgraph.namespace.id
}
}`
queryRenameNamespace = `{
namespaces as var(func: eq(dgraph.namespace.name, "%v"))
}`
)
type resultNamespaces struct {
Namespaces []struct {
ID uint64 `json:"dgraph.namespace.id"`
Name string `json:"dgraph.namespace.name"`
} `json:"namespaces"`
}
func (s *ServerV25) SignInUser(ctx context.Context,
request *apiv2.SignInUserRequest) (*apiv2.SignInUserResponse, error) {
req := &api.LoginRequest{Userid: request.UserId, Password: request.Password, Namespace: 0}
resp, err := (&Server{}).Login(ctx, req)
if err != nil {
return nil, err
}
jwt := api.Jwt{}
if err := proto.Unmarshal(resp.Json, &jwt); err != nil {
return nil, err
}
return &apiv2.SignInUserResponse{AccessJwt: jwt.AccessJwt, RefreshJwt: jwt.RefreshJwt}, nil
}
func (s *ServerV25) CreateNamespace(ctx context.Context, in *apiv2.CreateNamespaceRequest) (
*apiv2.CreateNamespaceResponse, error) {
if err := AuthSuperAdmin(ctx); err != nil {
s := status.Convert(err)
return nil, status.Error(s.Code(),
"Non superadmin user cannot create namespace. "+s.Message())
}
if err := isValidNamespaceName(in.NsName); err != nil {
return nil, err
}
if _, err := getNamespaceIDFromName(x.AttachJWTNamespace(ctx), in.NsName); err == nil {
return nil, errors.Errorf("namespace %q already exists", in.NsName)
} else if !strings.Contains(err.Error(), "not found") {
return nil, err
}
password := "password"
ns, err := (&Server{}).CreateNamespaceInternal(ctx, password)
if err != nil {
return nil, err
}
// If we crash at this point, it is possible that namespaces is created
// but no entry has been added to dgraph.namespace predicate. This is alright
// because we have not let the user know that namespace has been created.
// The user would have to try again and another namespace then would be
// assigned to the provided name here.
if err := insertNamespace(ctx, in.NsName, ns); err != nil {
return nil, err
}
glog.Infof("Created namespace [%v] with id [%d]", in.NsName, ns)
return &apiv2.CreateNamespaceResponse{}, nil
}
func (s *ServerV25) DropNamespace(ctx context.Context, in *apiv2.DropNamespaceRequest) (
*apiv2.DropNamespaceResponse, error) {
if err := AuthSuperAdmin(ctx); err != nil {
s := status.Convert(err)
return nil, status.Error(s.Code(),
"Non superadmin user cannot drop namespace. "+s.Message())
}
if err := isValidNamespaceToDelete(in.NsName); err != nil {
return nil, err
}
var nsID uint64
if isLgacyNamespace(in.NsName) {
ns, err := extractNsIDFromLegacyNamespace(in.NsName)
if err != nil {
return nil, err
}
if _, err := getNamespaceNameFromID(x.AttachJWTNamespace(ctx), ns); err == nil {
// We found the namespace with a real name
return nil, fmt.Errorf("namespace [%v] not found", in.NsName)
} else if !strings.Contains(err.Error(), "not found") {
return nil, err
}
nsID = ns
} else {
ns, err := deleteNamespaceByName(x.AttachJWTNamespace(ctx), in.NsName)
if err != nil {
return nil, err
}
nsID = ns
}
if nsID == 0 {
glog.Infof("Namespace [%v] not found", in.NsName)
return &apiv2.DropNamespaceResponse{}, nil
}
// If we crash at this point, it is possible that namespace is deleted
// by the name, but the entry has not been removed from dgraph.namespace
if err := (&Server{}).DeleteNamespace(ctx, nsID); err != nil {
if !strings.Contains(err.Error(), "error deleting non-existing namespace") {
return nil, err
} else {
glog.Infof("Namespace with id [%d] does not exist, cannot be deleted", nsID)
}
}
glog.Infof("Dropped namespace [%v] with id [%d]", in.NsName, nsID)
return &apiv2.DropNamespaceResponse{}, nil
}
func (s *ServerV25) UpdateNamespace(ctx context.Context, in *apiv2.UpdateNamespaceRequest) (
*apiv2.UpdateNamespaceResponse, error) {
if err := AuthSuperAdmin(ctx); err != nil {
s := status.Convert(err)
return nil, status.Error(s.Code(),
"Non superadmin user cannot rename a namespace. "+s.Message())
}
if err := isValidNamespaceToDelete(in.NsName); err != nil {
return nil, err
}
if err := isValidNamespaceName(in.RenameToNs); err != nil {
return nil, err
}
if isLgacyNamespace(in.NsName) {
err := renameLeagcyNamespace(ctx, in.NsName, in.RenameToNs)
return &apiv2.UpdateNamespaceResponse{}, err
}
if err := renameNamespace(x.AttachJWTNamespace(ctx), in.NsName, in.RenameToNs); err != nil {
return nil, err
}
glog.Infof("Renamed namespace [%v] to [%v]", in.NsName, in.RenameToNs)
return &apiv2.UpdateNamespaceResponse{}, nil
}
func (s *ServerV25) ListNamespaces(ctx context.Context, in *apiv2.ListNamespacesRequest) (
*apiv2.ListNamespacesResponse, error) {
if err := AuthSuperAdmin(ctx); err != nil {
s := status.Convert(err)
return nil, status.Error(s.Code(),
"Non superadmin user cannot list namespaces. "+s.Message())
}
resp, err := (&Server{}).doQuery(x.AttachJWTNamespace(ctx),
&Request{req: &api.Request{Query: queryAllNamespaces}, doAuth: NoAuthorize})
if err != nil {
return nil, err
}
var data resultNamespaces
if err := json.Unmarshal(resp.GetJson(), &data); err != nil {
return nil, err
}
dataNsList := make(map[uint64]string)
for _, e := range data.Namespaces {
dataNsList[e.ID] = e.Name
}
schNsList := schema.State().Namespaces()
result := &apiv2.ListNamespacesResponse{NsList: make(map[string]*apiv2.Namespace)}
for id := range schNsList {
if name, ok := dataNsList[id]; !ok {
name = fmt.Sprintf("dgraph-%d", id)
result.NsList[name] = &apiv2.Namespace{Name: name, Id: id}
} else {
result.NsList[name] = &apiv2.Namespace{Name: name, Id: id}
}
}
return result, nil
}
func isLgacyNamespace(nsName string) bool {
return strings.HasPrefix(nsName, "dgraph")
}
func extractNsIDFromLegacyNamespace(nsName string) (uint64, error) {
if len(nsName) < 8 {
return 0, errors.Errorf("namespace %q is not a legacy namespace", nsName)
}
ns, err := strconv.ParseUint(nsName[7:], 10, 64)
if err != nil {
return 0, errors.Errorf("invalid namespace name %q", nsName)
}
return ns, nil
}
func isSystemNamespace(nsName string) bool {
return nsName == "root" || nsName == "galaxy" || nsName == "dgraph-0"
}
func isValidNamespaceName(name string) error {
if name == "" {
return errors.Errorf("namespace name cannot be empty")
}
hasInvalidChars := strings.ContainsFunc(name, func(r rune) bool {
return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != '_' && r != '-'
})
if hasInvalidChars {
return fmt.Errorf("namespace name [%v] has invalid characters", name)
}
if strings.HasPrefix(name, "_") || strings.HasPrefix(name, "-") {
return fmt.Errorf("namespace name [%v] cannot start with _ or -", name)
}
if isLgacyNamespace(name) {
return fmt.Errorf("namespace name [%v] cannot start with dgraph", name)
}
if isSystemNamespace(name) {
return fmt.Errorf("namespace name [%v] is reserved", name)
}
if _, err := strconv.ParseInt(name, 10, 64); err == nil {
return fmt.Errorf("namespace name [%v] cannot be a number", name)
}
return nil
}
func isValidNamespaceToDelete(name string) error {
if name == "" {
return errors.Errorf("namespace name cannot be empty")
}
if isSystemNamespace(name) {
return fmt.Errorf("namespace [%v] cannot be renamed/dropped", name)
}
return nil
}
func getNamespaceIDFromName(ctx context.Context, nsName string) (uint64, error) {
if isSystemNamespace(nsName) {
return 0, nil
}
if isLgacyNamespace(nsName) {
return extractNsIDFromLegacyNamespace(nsName)
}
req := &api.Request{Query: fmt.Sprintf(queryNamespaceByName, nsName)}
resp, err := (&Server{}).doQuery(ctx, &Request{req: req, doAuth: NoAuthorize})
if err != nil {
return 0, err
}
var data resultNamespaces
if err := json.Unmarshal(resp.GetJson(), &data); err != nil {
return 0, err
}
if len(data.Namespaces) == 0 {
return 0, errors.Errorf("namespace %q not found", nsName)
}
glog.Infof("Found namespace [%v] with id [%d]", nsName, data.Namespaces[0].ID)
return data.Namespaces[0].ID, nil
}
func getNamespaceNameFromID(ctx context.Context, nsID uint64) (string, error) {
req := &api.Request{Query: fmt.Sprintf(queryNamespaceByID, nsID)}
resp, err := (&Server{}).doQuery(ctx, &Request{req: req, doAuth: NoAuthorize})
if err != nil {
return "", err
}
var data resultNamespaces
if err := json.Unmarshal(resp.GetJson(), &data); err != nil {
return "", err
}
if len(data.Namespaces) == 0 {
return "", errors.Errorf("namespace [%v] not found", nsID)
}
glog.Infof("Found namespace with ID [%v] with name [%v]", nsID, data.Namespaces[0].Name)
return data.Namespaces[0].Name, nil
}
func insertNamespace(ctx context.Context, nsName string, nsID uint64) error {
if nsID >= math.MaxInt64 {
return errors.Errorf("namespace ID %d exceeds int64 range", nsID)
}
_, err := (&Server{}).QueryNoGrpc(
context.WithValue(ctx, IsGraphql, true),
&api.Request{
Mutations: []*api.Mutation{{
Set: []*api.NQuad{
{
Subject: "_:ns",
Predicate: "dgraph.namespace.name",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: nsName}},
},
{
Subject: "_:ns",
Predicate: "dgraph.namespace.id",
ObjectValue: &api.Value{Val: &api.Value_IntVal{IntVal: int64(nsID)}},
},
{
Subject: "_:ns",
Predicate: "dgraph.type",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: "dgraph.namespace"}},
},
},
}},
CommitNow: true,
})
return err
}
func deleteNamespaceByName(ctx context.Context, nsName string) (uint64, error) {
req := &api.Request{
Query: fmt.Sprintf(queryDeleteNamespaceByName, nsName),
Mutations: []*api.Mutation{{DelNquads: []byte(`uid(nsUID) * * .`)}},
CommitNow: true,
}
resp, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true),
&Request{req: req, doAuth: NoAuthorize})
if err != nil {
return 0, err
}
var data resultNamespaces
if err := json.Unmarshal(resp.GetJson(), &data); err != nil {
return 0, err
}
if len(data.Namespaces) != 1 {
return 0, nil
}
return data.Namespaces[0].ID, nil
}
func renameLeagcyNamespace(ctx context.Context, fromNs, toNs string) error {
fromID, err := extractNsIDFromLegacyNamespace(fromNs)
if err != nil {
return err
}
_, err = getNamespaceNameFromID(x.AttachJWTNamespace(ctx), fromID)
if err == nil {
return fmt.Errorf("namespace [%v] not found: %v", err, fromNs)
}
if !strings.Contains(err.Error(), "not found") {
return err
}
if err := insertNamespace(x.AttachJWTNamespace(ctx), toNs, fromID); err != nil {
return err
}
return nil
}
func renameNamespace(ctx context.Context, fromNs, toNs string) error {
req := &api.Request{
Query: fmt.Sprintf(queryRenameNamespace, fromNs),
Mutations: []*api.Mutation{{
Cond: `@if(eq(len(namespaces), 1))`,
Set: []*api.NQuad{
{
Subject: "uid(namespaces)",
Predicate: "dgraph.namespace.name",
ObjectValue: &api.Value{Val: &api.Value_StrVal{StrVal: toNs}},
},
},
}},
CommitNow: true,
}
resp, err := (&Server{}).doQuery(context.WithValue(ctx, IsGraphql, true), &Request{req: req})
if err != nil {
return err
}
if resp.Metrics.NumUids["namespaces"] != 1 {
return errors.Errorf("namespace [%v] not found", fromNs)
}
return nil
}