github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/ee/acl/run_ee.go (about)

     1  // +build !oss
     2  
     3  /*
     4   * Copyright 2018 Dgraph Labs, Inc. and Contributors
     5   *
     6   * Licensed under the Dgraph Community License (the "License"); you
     7   * may not use this file except in compliance with the License. You
     8   * may obtain a copy of the License at
     9   *
    10   *     https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
    11   */
    12  
    13  package acl
    14  
    15  import (
    16  	"fmt"
    17  	"os"
    18  
    19  	"github.com/dgraph-io/dgraph/x"
    20  	"github.com/golang/glog"
    21  	"github.com/spf13/cobra"
    22  	"github.com/spf13/viper"
    23  )
    24  
    25  var (
    26  	// CmdAcl is the sub-command used to manage the ACL system.
    27  	CmdAcl x.SubCommand
    28  )
    29  
    30  const gPassword = "gpassword"
    31  const defaultGroupList = "dgraph-unused-group"
    32  
    33  func init() {
    34  	CmdAcl.Cmd = &cobra.Command{
    35  		Use:   "acl",
    36  		Short: "Run the Dgraph acl tool",
    37  	}
    38  
    39  	flag := CmdAcl.Cmd.PersistentFlags()
    40  	flag.StringP("alpha", "a", "127.0.0.1:9080", "Dgraph Alpha gRPC server address")
    41  	flag.StringP(gPassword, "x", "", "Groot password to authorize this operation")
    42  
    43  	// TLS configuration
    44  	x.RegisterClientTLSFlags(flag)
    45  
    46  	subcommands := initSubcommands()
    47  	for _, sc := range subcommands {
    48  		CmdAcl.Cmd.AddCommand(sc.Cmd)
    49  		sc.Conf = viper.New()
    50  		if err := sc.Conf.BindPFlags(sc.Cmd.Flags()); err != nil {
    51  			glog.Fatalf("Unable to bind flags for command %v: %v", sc, err)
    52  		}
    53  		if err := sc.Conf.BindPFlags(CmdAcl.Cmd.PersistentFlags()); err != nil {
    54  			glog.Fatalf("Unable to bind persistent flags from acl for command %v: %v", sc, err)
    55  		}
    56  		sc.Conf.SetEnvPrefix(sc.EnvPrefix)
    57  	}
    58  }
    59  
    60  func initSubcommands() []*x.SubCommand {
    61  	var cmdAdd x.SubCommand
    62  	cmdAdd.Cmd = &cobra.Command{
    63  		Use:   "add",
    64  		Short: "Run Dgraph acl tool to add a user or group",
    65  		Run: func(cmd *cobra.Command, args []string) {
    66  			if err := add(cmdAdd.Conf); err != nil {
    67  				fmt.Printf("%v\n", err)
    68  				os.Exit(1)
    69  			}
    70  		},
    71  	}
    72  
    73  	addFlags := cmdAdd.Cmd.Flags()
    74  	addFlags.StringP("user", "u", "", "The user id to be created")
    75  	addFlags.StringP("password", "p", "", "The password for the user")
    76  	addFlags.StringP("group", "g", "", "The group id to be created")
    77  
    78  	var cmdDel x.SubCommand
    79  	cmdDel.Cmd = &cobra.Command{
    80  		Use:   "del",
    81  		Short: "Run Dgraph acl tool to delete a user or group",
    82  		Run: func(cmd *cobra.Command, args []string) {
    83  			if err := del(cmdDel.Conf); err != nil {
    84  				fmt.Printf("Unable to delete the user: %v\n", err)
    85  				os.Exit(1)
    86  			}
    87  		},
    88  	}
    89  
    90  	delFlags := cmdDel.Cmd.Flags()
    91  	delFlags.StringP("user", "u", "", "The user id to be deleted")
    92  	delFlags.StringP("group", "g", "", "The group id to be deleted")
    93  
    94  	var cmdMod x.SubCommand
    95  	cmdMod.Cmd = &cobra.Command{
    96  		Use: "mod",
    97  		Short: "Run Dgraph acl tool to modify a user's password, a user's group list, or a" +
    98  			"group's predicate permissions",
    99  		Run: func(cmd *cobra.Command, args []string) {
   100  			if err := mod(cmdMod.Conf); err != nil {
   101  				fmt.Printf("Unable to modify: %v\n", err)
   102  				os.Exit(1)
   103  			}
   104  		},
   105  	}
   106  
   107  	modFlags := cmdMod.Cmd.Flags()
   108  	modFlags.StringP("user", "u", "", "The user id to be changed")
   109  	modFlags.BoolP("new_password", "n", false, "Whether to reset password for the user")
   110  	modFlags.StringP("group_list", "l", defaultGroupList,
   111  		"The list of groups to be set for the user")
   112  	modFlags.StringP("group", "g", "", "The group whose permission is to be changed")
   113  	modFlags.StringP("pred", "p", "", "The predicates whose acls are to be changed")
   114  	modFlags.StringP("pred_regex", "P", "", "The regular expression specifying predicates"+
   115  		" whose acls are to be changed")
   116  	modFlags.IntP("perm", "m", 0, "The acl represented using "+
   117  		"an integer: 4 for read, 2 for write, and 1 for modify. Use a negative value to remove a "+
   118  		"predicate from the group")
   119  
   120  	var cmdInfo x.SubCommand
   121  	cmdInfo.Cmd = &cobra.Command{
   122  		Use:   "info",
   123  		Short: "Show info about a user or group",
   124  		Run: func(cmd *cobra.Command, args []string) {
   125  			if err := info(cmdInfo.Conf); err != nil {
   126  				fmt.Printf("Unable to show info: %v\n", err)
   127  				os.Exit(1)
   128  			}
   129  		},
   130  	}
   131  	infoFlags := cmdInfo.Cmd.Flags()
   132  	infoFlags.StringP("user", "u", "", "The user to be shown")
   133  	infoFlags.StringP("group", "g", "", "The group to be shown")
   134  	return []*x.SubCommand{&cmdAdd, &cmdDel, &cmdMod, &cmdInfo}
   135  }