2015-08-18 05:57:27 +05:30
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2015-12-29 05:25:11 +05:30
|
|
|
"strings"
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
"github.com/coreos/dex/pkg/log"
|
|
|
|
"github.com/coreos/go-oidc/oidc"
|
2015-12-29 05:25:11 +05:30
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2015-08-18 05:57:27 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-12-29 05:25:11 +05:30
|
|
|
rootCmd = &cobra.Command{
|
|
|
|
Use: "dexctl",
|
|
|
|
Short: "A command line tool for interacting with the dex system",
|
|
|
|
Long: "",
|
|
|
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
// initialize flags from environment
|
|
|
|
fs := cmd.Flags()
|
|
|
|
|
|
|
|
// don't override flags set by command line flags
|
|
|
|
alreadySet := make(map[string]bool)
|
|
|
|
fs.Visit(func(f *pflag.Flag) { alreadySet[f.Name] = true })
|
|
|
|
|
|
|
|
var err error
|
|
|
|
fs.VisitAll(func(f *pflag.Flag) {
|
|
|
|
if err != nil || alreadySet[f.Name] {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
key := "DEXCTL_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
|
|
|
|
if val := os.Getenv(key); val != "" {
|
|
|
|
err = fs.Set(f.Name, val)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
},
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
cmd.Help()
|
|
|
|
os.Exit(2)
|
|
|
|
},
|
|
|
|
}
|
2015-08-18 05:57:27 +05:30
|
|
|
|
|
|
|
global struct {
|
|
|
|
endpoint string
|
|
|
|
creds oidc.ClientCredentials
|
|
|
|
dbURL string
|
|
|
|
help bool
|
|
|
|
logDebug bool
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.EnableTimestamps()
|
|
|
|
|
2015-12-29 05:25:11 +05:30
|
|
|
rootCmd.PersistentFlags().StringVar(&global.endpoint, "endpoint", "", "URL of dex API")
|
|
|
|
rootCmd.PersistentFlags().StringVar(&global.creds.ID, "client-id", "", "dex API user ID")
|
|
|
|
rootCmd.PersistentFlags().StringVar(&global.creds.Secret, "client-secret", "", "dex API user password")
|
|
|
|
rootCmd.PersistentFlags().StringVar(&global.dbURL, "db-url", "", "DSN-formatted database connection string")
|
|
|
|
rootCmd.PersistentFlags().BoolVar(&global.logDebug, "log-debug", false, "Log debug-level information")
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-12-29 05:25:11 +05:30
|
|
|
if err := rootCmd.Execute(); err != nil {
|
2015-08-18 05:57:27 +05:30
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-29 05:25:11 +05:30
|
|
|
func wrapRun(run func(cmd *cobra.Command, args []string) int) func(cmd *cobra.Command, args []string) {
|
|
|
|
return func(cmd *cobra.Command, args []string) {
|
|
|
|
os.Exit(run(cmd, args))
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getDriver() (drv driver) {
|
|
|
|
var err error
|
|
|
|
switch {
|
|
|
|
case len(global.dbURL) > 0:
|
|
|
|
drv, err = newDBDriver(global.dbURL)
|
|
|
|
case len(global.endpoint) > 0:
|
|
|
|
if len(global.creds.ID) == 0 || len(global.creds.Secret) == 0 {
|
|
|
|
err = errors.New("--client-id/--client-secret flags unset")
|
2015-09-05 01:15:32 +05:30
|
|
|
break
|
|
|
|
}
|
|
|
|
pcfg, err := oidc.FetchProviderConfig(http.DefaultClient, global.endpoint)
|
|
|
|
if err != nil {
|
|
|
|
stderr("Unable to fetch provider config: %v", err)
|
|
|
|
os.Exit(1)
|
2015-08-18 05:57:27 +05:30
|
|
|
}
|
2015-09-05 01:15:32 +05:30
|
|
|
drv, err = newAPIDriver(pcfg, global.creds)
|
2015-08-18 05:57:27 +05:30
|
|
|
default:
|
|
|
|
err = errors.New("--endpoint/--db-url flags unset")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
stderr("Unable to configure dexctl driver: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|