ldap_debug_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. "github.com/grafana/grafana/pkg/bus"
  8. "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/services/ldap"
  10. "github.com/grafana/grafana/pkg/services/multildap"
  11. "github.com/grafana/grafana/pkg/setting"
  12. "github.com/stretchr/testify/assert"
  13. "github.com/stretchr/testify/require"
  14. )
  15. type LDAPMock struct {
  16. Results []*models.ExternalUserInfo
  17. }
  18. var userSearchResult *models.ExternalUserInfo
  19. var userSearchConfig ldap.ServerConfig
  20. func (m *LDAPMock) Login(query *models.LoginUserQuery) (*models.ExternalUserInfo, error) {
  21. return &models.ExternalUserInfo{}, nil
  22. }
  23. func (m *LDAPMock) Users(logins []string) ([]*models.ExternalUserInfo, error) {
  24. s := []*models.ExternalUserInfo{}
  25. return s, nil
  26. }
  27. func (m *LDAPMock) User(login string) (*models.ExternalUserInfo, ldap.ServerConfig, error) {
  28. return userSearchResult, userSearchConfig, nil
  29. }
  30. func getUserFromLDAPContext(t *testing.T, requestURL string) *scenarioContext {
  31. t.Helper()
  32. sc := setupScenarioContext(requestURL)
  33. hs := &HTTPServer{Cfg: setting.NewCfg()}
  34. sc.defaultHandler = Wrap(func(c *models.ReqContext) Response {
  35. sc.context = c
  36. return hs.GetUserFromLDAP(c)
  37. })
  38. sc.m.Get("/api/admin/ldap/:username", sc.defaultHandler)
  39. sc.resp = httptest.NewRecorder()
  40. req, _ := http.NewRequest(http.MethodGet, requestURL, nil)
  41. sc.req = req
  42. sc.exec()
  43. return sc
  44. }
  45. func TestGetUserFromLDAPApiEndpoint_UserNotFound(t *testing.T) {
  46. getLDAPConfig = func() (*ldap.Config, error) {
  47. return &ldap.Config{}, nil
  48. }
  49. newLDAP = func(_ []*ldap.ServerConfig) multildap.IMultiLDAP {
  50. return &LDAPMock{}
  51. }
  52. userSearchResult = nil
  53. sc := getUserFromLDAPContext(t, "/api/admin/ldap/user-that-does-not-exist")
  54. require.Equal(t, sc.resp.Code, http.StatusNotFound)
  55. responseString, err := getBody(sc.resp)
  56. assert.Nil(t, err)
  57. assert.Equal(t, "{\"message\":\"No user was found on the LDAP server(s)\"}", responseString)
  58. }
  59. func TestGetUserFromLDAPApiEndpoint_OrgNotfound(t *testing.T) {
  60. isAdmin := true
  61. userSearchResult = &models.ExternalUserInfo{
  62. Name: "John Doe",
  63. Email: "john.doe@example.com",
  64. Login: "johndoe",
  65. OrgRoles: map[int64]models.RoleType{1: models.ROLE_ADMIN, 2: models.ROLE_VIEWER},
  66. IsGrafanaAdmin: &isAdmin,
  67. }
  68. userSearchConfig = ldap.ServerConfig{
  69. Attr: ldap.AttributeMap{
  70. Name: "ldap-name",
  71. Surname: "ldap-surname",
  72. Email: "ldap-email",
  73. Username: "ldap-username",
  74. },
  75. Groups: []*ldap.GroupToOrgRole{
  76. {
  77. GroupDN: "cn=admins,ou=groups,dc=grafana,dc=org",
  78. OrgID: 1,
  79. OrgRole: models.ROLE_ADMIN,
  80. },
  81. {
  82. GroupDN: "cn=admins,ou=groups,dc=grafana2,dc=org",
  83. OrgID: 2,
  84. OrgRole: models.ROLE_VIEWER,
  85. },
  86. },
  87. }
  88. mockOrgSearchResult := []*models.OrgDTO{
  89. {Id: 1, Name: "Main Org."},
  90. }
  91. bus.AddHandler("test", func(query *models.SearchOrgsQuery) error {
  92. query.Result = mockOrgSearchResult
  93. return nil
  94. })
  95. getLDAPConfig = func() (*ldap.Config, error) {
  96. return &ldap.Config{}, nil
  97. }
  98. newLDAP = func(_ []*ldap.ServerConfig) multildap.IMultiLDAP {
  99. return &LDAPMock{}
  100. }
  101. sc := getUserFromLDAPContext(t, "/api/admin/ldap/johndoe")
  102. require.Equal(t, sc.resp.Code, http.StatusBadRequest)
  103. jsonResponse, err := getJSONbody(sc.resp)
  104. assert.Nil(t, err)
  105. expected := `
  106. {
  107. "error": "Unable to find organization with ID '2'",
  108. "message": "Organization not found - Please verify your LDAP configuration"
  109. }
  110. `
  111. var expectedJSON interface{}
  112. _ = json.Unmarshal([]byte(expected), &expectedJSON)
  113. assert.Equal(t, jsonResponse, expectedJSON)
  114. }
  115. func TestGetUserFromLDAPApiEndpoint(t *testing.T) {
  116. isAdmin := true
  117. userSearchResult = &models.ExternalUserInfo{
  118. Name: "John Doe",
  119. Email: "john.doe@example.com",
  120. Login: "johndoe",
  121. OrgRoles: map[int64]models.RoleType{1: models.ROLE_ADMIN},
  122. IsGrafanaAdmin: &isAdmin,
  123. }
  124. userSearchConfig = ldap.ServerConfig{
  125. Attr: ldap.AttributeMap{
  126. Name: "ldap-name",
  127. Surname: "ldap-surname",
  128. Email: "ldap-email",
  129. Username: "ldap-username",
  130. },
  131. Groups: []*ldap.GroupToOrgRole{
  132. {
  133. GroupDN: "cn=admins,ou=groups,dc=grafana,dc=org",
  134. OrgID: 1,
  135. OrgRole: models.ROLE_ADMIN,
  136. },
  137. },
  138. }
  139. mockOrgSearchResult := []*models.OrgDTO{
  140. {Id: 1, Name: "Main Org."},
  141. }
  142. bus.AddHandler("test", func(query *models.SearchOrgsQuery) error {
  143. query.Result = mockOrgSearchResult
  144. return nil
  145. })
  146. getLDAPConfig = func() (*ldap.Config, error) {
  147. return &ldap.Config{}, nil
  148. }
  149. newLDAP = func(_ []*ldap.ServerConfig) multildap.IMultiLDAP {
  150. return &LDAPMock{}
  151. }
  152. sc := getUserFromLDAPContext(t, "/api/admin/ldap/johndoe")
  153. require.Equal(t, sc.resp.Code, http.StatusOK)
  154. jsonResponse, err := getJSONbody(sc.resp)
  155. assert.Nil(t, err)
  156. expected := `
  157. {
  158. "name": {
  159. "cfgAttrValue": "ldap-name", "ldapValue": "John"
  160. },
  161. "surname": {
  162. "cfgAttrValue": "ldap-surname", "ldapValue": "Doe"
  163. },
  164. "email": {
  165. "cfgAttrValue": "ldap-email", "ldapValue": "john.doe@example.com"
  166. },
  167. "login": {
  168. "cfgAttrValue": "ldap-username", "ldapValue": "johndoe"
  169. },
  170. "isGrafanaAdmin": true,
  171. "isDisabled": false,
  172. "roles": [
  173. { "orgId": 1, "orgRole": "Admin", "orgName": "Main Org.", "groupDN": "cn=admins,ou=groups,dc=grafana,dc=org" }
  174. ],
  175. "teams": null
  176. }
  177. `
  178. var expectedJSON interface{}
  179. _ = json.Unmarshal([]byte(expected), &expectedJSON)
  180. assert.Equal(t, jsonResponse, expectedJSON)
  181. }