forked from mystiq/dex
Merge pull request #277 from ericchiang/cmd_stdin
*: allow dexctl set-connector-configs to read from stdin
This commit is contained in:
commit
67c1bd6aee
4 changed files with 39 additions and 28 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue