ldap_login_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package ldap
  2. import (
  3. "testing"
  4. . "github.com/smartystreets/goconvey/convey"
  5. "gopkg.in/ldap.v3"
  6. "github.com/grafana/grafana/pkg/infra/log"
  7. "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/services/user"
  9. )
  10. func TestLDAPLogin(t *testing.T) {
  11. Convey("Login()", t, func() {
  12. serverScenario("When user is log in and updated", func(sc *scenarioContext) {
  13. // arrange
  14. mockConnection := &MockConnection{}
  15. server := &Server{
  16. Config: &ServerConfig{
  17. Host: "",
  18. RootCACert: "",
  19. Groups: []*GroupToOrgRole{
  20. {GroupDN: "*", OrgRole: "Admin"},
  21. },
  22. Attr: AttributeMap{
  23. Username: "username",
  24. Surname: "surname",
  25. Email: "email",
  26. Name: "name",
  27. MemberOf: "memberof",
  28. },
  29. SearchBaseDNs: []string{"BaseDNHere"},
  30. },
  31. Connection: mockConnection,
  32. log: log.New("test-logger"),
  33. }
  34. entry := ldap.Entry{
  35. DN: "dn", Attributes: []*ldap.EntryAttribute{
  36. {Name: "username", Values: []string{"roelgerrits"}},
  37. {Name: "surname", Values: []string{"Gerrits"}},
  38. {Name: "email", Values: []string{"roel@test.com"}},
  39. {Name: "name", Values: []string{"Roel"}},
  40. {Name: "memberof", Values: []string{"admins"}},
  41. }}
  42. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  43. mockConnection.setSearchResult(&result)
  44. query := &models.LoginUserQuery{
  45. Username: "roelgerrits",
  46. }
  47. sc.userQueryReturns(&models.User{
  48. Id: 1,
  49. Email: "roel@test.net",
  50. Name: "Roel Gerrits",
  51. Login: "roelgerrits",
  52. })
  53. sc.userOrgsQueryReturns([]*models.UserOrgDTO{})
  54. // act
  55. extUser, _ := server.Login(query)
  56. userInfo, err := user.Upsert(&user.UpsertArgs{
  57. SignupAllowed: true,
  58. ExternalUser: extUser,
  59. })
  60. // assert
  61. // Check absence of the error
  62. So(err, ShouldBeNil)
  63. // User should be searched in ldap
  64. So(mockConnection.SearchCalled, ShouldBeTrue)
  65. // Info should be updated (email differs)
  66. So(userInfo.Email, ShouldEqual, "roel@test.com")
  67. // User should have admin privileges
  68. So(sc.addOrgUserCmd.Role, ShouldEqual, "Admin")
  69. })
  70. serverScenario("When login with invalid credentials", func(scenario *scenarioContext) {
  71. connection := &MockConnection{}
  72. entry := ldap.Entry{}
  73. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  74. connection.setSearchResult(&result)
  75. connection.bindProvider = func(username, password string) error {
  76. return &ldap.Error{
  77. ResultCode: 49,
  78. }
  79. }
  80. server := &Server{
  81. Config: &ServerConfig{
  82. Attr: AttributeMap{
  83. Username: "username",
  84. Name: "name",
  85. MemberOf: "memberof",
  86. },
  87. SearchBaseDNs: []string{"BaseDNHere"},
  88. },
  89. Connection: connection,
  90. log: log.New("test-logger"),
  91. }
  92. _, err := server.Login(scenario.loginUserQuery)
  93. Convey("it should return invalid credentials error", func() {
  94. So(err, ShouldEqual, ErrInvalidCredentials)
  95. })
  96. })
  97. serverScenario("When login with valid credentials", func(scenario *scenarioContext) {
  98. connection := &MockConnection{}
  99. entry := ldap.Entry{
  100. DN: "dn", Attributes: []*ldap.EntryAttribute{
  101. {Name: "username", Values: []string{"markelog"}},
  102. {Name: "surname", Values: []string{"Gaidarenko"}},
  103. {Name: "email", Values: []string{"markelog@gmail.com"}},
  104. {Name: "name", Values: []string{"Oleg"}},
  105. {Name: "memberof", Values: []string{"admins"}},
  106. },
  107. }
  108. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  109. connection.setSearchResult(&result)
  110. connection.bindProvider = func(username, password string) error {
  111. return nil
  112. }
  113. server := &Server{
  114. Config: &ServerConfig{
  115. Attr: AttributeMap{
  116. Username: "username",
  117. Name: "name",
  118. MemberOf: "memberof",
  119. },
  120. SearchBaseDNs: []string{"BaseDNHere"},
  121. },
  122. Connection: connection,
  123. log: log.New("test-logger"),
  124. }
  125. resp, err := server.Login(scenario.loginUserQuery)
  126. So(err, ShouldBeNil)
  127. So(resp.Login, ShouldEqual, "markelog")
  128. })
  129. serverScenario("When user not found in LDAP, but exist in Grafana", func(scenario *scenarioContext) {
  130. connection := &MockConnection{}
  131. result := ldap.SearchResult{Entries: []*ldap.Entry{}}
  132. connection.setSearchResult(&result)
  133. externalUser := &models.ExternalUserInfo{UserId: 42, IsDisabled: false}
  134. scenario.getExternalUserInfoByLoginQueryReturns(externalUser)
  135. connection.bindProvider = func(username, password string) error {
  136. return nil
  137. }
  138. server := &Server{
  139. Config: &ServerConfig{
  140. SearchBaseDNs: []string{"BaseDNHere"},
  141. },
  142. Connection: connection,
  143. log: log.New("test-logger"),
  144. }
  145. _, err := server.Login(scenario.loginUserQuery)
  146. Convey("it should disable user", func() {
  147. So(scenario.disableExternalUserCalled, ShouldBeTrue)
  148. So(scenario.disableUserCmd.IsDisabled, ShouldBeTrue)
  149. So(scenario.disableUserCmd.UserId, ShouldEqual, 42)
  150. })
  151. Convey("it should return invalid credentials error", func() {
  152. So(err, ShouldEqual, ErrInvalidCredentials)
  153. })
  154. })
  155. serverScenario("When user not found in LDAP, and disabled in Grafana already", func(scenario *scenarioContext) {
  156. connection := &MockConnection{}
  157. result := ldap.SearchResult{Entries: []*ldap.Entry{}}
  158. connection.setSearchResult(&result)
  159. externalUser := &models.ExternalUserInfo{UserId: 42, IsDisabled: true}
  160. scenario.getExternalUserInfoByLoginQueryReturns(externalUser)
  161. connection.bindProvider = func(username, password string) error {
  162. return nil
  163. }
  164. server := &Server{
  165. Config: &ServerConfig{
  166. SearchBaseDNs: []string{"BaseDNHere"},
  167. },
  168. Connection: connection,
  169. log: log.New("test-logger"),
  170. }
  171. _, err := server.Login(scenario.loginUserQuery)
  172. Convey("it should't call disable function", func() {
  173. So(scenario.disableExternalUserCalled, ShouldBeFalse)
  174. })
  175. Convey("it should return invalid credentials error", func() {
  176. So(err, ShouldEqual, ErrInvalidCredentials)
  177. })
  178. })
  179. serverScenario("When user found in LDAP, and disabled in Grafana", func(scenario *scenarioContext) {
  180. connection := &MockConnection{}
  181. entry := ldap.Entry{}
  182. result := ldap.SearchResult{Entries: []*ldap.Entry{&entry}}
  183. connection.setSearchResult(&result)
  184. scenario.userQueryReturns(&models.User{Id: 42, IsDisabled: true})
  185. connection.bindProvider = func(username, password string) error {
  186. return nil
  187. }
  188. server := &Server{
  189. Config: &ServerConfig{
  190. SearchBaseDNs: []string{"BaseDNHere"},
  191. },
  192. Connection: connection,
  193. log: log.New("test-logger"),
  194. }
  195. extUser, _ := server.Login(scenario.loginUserQuery)
  196. _, err := user.Upsert(&user.UpsertArgs{
  197. SignupAllowed: true,
  198. ExternalUser: extUser,
  199. })
  200. Convey("it should re-enable user", func() {
  201. So(scenario.disableExternalUserCalled, ShouldBeTrue)
  202. So(scenario.disableUserCmd.IsDisabled, ShouldBeFalse)
  203. So(scenario.disableUserCmd.UserId, ShouldEqual, 42)
  204. })
  205. Convey("it should not return error", func() {
  206. So(err, ShouldBeNil)
  207. })
  208. })
  209. })
  210. }