From ec3bc7f258c43f74207a390bc50fab332ac1782e Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Tue, 19 Jan 2016 08:59:34 -0800 Subject: [PATCH] *: allow dexctl set-connector-configs to read from stdin Closes #276 --- cmd/dexctl/command_config.go | 26 +++++++++++++++++--------- connector/config_repo.go | 18 +----------------- functional/dexctl_test.go | 13 +++++++++++++ server/config.go | 10 ++++++++-- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/cmd/dexctl/command_config.go b/cmd/dexctl/command_config.go index 5fda0927..8fb9bb4b 100644 --- a/cmd/dexctl/command_config.go +++ b/cmd/dexctl/command_config.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "io" + "os" "github.com/coreos/dex/connector" "github.com/spf13/cobra" @@ -18,8 +20,8 @@ var ( cmdSetConnectorConfigs = &cobra.Command{ Use: "set-connector-configs", - Short: "Overwrite the current IdP connector configs with those from a local file.", - Long: "Overwrite the current IdP connector configs with those from a local file.", + Short: "Overwrite the current IdP connector configs with those from a local file. Provide the argument '-' to read from stdin.", + Long: "Overwrite the current IdP connector configs with those from a local file. Provide the argument '-' to read from stdin.", Example: ` dexctl set-connector-configs --db-url=${DB_URL} ./static/conn_conf.json`, Run: wrapRun(runSetConnectorConfigs), } @@ -36,15 +38,22 @@ func runSetConnectorConfigs(cmd *cobra.Command, args []string) int { return 2 } - rf, err := connector.NewConnectorConfigRepoFromFile(args[0]) - if err != nil { - stderr("Unable to retrieve connector configs from file: %v", err) - return 1 + var r io.Reader + if from := args[0]; from == "-" { + r = os.Stdin + } else { + f, err := os.Open(from) + if err != nil { + stderr("Unable to open specified file: %v", err) + return 1 + } + defer f.Close() + r = f } - cfgs, err := rf.All() + cfgs, err := connector.ReadConfigs(r) if err != nil { - stderr("Unable to retrieve connector configs from file: %v", err) + stderr("Unable to decode connector configs: %v", err) return 1 } @@ -54,7 +63,6 @@ func runSetConnectorConfigs(cmd *cobra.Command, args []string) int { } fmt.Printf("Saved %d connector config(s)\n", len(cfgs)) - return 0 } diff --git a/connector/config_repo.go b/connector/config_repo.go index dd2d2c09..e44e5fe3 100644 --- a/connector/config_repo.go +++ b/connector/config_repo.go @@ -3,12 +3,11 @@ package connector import ( "encoding/json" "io" - "os" "github.com/coreos/dex/repo" ) -func newConnectorConfigsFromReader(r io.Reader) ([]ConnectorConfig, error) { +func ReadConfigs(r io.Reader) ([]ConnectorConfig, error) { var ms []map[string]interface{} if err := json.NewDecoder(r).Decode(&ms); err != nil { return nil, err @@ -24,21 +23,6 @@ func newConnectorConfigsFromReader(r io.Reader) ([]ConnectorConfig, error) { return cfgs, nil } -func NewConnectorConfigRepoFromFile(loc string) (ConnectorConfigRepo, error) { - cf, err := os.Open(loc) - if err != nil { - return nil, err - } - defer cf.Close() - - cfgs, err := newConnectorConfigsFromReader(cf) - if err != nil { - return nil, err - } - - return &memConnectorConfigRepo{configs: cfgs}, nil -} - type memConnectorConfigRepo struct { configs []ConnectorConfig } diff --git a/functional/dexctl_test.go b/functional/dexctl_test.go index 10ff7179..6db38349 100644 --- a/functional/dexctl_test.go +++ b/functional/dexctl_test.go @@ -1,6 +1,8 @@ package functional import ( + "bytes" + "io" "io/ioutil" "os" "os/exec" @@ -42,6 +44,7 @@ func TestDexctlCommands(t *testing.T) { args []string env []string expErr bool + stdin io.Reader }{ { args: []string{"new-client", "https://example.com/callback"}, @@ -57,10 +60,20 @@ func TestDexctlCommands(t *testing.T) { { args: []string{"set-connector-configs", "--db-url", dsn, connConfig}, }, + { + args: []string{"set-connector-configs", "--db-url", dsn, "-"}, + stdin: bytes.NewReader(connConfigExample), + }, + { + args: []string{"set-connector-configs", "-"}, + env: []string{"DEXCTL_DB_URL=" + dsn}, + stdin: bytes.NewReader(connConfigExample), + }, } for _, tt := range tests { cmd := exec.Command("../bin/dexctl", tt.args...) + cmd.Stdin = tt.stdin cmd.Env = tt.env out, err := cmd.CombinedOutput() if !tt.expErr && err != nil { diff --git a/server/config.go b/server/config.go index f70065a3..acbdce42 100644 --- a/server/config.go +++ b/server/config.go @@ -115,10 +115,16 @@ func (cfg *SingleServerConfig) Configure(srv *Server) error { return fmt.Errorf("unable to read client identities from file %s: %v", cfg.ClientsFile, err) } - cfgRepo, err := connector.NewConnectorConfigRepoFromFile(cfg.ConnectorsFile) + f, err := os.Open(cfg.ConnectorsFile) if err != nil { - return fmt.Errorf("unable to create ConnectorConfigRepo: %v", err) + return fmt.Errorf("opening connectors file: %v", err) } + defer f.Close() + cfgs, err := connector.ReadConfigs(f) + if err != nil { + return fmt.Errorf("decoding connector configs: %v", err) + } + cfgRepo := connector.NewConnectorConfigRepoFromConfigs(cfgs) sRepo := session.NewSessionRepo() skRepo := session.NewSessionKeyRepo()