Merge pull request #1853 from dexidp/linter-config

Linter config
This commit is contained in:
Márk Sági-Kazár 2020-11-05 10:33:19 +01:00 committed by GitHub
commit bca77245df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 167 additions and 97 deletions

15
.editorconfig Normal file
View File

@ -0,0 +1,15 @@
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.go]
indent_style = tab
[*.proto]
indent_size = 2

View File

@ -1,2 +1,2 @@
[*.yml] [{*.yml,*.yaml}]
indent_size = 2 indent_size = 2

View File

@ -1,45 +1,83 @@
run: run:
timeout: 2m timeout: 2m
linters-settings: linters-settings:
golint: gci:
min-confidence: 0.1 local-prefixes: github.com/dexidp/dex
goimports: goimports:
local-prefixes: github.com/dexidp/dex local-prefixes: github.com/dexidp/dex
golint:
min-confidence: 0
linters: linters:
disable-all: true disable-all: true
enable: enable:
- bodyclose - bodyclose
- deadcode - deadcode
- depguard - dogsled
- dogsled - exhaustive
- gochecknoinits - exportloopref
- gofmt - gci
- goimports - gochecknoinits
- golint - gocritic
- gosimple - gofmt
- gocritic - gofumpt
- govet - goimports
- ineffassign - golint
- interfacer - goprintffuncname
- misspell - gosimple
- nakedret - govet
- staticcheck - ineffassign
- structcheck - misspell
- stylecheck - nakedret
- typecheck - nolintlint
- unconvert - rowserrcheck
- unused - staticcheck
- varcheck - structcheck
- whitespace - stylecheck
- tparallel
- typecheck
- unconvert
- unused
- varcheck
- whitespace
# TODO: fix linter errors before enabling # TODO: fix linter errors before enabling
# - unparam # - exhaustivestruct
# - scopelint # - gochecknoglobals
# - gosec # - errorlint
# - gocyclo # - gocognit
# - lll # - godot
# - goconst # - nlreturn
# - errcheck # - noctx
# - dupl # - prealloc
# - sqlclosecheck
# - wrapcheck
# TODO: fix linter errors before enabling (from original config)
# - dupl
# - errcheck
# - goconst
# - gocyclo
# - gosec
# - lll
# - scopelint
# - unparam
# unused
# - depguard
# - goheader
# - gomodguard
# don't enable:
# - asciicheck
# - funlen
# - godox
# - goerr113
# - gomnd
# - interfacer
# - maligned
# - nestif
# - testpackage
# - wsl

View File

@ -18,7 +18,7 @@ export GOBIN=$(PWD)/bin
LD_FLAGS="-w -X $(REPO_PATH)/version.Version=$(VERSION)" LD_FLAGS="-w -X $(REPO_PATH)/version.Version=$(VERSION)"
# Dependency versions # Dependency versions
GOLANGCI_VERSION = 1.31.0 GOLANGCI_VERSION = 1.32.2
build: bin/dex build: bin/dex

View File

@ -59,6 +59,7 @@ func TestInvalidConfiguration(t *testing.T) {
t.Fatalf("Expected error message to be %q, got %q", wanted, got) t.Fatalf("Expected error message to be %q, got %q", wanted, got)
} }
} }
func TestUnmarshalConfig(t *testing.T) { func TestUnmarshalConfig(t *testing.T) {
rawConfig := []byte(` rawConfig := []byte(`
issuer: http://127.0.0.1:5556/dex issuer: http://127.0.0.1:5556/dex

View File

@ -35,8 +35,10 @@ const (
// Pagination URL patterns // Pagination URL patterns
// https://developer.github.com/v3/#pagination // https://developer.github.com/v3/#pagination
var reNext = regexp.MustCompile("<([^>]+)>; rel=\"next\"") var (
var reLast = regexp.MustCompile("<([^>]+)>; rel=\"last\"") reNext = regexp.MustCompile("<([^>]+)>; rel=\"next\"")
reLast = regexp.MustCompile("<([^>]+)>; rel=\"last\"")
)
// Config holds configuration options for github logins. // Config holds configuration options for github logins.
type Config struct { type Config struct {
@ -626,7 +628,6 @@ func (c *githubConnector) userInOrg(ctx context.Context, client *http.Client, us
apiURL := fmt.Sprintf("%s/orgs/%s/members/%s", c.apiURL, orgName, userName) apiURL := fmt.Sprintf("%s/orgs/%s/members/%s", c.apiURL, orgName, userName)
req, err := http.NewRequest("GET", apiURL, nil) req, err := http.NewRequest("GET", apiURL, nil)
if err != nil { if err != nil {
return false, fmt.Errorf("github: new req: %v", err) return false, fmt.Errorf("github: new req: %v", err)
} }

View File

@ -115,7 +115,8 @@ func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error)
c.Host, c.Host,
c.AdminUsername, c.AdminUsername,
c.AdminPassword, c.AdminPassword,
logger}, nil logger,
}, nil
} }
func (p *conn) Close() error { return nil } func (p *conn) Close() error { return nil }
@ -137,7 +138,7 @@ func (p *conn) Login(ctx context.Context, scopes connector.Scopes, username, pas
return identity, false, err return identity, false, err
} }
defer resp.Body.Close() defer resp.Body.Close()
var tokenResp = new(tokenResponse) tokenResp := new(tokenResponse)
err = json.Unmarshal(data, &tokenResp) err = json.Unmarshal(data, &tokenResp)
if err != nil { if err != nil {
return identity, false, fmt.Errorf("keystone: invalid token response: %v", err) return identity, false, fmt.Errorf("keystone: invalid token response: %v", err)
@ -295,7 +296,7 @@ func (p *conn) getUserGroups(ctx context.Context, userID string, token string) (
} }
defer resp.Body.Close() defer resp.Body.Close()
var groupsResp = new(groupsResponse) groupsResp := new(groupsResponse)
err = json.Unmarshal(data, &groupsResp) err = json.Unmarshal(data, &groupsResp)
if err != nil { if err != nil {

View File

@ -84,7 +84,7 @@ func getAdminToken(t *testing.T, adminName, adminPass string) (token, id string)
} }
defer resp.Body.Close() defer resp.Body.Close()
var tokenResp = new(tokenResponse) tokenResp := new(tokenResponse)
err = json.Unmarshal(data, &tokenResp) err = json.Unmarshal(data, &tokenResp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -128,7 +128,7 @@ func createUser(t *testing.T, token, userName, userEmail, userPass string) strin
} }
defer resp.Body.Close() defer resp.Body.Close()
var userResp = new(userResponse) userResp := new(userResponse)
err = json.Unmarshal(data, &userResp) err = json.Unmarshal(data, &userResp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -189,7 +189,7 @@ func createGroup(t *testing.T, token, description, name string) string {
} }
defer resp.Body.Close() defer resp.Body.Close()
var groupResp = new(groupResponse) groupResp := new(groupResponse)
err = json.Unmarshal(data, &groupResp) err = json.Unmarshal(data, &groupResp)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -219,8 +219,10 @@ func addUserToGroup(t *testing.T, token, groupID, userID string) error {
func TestIncorrectCredentialsLogin(t *testing.T) { func TestIncorrectCredentialsLogin(t *testing.T) {
setupVariables(t) setupVariables(t)
c := conn{Host: keystoneURL, Domain: testDomain, c := conn{
AdminUsername: adminUser, AdminPassword: adminPass} Host: keystoneURL, Domain: testDomain,
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true} s := connector.Scopes{OfflineAccess: true, Groups: true}
_, validPW, err := c.Login(context.Background(), s, adminUser, invalidPass) _, validPW, err := c.Login(context.Background(), s, adminUser, invalidPass)
@ -254,7 +256,7 @@ func TestValidUserLogin(t *testing.T) {
verifiedEmail bool verifiedEmail bool
} }
var tests = []struct { tests := []struct {
name string name string
input tUser input tUser
expected expect expected expect
@ -294,8 +296,10 @@ func TestValidUserLogin(t *testing.T) {
userID := createUser(t, token, tt.input.username, tt.input.email, tt.input.password) userID := createUser(t, token, tt.input.username, tt.input.email, tt.input.password)
defer deleteResource(t, token, userID, usersURL) defer deleteResource(t, token, userID, usersURL)
c := conn{Host: keystoneURL, Domain: tt.input.domain, c := conn{
AdminUsername: adminUser, AdminPassword: adminPass} Host: keystoneURL, Domain: tt.input.domain,
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true} s := connector.Scopes{OfflineAccess: true, Groups: true}
identity, validPW, err := c.Login(context.Background(), s, tt.input.username, tt.input.password) identity, validPW, err := c.Login(context.Background(), s, tt.input.username, tt.input.password)
if err != nil { if err != nil {
@ -329,8 +333,10 @@ func TestUseRefreshToken(t *testing.T) {
addUserToGroup(t, token, groupID, adminID) addUserToGroup(t, token, groupID, adminID)
defer deleteResource(t, token, groupID, groupsURL) defer deleteResource(t, token, groupID, groupsURL)
c := conn{Host: keystoneURL, Domain: testDomain, c := conn{
AdminUsername: adminUser, AdminPassword: adminPass} Host: keystoneURL, Domain: testDomain,
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true} s := connector.Scopes{OfflineAccess: true, Groups: true}
identityLogin, _, err := c.Login(context.Background(), s, adminUser, adminPass) identityLogin, _, err := c.Login(context.Background(), s, adminUser, adminPass)
@ -352,8 +358,10 @@ func TestUseRefreshTokenUserDeleted(t *testing.T) {
token, _ := getAdminToken(t, adminUser, adminPass) token, _ := getAdminToken(t, adminUser, adminPass)
userID := createUser(t, token, testUser, testEmail, testPass) userID := createUser(t, token, testUser, testEmail, testPass)
c := conn{Host: keystoneURL, Domain: testDomain, c := conn{
AdminUsername: adminUser, AdminPassword: adminPass} Host: keystoneURL, Domain: testDomain,
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true} s := connector.Scopes{OfflineAccess: true, Groups: true}
identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass) identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass)
@ -380,8 +388,10 @@ func TestUseRefreshTokenGroupsChanged(t *testing.T) {
userID := createUser(t, token, testUser, testEmail, testPass) userID := createUser(t, token, testUser, testEmail, testPass)
defer deleteResource(t, token, userID, usersURL) defer deleteResource(t, token, userID, usersURL)
c := conn{Host: keystoneURL, Domain: testDomain, c := conn{
AdminUsername: adminUser, AdminPassword: adminPass} Host: keystoneURL, Domain: testDomain,
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: true} s := connector.Scopes{OfflineAccess: true, Groups: true}
identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass) identityLogin, _, err := c.Login(context.Background(), s, testUser, testPass)
@ -414,8 +424,10 @@ func TestNoGroupsInScope(t *testing.T) {
userID := createUser(t, token, testUser, testEmail, testPass) userID := createUser(t, token, testUser, testEmail, testPass)
defer deleteResource(t, token, userID, usersURL) defer deleteResource(t, token, userID, usersURL)
c := conn{Host: keystoneURL, Domain: testDomain, c := conn{
AdminUsername: adminUser, AdminPassword: adminPass} Host: keystoneURL, Domain: testDomain,
AdminUsername: adminUser, AdminPassword: adminPass,
}
s := connector.Scopes{OfflineAccess: true, Groups: false} s := connector.Scopes{OfflineAccess: true, Groups: false}
groupID := createGroup(t, token, "Test group", testGroup) groupID := createGroup(t, token, "Test group", testGroup)

View File

@ -12,13 +12,12 @@ import (
"strings" "strings"
"time" "time"
"golang.org/x/oauth2"
"github.com/dexidp/dex/connector" "github.com/dexidp/dex/connector"
"github.com/dexidp/dex/pkg/groups" "github.com/dexidp/dex/pkg/groups"
"github.com/dexidp/dex/pkg/log" "github.com/dexidp/dex/pkg/log"
"github.com/dexidp/dex/storage/kubernetes/k8sapi" "github.com/dexidp/dex/storage/kubernetes/k8sapi"
"golang.org/x/oauth2"
) )
// Config holds configuration options for OpenShift login // Config holds configuration options for OpenShift login
@ -32,9 +31,7 @@ type Config struct {
RootCA string `json:"rootCA"` RootCA string `json:"rootCA"`
} }
var ( var _ connector.CallbackConnector = (*openshiftConnector)(nil)
_ connector.CallbackConnector = (*openshiftConnector)(nil)
)
type openshiftConnector struct { type openshiftConnector struct {
apiURL string apiURL string
@ -89,7 +86,6 @@ func (c *Config) Open(id string, logger log.Logger) (conn connector.Connector, e
} }
resp, err := openshiftConnector.httpClient.Do(req.WithContext(ctx)) resp, err := openshiftConnector.httpClient.Do(req.WithContext(ctx))
if err != nil { if err != nil {
cancel() cancel()
return nil, fmt.Errorf("failed to query OpenShift endpoint %v", err) return nil, fmt.Errorf("failed to query OpenShift endpoint %v", err)
@ -160,7 +156,6 @@ func (c *openshiftConnector) HandleCallback(s connector.Scopes, r *http.Request)
client := c.oauth2Config.Client(ctx, token) client := c.oauth2Config.Client(ctx, token)
user, err := c.user(ctx, client) user, err := c.user(ctx, client)
if err != nil { if err != nil {
return identity, fmt.Errorf("openshift: get user: %v", err) return identity, fmt.Errorf("openshift: get user: %v", err)
} }

View File

@ -10,12 +10,11 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/storage/kubernetes/k8sapi"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"github.com/dexidp/dex/connector"
"github.com/dexidp/dex/storage/kubernetes/k8sapi"
) )
func TestOpen(t *testing.T) { func TestOpen(t *testing.T) {

View File

@ -0,0 +1,2 @@
[{*.yml,*.yaml}]
indent_size = 2

View File

@ -96,7 +96,6 @@ func (d dexAPI) UpdateClient(ctx context.Context, req *api.UpdateClientReq) (*ap
} }
return old, nil return old, nil
}) })
if err != nil { if err != nil {
if err == storage.ErrNotFound { if err == storage.ErrNotFound {
return &api.UpdateClientResp{NotFound: true}, nil return &api.UpdateClientResp{NotFound: true}, nil

View File

@ -7,13 +7,11 @@ import (
"testing" "testing"
"time" "time"
"github.com/kylelemons/godebug/pretty"
"golang.org/x/crypto/bcrypt"
jose "gopkg.in/square/go-jose.v2" jose "gopkg.in/square/go-jose.v2"
"golang.org/x/crypto/bcrypt"
"github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage"
"github.com/kylelemons/godebug/pretty"
) )
// ensure that values being tested on never expire. // ensure that values being tested on never expire.

View File

@ -11,9 +11,7 @@ import (
"github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage"
) )
var ( var defaultDialTimeout = 2 * time.Second
defaultDialTimeout = 2 * time.Second
)
// SSL represents SSL options for etcd databases. // SSL represents SSL options for etcd databases.
type SSL struct { type SSL struct {

View File

@ -34,8 +34,10 @@ func withTimeout(t time.Duration, f func()) {
} }
func cleanDB(c *conn) error { func cleanDB(c *conn) error {
tables := []string{"client", "auth_request", "auth_code", tables := []string{
"refresh_token", "keys", "password"} "client", "auth_request", "auth_code",
"refresh_token", "keys", "password",
}
for _, tbl := range tables { for _, tbl := range tables {
_, err := c.Exec("delete from " + tbl) _, err := c.Exec("delete from " + tbl)
@ -97,7 +99,7 @@ func getenv(key, defaultVal string) string {
const testPostgresEnv = "DEX_POSTGRES_HOST" const testPostgresEnv = "DEX_POSTGRES_HOST"
func TestCreateDataSourceName(t *testing.T) { func TestCreateDataSourceName(t *testing.T) {
var testCases = []struct { testCases := []struct {
description string description string
input *Postgres input *Postgres
expected string expected string

View File

@ -244,7 +244,6 @@ func (c *conn) CreateAuthCode(a storage.AuthCode) error {
encoder(a.Claims.Groups), a.ConnectorID, a.ConnectorData, a.Expiry, encoder(a.Claims.Groups), a.ConnectorID, a.ConnectorData, a.Expiry,
a.PKCE.CodeChallenge, a.PKCE.CodeChallengeMethod, a.PKCE.CodeChallenge, a.PKCE.CodeChallengeMethod,
) )
if err != nil { if err != nil {
if c.alreadyExistsCheck(err) { if c.alreadyExistsCheck(err) {
return storage.ErrAlreadyExists return storage.ErrAlreadyExists

View File

@ -82,7 +82,8 @@ type migration struct {
// All SQL flavors share migration strategies. // All SQL flavors share migration strategies.
var migrations = []migration{ var migrations = []migration{
{ {
stmts: []string{` stmts: []string{
`
create table client ( create table client (
id text not null primary key, id text not null primary key,
secret text not null, secret text not null,
@ -170,7 +171,8 @@ var migrations = []migration{
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
alter table refresh_token alter table refresh_token
add column token text not null default '';`, add column token text not null default '';`,
` `
@ -182,7 +184,8 @@ var migrations = []migration{
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
create table offline_session ( create table offline_session (
user_id text not null, user_id text not null,
conn_id text not null, conn_id text not null,
@ -192,7 +195,8 @@ var migrations = []migration{
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
create table connector ( create table connector (
id text not null primary key, id text not null primary key,
type text not null, type text not null,
@ -203,7 +207,8 @@ var migrations = []migration{
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
alter table auth_code alter table auth_code
add column claims_preferred_username text not null default '';`, add column claims_preferred_username text not null default '';`,
` `
@ -215,14 +220,16 @@ var migrations = []migration{
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
alter table offline_session alter table offline_session
add column connector_data bytea; add column connector_data bytea;
`, `,
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
alter table auth_request alter table auth_request
modify column state varchar(4096); modify column state varchar(4096);
`, `,
@ -230,7 +237,8 @@ var migrations = []migration{
flavor: &flavorMySQL, flavor: &flavorMySQL,
}, },
{ {
stmts: []string{` stmts: []string{
`
create table device_request ( create table device_request (
user_code text not null primary key, user_code text not null primary key,
device_code text not null, device_code text not null,
@ -251,7 +259,8 @@ var migrations = []migration{
}, },
}, },
{ {
stmts: []string{` stmts: []string{
`
alter table auth_request alter table auth_request
add column code_challenge text not null default '';`, add column code_challenge text not null default '';`,
` `

View File

@ -34,7 +34,8 @@ func TestPostgresTunables(t *testing.T) {
}, },
SSL: SSL{ SSL: SSL{
Mode: pgSSLDisable, // Postgres container doesn't support SSL. Mode: pgSSLDisable, // Postgres container doesn't support SSL.
}} },
}
t.Run("with nothing set, uses defaults", func(t *testing.T) { t.Run("with nothing set, uses defaults", func(t *testing.T) {
cfg := *baseCfg cfg := *baseCfg