mirror of
https://github.com/nabbar/golib.git
synced 2025-10-07 00:33:45 +08:00
Add Validator from github.com/go-playground/validator for LDAP config
Add error function to check if there are any parent error
This commit is contained in:
@@ -92,6 +92,7 @@ type Error interface {
|
||||
|
||||
IsError(e error) bool
|
||||
HasError(err error) bool
|
||||
HasParent() bool
|
||||
|
||||
AddParent(parent ...error)
|
||||
SetParent(parent ...error)
|
||||
@@ -243,6 +244,10 @@ func (e *errors) HasError(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *errors) HasParent() bool {
|
||||
return len(e.p) > 0
|
||||
}
|
||||
|
||||
func (e *errors) SetParent(parent ...error) {
|
||||
e.p = make([]Error, 0)
|
||||
e.AddParent(parent...)
|
||||
|
@@ -45,6 +45,7 @@ const (
|
||||
ErrorLDAPInvalidUID
|
||||
ErrorLDAPAttributeNotFound
|
||||
ErrorLDAPAttributeEmpty
|
||||
ErrorLDAPValidatorError
|
||||
)
|
||||
|
||||
var isCodeError = false
|
||||
@@ -94,6 +95,8 @@ func getMessage(code errors.CodeError) (message string) {
|
||||
return "requested attribute is not found"
|
||||
case ErrorLDAPAttributeEmpty:
|
||||
return "requested attribute is empty"
|
||||
case ErrorLDAPValidatorError:
|
||||
return "invalid validation config"
|
||||
}
|
||||
|
||||
return ""
|
||||
|
@@ -92,7 +92,7 @@ func (lc *HelperLDAP) ForceTLSMode(tlsMode TLSMode, tlsConfig *tls.Config) {
|
||||
if tlsConfig != nil {
|
||||
lc.tlsConfig = tlsConfig
|
||||
} else {
|
||||
//nosec nolint gosec
|
||||
//nolint gosec /* #nosec */
|
||||
lc.tlsConfig = &tls.Config{}
|
||||
}
|
||||
|
||||
|
@@ -27,6 +27,9 @@ package ldap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/nabbar/golib/errors"
|
||||
)
|
||||
|
||||
type TLSMode uint8
|
||||
@@ -61,12 +64,12 @@ func GetDefaultAttributes() []string {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri"`
|
||||
PortLdap int `cloud:"port-ldap" mapstructure:"port-ldap" json:"port-ldap" yaml:"port-ldap" toml:"port-ldap"`
|
||||
Portldaps int `cloud:"port-ldaps" mapstructure:"port-ldaps" json:"port-ldaps" yaml:"port-ldaps" toml:"port-ldaps"`
|
||||
Basedn string `cloud:"basedn" mapstructure:"basedn" json:"basedn" yaml:"basedn" toml:"basedn"`
|
||||
FilterGroup string `cloud:"filter-group" mapstructure:"filter-group" json:"filter-group" yaml:"filter-group" toml:"filter-group"`
|
||||
FilterUser string `cloud:"filter-user" mapstructure:"filter-user" json:"filter-user" yaml:"filter-user" toml:"filter-user"`
|
||||
Uri string `cloud:"uri" mapstructure:"uri" json:"uri" yaml:"uri" toml:"uri" validate:"url,required"`
|
||||
PortLdap int `cloud:"port-ldap" mapstructure:"port-ldap" json:"port-ldap" yaml:"port-ldap" toml:"port-ldap" validate:"int,gte=0,nefield=Portldaps,required"`
|
||||
Portldaps int `cloud:"port-ldaps" mapstructure:"port-ldaps" json:"port-ldaps" yaml:"port-ldaps" toml:"port-ldaps" validate:"int,nefield=Portldap,omitempty"`
|
||||
Basedn string `cloud:"basedn" mapstructure:"basedn" json:"basedn" yaml:"basedn" toml:"basedn" validate:"printascii,omitempty"`
|
||||
FilterGroup string `cloud:"filter-group" mapstructure:"filter-group" json:"filter-group" yaml:"filter-group" toml:"filter-group" validate:"printascii,required"`
|
||||
FilterUser string `cloud:"filter-user" mapstructure:"filter-user" json:"filter-user" yaml:"filter-user" toml:"filter-user" validate:"printascii,required"`
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
@@ -103,3 +106,25 @@ func (cnf Config) PatternFilterGroup() string {
|
||||
func (cnf Config) PatternFilterUser() string {
|
||||
return cnf.FilterUser
|
||||
}
|
||||
|
||||
func (cnf Config) Validate() errors.Error {
|
||||
val := validator.New()
|
||||
err := val.Struct(cnf)
|
||||
|
||||
if e, ok := err.(*validator.InvalidValidationError); ok {
|
||||
return ErrorLDAPValidatorError.ErrorParent(e)
|
||||
}
|
||||
|
||||
out := ErrorLDAPValidatorError.Error(nil)
|
||||
|
||||
for _, e := range err.(validator.ValidationErrors) {
|
||||
//nolint goerr113
|
||||
out.AddParent(fmt.Errorf("config field '%s' is not validated by constraint '%s'", e.Field(), e.ActualTag()))
|
||||
}
|
||||
|
||||
if out.HasParent() {
|
||||
return out
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user