-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathx.go
1132 lines (1005 loc) · 31.5 KB
/
x.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2015-2018 Dgraph Labs, Inc. and Contributors
*
* 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 x
import (
"bufio"
"bytes"
builtinGzip "compress/gzip"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"math"
"math/rand"
"net"
"net/http"
"os"
"regexp"
"sort"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"google.golang.org/grpc/peer"
"github.com/dgraph-io/badger/v2"
"github.com/dgraph-io/badger/v2/y"
"github.com/dgraph-io/dgo/v200"
"github.com/dgraph-io/dgo/v200/protos/api"
"github.com/golang/glog"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.opencensus.io/plugin/ocgrpc"
"go.opencensus.io/trace"
"golang.org/x/crypto/ssh/terminal"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// Error constants representing different types of errors.
var (
// ErrNotSupported is thrown when an enterprise feature is requested in the open source version.
ErrNotSupported = errors.Errorf("Feature available only in Dgraph Enterprise Edition")
ErrNoJwt = errors.New("no accessJwt available")
)
const (
// Success is equivalent to the HTTP 200 error code.
Success = "Success"
// ErrorUnauthorized is equivalent to the HTTP 401 error code.
ErrorUnauthorized = "ErrorUnauthorized"
// ErrorInvalidMethod is equivalent to the HTTP 405 error code.
ErrorInvalidMethod = "ErrorInvalidMethod"
// ErrorInvalidRequest is equivalent to the HTTP 400 error code.
ErrorInvalidRequest = "ErrorInvalidRequest"
// Error is a general error code.
Error = "Error"
// ErrorNoData is an error returned when the requested data cannot be returned.
ErrorNoData = "ErrorNoData"
// ValidHostnameRegex is a regex that accepts our expected hostname format.
ValidHostnameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]" +
"|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
// Star is equivalent to using * in a mutation.
// When changing this value also remember to change in in client/client.go:DeleteEdges.
Star = "_STAR_ALL"
// GrpcMaxSize is the maximum possible size for a gRPC message.
// Dgraph uses the maximum size for the most flexibility (2GB - equal
// to the max grpc frame size). Users will still need to set the max
// message sizes allowable on the client size when dialing.
GrpcMaxSize = math.MaxInt32
// PortZeroGrpc is the default gRPC port for zero.
PortZeroGrpc = 5080
// PortZeroHTTP is the default HTTP port for zero.
PortZeroHTTP = 6080
// PortInternal is the default port for internal use.
PortInternal = 7080
// PortHTTP is the default HTTP port for alpha.
PortHTTP = 8080
// PortGrpc is the default gRPC port for alpha.
PortGrpc = 9080
// ForceAbortDifference is the maximum allowed difference between
// AppliedUntil - TxnMarks.DoneUntil() before old transactions start getting aborted.
ForceAbortDifference = 5000
// FacetDelimeter is the symbol used to distinguish predicate names from facets.
FacetDelimeter = "|"
// GrootId is the ID of the admin user for ACLs.
GrootId = "groot"
// GuardiansId is the ID of the admin group for ACLs.
GuardiansId = "guardians"
// AclPredicates is the JSON representation of the predicates reserved for use
// by the ACL system.
AclPredicates = `
{"predicate":"dgraph.xid","type":"string", "index":true, "tokenizer":["exact"], "upsert":true},
{"predicate":"dgraph.password","type":"password"},
{"predicate":"dgraph.user.group","list":true, "reverse":true, "type":"uid"},
{"predicate":"dgraph.acl.rule","type":"uid","list":true},
{"predicate":"dgraph.rule.predicate","type":"string","index":true,"tokenizer":["exact"],"upsert":true},
{"predicate":"dgraph.rule.permission","type":"int"}
`
// CorsPredicate is the json representation of the predicate reserved by dgraph for the use
//of cors
CorsPredicate = `{"predicate":"dgraph.cors","type":"string","list":true,"type":"string","index":true,"tokenizer":["exact"],"upsert":true}`
InitialTypes = `
"types": [{
"fields": [{"name": "dgraph.graphql.schema"},{"name": "dgraph.graphql.xid"}],
"name": "dgraph.graphql"
},{
"fields": [{"name": "dgraph.password"},{"name": "dgraph.xid"},{"name": "dgraph.user.group"}],
"name": "dgraph.type.User"
},{
"fields": [{"name": "dgraph.acl.rule"},{"name": "dgraph.xid"}],
"name": "dgraph.type.Group"
},{
"fields": [{"name": "dgraph.rule.predicate"},{"name": "dgraph.rule.permission"}],
"name": "dgraph.type.Rule"
}]`
// GroupIdFileName is the name of the file storing the ID of the group to which
// the data in a postings directory belongs. This ID is used to join the proper
// group the first time an Alpha comes up with data from a restored backup or a
// bulk load.
GroupIdFileName = "group_id"
AccessControlAllowedHeaders = "X-Dgraph-AccessToken, " +
"Content-Type, Content-Length, Accept-Encoding, Cache-Control, " +
"X-CSRF-Token, X-Auth-Token, X-Requested-With"
DgraphCostHeader = "Dgraph-TouchedUids"
// GraphqlPredicates is the json representation of the predicate reserved for graphql system.
GraphqlPredicates = `
{"predicate":"dgraph.graphql.schema", "type": "string"},
{"predicate":"dgraph.graphql.xid","type":"string","index":true,"tokenizer":["exact"],"upsert":true}
`
)
var (
// Useful for running multiple servers on the same machine.
regExpHostName = regexp.MustCompile(ValidHostnameRegex)
// Nilbyte is a nil byte slice. Used
Nilbyte []byte
// AcceptedOrigins is allowed list of origins to make request to the graphql endpoint.
AcceptedOrigins = atomic.Value{}
)
func init() {
AcceptedOrigins.Store(map[string]struct{}{})
}
// UpdateCorsOrigins updates the cors allowlist with the given origins.
func UpdateCorsOrigins(origins []string) {
if len(origins) == 1 && origins[0] == "*" {
AcceptedOrigins.Store(map[string]struct{}{})
return
}
allowList := make(map[string]struct{}, len(origins))
for _, origin := range origins {
allowList[origin] = struct{}{}
}
AcceptedOrigins.Store(allowList)
}
// ShouldCrash returns true if the error should cause the process to crash.
func ShouldCrash(err error) bool {
if err == nil {
return false
}
errStr := status.Convert(err).Message()
return strings.Contains(errStr, "REUSE_RAFTID") ||
strings.Contains(errStr, "REUSE_ADDR") ||
strings.Contains(errStr, "NO_ADDR") ||
strings.Contains(errStr, "ENTERPRISE_LIMIT_REACHED")
}
// WhiteSpace Replacer removes spaces and tabs from a string.
var WhiteSpace = strings.NewReplacer(" ", "", "\t", "")
// GqlError is a GraphQL spec compliant error structure. See GraphQL spec on
// errors here: https://graphql.github.io/graphql-spec/June2018/#sec-Errors
//
// Note: "Every error must contain an entry with the key message with a string
// description of the error intended for the developer as a guide to understand
// and correct the error."
//
// "If an error can be associated to a particular point in the request [the error]
// should contain an entry with the key locations with a list of locations"
//
// Path is about GraphQL results and Errors for GraphQL layer.
//
// Extensions is for everything else.
type GqlError struct {
Message string `json:"message"`
Locations []Location `json:"locations,omitempty"`
Path []interface{} `json:"path,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
}
// A Location is the Line+Column index of an error in a request.
type Location struct {
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
// GqlErrorList is a list of GraphQL errors as would be found in a response.
type GqlErrorList []*GqlError
type queryRes struct {
Errors GqlErrorList `json:"errors"`
}
func (gqlErr *GqlError) Error() string {
var buf bytes.Buffer
if gqlErr == nil {
return ""
}
Check2(buf.WriteString(gqlErr.Message))
if len(gqlErr.Locations) > 0 {
Check2(buf.WriteString(" (Locations: ["))
for i, loc := range gqlErr.Locations {
if i > 0 {
Check2(buf.WriteString(", "))
}
Check2(buf.WriteString(fmt.Sprintf("{Line: %v, Column: %v}", loc.Line, loc.Column)))
}
Check2(buf.WriteString("])"))
}
return buf.String()
}
func (errList GqlErrorList) Error() string {
var buf bytes.Buffer
for i, gqlErr := range errList {
if i > 0 {
Check(buf.WriteByte('\n'))
}
Check2(buf.WriteString(gqlErr.Error()))
}
return buf.String()
}
// GqlErrorf returns a new GqlError with the message and args Sprintf'ed as the
// GqlError's Message.
func GqlErrorf(message string, args ...interface{}) *GqlError {
return &GqlError{
Message: fmt.Sprintf(message, args...),
}
}
func ExtractJwt(ctx context.Context) ([]string, error) {
// extract the jwt and unmarshal the jwt to get the list of groups
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return nil, ErrNoJwt
}
accessJwt := md.Get("accessJwt")
if len(accessJwt) == 0 {
return nil, ErrNoJwt
}
return accessJwt, nil
}
// WithLocations adds a list of locations to a GqlError and returns the same
// GqlError (fluent style).
func (gqlErr *GqlError) WithLocations(locs ...Location) *GqlError {
if gqlErr == nil {
return nil
}
gqlErr.Locations = append(gqlErr.Locations, locs...)
return gqlErr
}
// WithPath adds a path to a GqlError and returns the same
// GqlError (fluent style).
func (gqlErr *GqlError) WithPath(path []interface{}) *GqlError {
if gqlErr == nil {
return nil
}
gqlErr.Path = path
return gqlErr
}
// SetStatus sets the error code, message and the newly assigned uids
// in the http response.
func SetStatus(w http.ResponseWriter, code, msg string) {
var qr queryRes
ext := make(map[string]interface{})
ext["code"] = code
qr.Errors = append(qr.Errors, &GqlError{Message: msg, Extensions: ext})
if js, err := json.Marshal(qr); err == nil {
if _, err := w.Write(js); err != nil {
glog.Errorf("Error while writing: %+v", err)
}
} else {
Panic(errors.Errorf("Unable to marshal: %+v", qr))
}
}
// SetHttpStatus is similar to SetStatus but sets a proper HTTP status code
// in the response instead of always returning HTTP 200 (OK).
func SetHttpStatus(w http.ResponseWriter, code int, msg string) {
w.WriteHeader(code)
SetStatus(w, "error", msg)
}
// AddCorsHeaders adds the CORS headers to an HTTP response.
func AddCorsHeaders(w http.ResponseWriter) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", AccessControlAllowedHeaders)
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Connection", "close")
}
// QueryResWithData represents a response that holds errors as well as data.
type QueryResWithData struct {
Errors GqlErrorList `json:"errors"`
Data *string `json:"data"`
}
// SetStatusWithData sets the errors in the response and ensures that the data key
// in the data is present with value nil.
// In case an error was encountered after the query execution started, we have to return data
// key with null value according to GraphQL spec.
func SetStatusWithData(w http.ResponseWriter, code, msg string) {
var qr QueryResWithData
ext := make(map[string]interface{})
ext["code"] = code
qr.Errors = append(qr.Errors, &GqlError{Message: msg, Extensions: ext})
// This would ensure that data key is present with value null.
if js, err := json.Marshal(qr); err == nil {
if _, err := w.Write(js); err != nil {
glog.Errorf("Error while writing: %+v", err)
}
} else {
Panic(errors.Errorf("Unable to marshal: %+v", qr))
}
}
// Reply sets the body of an HTTP response to the JSON representation of the given reply.
func Reply(w http.ResponseWriter, rep interface{}) {
if js, err := json.Marshal(rep); err == nil {
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(js))
} else {
SetStatus(w, Error, "Internal server error")
}
}
// ParseRequest parses the body of the given request.
func ParseRequest(w http.ResponseWriter, r *http.Request, data interface{}) bool {
defer r.Body.Close()
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&data); err != nil {
SetStatus(w, Error, fmt.Sprintf("While parsing request: %v", err))
return false
}
return true
}
// AttachAccessJwt adds any incoming JWT header data into the grpc context metadata
func AttachAccessJwt(ctx context.Context, r *http.Request) context.Context {
if accessJwt := r.Header.Get("X-Dgraph-AccessToken"); accessJwt != "" {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
md = metadata.New(nil)
}
md.Append("accessJwt", accessJwt)
ctx = metadata.NewIncomingContext(ctx, md)
}
return ctx
}
// AttachRemoteIP adds any incoming IP data into the grpc context metadata
func AttachRemoteIP(ctx context.Context, r *http.Request) context.Context {
if ip, port, err := net.SplitHostPort(r.RemoteAddr); err == nil {
if intPort, convErr := strconv.Atoi(port); convErr == nil {
ctx = peer.NewContext(ctx, &peer.Peer{
Addr: &net.TCPAddr{
IP: net.ParseIP(ip),
Port: intPort,
},
})
}
}
return ctx
}
// isIpWhitelisted checks if the given ipString is within the whitelisted ip range
func isIpWhitelisted(ipString string) bool {
ip := net.ParseIP(ipString)
if ip == nil {
return false
}
if ip.IsLoopback() {
return true
}
for _, ipRange := range WorkerConfig.WhiteListedIPRanges {
if bytes.Compare(ip, ipRange.Lower) >= 0 && bytes.Compare(ip, ipRange.Upper) <= 0 {
return true
}
}
return false
}
// HasWhitelistedIP checks whether the source IP in ctx is whitelisted or not.
// It returns the IP address if the IP is whitelisted, otherwise an error is returned.
func HasWhitelistedIP(ctx context.Context) (net.Addr, error) {
peerInfo, ok := peer.FromContext(ctx)
if !ok {
return nil, errors.New("unable to find source ip")
}
ip, _, err := net.SplitHostPort(peerInfo.Addr.String())
if err != nil {
return nil, err
}
if !isIpWhitelisted(ip) {
return nil, errors.Errorf("unauthorized ip address: %s", ip)
}
return peerInfo.Addr, nil
}
// Write response body, transparently compressing if necessary.
func WriteResponse(w http.ResponseWriter, r *http.Request, b []byte) (int, error) {
var out io.Writer = w
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
w.Header().Set("Content-Encoding", "gzip")
gzw := builtinGzip.NewWriter(w)
defer gzw.Close()
out = gzw
}
return out.Write(b)
}
// Min returns the minimum of the two given numbers.
func Min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
// Max returns the maximum of the two given numbers.
func Max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
// RetryUntilSuccess runs the given function until it succeeds or can no longer be retried.
func RetryUntilSuccess(maxRetries int, waitAfterFailure time.Duration,
f func() error) error {
var err error
for retry := maxRetries; retry != 0; retry-- {
if err = f(); err == nil {
return nil
}
if waitAfterFailure > 0 {
time.Sleep(waitAfterFailure)
}
}
return err
}
// HasString returns whether the slice contains the given string.
func HasString(a []string, b string) bool {
for _, k := range a {
if k == b {
return true
}
}
return false
}
// Unique takes an array and returns it with no duplicate entries.
func Unique(a []string) []string {
if len(a) < 2 {
return a
}
sort.Strings(a)
idx := 1
for _, val := range a {
if a[idx-1] == val {
continue
}
a[idx] = val
idx++
}
return a[:idx]
}
// ReadLine reads a single line from a buffered reader. The line is read into the
// passed in buffer to minimize allocations. This is the preferred
// method for loading long lines which could be longer than the buffer
// size of bufio.Scanner.
func ReadLine(r *bufio.Reader, buf *bytes.Buffer) error {
isPrefix := true
var err error
buf.Reset()
for isPrefix && err == nil {
var line []byte
// The returned line is an pb.buffer in bufio and is only
// valid until the next call to ReadLine. It needs to be copied
// over to our own buffer.
line, isPrefix, err = r.ReadLine()
if err == nil {
if _, err := buf.Write(line); err != nil {
return err
}
}
}
return err
}
// FixedDuration returns the given duration as a string of fixed length.
func FixedDuration(d time.Duration) string {
str := fmt.Sprintf("%02ds", int(d.Seconds())%60)
if d >= time.Minute {
str = fmt.Sprintf("%02dm", int(d.Minutes())%60) + str
}
if d >= time.Hour {
str = fmt.Sprintf("%02dh", int(d.Hours())) + str
}
return str
}
// PageRange returns start and end indices given pagination params. Note that n
// is the size of the input list.
func PageRange(count, offset, n int) (int, int) {
if n == 0 {
return 0, 0
}
if count == 0 && offset == 0 {
return 0, n
}
if count < 0 {
// Items from the back of the array, like Python arrays. Do a positive mod n.
if count*-1 > n {
count = -n
}
return (((n + count) % n) + n) % n, n
}
start := offset
if start < 0 {
start = 0
}
if start > n {
return n, n
}
if count == 0 { // No count specified. Just take the offset parameter.
return start, n
}
end := start + count
if end > n {
end = n
}
return start, end
}
// ValidateAddress checks whether given address can be used with grpc dial function
func ValidateAddress(addr string) error {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return err
}
if p, err := strconv.Atoi(port); err != nil || p <= 0 || p >= 65536 {
return errors.Errorf("Invalid port: %v", p)
}
if ip := net.ParseIP(host); ip != nil {
return nil
}
// try to parse as hostname as per hostname RFC
if len(strings.Replace(host, ".", "", -1)) > 255 {
return errors.Errorf("Hostname should be less than or equal to 255 characters")
}
if !regExpHostName.MatchString(host) {
return errors.Errorf("Invalid hostname: %v", host)
}
return nil
}
// RemoveDuplicates sorts the slice of strings and removes duplicates. changes the input slice.
// This function should be called like: someSlice = RemoveDuplicates(someSlice)
func RemoveDuplicates(s []string) (out []string) {
sort.Strings(s)
out = s[:0]
for i := range s {
if i > 0 && s[i] == s[i-1] {
continue
}
out = append(out, s[i])
}
return
}
// BytesBuffer provides a buffer backed by byte slices.
type BytesBuffer struct {
data [][]byte
off int
sz int
}
func (b *BytesBuffer) grow(n int) {
if n < 128 {
n = 128
}
if len(b.data) == 0 {
b.data = append(b.data, make([]byte, n))
}
last := len(b.data) - 1
// Return if we have sufficient space
if len(b.data[last])-b.off >= n {
return
}
sz := len(b.data[last]) * 2
if sz > 512<<10 {
sz = 512 << 10 // 512 KB
}
if sz < n {
sz = n
}
b.data[last] = b.data[last][:b.off]
b.sz += len(b.data[last])
b.data = append(b.data, make([]byte, sz))
b.off = 0
}
// Slice returns a slice of length n to be used for writing.
func (b *BytesBuffer) Slice(n int) []byte {
b.grow(n)
last := len(b.data) - 1
b.off += n
b.sz += n
return b.data[last][b.off-n : b.off]
}
// Length returns the size of the buffer.
func (b *BytesBuffer) Length() int {
return b.sz
}
// CopyTo copies the contents of the buffer to the given byte slice.
// Caller should ensure that o is of appropriate length.
func (b *BytesBuffer) CopyTo(o []byte) int {
offset := 0
for i, d := range b.data {
if i == len(b.data)-1 {
copy(o[offset:], d[:b.off])
offset += b.off
} else {
copy(o[offset:], d)
offset += len(d)
}
}
return offset
}
// TruncateBy reduces the size of the bugger by the given amount.
// Always give back <= touched bytes.
func (b *BytesBuffer) TruncateBy(n int) {
b.off -= n
b.sz -= n
AssertTrue(b.off >= 0 && b.sz >= 0)
}
type record struct {
Name string
Dur time.Duration
}
// Timer implements a timer that supports recording the duration of events.
type Timer struct {
start time.Time
last time.Time
records []record
}
// Start starts the timer and clears the list of records.
func (t *Timer) Start() {
t.start = time.Now()
t.last = t.start
t.records = t.records[:0]
}
// Record records an event and assigns it the given name.
func (t *Timer) Record(name string) {
now := time.Now()
t.records = append(t.records, record{
Name: name,
Dur: now.Sub(t.last).Round(time.Millisecond),
})
t.last = now
}
// Total returns the duration since the timer was started.
func (t *Timer) Total() time.Duration {
return time.Since(t.start).Round(time.Millisecond)
}
func (t *Timer) String() string {
sort.Slice(t.records, func(i, j int) bool {
return t.records[i].Dur > t.records[j].Dur
})
return fmt.Sprintf("Timer Total: %s. Breakdown: %v", t.Total(), t.records)
}
// PredicateLang extracts the language from a predicate (or facet) name.
// Returns the predicate and the language tag, if any.
func PredicateLang(s string) (string, string) {
i := strings.LastIndex(s, "@")
if i <= 0 {
return s, ""
}
return s[0:i], s[i+1:]
}
// DivideAndRule is used to divide a number of tasks among multiple go routines.
func DivideAndRule(num int) (numGo, width int) {
numGo, width = 64, 0
for ; numGo >= 1; numGo /= 2 {
widthF := math.Ceil(float64(num) / float64(numGo))
if numGo == 1 || widthF >= 256.0 {
width = int(widthF)
return
}
}
return
}
// SetupConnection starts a secure gRPC connection to the given host.
func SetupConnection(host string, tlsCfg *tls.Config, useGz bool, dialOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
callOpts := append([]grpc.CallOption{},
grpc.MaxCallRecvMsgSize(GrpcMaxSize),
grpc.MaxCallSendMsgSize(GrpcMaxSize))
if useGz {
fmt.Fprintf(os.Stderr, "Using compression with %s\n", host)
callOpts = append(callOpts, grpc.UseCompressor(gzip.Name))
}
dialOpts = append(dialOpts,
grpc.WithStatsHandler(&ocgrpc.ClientHandler{}),
grpc.WithDefaultCallOptions(callOpts...),
grpc.WithBlock())
if tlsCfg != nil {
dialOpts = append(dialOpts, grpc.WithTransportCredentials(credentials.NewTLS(tlsCfg)))
} else {
dialOpts = append(dialOpts, grpc.WithInsecure())
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
return grpc.DialContext(ctx, host, dialOpts...)
}
// Diff computes the difference between the keys of the two given maps.
func Diff(dst map[string]struct{}, src map[string]struct{}) ([]string, []string) {
var add []string
var del []string
for g := range dst {
if _, ok := src[g]; !ok {
add = append(add, g)
}
}
for g := range src {
if _, ok := dst[g]; !ok {
del = append(del, g)
}
}
return add, del
}
// SpanTimer returns a function used to record the duration of the given span.
func SpanTimer(span *trace.Span, name string) func() {
if span == nil {
return func() {}
}
uniq := int64(rand.Int31())
attrs := []trace.Attribute{
trace.Int64Attribute("funcId", uniq),
trace.StringAttribute("funcName", name),
}
span.Annotate(attrs, "Start.")
start := time.Now()
return func() {
span.Annotatef(attrs, "End. Took %s", time.Since(start))
// TODO: We can look into doing a latency record here.
}
}
// CloseFunc needs to be called to close all the client connections.
type CloseFunc func()
// CredOpt stores the options for logging in, including the password and user.
type CredOpt struct {
Conf *viper.Viper
UserID string
PasswordOpt string
}
type authorizationCredentials struct {
token string
}
func (a *authorizationCredentials) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{"Authorization": a.token}, nil
}
func (a *authorizationCredentials) RequireTransportSecurity() bool {
return true
}
// WithAuthorizationCredentials adds Authorization: <api-token> to every GRPC request
// This is mostly used by Slash GraphQL to authenticate requests
func WithAuthorizationCredentials(authToken string) grpc.DialOption {
return grpc.WithPerRPCCredentials(&authorizationCredentials{authToken})
}
// GetDgraphClient creates a Dgraph client based on the following options in the configuration:
// --slash_grpc_endpoint specifies the grpc endpoint for slash. It takes precedence over --alpha and TLS
// --alpha specifies a comma separated list of endpoints to connect to
// --tls_cacert, --tls_cert, --tls_key etc specify the TLS configuration of the connection
// --retries specifies how many times we should retry the connection to each endpoint upon failures
// --user and --password specify the credentials we should use to login with the server
func GetDgraphClient(conf *viper.Viper, login bool) (*dgo.Dgraph, CloseFunc) {
var alphas string
if conf.GetString("slash_grpc_endpoint") != "" {
alphas = conf.GetString("slash_grpc_endpoint")
} else {
alphas = conf.GetString("alpha")
}
if len(alphas) == 0 {
glog.Fatalf("The --alpha option must be set in order to connect to Dgraph")
}
fmt.Printf("\nRunning transaction with dgraph endpoint: %v\n", alphas)
tlsCfg, err := LoadClientTLSConfig(conf)
Checkf(err, "While loading TLS configuration")
ds := strings.Split(alphas, ",")
var conns []*grpc.ClientConn
var clients []api.DgraphClient
retries := 1
if conf.IsSet("retries") {
retries = conf.GetInt("retries")
if retries < 1 {
retries = 1
}
}
dialOpts := []grpc.DialOption{}
if conf.GetString("slash_grpc_endpoint") != "" && conf.IsSet("auth_token") {
dialOpts = append(dialOpts, WithAuthorizationCredentials(conf.GetString("auth_token")))
}
for _, d := range ds {
var conn *grpc.ClientConn
for i := 0; i < retries; i++ {
conn, err = SetupConnection(d, tlsCfg, false, dialOpts...)
if err == nil {
break
}
fmt.Printf("While trying to setup connection: %v. Retrying...\n", err)
time.Sleep(time.Second)
}
if conn == nil {
Fatalf("Could not setup connection after %d retries", retries)
}
conns = append(conns, conn)
dc := api.NewDgraphClient(conn)
clients = append(clients, dc)
}
dg := dgo.NewDgraphClient(clients...)
user := conf.GetString("user")
if login && len(user) > 0 {
err = GetPassAndLogin(dg, &CredOpt{
Conf: conf,
UserID: user,
PasswordOpt: "password",
})
Checkf(err, "While retrieving password and logging in")
}
closeFunc := func() {
for _, c := range conns {
if err := c.Close(); err != nil {
glog.Warningf("Error closing connection to Dgraph client: %v", err)
}
}
}
return dg, closeFunc
}
// AskUserPassword prompts the user to enter the password for the given user ID.
func AskUserPassword(userid string, pwdType string, times int) (string, error) {
AssertTrue(times == 1 || times == 2)
AssertTrue(pwdType == "Current" || pwdType == "New")
// ask for the user's password
fmt.Printf("%s password for %v:", pwdType, userid)
pd, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", errors.Wrapf(err, "while reading password")
}
fmt.Println()
password := string(pd)
if times == 2 {
fmt.Printf("Retype %s password for %v:", strings.ToLower(pwdType), userid)
pd2, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", errors.Wrapf(err, "while reading password")
}
fmt.Println()
password2 := string(pd2)
if password2 != password {
return "", errors.Errorf("the two typed passwords do not match")
}
}
return password, nil
}
// GetPassAndLogin uses the given credentials and client to perform the login operation.
func GetPassAndLogin(dg *dgo.Dgraph, opt *CredOpt) error {
password := opt.Conf.GetString(opt.PasswordOpt)
if len(password) == 0 {
var err error
password, err = AskUserPassword(opt.UserID, "Current", 1)
if err != nil {
return err
}
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := dg.Login(ctx, opt.UserID, password); err != nil {
return errors.Wrapf(err, "unable to login to the %v account", opt.UserID)
}
fmt.Println("Login successful.")
// update the context so that it has the admin jwt token
return nil
}
func IsGuardian(groups []string) bool {
for _, group := range groups {
if group == GuardiansId {
return true
}
}
return false
}