LDAP: Add API endpoint to query the LDAP server(s) status (#18868)

* LDAP: Add API endpoint to query the LDAP server(s) status|

This endpoint returns the current status(es) of the configured LDAP server(s).

The status of each server is verified by dialling and if no error is returned we assume the server is operational.

This is the last piece I'll produce as an API before moving into #18759 and see the view come to life.
This commit is contained in:
gotjosh
2019-09-04 15:29:14 +01:00
committed by GitHub
parent a5d8b0212b
commit 7c6dd1868b
7 changed files with 244 additions and 7 deletions
+42
View File
@@ -28,8 +28,17 @@ var ErrNoLDAPServers = errors.New("No LDAP servers are configured")
// ErrDidNotFindUser if request for user is unsuccessful
var ErrDidNotFindUser = errors.New("Did not find a user")
// ServerStatus holds the LDAP server status
type ServerStatus struct {
Host string
Port int
Available bool
Error error
}
// IMultiLDAP is interface for MultiLDAP
type IMultiLDAP interface {
Ping() ([]*ServerStatus, error)
Login(query *models.LoginUserQuery) (
*models.ExternalUserInfo, error,
)
@@ -55,6 +64,39 @@ func New(configs []*ldap.ServerConfig) IMultiLDAP {
}
}
// Ping dials each of the LDAP servers and returns their status. If the server is unavailable, it also returns the error.
func (multiples *MultiLDAP) Ping() ([]*ServerStatus, error) {
if len(multiples.configs) == 0 {
return nil, ErrNoLDAPServers
}
serverStatuses := []*ServerStatus{}
for _, config := range multiples.configs {
status := &ServerStatus{}
status.Host = config.Host
status.Port = config.Port
server := newLDAP(config)
err := server.Dial()
if err == nil {
status.Available = true
serverStatuses = append(serverStatuses, status)
} else {
status.Available = false
status.Error = err
serverStatuses = append(serverStatuses, status)
}
defer server.Close()
}
return serverStatuses, nil
}
// Login tries to log in the user in multiples LDAP
func (multiples *MultiLDAP) Login(query *models.LoginUserQuery) (
*models.ExternalUserInfo, error,
+50
View File
@@ -11,6 +11,56 @@ import (
func TestMultiLDAP(t *testing.T) {
Convey("Multildap", t, func() {
Convey("Ping()", func() {
Convey("Should return error for absent config list", func() {
setup()
multi := New([]*ldap.ServerConfig{})
_, err := multi.Ping()
So(err, ShouldBeError)
So(err, ShouldEqual, ErrNoLDAPServers)
teardown()
})
Convey("Should return an unavailable status on dial error", func() {
mock := setup()
expectedErr := errors.New("Dial error")
mock.dialErrReturn = expectedErr
multi := New([]*ldap.ServerConfig{
{Host: "10.0.0.1", Port: 361},
})
statuses, err := multi.Ping()
So(err, ShouldBeNil)
So(statuses[0].Host, ShouldEqual, "10.0.0.1")
So(statuses[0].Port, ShouldEqual, 361)
So(statuses[0].Available, ShouldBeFalse)
So(statuses[0].Error, ShouldEqual, expectedErr)
teardown()
})
Convey("Shoudl get the LDAP server statuses", func() {
setup()
multi := New([]*ldap.ServerConfig{
{Host: "10.0.0.1", Port: 361},
})
statuses, err := multi.Ping()
So(err, ShouldBeNil)
So(statuses[0].Host, ShouldEqual, "10.0.0.1")
So(statuses[0].Port, ShouldEqual, 361)
So(statuses[0].Available, ShouldBeTrue)
So(statuses[0].Error, ShouldBeNil)
teardown()
})
})
Convey("Login()", func() {
Convey("Should return error for absent config list", func() {
setup()
+7
View File
@@ -69,10 +69,17 @@ type MockMultiLDAP struct {
LoginCalledTimes int
UsersCalledTimes int
UserCalledTimes int
PingCalledTimes int
UsersResult []*models.ExternalUserInfo
}
func (mock *MockMultiLDAP) Ping() ([]*ServerStatus, error) {
mock.PingCalledTimes = mock.PingCalledTimes + 1
return nil, nil
}
// Login test fn
func (mock *MockMultiLDAP) Login(query *models.LoginUserQuery) (
*models.ExternalUserInfo, error,