Skip to content

Commit

Permalink
Implement gocritic recommendations
Browse files Browse the repository at this point in the history
The outstanding recommendations are due to be addressed in #959 and #971 respectively.
  • Loading branch information
nightah committed May 5, 2020
1 parent 2f9d5ad commit 71c1e4c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 46 deletions.
25 changes: 13 additions & 12 deletions cmd/authelia-scripts/cmd_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,13 @@ func deployManifest(docker *Docker, tag string, amd64tag string, arm32v7tag stri
func publishDockerImage(arch string) {
docker := &Docker{}

if ciTag != "" {
switch {
case ciTag != "":
if len(tags) == 4 {
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])

login(docker)
deploy(docker, tags[1]+"-"+arch)

if !ignoredSuffixes.MatchString(ciTag) {
deploy(docker, tags[2]+"-"+arch)
deploy(docker, tags[3]+"-"+arch)
Expand All @@ -213,27 +214,27 @@ func publishDockerImage(arch string) {
} else {
log.Fatal("Docker image will not be published, the specified tag does not conform to the standard")
}
} else if ciBranch != masterTag && !publicRepo.MatchString(ciBranch) {
case ciBranch != masterTag && !publicRepo.MatchString(ciBranch):
login(docker)
deploy(docker, ciBranch+"-"+arch)
} else if ciBranch != masterTag && publicRepo.MatchString(ciBranch) {
case ciBranch != masterTag && publicRepo.MatchString(ciBranch):
login(docker)
deploy(docker, "PR"+ciPullRequest+"-"+arch)
} else if ciBranch == masterTag && ciPullRequest == stringFalse {
case ciBranch == masterTag && ciPullRequest == stringFalse:
login(docker)
deploy(docker, "master-"+arch)
} else {
default:
log.Info("Docker image will not be published")
}
}

func publishDockerManifest() {
docker := &Docker{}

if ciTag != "" {
switch {
case ciTag != "":
if len(tags) == 4 {
log.Infof("Detected tags: '%s' | '%s' | '%s'", tags[1], tags[2], tags[3])

login(docker)
deployManifest(docker, tags[1], tags[1]+"-amd64", tags[1]+"-arm32v7", tags[1]+"-arm64v8")
publishDockerReadme(docker)
Expand All @@ -248,17 +249,17 @@ func publishDockerManifest() {
} else {
log.Fatal("Docker manifest will not be published, the specified tag does not conform to the standard")
}
} else if ciBranch != masterTag && !publicRepo.MatchString(ciBranch) {
case ciBranch != masterTag && !publicRepo.MatchString(ciBranch):
login(docker)
deployManifest(docker, ciBranch, ciBranch+"-amd64", ciBranch+"-arm32v7", ciBranch+"-arm64v8")
} else if ciBranch != masterTag && publicRepo.MatchString(ciBranch) {
case ciBranch != masterTag && publicRepo.MatchString(ciBranch):
login(docker)
deployManifest(docker, "PR"+ciPullRequest, "PR"+ciPullRequest+"-amd64", "PR"+ciPullRequest+"-arm32v7", "PR"+ciPullRequest+"-arm64v8")
} else if ciBranch == masterTag && ciPullRequest == stringFalse {
case ciBranch == masterTag && ciPullRequest == stringFalse:
login(docker)
deployManifest(docker, "master", "master-amd64", "master-arm32v7", "master-arm64v8")
publishDockerReadme(docker)
} else {
default:
log.Info("Docker manifest will not be published")
}
}
Expand Down
24 changes: 14 additions & 10 deletions cmd/authelia/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,33 +62,37 @@ func startServer() {

var userProvider authentication.UserProvider

if config.AuthenticationBackend.File != nil {
switch {
case config.AuthenticationBackend.File != nil:
userProvider = authentication.NewFileUserProvider(config.AuthenticationBackend.File)
} else if config.AuthenticationBackend.Ldap != nil {
case config.AuthenticationBackend.Ldap != nil:
userProvider = authentication.NewLDAPUserProvider(*config.AuthenticationBackend.Ldap)
} else {
default:
log.Fatalf("Unrecognized authentication backend")
}

var storageProvider storage.Provider
if config.Storage.PostgreSQL != nil {
switch {
case config.Storage.PostgreSQL != nil:
storageProvider = storage.NewPostgreSQLProvider(*config.Storage.PostgreSQL)
} else if config.Storage.MySQL != nil {
case config.Storage.MySQL != nil:
storageProvider = storage.NewMySQLProvider(*config.Storage.MySQL)
} else if config.Storage.Local != nil {
case config.Storage.Local != nil:
storageProvider = storage.NewSQLiteProvider(config.Storage.Local.Path)
} else {
default:
log.Fatalf("Unrecognized storage backend")
}

var notifier notification.Notifier
if config.Notifier.SMTP != nil {
switch {
case config.Notifier.SMTP != nil:
notifier = notification.NewSMTPNotifier(*config.Notifier.SMTP)
} else if config.Notifier.FileSystem != nil {
case config.Notifier.FileSystem != nil:
notifier = notification.NewFileNotifier(*config.Notifier.FileSystem)
} else {
default:
log.Fatalf("Unrecognized notifier")
}

if !config.Notifier.DisableStartupCheck {
_, err := notifier.StartupCheck()
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/authorization/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ func selectMatchingSubjectRules(rules []schema.ACLRule, subject Subject) []schem
selectedRules := []schema.ACLRule{}

for _, rule := range rules {
if len(rule.Subjects) > 0 {
switch {
case len(rule.Subjects) > 0:
for _, subjectRule := range rule.Subjects {
if isSubjectMatching(subject, subjectRule) && isIPMatching(subject.IP, rule.Networks) {
selectedRules = append(selectedRules, rule)
}
}
} else {
default:
if isIPMatching(subject.IP, rule.Networks) {
selectedRules = append(selectedRules, rule)
}
Expand Down
13 changes: 6 additions & 7 deletions internal/configuration/validator/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func validateFileAuthenticationBackend(configuration *schema.FileAuthenticationB
}

//Salt Length
if configuration.Password.SaltLength == 0 {
switch {
case configuration.Password.SaltLength == 0:
configuration.Password.SaltLength = schema.DefaultPasswordConfiguration.SaltLength
} else if configuration.Password.SaltLength < 2 {
case configuration.Password.SaltLength < 2:
validator.Push(fmt.Errorf("The salt length must be 2 or more, you configured %d", configuration.Password.SaltLength))
} else if configuration.Password.SaltLength > 16 {
case configuration.Password.SaltLength > 16:
validator.Push(fmt.Errorf("The salt length must be 16 or less, you configured %d", configuration.Password.SaltLength))
}

Expand Down Expand Up @@ -137,10 +138,8 @@ func validateLdapAuthenticationBackend(configuration *schema.LDAPAuthenticationB

if configuration.GroupsFilter == "" {
validator.Push(errors.New("Please provide a groups filter with `groups_filter` attribute"))
} else {
if !strings.HasPrefix(configuration.GroupsFilter, "(") || !strings.HasSuffix(configuration.GroupsFilter, ")") {
validator.Push(errors.New("The groups filter should contain enclosing parenthesis. For instance cn={input} should be (cn={input})"))
}
} else if !strings.HasPrefix(configuration.GroupsFilter, "(") || !strings.HasSuffix(configuration.GroupsFilter, ")") {
validator.Push(errors.New("The groups filter should contain enclosing parenthesis. For instance cn={input} should be (cn={input})"))
}

if configuration.UsernameAttribute == "" {
Expand Down
7 changes: 4 additions & 3 deletions internal/configuration/validator/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ func ValidateStorage(configuration schema.StorageConfiguration, validator *schem
validator.Push(errors.New("A storage configuration must be provided. It could be 'local', 'mysql' or 'postgres'"))
}

if configuration.MySQL != nil {
switch {
case configuration.MySQL != nil:
validateSQLConfiguration(&configuration.MySQL.SQLStorageConfiguration, validator)
} else if configuration.PostgreSQL != nil {
case configuration.PostgreSQL != nil:
validatePostgreSQLConfiguration(configuration.PostgreSQL, validator)
} else if configuration.Local != nil {
case configuration.Local != nil:
validateLocalStorageConfiguration(configuration.Local, validator)
}
}
Expand Down
7 changes: 4 additions & 3 deletions internal/middlewares/identity_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,15 @@ func IdentityVerificationFinish(args IdentityVerificationFinishArgs, next func(c

if err != nil {
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
switch {
case ve.Errors&jwt.ValidationErrorMalformed != 0:
ctx.Error(fmt.Errorf("Cannot parse token"), operationFailedMessage)
return
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
case ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0:
// Token is either expired or not active yet
ctx.Error(fmt.Errorf("Token expired"), identityVerificationTokenHasExpiredMessage)
return
} else {
default:
ctx.Error(fmt.Errorf("Cannot handle this token: %s", ve), operationFailedMessage)
return
}
Expand Down
13 changes: 7 additions & 6 deletions internal/notification/smtp_notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,18 @@ func (n *SMTPNotifier) startTLS() error {
return nil
}

ok, _ := n.client.Extension("STARTTLS")
if ok {
log.Debugf("Notifier SMTP server supports STARTTLS (disableVerifyCert: %t, ServerName: %s), attempting", n.tlsConfig.InsecureSkipVerify, n.tlsConfig.ServerName)
if n.disableRequireTLS {
log.Warn("Notifier SMTP server does not support STARTTLS and SMTP configuration is set to disable the TLS requirement (only useful for unauthenticated emails over plain text)")
}

switch ok, _ := n.client.Extension("STARTTLS"); ok {
case true:
log.Debugf("Notifier SMTP server supports STARTTLS (disableVerifyCert: %t, ServerName: %s), attempting", n.tlsConfig.InsecureSkipVerify, n.tlsConfig.ServerName)
if err := n.client.StartTLS(n.tlsConfig); err != nil {
return err
}
log.Debug("Notifier SMTP STARTTLS completed without error")
} else if n.disableRequireTLS {
log.Warn("Notifier SMTP server does not support STARTTLS and SMTP configuration is set to disable the TLS requirement (only useful for unauthenticated emails over plain text)")
} else {
case false:
return errors.New("Notifier SMTP server does not support TLS and it is required by default (see documentation if you want to disable this highly recommended requirement)")
}
return nil
Expand Down
7 changes: 4 additions & 3 deletions internal/utils/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
func ParseDurationString(input string) (time.Duration, error) {
var duration time.Duration
matches := parseDurationRegexp.FindStringSubmatch(input)
if len(matches) == 3 && matches[2] != "" {
switch {
case len(matches) == 3 && matches[2] != "":
d, _ := strconv.Atoi(matches[1])
switch matches[2] {
case "y":
Expand All @@ -31,13 +32,13 @@ func ParseDurationString(input string) (time.Duration, error) {
case "s":
duration = time.Duration(d) * time.Second
}
} else if input == "0" || len(matches) == 3 {
case input == "0" || len(matches) == 3:
seconds, err := strconv.Atoi(input)
if err != nil {
return 0, fmt.Errorf("Could not convert the input string of %s into a duration: %s", input, err)
}
duration = time.Duration(seconds) * time.Second
} else if input != "" {
case input != "":
// Throw this error if input is anything other than a blank string, blank string will default to a duration of nothing
return 0, fmt.Errorf("Could not convert the input string of %s into a duration", input)
}
Expand Down

0 comments on commit 71c1e4c

Please sign in to comment.