From c242d3830148ca7e3061305781fcd2c13fe5fe9e Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Fri, 8 Mar 2019 13:16:35 +0100 Subject: [PATCH] fix allow anonymous server bind for ldap search --- pkg/login/ldap.go | 12 +++++++- pkg/login/ldap_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/pkg/login/ldap.go b/pkg/login/ldap.go index 8bb331b7e59..24ab6fdc0f8 100644 --- a/pkg/login/ldap.go +++ b/pkg/login/ldap.go @@ -219,8 +219,18 @@ func (a *ldapAuther) GetGrafanaUserFor(ctx *m.ReqContext, ldapUser *LdapUserInfo } func (a *ldapAuther) serverBind() error { + bindFn := func() error { + return a.conn.Bind(a.server.BindDN, a.server.BindPassword) + } + + if a.server.BindPassword == "" { + bindFn = func() error { + return a.conn.UnauthenticatedBind(a.server.BindDN) + } + } + // bind_dn and bind_password to bind - if err := a.conn.Bind(a.server.BindDN, a.server.BindPassword); err != nil { + if err := bindFn(); err != nil { a.log.Info("LDAP initial bind failed, %v", err) if ldapErr, ok := err.(*ldap.Error); ok { diff --git a/pkg/login/ldap_test.go b/pkg/login/ldap_test.go index dabafee65a6..543cc90378c 100644 --- a/pkg/login/ldap_test.go +++ b/pkg/login/ldap_test.go @@ -78,6 +78,69 @@ func TestLdapAuther(t *testing.T) { }) }) + Convey("serverBind", t, func() { + Convey("Given bind dn and password configured", func() { + conn := &mockLdapConn{} + var actualUsername, actualPassword string + conn.bindProvider = func(username, password string) error { + actualUsername = username + actualPassword = password + return nil + } + ldapAuther := &ldapAuther{ + conn: conn, + server: &LdapServerConf{ + BindDN: "o=users,dc=grafana,dc=org", + BindPassword: "bindpwd", + }, + } + err := ldapAuther.serverBind() + So(err, ShouldBeNil) + So(actualUsername, ShouldEqual, "o=users,dc=grafana,dc=org") + So(actualPassword, ShouldEqual, "bindpwd") + }) + + Convey("Given bind dn configured", func() { + conn := &mockLdapConn{} + unauthenticatedBindWasCalled := false + var actualUsername string + conn.unauthenticatedBindProvider = func(username string) error { + unauthenticatedBindWasCalled = true + actualUsername = username + return nil + } + ldapAuther := &ldapAuther{ + conn: conn, + server: &LdapServerConf{ + BindDN: "o=users,dc=grafana,dc=org", + }, + } + err := ldapAuther.serverBind() + So(err, ShouldBeNil) + So(unauthenticatedBindWasCalled, ShouldBeTrue) + So(actualUsername, ShouldEqual, "o=users,dc=grafana,dc=org") + }) + + Convey("Given empty bind dn and password", func() { + conn := &mockLdapConn{} + unauthenticatedBindWasCalled := false + var actualUsername string + conn.unauthenticatedBindProvider = func(username string) error { + unauthenticatedBindWasCalled = true + actualUsername = username + return nil + } + ldapAuther := &ldapAuther{ + conn: conn, + server: &LdapServerConf{}, + } + err := ldapAuther.serverBind() + So(err, ShouldBeNil) + So(unauthenticatedBindWasCalled, ShouldBeTrue) + So(actualUsername, ShouldBeEmpty) + }) + }) + Convey("When translating ldap user to grafana user", t, func() { var user1 = &m.User{}