cmd/dex: add logging config and serve logger for different modules.

This commit is contained in:
rithu john
2016-11-22 15:35:46 -08:00
parent 79c51f2983
commit 2e22a948cf
20 changed files with 191 additions and 43 deletions

View File

@@ -13,6 +13,7 @@ import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"
"github.com/Sirupsen/logrus"
"github.com/coreos/dex/connector"
)
@@ -31,12 +32,13 @@ type Config struct {
}
// Open returns a strategy for logging in through GitHub.
func (c *Config) Open() (connector.Connector, error) {
func (c *Config) Open(logger logrus.FieldLogger) (connector.Connector, error) {
return &githubConnector{
redirectURI: c.RedirectURI,
org: c.Org,
clientID: c.ClientID,
clientSecret: c.ClientSecret,
logger: logger,
}, nil
}
@@ -55,6 +57,7 @@ type githubConnector struct {
org string
clientID string
clientSecret string
logger logrus.FieldLogger
}
func (c *githubConnector) oauth2Config(scopes connector.Scopes) *oauth2.Config {

View File

@@ -13,6 +13,7 @@ import (
"golang.org/x/net/context"
"gopkg.in/ldap.v2"
"github.com/Sirupsen/logrus"
"github.com/coreos/dex/connector"
)
@@ -135,8 +136,8 @@ func parseScope(s string) (int, bool) {
}
// Open returns an authentication strategy using LDAP.
func (c *Config) Open() (connector.Connector, error) {
conn, err := c.OpenConnector()
func (c *Config) Open(logger logrus.FieldLogger) (connector.Connector, error) {
conn, err := c.OpenConnector(logger)
if err != nil {
return nil, err
}
@@ -149,7 +150,7 @@ type refreshData struct {
}
// OpenConnector is the same as Open but returns a type with all implemented connector interfaces.
func (c *Config) OpenConnector() (interface {
func (c *Config) OpenConnector(logger logrus.FieldLogger) (interface {
connector.Connector
connector.PasswordConnector
connector.RefreshConnector
@@ -206,7 +207,7 @@ func (c *Config) OpenConnector() (interface {
if !ok {
return nil, fmt.Errorf("userSearch.Scope unknown value %q", c.GroupSearch.Scope)
}
return &ldapConnector{*c, userSearchScope, groupSearchScope, tlsConfig}, nil
return &ldapConnector{*c, userSearchScope, groupSearchScope, tlsConfig, logger}, nil
}
type ldapConnector struct {
@@ -216,6 +217,8 @@ type ldapConnector struct {
groupSearchScope int
tlsConfig *tls.Config
logger logrus.FieldLogger
}
var (

View File

@@ -9,12 +9,13 @@ import (
"golang.org/x/net/context"
"github.com/Sirupsen/logrus"
"github.com/coreos/dex/connector"
)
// NewCallbackConnector returns a mock connector which requires no user interaction. It always returns
// the same (fake) identity.
func NewCallbackConnector() connector.Connector {
func NewCallbackConnector(logger logrus.FieldLogger) connector.Connector {
return &Callback{
Identity: connector.Identity{
UserID: "0-385-28089-0",
@@ -24,6 +25,7 @@ func NewCallbackConnector() connector.Connector {
Groups: []string{"authors"},
ConnectorData: connectorData,
},
Logger: logger,
}
}
@@ -37,6 +39,7 @@ var (
type Callback struct {
// The returned identity.
Identity connector.Identity
Logger logrus.FieldLogger
}
// LoginURL returns the URL to redirect the user to login with.
@@ -67,8 +70,8 @@ func (m *Callback) Refresh(ctx context.Context, s connector.Scopes, identity con
type CallbackConfig struct{}
// Open returns an authentication strategy which requires no user interaction.
func (c *CallbackConfig) Open() (connector.Connector, error) {
return NewCallbackConnector(), nil
func (c *CallbackConfig) Open(logger logrus.FieldLogger) (connector.Connector, error) {
return NewCallbackConnector(logger), nil
}
// PasswordConfig holds the configuration for a mock connector which prompts for the supplied
@@ -79,19 +82,20 @@ type PasswordConfig struct {
}
// Open returns an authentication strategy which prompts for a predefined username and password.
func (c *PasswordConfig) Open() (connector.Connector, error) {
func (c *PasswordConfig) Open(logger logrus.FieldLogger) (connector.Connector, error) {
if c.Username == "" {
return nil, errors.New("no username supplied")
}
if c.Password == "" {
return nil, errors.New("no password supplied")
}
return &passwordConnector{c.Username, c.Password}, nil
return &passwordConnector{c.Username, c.Password, logger}, nil
}
type passwordConnector struct {
username string
password string
logger logrus.FieldLogger
}
func (p passwordConnector) Close() error { return nil }

View File

@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"github.com/Sirupsen/logrus"
"github.com/coreos/go-oidc"
"golang.org/x/net/context"
"golang.org/x/oauth2"
@@ -25,7 +26,7 @@ type Config struct {
// Open returns a connector which can be used to login users through an upstream
// OpenID Connect provider.
func (c *Config) Open() (conn connector.Connector, err error) {
func (c *Config) Open(logger logrus.FieldLogger) (conn connector.Connector, err error) {
ctx, cancel := context.WithCancel(context.Background())
provider, err := oidc.NewProvider(ctx, c.Issuer)
@@ -55,6 +56,7 @@ func (c *Config) Open() (conn connector.Connector, err error) {
oidc.VerifyExpiry(),
oidc.VerifyAudience(clientID),
),
logger: logger,
}, nil
}
@@ -68,6 +70,7 @@ type oidcConnector struct {
verifier *oidc.IDTokenVerifier
ctx context.Context
cancel context.CancelFunc
logger logrus.FieldLogger
}
func (c *oidcConnector) Close() error {