-
Notifications
You must be signed in to change notification settings - Fork 568
/
configure.go
119 lines (112 loc) · 3.96 KB
/
configure.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
package cmds
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/pachyderm/pachyderm/v2/src/auth"
"github.com/pachyderm/pachyderm/v2/src/internal/cmdutil"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/grpcutil"
"github.com/pachyderm/pachyderm/v2/src/internal/pachctl"
"github.com/pachyderm/pachyderm/v2/src/internal/serde"
)
// GetConfigCmd returns a cobra command that lets the caller see the configured
// auth backends in Pachyderm
func GetConfigCmd(pachctlCfg *pachctl.Config) *cobra.Command {
var enterprise bool
var format string
getConfig := &cobra.Command{
Short: "Retrieve Pachyderm's current auth configuration",
Long: "Retrieve Pachyderm's current auth configuration",
Run: cmdutil.RunFixedArgs(0, func(cmd *cobra.Command, args []string) error {
c, err := pachctlCfg.NewOnUserMachine(cmd.Context(), enterprise)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
resp, err := c.GetConfiguration(c.Ctx(), &auth.GetConfigurationRequest{})
if err != nil {
return grpcutil.ScrubGRPC(err)
}
if resp.Configuration == nil {
fmt.Println("no auth config set")
return nil
}
var buf bytes.Buffer
if format == "" {
format = "json"
} else {
format = strings.ToLower(format)
}
e, err := serde.GetEncoder(format, &buf, serde.WithIndent(2),
serde.WithOrigName(true))
if err != nil {
return err
}
// Use Encode() rather than EncodeProto, because the official proto->json
// spec (https://developers.google.com/protocol-buffers/docs/proto3#json)
// requires that int64 fields (e.g. live_config_version) be serialized as
// strings rather than ints, which would break existing auth configs. Go's
// built-in json serializer marshals int64 fields to JSON numbers
if err := e.Encode(resp.Configuration); err != nil {
return errors.EnsureStack(err)
}
fmt.Println(buf.String())
return nil
}),
}
getConfig.PersistentFlags().BoolVar(&enterprise, "enterprise", false, "Get auth config for the active enterprise context")
getConfig.Flags().StringVarP(&format, "output-format", "o", "json", "output "+
"format (\"json\" or \"yaml\")")
return cmdutil.CreateAlias(getConfig, "auth get-config")
}
// SetConfigCmd returns a cobra command that lets the caller configure auth
// backends in Pachyderm
func SetConfigCmd(pachctlCfg *pachctl.Config) *cobra.Command {
var enterprise bool
var file string
setConfig := &cobra.Command{
Short: "Set Pachyderm's current auth configuration",
Long: "Set Pachyderm's current auth configuration",
Run: cmdutil.RunFixedArgs(0, func(cmd *cobra.Command, args []string) error {
c, err := pachctlCfg.NewOnUserMachine(cmd.Context(), enterprise)
if err != nil {
return errors.Wrapf(err, "could not connect")
}
defer c.Close()
var rawConfigBytes []byte
if file == "-" {
var err error
rawConfigBytes, err = io.ReadAll(os.Stdin)
if err != nil {
return errors.Wrapf(err, "could not read config from stdin")
}
} else if file != "" {
var err error
rawConfigBytes, err = os.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "could not read config from %q", file)
}
} else {
return errors.New("must set input file (use \"-\" to read from stdin)")
}
// parse config
var config auth.OIDCConfig
if err := serde.Decode(rawConfigBytes, &config); err != nil {
return errors.Wrapf(err, "could not parse config")
}
// TODO(msteffen): try to handle empty config?
_, err = c.SetConfiguration(c.Ctx(), &auth.SetConfigurationRequest{
Configuration: &config,
})
return grpcutil.ScrubGRPC(err)
}),
}
setConfig.PersistentFlags().BoolVar(&enterprise, "enterprise", false, "Set auth config for the active enterprise context")
setConfig.Flags().StringVarP(&file, "file", "f", "-", "input file (to use "+
"as the new config")
return cmdutil.CreateAlias(setConfig, "auth set-config")
}