Commit 1c08e586 by Oleg Gaidarenko Committed by GitHub

LDAP: small improvements to various LDAP parts (#17662)

* Add multildap config example

* Publicize mocks for multildap module
parent 17826796
# --- First LDAP Server ---
[[servers]]
host = "10.0.0.1"
port = 389
use_ssl = false
start_tls = false
ssl_skip_verify = false
bind_dn = "cn=admin,dc=grafana,dc=org"
bind_password = 'grafana'
search_filter = "(cn=%s)"
search_base_dns = ["ou=users,dc=grafana,dc=org"]
[servers.attributes]
name = "givenName"
surname = "sn"
username = "cn"
member_of = "memberOf"
email = "email"
[[servers.group_mappings]]
group_dn = "cn=admins,ou=groups,dc=grafana,dc=org"
org_role = "Admin"
grafana_admin = true
# --- Second LDAP Server ---
[[servers]]
host = "10.0.0.2"
port = 389
use_ssl = false
start_tls = false
ssl_skip_verify = false
bind_dn = "cn=admin,dc=grafana,dc=org"
bind_password = 'grafana'
search_filter = "(cn=%s)"
search_base_dns = ["ou=users,dc=grafana,dc=org"]
[servers.attributes]
name = "givenName"
surname = "sn"
username = "cn"
member_of = "memberOf"
email = "email"
[[servers.group_mappings]]
group_dn = "cn=editors,ou=groups,dc=grafana,dc=org"
org_role = "Editor"
[[servers.group_mappings]]
group_dn = "*"
org_role = "Viewer"
...@@ -13,6 +13,9 @@ var GetConfig = ldap.GetConfig ...@@ -13,6 +13,9 @@ var GetConfig = ldap.GetConfig
// IsEnabled checks if LDAP is enabled // IsEnabled checks if LDAP is enabled
var IsEnabled = ldap.IsEnabled var IsEnabled = ldap.IsEnabled
// newLDAP return instance of the single LDAP server
var newLDAP = ldap.New
// ErrInvalidCredentials is returned if username and password do not match // ErrInvalidCredentials is returned if username and password do not match
var ErrInvalidCredentials = ldap.ErrInvalidCredentials var ErrInvalidCredentials = ldap.ErrInvalidCredentials
...@@ -22,9 +25,6 @@ var ErrNoLDAPServers = errors.New("No LDAP servers are configured") ...@@ -22,9 +25,6 @@ var ErrNoLDAPServers = errors.New("No LDAP servers are configured")
// ErrDidNotFindUser if request for user is unsuccessful // ErrDidNotFindUser if request for user is unsuccessful
var ErrDidNotFindUser = errors.New("Did not find a user") var ErrDidNotFindUser = errors.New("Did not find a user")
// newLDAP return instance of the single LDAP server
var newLDAP = ldap.New
// IMultiLDAP is interface for MultiLDAP // IMultiLDAP is interface for MultiLDAP
type IMultiLDAP interface { type IMultiLDAP interface {
Login(query *models.LoginUserQuery) ( Login(query *models.LoginUserQuery) (
......
...@@ -5,7 +5,8 @@ import ( ...@@ -5,7 +5,8 @@ import (
"github.com/grafana/grafana/pkg/services/ldap" "github.com/grafana/grafana/pkg/services/ldap"
) )
type mockLDAP struct { // MockLDAP represents testing struct for ldap testing
type MockLDAP struct {
dialCalledTimes int dialCalledTimes int
loginCalledTimes int loginCalledTimes int
closeCalledTimes int closeCalledTimes int
...@@ -21,12 +22,15 @@ type mockLDAP struct { ...@@ -21,12 +22,15 @@ type mockLDAP struct {
usersRestReturn []*models.ExternalUserInfo usersRestReturn []*models.ExternalUserInfo
} }
func (mock *mockLDAP) Login(*models.LoginUserQuery) (*models.ExternalUserInfo, error) { // Login test fn
func (mock *MockLDAP) Login(*models.LoginUserQuery) (*models.ExternalUserInfo, error) {
mock.loginCalledTimes = mock.loginCalledTimes + 1 mock.loginCalledTimes = mock.loginCalledTimes + 1
return mock.loginReturn, mock.loginErrReturn return mock.loginReturn, mock.loginErrReturn
} }
func (mock *mockLDAP) Users([]string) ([]*models.ExternalUserInfo, error) {
// Users test fn
func (mock *MockLDAP) Users([]string) ([]*models.ExternalUserInfo, error) {
mock.usersCalledTimes = mock.usersCalledTimes + 1 mock.usersCalledTimes = mock.usersCalledTimes + 1
if mock.usersCalledTimes == 1 { if mock.usersCalledTimes == 1 {
...@@ -35,19 +39,58 @@ func (mock *mockLDAP) Users([]string) ([]*models.ExternalUserInfo, error) { ...@@ -35,19 +39,58 @@ func (mock *mockLDAP) Users([]string) ([]*models.ExternalUserInfo, error) {
return mock.usersRestReturn, mock.usersErrReturn return mock.usersRestReturn, mock.usersErrReturn
} }
func (mock *mockLDAP) Auth(string, string) error {
// Auth test fn
func (mock *MockLDAP) Auth(string, string) error {
return nil return nil
} }
func (mock *mockLDAP) Dial() error {
// Dial test fn
func (mock *MockLDAP) Dial() error {
mock.dialCalledTimes = mock.dialCalledTimes + 1 mock.dialCalledTimes = mock.dialCalledTimes + 1
return mock.dialErrReturn return mock.dialErrReturn
} }
func (mock *mockLDAP) Close() {
// Close test fn
func (mock *MockLDAP) Close() {
mock.closeCalledTimes = mock.closeCalledTimes + 1 mock.closeCalledTimes = mock.closeCalledTimes + 1
} }
func setup() *mockLDAP { // MockMultiLDAP represents testing struct for multildap testing
mock := &mockLDAP{} type MockMultiLDAP struct {
LoginCalledTimes int
UsersCalledTimes int
UserCalledTimes int
UsersResult []*models.ExternalUserInfo
}
// Login test fn
func (mock *MockMultiLDAP) Login(query *models.LoginUserQuery) (
*models.ExternalUserInfo, error,
) {
mock.LoginCalledTimes = mock.LoginCalledTimes + 1
return nil, nil
}
// Users test fn
func (mock *MockMultiLDAP) Users(logins []string) (
[]*models.ExternalUserInfo, error,
) {
mock.UsersCalledTimes = mock.UsersCalledTimes + 1
return mock.UsersResult, nil
}
// User test fn
func (mock *MockMultiLDAP) User(login string) (
*models.ExternalUserInfo, error,
) {
mock.UserCalledTimes = mock.UserCalledTimes + 1
return nil, nil
}
func setup() *MockLDAP {
mock := &MockLDAP{}
newLDAP = func(config *ldap.ServerConfig) ldap.IServer { newLDAP = func(config *ldap.ServerConfig) ldap.IServer {
return mock return mock
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment