Skip to content

Commit

Permalink
Add logger interface and stop relying on Logrus directly
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed Feb 22, 2019
1 parent ca66289 commit be581fa
Show file tree
Hide file tree
Showing 38 changed files with 203 additions and 133 deletions.
4 changes: 2 additions & 2 deletions cmd/dex/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"fmt"
"os"

"github.com/sirupsen/logrus"
"golang.org/x/crypto/bcrypt"

"github.com/dexidp/dex/pkg/log"
"github.com/dexidp/dex/server"
"github.com/dexidp/dex/storage"
"github.com/dexidp/dex/storage/etcd"
Expand Down Expand Up @@ -127,7 +127,7 @@ type Storage struct {

// StorageConfig is a configuration that can create a storage.
type StorageConfig interface {
Open(logrus.FieldLogger) (storage.Storage, error)
Open(logger log.Logger) (storage.Storage, error)
}

var storages = map[string]func() StorageConfig{
Expand Down
7 changes: 4 additions & 3 deletions cmd/dex/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"google.golang.org/grpc/credentials"

"github.com/dexidp/dex/api"
"github.com/dexidp/dex/pkg/log"
"github.com/dexidp/dex/server"
"github.com/dexidp/dex/storage"
)
Expand Down Expand Up @@ -324,7 +325,7 @@ func (f *utcFormatter) Format(e *logrus.Entry) ([]byte, error) {
return f.f.Format(e)
}

func newLogger(level string, format string) (logrus.FieldLogger, error) {
func newLogger(level string, format string) (log.Logger, error) {
var logLevel logrus.Level
switch strings.ToLower(level) {
case "debug":
Expand All @@ -347,9 +348,9 @@ func newLogger(level string, format string) (logrus.FieldLogger, error) {
return nil, fmt.Errorf("log format is not one of the supported values (%s): %s", strings.Join(logFormats, ", "), format)
}

return &logrus.Logger{
return log.NewLogrusLogger(&logrus.Logger{
Out: os.Stderr,
Formatter: &formatter,
Level: logLevel,
}, nil
}), nil
}
7 changes: 3 additions & 4 deletions connector/authproxy/authproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ import (
"net/http"
"net/url"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

// Config holds the configuration parameters for a connector which returns an
// identity with the HTTP header X-Remote-User as verified email.
type Config struct{}

// Open returns an authentication strategy which requires no user interaction.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
return &callback{logger: logger, pathSuffix: "/" + id}, nil
}

// Callback is a connector which returns an identity with the HTTP header
// X-Remote-User as verified email.
type callback struct {
logger logrus.FieldLogger
logger log.Logger
pathSuffix string
}

Expand Down
7 changes: 3 additions & 4 deletions connector/bitbucketcloud/bitbucketcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/dexidp/dex/pkg/log"
"io/ioutil"
"net/http"
"sync"
Expand All @@ -14,8 +15,6 @@ import (
"golang.org/x/oauth2"
"golang.org/x/oauth2/bitbucket"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
)

Expand All @@ -40,7 +39,7 @@ type Config struct {
}

// Open returns a strategy for logging in through Bitbucket.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {

b := bitbucketConnector{
redirectURI: c.RedirectURI,
Expand Down Expand Up @@ -70,7 +69,7 @@ type bitbucketConnector struct {
teams []string
clientID string
clientSecret string
logger logrus.FieldLogger
logger log.Logger
apiURL string

// the following are used only for tests
Expand Down
9 changes: 4 additions & 5 deletions connector/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import (
"strings"
"time"

"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"golang.org/x/oauth2/github"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

const (
Expand Down Expand Up @@ -53,7 +53,6 @@ type Config struct {

// Org holds org-team filters, in which teams are optional.
type Org struct {

// Organization name in github (not slug, full name). Only users in this github
// organization can authenticate.
Name string `json:"name"`
Expand All @@ -66,14 +65,14 @@ type Org struct {
}

// Open returns a strategy for logging in through GitHub.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {

if c.Org != "" {
// Return error if both 'org' and 'orgs' fields are used.
if len(c.Orgs) > 0 {
return nil, errors.New("github: cannot use both 'org' and 'orgs' fields simultaneously")
}
logger.Warnln("github: legacy field 'org' being used. Switch to the newer 'orgs' field structure")
logger.Warn("github: legacy field 'org' being used. Switch to the newer 'orgs' field structure")
}

g := githubConnector{
Expand Down Expand Up @@ -137,7 +136,7 @@ type githubConnector struct {
orgs []Org
clientID string
clientSecret string
logger logrus.FieldLogger
logger log.Logger
// apiURL defaults to "https://api.github.com"
apiURL string
// hostName of the GitHub enterprise account.
Expand Down
6 changes: 3 additions & 3 deletions connector/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"net/http"
"strconv"

"github.com/sirupsen/logrus"
"golang.org/x/oauth2"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

const (
Expand Down Expand Up @@ -42,7 +42,7 @@ type gitlabUser struct {
}

// Open returns a strategy for logging in through GitLab.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
if c.BaseURL == "" {
c.BaseURL = "https://gitlab.com"
}
Expand Down Expand Up @@ -71,7 +71,7 @@ type gitlabConnector struct {
org string
clientID string
clientSecret string
logger logrus.FieldLogger
logger log.Logger
}

func (c *gitlabConnector) oauth2Config(scopes connector.Scopes) *oauth2.Config {
Expand Down
7 changes: 3 additions & 4 deletions connector/keystone/keystone.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
"io/ioutil"
"net/http"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

type conn struct {
Domain string
Host string
AdminUsername string
AdminPassword string
Logger logrus.FieldLogger
Logger log.Logger
}

type userKeystone struct {
Expand Down Expand Up @@ -102,7 +101,7 @@ var (
)

// Open returns an authentication strategy using Keystone.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
return &conn{
c.Domain,
c.Host,
Expand Down
11 changes: 5 additions & 6 deletions connector/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (

"gopkg.in/ldap.v2"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

// Config holds the configuration parameters for the LDAP connector. The LDAP
Expand Down Expand Up @@ -165,7 +164,7 @@ func parseScope(s string) (int, bool) {
}

// Open returns an authentication strategy using LDAP.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
conn, err := c.OpenConnector(logger)
if err != nil {
return nil, err
Expand All @@ -179,15 +178,15 @@ type refreshData struct {
}

// OpenConnector is the same as Open but returns a type with all implemented connector interfaces.
func (c *Config) OpenConnector(logger logrus.FieldLogger) (interface {
func (c *Config) OpenConnector(logger log.Logger) (interface {
connector.Connector
connector.PasswordConnector
connector.RefreshConnector
}, error) {
return c.openConnector(logger)
}

func (c *Config) openConnector(logger logrus.FieldLogger) (*ldapConnector, error) {
func (c *Config) openConnector(logger log.Logger) (*ldapConnector, error) {

requiredFields := []struct {
name string
Expand Down Expand Up @@ -259,7 +258,7 @@ type ldapConnector struct {

tlsConfig *tls.Config

logger logrus.FieldLogger
logger log.Logger
}

var (
Expand Down
3 changes: 2 additions & 1 deletion connector/ldap/ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

const envVar = "DEX_LDAP_TESTS"
Expand Down Expand Up @@ -875,7 +876,7 @@ func runTests(t *testing.T, schema string, connMethod connectionMethod, config *
c.BindDN = "cn=admin,dc=example,dc=org"
c.BindPW = "admin"

l := &logrus.Logger{Out: ioutil.Discard, Formatter: &logrus.TextFormatter{}}
l := log.NewLogrusLogger(&logrus.Logger{Out: ioutil.Discard, Formatter: &logrus.TextFormatter{}})

conn, err := c.openConnector(l)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions connector/linkedin/linkedin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ import (

"golang.org/x/oauth2"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

const (
Expand All @@ -30,7 +29,7 @@ type Config struct {
}

// Open returns a strategy for logging in through LinkedIn
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
return &linkedInConnector{
oauth2Config: &oauth2.Config{
ClientID: c.ClientID,
Expand All @@ -52,7 +51,7 @@ type connectorData struct {

type linkedInConnector struct {
oauth2Config *oauth2.Config
logger logrus.FieldLogger
logger log.Logger
}

// LinkedIn doesn't provide refresh tokens, so refresh tokens issued by Dex
Expand Down
7 changes: 3 additions & 4 deletions connector/microsoft/microsoft.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import (

"golang.org/x/oauth2"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

const (
Expand All @@ -39,7 +38,7 @@ type Config struct {
}

// Open returns a strategy for logging in through Microsoft.
func (c *Config) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
m := microsoftConnector{
redirectURI: c.RedirectURI,
clientID: c.ClientID,
Expand Down Expand Up @@ -76,7 +75,7 @@ type microsoftConnector struct {
tenant string
onlySecurityGroups bool
groups []string
logger logrus.FieldLogger
logger log.Logger
}

func (c *microsoftConnector) isOrgTenant() bool {
Expand Down
13 changes: 6 additions & 7 deletions connector/mock/connectortest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"net/http"
"net/url"

"github.com/sirupsen/logrus"

"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/log"
)

// NewCallbackConnector returns a mock connector which requires no user interaction. It always returns
// the same (fake) identity.
func NewCallbackConnector(logger logrus.FieldLogger) connector.Connector {
func NewCallbackConnector(logger log.Logger) connector.Connector {
return &Callback{
Identity: connector.Identity{
UserID: "0-385-28089-0",
Expand All @@ -40,7 +39,7 @@ var (
type Callback struct {
// The returned identity.
Identity connector.Identity
Logger logrus.FieldLogger
Logger log.Logger
}

// LoginURL returns the URL to redirect the user to login with.
Expand Down Expand Up @@ -71,7 +70,7 @@ 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(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *CallbackConfig) Open(id string, logger log.Logger) (connector.Connector, error) {
return NewCallbackConnector(logger), nil
}

Expand All @@ -83,7 +82,7 @@ type PasswordConfig struct {
}

// Open returns an authentication strategy which prompts for a predefined username and password.
func (c *PasswordConfig) Open(id string, logger logrus.FieldLogger) (connector.Connector, error) {
func (c *PasswordConfig) Open(id string, logger log.Logger) (connector.Connector, error) {
if c.Username == "" {
return nil, errors.New("no username supplied")
}
Expand All @@ -96,7 +95,7 @@ func (c *PasswordConfig) Open(id string, logger logrus.FieldLogger) (connector.C
type passwordConnector struct {
username string
password string
logger logrus.FieldLogger
logger log.Logger
}

func (p passwordConnector) Close() error { return nil }
Expand Down
Loading

0 comments on commit be581fa

Please sign in to comment.