2019-04-26 07:47:16 -05:00
|
|
|
package ldap
|
2015-07-14 03:20:21 -05:00
|
|
|
|
|
|
|
import (
|
2019-06-13 09:47:52 -05:00
|
|
|
"errors"
|
2015-07-14 03:20:21 -05:00
|
|
|
"testing"
|
|
|
|
|
2019-04-26 07:47:16 -05:00
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
2019-06-13 09:47:52 -05:00
|
|
|
"gopkg.in/ldap.v3"
|
2019-04-26 07:47:16 -05:00
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2015-07-14 03:20:21 -05:00
|
|
|
)
|
|
|
|
|
2019-05-27 02:36:49 -05:00
|
|
|
func TestPublicAPI(t *testing.T) {
|
2019-06-13 09:47:52 -05:00
|
|
|
Convey("New()", t, func() {
|
|
|
|
Convey("Should return ", func() {
|
|
|
|
result := New(&ServerConfig{
|
|
|
|
Attr: AttributeMap{},
|
|
|
|
SearchBaseDNs: []string{"BaseDNHere"},
|
|
|
|
})
|
|
|
|
|
|
|
|
So(result, ShouldImplement, (*IServer)(nil))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
Convey("Users()", t, func() {
|
2019-06-13 09:47:52 -05:00
|
|
|
Convey("Finds one user", func() {
|
2019-05-27 02:36:49 -05:00
|
|
|
MockConnection := &MockConnection{}
|
2019-05-17 06:57:26 -05:00
|
|
|
entry := ldap.Entry{
|
|
|
|
DN: "dn", Attributes: []*ldap.EntryAttribute{
|
|
|
|
{Name: "username", Values: []string{"roelgerrits"}},
|
|
|
|
{Name: "surname", Values: []string{"Gerrits"}},
|
|
|
|
{Name: "email", Values: []string{"roel@test.com"}},
|
|
|
|
{Name: "name", Values: []string{"Roel"}},
|
|
|
|
{Name: "memberof", Values: []string{"admins"}},
|
|
|
|
}}
|
|
|
|
result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
|
2019-05-27 02:36:49 -05:00
|
|
|
MockConnection.setSearchResult(&result)
|
2019-05-17 06:57:26 -05:00
|
|
|
|
|
|
|
// Set up attribute map without surname and email
|
|
|
|
server := &Server{
|
2019-05-27 02:36:49 -05:00
|
|
|
Config: &ServerConfig{
|
2019-05-17 06:57:26 -05:00
|
|
|
Attr: AttributeMap{
|
|
|
|
Username: "username",
|
|
|
|
Name: "name",
|
|
|
|
MemberOf: "memberof",
|
|
|
|
},
|
|
|
|
SearchBaseDNs: []string{"BaseDNHere"},
|
|
|
|
},
|
2019-05-27 02:36:49 -05:00
|
|
|
Connection: MockConnection,
|
2019-05-17 06:57:26 -05:00
|
|
|
log: log.New("test-logger"),
|
|
|
|
}
|
2019-01-13 14:22:01 -06:00
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
searchResult, err := server.Users([]string{"roelgerrits"})
|
2019-01-13 14:22:01 -06:00
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(searchResult, ShouldNotBeNil)
|
2019-01-13 14:22:01 -06:00
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
// User should be searched in ldap
|
2019-05-27 02:36:49 -05:00
|
|
|
So(MockConnection.SearchCalled, ShouldBeTrue)
|
2019-01-13 14:22:01 -06:00
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
// No empty attributes should be added to the search request
|
2019-05-27 02:36:49 -05:00
|
|
|
So(len(MockConnection.SearchAttributes), ShouldEqual, 3)
|
|
|
|
})
|
2019-06-13 09:47:52 -05:00
|
|
|
|
|
|
|
Convey("Handles a error", func() {
|
|
|
|
expected := errors.New("Killa-gorilla")
|
|
|
|
MockConnection := &MockConnection{}
|
|
|
|
MockConnection.setSearchError(expected)
|
|
|
|
|
|
|
|
// Set up attribute map without surname and email
|
|
|
|
server := &Server{
|
|
|
|
Config: &ServerConfig{
|
|
|
|
SearchBaseDNs: []string{"BaseDNHere"},
|
|
|
|
},
|
|
|
|
Connection: MockConnection,
|
|
|
|
log: log.New("test-logger"),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := server.Users([]string{"roelgerrits"})
|
|
|
|
|
|
|
|
So(err, ShouldEqual, expected)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Should return empty slice if none were found", func() {
|
|
|
|
MockConnection := &MockConnection{}
|
|
|
|
result := ldap.SearchResult{Entries: []*ldap.Entry{}}
|
|
|
|
MockConnection.setSearchResult(&result)
|
|
|
|
|
|
|
|
// Set up attribute map without surname and email
|
|
|
|
server := &Server{
|
|
|
|
Config: &ServerConfig{
|
|
|
|
SearchBaseDNs: []string{"BaseDNHere"},
|
|
|
|
},
|
|
|
|
Connection: MockConnection,
|
|
|
|
log: log.New("test-logger"),
|
|
|
|
}
|
|
|
|
|
|
|
|
searchResult, err := server.Users([]string{"roelgerrits"})
|
|
|
|
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(searchResult, ShouldBeEmpty)
|
|
|
|
})
|
2019-05-27 02:36:49 -05:00
|
|
|
})
|
|
|
|
|
2019-06-13 09:47:52 -05:00
|
|
|
Convey("Auth()", t, func() {
|
|
|
|
Convey("Should ignore passsed username and password", func() {
|
2019-05-27 02:36:49 -05:00
|
|
|
connection := &MockConnection{}
|
|
|
|
var actualUsername, actualPassword string
|
|
|
|
connection.bindProvider = func(username, password string) error {
|
|
|
|
actualUsername = username
|
|
|
|
actualPassword = password
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
server := &Server{
|
|
|
|
Connection: connection,
|
|
|
|
Config: &ServerConfig{
|
2019-06-13 09:47:52 -05:00
|
|
|
BindDN: "cn=admin,dc=grafana,dc=org",
|
2019-05-27 02:36:49 -05:00
|
|
|
BindPassword: "bindpwd",
|
|
|
|
},
|
|
|
|
}
|
2019-06-13 09:47:52 -05:00
|
|
|
err := server.Auth("user", "pwd")
|
2019-05-27 02:36:49 -05:00
|
|
|
So(err, ShouldBeNil)
|
2019-06-13 09:47:52 -05:00
|
|
|
So(actualUsername, ShouldEqual, "cn=admin,dc=grafana,dc=org")
|
2019-05-27 02:36:49 -05:00
|
|
|
So(actualPassword, ShouldEqual, "bindpwd")
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("Given bind dn configured", func() {
|
|
|
|
connection := &MockConnection{}
|
|
|
|
var actualUsername, actualPassword string
|
|
|
|
connection.bindProvider = func(username, password string) error {
|
|
|
|
actualUsername = username
|
|
|
|
actualPassword = password
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
server := &Server{
|
|
|
|
Connection: connection,
|
|
|
|
Config: &ServerConfig{
|
|
|
|
BindDN: "cn=%s,o=users,dc=grafana,dc=org",
|
|
|
|
},
|
|
|
|
}
|
2019-06-13 09:47:52 -05:00
|
|
|
err := server.Auth("user", "pwd")
|
2019-05-27 02:36:49 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
So(actualUsername, ShouldEqual, "cn=user,o=users,dc=grafana,dc=org")
|
|
|
|
So(actualPassword, ShouldEqual, "pwd")
|
|
|
|
})
|
|
|
|
|
2019-06-13 09:47:52 -05:00
|
|
|
Convey("Should handle an error", func() {
|
2019-05-27 02:36:49 -05:00
|
|
|
connection := &MockConnection{}
|
2019-06-13 09:47:52 -05:00
|
|
|
expected := &ldap.Error{
|
|
|
|
ResultCode: uint16(25),
|
|
|
|
}
|
|
|
|
connection.bindProvider = func(username, password string) error {
|
|
|
|
return expected
|
2019-05-27 02:36:49 -05:00
|
|
|
}
|
|
|
|
server := &Server{
|
|
|
|
Connection: connection,
|
2019-06-13 09:47:52 -05:00
|
|
|
Config: &ServerConfig{
|
|
|
|
BindDN: "cn=%s,o=users,dc=grafana,dc=org",
|
|
|
|
},
|
|
|
|
log: log.New("test-logger"),
|
2019-05-27 02:36:49 -05:00
|
|
|
}
|
2019-06-13 09:47:52 -05:00
|
|
|
err := server.Auth("user", "pwd")
|
|
|
|
So(err, ShouldEqual, expected)
|
2019-05-17 06:57:26 -05:00
|
|
|
})
|
2019-01-13 14:22:01 -06:00
|
|
|
})
|
2016-02-23 07:22:28 -06:00
|
|
|
}
|