github.com/pachyderm/pachyderm@v1.13.4/src/server/auth/cmds/configure.go (about) 1 package cmds 2 3 import ( 4 "bytes" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "strings" 9 10 "github.com/pachyderm/pachyderm/src/client" 11 "github.com/pachyderm/pachyderm/src/client/auth" 12 "github.com/pachyderm/pachyderm/src/client/pkg/errors" 13 "github.com/pachyderm/pachyderm/src/client/pkg/grpcutil" 14 "github.com/pachyderm/pachyderm/src/server/pkg/cmdutil" 15 "github.com/pachyderm/pachyderm/src/server/pkg/serde" 16 17 "github.com/spf13/cobra" 18 ) 19 20 // GetConfigCmd returns a cobra command that lets the caller see the configured 21 // auth backends in Pachyderm 22 func GetConfigCmd() *cobra.Command { 23 var format string 24 getConfig := &cobra.Command{ 25 Short: "Retrieve Pachyderm's current auth configuration", 26 Long: "Retrieve Pachyderm's current auth configuration", 27 Run: cmdutil.RunFixedArgs(0, func(args []string) error { 28 c, err := client.NewOnUserMachine("user") 29 if err != nil { 30 return errors.Wrapf(err, "could not connect") 31 } 32 defer c.Close() 33 resp, err := c.GetConfiguration(c.Ctx(), &auth.GetConfigurationRequest{}) 34 if err != nil { 35 return grpcutil.ScrubGRPC(err) 36 } 37 if resp.Configuration == nil { 38 fmt.Println("no auth config set") 39 return nil 40 } 41 42 var buf bytes.Buffer 43 if format == "" { 44 format = "json" 45 } else { 46 format = strings.ToLower(format) 47 } 48 e, err := serde.GetEncoder(format, &buf, serde.WithIndent(2), 49 serde.WithOrigName(true)) 50 if err != nil { 51 return err 52 } 53 // Use Encode() rather than EncodeProto, because the official proto->json 54 // spec (https://developers.google.com/protocol-buffers/docs/proto3#json) 55 // requires that int64 fields (e.g. live_config_version) be serialized as 56 // strings rather than ints, which would break existing auth configs. Go's 57 // built-in json serializer marshals int64 fields to JSON numbers 58 if err := e.Encode(resp.Configuration); err != nil { 59 return err 60 } 61 fmt.Println(buf.String()) 62 return nil 63 }), 64 } 65 getConfig.Flags().StringVarP(&format, "output-format", "o", "json", "output "+ 66 "format (\"json\" or \"yaml\")") 67 return cmdutil.CreateAlias(getConfig, "auth get-config") 68 } 69 70 // SetConfigCmd returns a cobra command that lets the caller configure auth 71 // backends in Pachyderm 72 func SetConfigCmd() *cobra.Command { 73 var file string 74 setConfig := &cobra.Command{ 75 Short: "Set Pachyderm's current auth configuration", 76 Long: "Set Pachyderm's current auth configuration", 77 Run: cmdutil.RunFixedArgs(0, func(args []string) error { 78 c, err := client.NewOnUserMachine("user") 79 if err != nil { 80 return errors.Wrapf(err, "could not connect") 81 } 82 defer c.Close() 83 var rawConfigBytes []byte 84 if file == "-" { 85 var err error 86 rawConfigBytes, err = ioutil.ReadAll(os.Stdin) 87 if err != nil { 88 return errors.Wrapf(err, "could not read config from stdin") 89 } 90 } else if file != "" { 91 var err error 92 rawConfigBytes, err = ioutil.ReadFile(file) 93 if err != nil { 94 return errors.Wrapf(err, "could not read config from %q", file) 95 } 96 } else { 97 return errors.New("must set input file (use \"-\" to read from stdin)") 98 } 99 100 // parse config 101 var config auth.AuthConfig 102 if err := serde.DecodeYAML(rawConfigBytes, &config); err != nil { 103 return errors.Wrapf(err, "could not parse config") 104 } 105 // TODO(msteffen): try to handle empty config? 106 _, err = c.SetConfiguration(c.Ctx(), &auth.SetConfigurationRequest{ 107 Configuration: &config, 108 }) 109 return grpcutil.ScrubGRPC(err) 110 }), 111 } 112 setConfig.Flags().StringVarP(&file, "file", "f", "-", "input file (to use "+ 113 "as the new config") 114 return cmdutil.CreateAlias(setConfig, "auth set-config") 115 }