2016-07-25 20:00:28 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2016-10-04 07:27:50 +00:00
|
|
|
"net"
|
2016-07-25 20:00:28 +00:00
|
|
|
"net/http"
|
|
|
|
|
2016-08-05 16:50:22 +00:00
|
|
|
"github.com/spf13/cobra"
|
2016-10-04 07:27:50 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
2016-07-25 20:00:28 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
|
|
|
|
2016-10-04 07:27:50 +00:00
|
|
|
"github.com/coreos/dex/api"
|
2016-08-11 05:31:42 +00:00
|
|
|
"github.com/coreos/dex/server"
|
|
|
|
"github.com/coreos/dex/storage"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func commandServe() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "serve [ config file ]",
|
|
|
|
Short: "Connect to the storage and begin serving requests.",
|
|
|
|
Long: ``,
|
2016-10-04 07:27:50 +00:00
|
|
|
Example: "dex serve config.yaml",
|
2016-07-25 20:00:28 +00:00
|
|
|
RunE: serve,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serve(cmd *cobra.Command, args []string) error {
|
|
|
|
switch len(args) {
|
|
|
|
default:
|
|
|
|
return errors.New("surplus arguments")
|
|
|
|
case 0:
|
|
|
|
// TODO(ericchiang): Consider having a default config file location.
|
|
|
|
return errors.New("no config file specified")
|
|
|
|
case 1:
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := args[0]
|
|
|
|
configData, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("read config file %s: %v", configFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
if err := yaml.Unmarshal(configData, &c); err != nil {
|
|
|
|
return fmt.Errorf("parse config file %s: %v", configFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fast checks. Perform these first for a more responsive CLI.
|
|
|
|
checks := []struct {
|
|
|
|
bad bool
|
|
|
|
errMsg string
|
|
|
|
}{
|
|
|
|
{c.Issuer == "", "no issuer specified in config file"},
|
2016-10-05 23:50:02 +00:00
|
|
|
{len(c.Connectors) == 0 && !c.EnablePasswordDB, "no connectors supplied in config file"},
|
|
|
|
{!c.EnablePasswordDB && len(c.StaticPasswords) != 0, "cannot specify static passwords without enabling password db"},
|
2016-07-25 20:00:28 +00:00
|
|
|
{c.Storage.Config == nil, "no storage suppied in config file"},
|
|
|
|
{c.Web.HTTP == "" && c.Web.HTTPS == "", "must supply a HTTP/HTTPS address to listen on"},
|
|
|
|
{c.Web.HTTPS != "" && c.Web.TLSCert == "", "no cert specified for HTTPS"},
|
|
|
|
{c.Web.HTTPS != "" && c.Web.TLSKey == "", "no private key specified for HTTPS"},
|
2016-10-04 07:27:50 +00:00
|
|
|
{c.GRPC.TLSCert != "" && c.GRPC.Addr == "", "no address specified for gRPC"},
|
|
|
|
{c.GRPC.TLSKey != "" && c.GRPC.Addr == "", "no address specified for gRPC"},
|
|
|
|
{(c.GRPC.TLSCert == "") != (c.GRPC.TLSKey == ""), "must specific both a gRPC TLS cert and key"},
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, check := range checks {
|
|
|
|
if check.bad {
|
|
|
|
return errors.New(check.errMsg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-04 07:27:50 +00:00
|
|
|
var grpcOptions []grpc.ServerOption
|
|
|
|
if c.GRPC.TLSCert != "" {
|
|
|
|
opt, err := credentials.NewServerTLSFromFile(c.GRPC.TLSCert, c.GRPC.TLSKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("load grpc certs: %v", err)
|
|
|
|
}
|
|
|
|
grpcOptions = append(grpcOptions, grpc.Creds(opt))
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
connectors := make([]server.Connector, len(c.Connectors))
|
|
|
|
for i, conn := range c.Connectors {
|
|
|
|
if conn.Config == nil {
|
|
|
|
return fmt.Errorf("no config field for connector %q", conn.ID)
|
|
|
|
}
|
|
|
|
c, err := conn.Config.Open()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("open %s: %v", conn.ID, err)
|
|
|
|
}
|
|
|
|
connectors[i] = server.Connector{
|
|
|
|
ID: conn.ID,
|
|
|
|
DisplayName: conn.Name,
|
|
|
|
Connector: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := c.Storage.Config.Open()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing storage: %v", err)
|
|
|
|
}
|
2016-08-05 16:50:22 +00:00
|
|
|
if len(c.StaticClients) > 0 {
|
|
|
|
s = storage.WithStaticClients(s, c.StaticClients)
|
|
|
|
}
|
2016-10-05 23:50:02 +00:00
|
|
|
if len(c.StaticPasswords) > 0 {
|
|
|
|
p := make([]storage.Password, len(c.StaticPasswords))
|
|
|
|
for i, pw := range c.StaticPasswords {
|
|
|
|
if p[i], err = pw.toPassword(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = storage.WithStaticPasswords(s, p)
|
|
|
|
}
|
2016-07-25 20:00:28 +00:00
|
|
|
|
|
|
|
serverConfig := server.Config{
|
2016-08-25 23:18:09 +00:00
|
|
|
SupportedResponseTypes: c.OAuth2.ResponseTypes,
|
2016-10-07 18:53:01 +00:00
|
|
|
SkipApprovalScreen: c.OAuth2.SkipApprovalScreen,
|
2016-08-25 20:10:19 +00:00
|
|
|
Issuer: c.Issuer,
|
|
|
|
Connectors: connectors,
|
|
|
|
Storage: s,
|
|
|
|
TemplateConfig: c.Templates,
|
2016-10-05 23:50:02 +00:00
|
|
|
EnablePasswordDB: c.EnablePasswordDB,
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-04 07:27:50 +00:00
|
|
|
serv, err := server.NewServer(serverConfig)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("initializing server: %v", err)
|
|
|
|
}
|
2016-10-04 07:27:50 +00:00
|
|
|
errc := make(chan error, 3)
|
2016-07-25 20:00:28 +00:00
|
|
|
if c.Web.HTTP != "" {
|
2016-10-04 07:27:50 +00:00
|
|
|
log.Printf("listening (http) on %s", c.Web.HTTP)
|
2016-07-25 20:00:28 +00:00
|
|
|
go func() {
|
|
|
|
errc <- http.ListenAndServe(c.Web.HTTP, serv)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if c.Web.HTTPS != "" {
|
2016-10-04 07:27:50 +00:00
|
|
|
log.Printf("listening (https) on %s", c.Web.HTTPS)
|
2016-07-25 20:00:28 +00:00
|
|
|
go func() {
|
|
|
|
errc <- http.ListenAndServeTLS(c.Web.HTTPS, c.Web.TLSCert, c.Web.TLSKey, serv)
|
|
|
|
}()
|
|
|
|
}
|
2016-10-04 07:27:50 +00:00
|
|
|
if c.GRPC.Addr != "" {
|
|
|
|
log.Printf("listening (grpc) on %s", c.GRPC.Addr)
|
|
|
|
go func() {
|
|
|
|
errc <- func() error {
|
|
|
|
list, err := net.Listen("tcp", c.GRPC.Addr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("listen grpc: %v", err)
|
|
|
|
}
|
|
|
|
s := grpc.NewServer(grpcOptions...)
|
|
|
|
api.RegisterDexServer(s, server.NewAPI(serverConfig.Storage))
|
|
|
|
return s.Serve(list)
|
|
|
|
}()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
return <-errc
|
|
|
|
}
|