@@ -36,6 +36,8 @@ import (
3636 "time"
3737
3838 badgerpb "github.com/dgraph-io/badger/v3/pb"
39+ "github.com/dgraph-io/dgraph/ee/audit"
40+
3941 "github.com/dgraph-io/dgo/v200/protos/api"
4042 "github.com/dgraph-io/dgraph/edgraph"
4143 "github.com/dgraph-io/dgraph/ee/enc"
@@ -191,6 +193,14 @@ they form a Raft group and provide synchronous replication.
191193 `Cache percentages summing up to 100 for various caches (FORMAT:
192194 PostingListCache,PstoreBlockCache,PstoreIndexCache,WAL).` )
193195
196+ flag .String ("audit" , "" ,
197+ `Various audit options.
198+ dir=/path/to/audits to define the path where to store the audit logs.
199+ compress=true/false to enabled the compression of old audit logs (default behaviour is false).
200+ encrypt_file=enc/key/file enables the audit log encryption with the key path provided with the
201+ flag.
202+ Sample flag could look like --audit dir=aa;encrypt_file=/filepath;compress=true` )
203+
194204 // TLS configurations
195205 x .RegisterServerTLSFlags (flag )
196206}
@@ -379,6 +389,7 @@ func serveGRPC(l net.Listener, tlsCfg *tls.Config, closer *z.Closer) {
379389 grpc .MaxSendMsgSize (x .GrpcMaxSize ),
380390 grpc .MaxConcurrentStreams (1000 ),
381391 grpc .StatsHandler (& ocgrpc.ServerHandler {}),
392+ grpc .UnaryInterceptor (audit .AuditRequestGRPC ),
382393 }
383394 if tlsCfg != nil {
384395 opt = append (opt , grpc .Creds (credentials .NewTLS (tlsCfg )))
@@ -417,15 +428,18 @@ func setupServer(closer *z.Closer) {
417428 log .Fatal (err )
418429 }
419430
420- http .HandleFunc ("/query" , queryHandler )
421- http .HandleFunc ("/query/" , queryHandler )
422- http .HandleFunc ("/mutate" , mutationHandler )
423- http .HandleFunc ("/mutate/" , mutationHandler )
424- http .HandleFunc ("/commit" , commitHandler )
425- http .HandleFunc ("/alter" , alterHandler )
426- http .HandleFunc ("/health" , healthCheck )
427- http .HandleFunc ("/state" , stateHandler )
428- http .HandleFunc ("/jemalloc" , x .JemallocHandler )
431+ baseMux := http .NewServeMux ()
432+ http .Handle ("/" , audit .AuditRequestHttp (baseMux ))
433+
434+ baseMux .HandleFunc ("/query" , queryHandler )
435+ baseMux .HandleFunc ("/query/" , queryHandler )
436+ baseMux .HandleFunc ("/mutate" , mutationHandler )
437+ baseMux .HandleFunc ("/mutate/" , mutationHandler )
438+ baseMux .HandleFunc ("/commit" , commitHandler )
439+ baseMux .HandleFunc ("/alter" , alterHandler )
440+ baseMux .HandleFunc ("/health" , healthCheck )
441+ baseMux .HandleFunc ("/state" , stateHandler )
442+ baseMux .HandleFunc ("/jemalloc" , x .JemallocHandler )
429443
430444 // TODO: Figure out what this is for?
431445 http .HandleFunc ("/debug/store" , storeStatsHandler )
@@ -451,8 +465,9 @@ func setupServer(closer *z.Closer) {
451465 var gqlHealthStore * admin.GraphQLHealthStore
452466 // Do not use := notation here because adminServer is a global variable.
453467 mainServer , adminServer , gqlHealthStore = admin .NewServers (introspection , & globalEpoch , closer )
454- http .Handle ("/graphql" , mainServer .HTTPHandler ())
455- http .HandleFunc ("/probe/graphql" , func (w http.ResponseWriter , r * http.Request ) {
468+ baseMux .Handle ("/graphql" , mainServer .HTTPHandler ())
469+ baseMux .HandleFunc ("/probe/graphql" , func (w http.ResponseWriter ,
470+ r * http.Request ) {
456471 healthStatus := gqlHealthStore .GetHealth ()
457472 httpStatusCode := http .StatusOK
458473 if ! healthStatus .Healthy {
@@ -463,18 +478,19 @@ func setupServer(closer *z.Closer) {
463478 x .Check2 (w .Write ([]byte (fmt .Sprintf (`{"status":"%s","schemaUpdateCounter":%d}` ,
464479 healthStatus .StatusMsg , atomic .LoadUint64 (& globalEpoch )))))
465480 })
466- http .Handle ("/admin" , allowedMethodsHandler (allowedMethods {
481+ baseMux .Handle ("/admin" , allowedMethodsHandler (allowedMethods {
467482 http .MethodGet : true ,
468483 http .MethodPost : true ,
469484 http .MethodOptions : true ,
470485 }, adminAuthHandler (adminServer .HTTPHandler ())))
471486
472- http .Handle ("/admin/schema" , adminAuthHandler (http .HandlerFunc (func (w http.ResponseWriter ,
487+ baseMux .Handle ("/admin/schema" , adminAuthHandler (http .HandlerFunc (func (
488+ w http.ResponseWriter ,
473489 r * http.Request ) {
474490 adminSchemaHandler (w , r , adminServer )
475491 })))
476492
477- http . Handle ("/admin/schema/validate" , http . HandlerFunc ( func (w http.ResponseWriter ,
493+ baseMux . HandleFunc ("/admin/schema/validate" , func (w http.ResponseWriter ,
478494 r * http.Request ) {
479495 schema := readRequest (w , r )
480496 w .Header ().Set ("Content-Type" , "application/json" )
@@ -489,26 +505,28 @@ func setupServer(closer *z.Closer) {
489505 w .WriteHeader (http .StatusBadRequest )
490506 errs := strings .Split (strings .TrimSpace (err .Error ()), "\n " )
491507 x .SetStatusWithErrors (w , x .ErrorInvalidRequest , errs )
492- }))
508+ })
493509
494- http .Handle ("/admin/shutdown" , allowedMethodsHandler (allowedMethods {http .MethodGet : true },
510+ baseMux .Handle ("/admin/shutdown" , allowedMethodsHandler (allowedMethods {http .
511+ MethodGet : true },
495512 adminAuthHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
496513 shutDownHandler (w , r , adminServer )
497514 }))))
498515
499- http .Handle ("/admin/draining" , allowedMethodsHandler (allowedMethods {
516+ baseMux .Handle ("/admin/draining" , allowedMethodsHandler (allowedMethods {
500517 http .MethodPut : true ,
501518 http .MethodPost : true ,
502519 }, adminAuthHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
503520 drainingHandler (w , r , adminServer )
504521 }))))
505522
506- http .Handle ("/admin/export" , allowedMethodsHandler (allowedMethods {http .MethodGet : true },
523+ baseMux .Handle ("/admin/export" , allowedMethodsHandler (
524+ allowedMethods {http .MethodGet : true },
507525 adminAuthHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
508526 exportHandler (w , r , adminServer )
509527 }))))
510528
511- http .Handle ("/admin/config/cache_mb" , allowedMethodsHandler (allowedMethods {
529+ baseMux .Handle ("/admin/config/cache_mb" , allowedMethodsHandler (allowedMethods {
512530 http .MethodGet : true ,
513531 http .MethodPut : true ,
514532 }, adminAuthHandler (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
@@ -520,10 +538,10 @@ func setupServer(closer *z.Closer) {
520538 glog .Infof ("Bringing up GraphQL HTTP admin API at %s/admin" , addr )
521539
522540 // Add OpenCensus z-pages.
523- zpages .Handle (http . DefaultServeMux , "/z" )
541+ zpages .Handle (baseMux , "/z" )
524542
525- http . HandleFunc ("/" , homeHandler )
526- http . HandleFunc ("/ui/keywords" , keywordHandler )
543+ baseMux . Handle ("/" , http . HandlerFunc ( homeHandler ) )
544+ baseMux . Handle ("/ui/keywords" , http . HandlerFunc ( keywordHandler ) )
527545
528546 // Initialize the servers.
529547 admin .ServerCloser .AddRunning (3 )
@@ -585,6 +603,8 @@ func run() {
585603 walCache := (cachePercent [3 ] * (totalCache << 20 )) / 100
586604
587605 ctype , clevel := x .ParseCompression (Alpha .Conf .GetString ("badger.compression" ))
606+
607+ conf := audit .GetAuditConf (Alpha .Conf .GetString ("audit" ))
588608 opts := worker.Options {
589609 PostingDir : Alpha .Conf .GetString ("postings" ),
590610 WALDir : Alpha .Conf .GetString ("wal" ),
@@ -597,6 +617,7 @@ func run() {
597617
598618 MutationsMode : worker .AllowMutations ,
599619 AuthToken : Alpha .Conf .GetString ("auth_token" ),
620+ Audit : conf ,
600621 }
601622
602623 secretFile := Alpha .Conf .GetString ("acl_secret_file" )
@@ -658,6 +679,8 @@ func run() {
658679 LudicrousConcurrency : Alpha .Conf .GetInt ("ludicrous_concurrency" ),
659680 TLSClientConfig : tlsClientConf ,
660681 TLSServerConfig : tlsServerConf ,
682+ HmacSecret : opts .HmacSecret ,
683+ Audit : opts .Audit != nil ,
661684 }
662685 x .WorkerConfig .Parse (Alpha .Conf )
663686
@@ -699,6 +722,9 @@ func run() {
699722
700723 worker .InitServerState ()
701724
725+ // Audit is enterprise feature.
726+ x .Check (audit .InitAuditorIfNecessary (opts .Audit , worker .EnterpriseEnabled ))
727+
702728 if Alpha .Conf .GetBool ("expose_trace" ) {
703729 // TODO: Remove this once we get rid of event logs.
704730 trace .AuthRequest = func (req * http.Request ) (any , sensitive bool ) {
@@ -792,6 +818,8 @@ func run() {
792818 adminCloser .SignalAndWait ()
793819 glog .Infoln ("adminCloser closed." )
794820
821+ audit .Close ()
822+
795823 worker .State .Dispose ()
796824 x .RemoveCidFile ()
797825 glog .Info ("worker.State disposed." )
0 commit comments