More work on ldap auth, got memberOf working in the docker ldap test server, playing with config options and structures, #1450
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
package ldapauth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-ldap/ldap"
|
||||
"github.com/grafana/grafana/pkg/log"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidCredentials = errors.New("Invalid Username or Password")
|
||||
)
|
||||
|
||||
func Login(username, password string) error {
|
||||
url, err := url.Parse(setting.LdapHosts[0])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("Host: %v", url.Host)
|
||||
conn, err := ldap.Dial("tcp", url.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer conn.Close()
|
||||
|
||||
bindFormat := "cn=%s,dc=grafana,dc=org"
|
||||
|
||||
nx := fmt.Sprintf(bindFormat, username)
|
||||
err = conn.Bind(nx, password)
|
||||
|
||||
if err != nil {
|
||||
if ldapErr, ok := err.(*ldap.Error); ok {
|
||||
if ldapErr.ResultCode == 49 {
|
||||
return ErrInvalidCredentials
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
// search := ldap.NewSearchRequest(url.Path,
|
||||
// ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||
// fmt.Sprintf(ls.Filter, name),
|
||||
// []string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail},
|
||||
// nil)
|
||||
// sr, err := l.Search(search)
|
||||
// if err != nil {
|
||||
// log.Debug("LDAP Authen OK but not in filter %s", name)
|
||||
// return "", "", "", "", false
|
||||
// }
|
||||
}
|
||||
+7
-27
@@ -13,32 +13,6 @@ var (
|
||||
ErrInvalidCredentials = errors.New("Invalid Username or Password")
|
||||
)
|
||||
|
||||
type LoginSettings struct {
|
||||
LdapEnabled bool
|
||||
}
|
||||
|
||||
type LdapFilterToOrg struct {
|
||||
Filter string
|
||||
OrgId int
|
||||
OrgRole string
|
||||
}
|
||||
|
||||
type LdapSettings struct {
|
||||
Enabled bool
|
||||
Hosts []string
|
||||
UseSSL bool
|
||||
BindDN string
|
||||
AttrUsername string
|
||||
AttrName string
|
||||
AttrSurname string
|
||||
AttrMail string
|
||||
Filters []LdapFilterToOrg
|
||||
}
|
||||
|
||||
type AuthSource interface {
|
||||
AuthenticateUser(username, password string) (*m.User, error)
|
||||
}
|
||||
|
||||
type AuthenticateUserQuery struct {
|
||||
Username string
|
||||
Password string
|
||||
@@ -56,7 +30,13 @@ func AuthenticateUser(query *AuthenticateUserQuery) error {
|
||||
}
|
||||
|
||||
if setting.LdapEnabled {
|
||||
err = loginUsingLdap(query)
|
||||
for _, server := range setting.LdapServers {
|
||||
auther := NewLdapAuthenticator(server)
|
||||
err = auther.login(query)
|
||||
if err == nil || err != ErrInvalidCredentials {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
|
||||
+76
-19
@@ -1,8 +1,8 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
||||
"github.com/go-ldap/ldap"
|
||||
"github.com/grafana/grafana/pkg/bus"
|
||||
@@ -11,23 +11,49 @@ import (
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
|
||||
func loginUsingLdap(query *AuthenticateUserQuery) error {
|
||||
url, err := url.Parse(setting.LdapHosts[0])
|
||||
if err != nil {
|
||||
return err
|
||||
func init() {
|
||||
setting.LdapServers = []*setting.LdapServerConf{
|
||||
&setting.LdapServerConf{
|
||||
UseSSL: false,
|
||||
Host: "127.0.0.1",
|
||||
Port: "389",
|
||||
BindDN: "cn=%s,dc=grafana,dc=org",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type ldapAuther struct {
|
||||
server *setting.LdapServerConf
|
||||
conn *ldap.Conn
|
||||
}
|
||||
|
||||
func NewLdapAuthenticator(server *setting.LdapServerConf) *ldapAuther {
|
||||
return &ldapAuther{
|
||||
server: server,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ldapAuther) Dial() error {
|
||||
address := fmt.Sprintf("%s:%s", a.server.Host, a.server.Port)
|
||||
var err error
|
||||
if a.server.UseSSL {
|
||||
a.conn, err = ldap.DialTLS("tcp", address, nil)
|
||||
} else {
|
||||
a.conn, err = ldap.Dial("tcp", address)
|
||||
}
|
||||
|
||||
conn, err := ldap.Dial("tcp", url.Host)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *ldapAuther) login(query *AuthenticateUserQuery) error {
|
||||
if err := a.Dial(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer a.conn.Close()
|
||||
|
||||
defer conn.Close()
|
||||
bindPath := fmt.Sprintf(a.server.BindDN, query.Username)
|
||||
|
||||
bindPath := fmt.Sprintf(setting.LdapBindPath, query.Username)
|
||||
err = conn.Bind(bindPath, query.Password)
|
||||
|
||||
if err != nil {
|
||||
if err := a.conn.Bind(bindPath, query.Password); err != nil {
|
||||
if ldapErr, ok := err.(*ldap.Error); ok {
|
||||
if ldapErr.ResultCode == 49 {
|
||||
return ErrInvalidCredentials
|
||||
@@ -40,22 +66,33 @@ func loginUsingLdap(query *AuthenticateUserQuery) error {
|
||||
BaseDN: "dc=grafana,dc=org",
|
||||
Scope: ldap.ScopeWholeSubtree,
|
||||
DerefAliases: ldap.NeverDerefAliases,
|
||||
Attributes: []string{"cn", "sn", "email"},
|
||||
Attributes: []string{"sn", "email", "givenName", "memberOf"},
|
||||
Filter: fmt.Sprintf("(cn=%s)", query.Username),
|
||||
}
|
||||
|
||||
result, err := conn.Search(&searchReq)
|
||||
result, err := a.conn.Search(&searchReq)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("Search result: %v, error: %v", result, err)
|
||||
|
||||
for _, entry := range result.Entries {
|
||||
log.Info("cn: %s", entry.Attributes[0].Values[0])
|
||||
log.Info("email: %s", entry.Attributes[2].Values[0])
|
||||
if len(result.Entries) == 0 {
|
||||
return errors.New("Ldap search matched no entry, please review your filter setting.")
|
||||
}
|
||||
|
||||
if len(result.Entries) > 1 {
|
||||
return errors.New("Ldap search matched mopre than one entry, please review your filter setting")
|
||||
}
|
||||
|
||||
surname := getLdapAttr("sn", result)
|
||||
givenName := getLdapAttr("givenName", result)
|
||||
email := getLdapAttr("email", result)
|
||||
memberOf := getLdapAttrArray("memberOf", result)
|
||||
|
||||
log.Info("Surname: %s", surname)
|
||||
log.Info("givenName: %s", givenName)
|
||||
log.Info("email: %s", email)
|
||||
log.Info("memberOf: %s", memberOf)
|
||||
|
||||
userQuery := m.GetUserByLoginQuery{LoginOrEmail: query.Username}
|
||||
err = bus.Dispatch(&userQuery)
|
||||
|
||||
@@ -70,6 +107,26 @@ func loginUsingLdap(query *AuthenticateUserQuery) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func getLdapAttr(name string, result *ldap.SearchResult) string {
|
||||
for _, attr := range result.Entries[0].Attributes {
|
||||
if attr.Name == name {
|
||||
if len(attr.Values) > 0 {
|
||||
return attr.Values[0]
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func getLdapAttrArray(name string, result *ldap.SearchResult) []string {
|
||||
for _, attr := range result.Entries[0].Attributes {
|
||||
if attr.Name == name {
|
||||
return attr.Values
|
||||
}
|
||||
}
|
||||
return []string{}
|
||||
}
|
||||
|
||||
func createUserFromLdapInfo() error {
|
||||
return nil
|
||||
|
||||
|
||||
@@ -118,9 +118,8 @@ var (
|
||||
GoogleAnalyticsId string
|
||||
|
||||
// LDAP
|
||||
LdapEnabled bool
|
||||
LdapHosts []string
|
||||
LdapBindPath string
|
||||
LdapEnabled bool
|
||||
LdapServers []*LdapServerConf
|
||||
|
||||
// SMTP email settings
|
||||
Smtp SmtpSettings
|
||||
@@ -419,8 +418,6 @@ func NewConfigContext(args *CommandLineArgs) {
|
||||
|
||||
ldapSec := Cfg.Section("auth.ldap")
|
||||
LdapEnabled = ldapSec.Key("enabled").MustBool(false)
|
||||
LdapHosts = ldapSec.Key("hosts").Strings(" ")
|
||||
LdapBindPath = ldapSec.Key("bind_path").String()
|
||||
|
||||
readSessionConfig()
|
||||
readSmtpSettings()
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
package setting
|
||||
|
||||
type LdapFilterToOrg struct {
|
||||
Filter string
|
||||
OrgId int
|
||||
OrgRole string
|
||||
type LdapMemberToOrgRole struct {
|
||||
LdapMemberPattern string
|
||||
OrgId int
|
||||
OrgRole string
|
||||
}
|
||||
|
||||
type LdapSettings struct {
|
||||
Enabled bool
|
||||
Hosts []string
|
||||
type LdapServerConf struct {
|
||||
Host string
|
||||
Port string
|
||||
UseSSL bool
|
||||
BindDN string
|
||||
BindPassword string
|
||||
AttrUsername string
|
||||
AttrName string
|
||||
AttrSurname string
|
||||
AttrMail string
|
||||
Filters []LdapFilterToOrg
|
||||
AttrMemberOf string
|
||||
|
||||
SearchFilter []string
|
||||
SearchBaseDNs []string
|
||||
|
||||
LdapMemberMap []LdapMemberToOrgRole
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user