auth_proxy_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package authproxy
  2. import (
  3. "encoding/base32"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "testing"
  8. "github.com/grafana/grafana/pkg/bus"
  9. "github.com/grafana/grafana/pkg/infra/remotecache"
  10. "github.com/grafana/grafana/pkg/models"
  11. "github.com/grafana/grafana/pkg/services/ldap"
  12. "github.com/grafana/grafana/pkg/services/multildap"
  13. "github.com/grafana/grafana/pkg/setting"
  14. . "github.com/smartystreets/goconvey/convey"
  15. "gopkg.in/macaron.v1"
  16. )
  17. type TestMultiLDAP struct {
  18. multildap.MultiLDAP
  19. ID int64
  20. userCalled bool
  21. loginCalled bool
  22. }
  23. func (stub *TestMultiLDAP) Login(query *models.LoginUserQuery) (
  24. *models.ExternalUserInfo, error,
  25. ) {
  26. stub.loginCalled = true
  27. result := &models.ExternalUserInfo{
  28. UserId: stub.ID,
  29. }
  30. return result, nil
  31. }
  32. func (stub *TestMultiLDAP) User(login string) (
  33. *models.ExternalUserInfo,
  34. error,
  35. ) {
  36. stub.userCalled = true
  37. result := &models.ExternalUserInfo{
  38. UserId: stub.ID,
  39. }
  40. return result, nil
  41. }
  42. func prepareMiddleware(t *testing.T, req *http.Request, store *remotecache.RemoteCache) *AuthProxy {
  43. t.Helper()
  44. ctx := &models.ReqContext{
  45. Context: &macaron.Context{
  46. Req: macaron.Request{
  47. Request: req,
  48. },
  49. },
  50. }
  51. auth := New(&Options{
  52. Store: store,
  53. Ctx: ctx,
  54. OrgID: 4,
  55. })
  56. return auth
  57. }
  58. func TestMiddlewareContext(t *testing.T) {
  59. Convey("auth_proxy helper", t, func() {
  60. req, _ := http.NewRequest("POST", "http://example.com", nil)
  61. setting.AuthProxyHeaderName = "X-Killa"
  62. store := remotecache.NewFakeStore(t)
  63. name := "markelog"
  64. req.Header.Add(setting.AuthProxyHeaderName, name)
  65. Convey("when the cache only contains the main header", func() {
  66. Convey("with a simple cache key", func() {
  67. // Set cache key
  68. key := fmt.Sprintf(CachePrefix, base32.StdEncoding.EncodeToString([]byte(name)))
  69. store.Set(key, int64(33), 0)
  70. // Set up the middleware
  71. auth := prepareMiddleware(t, req, store)
  72. id, err := auth.Login()
  73. So(auth.getKey(), ShouldEqual, "auth-proxy-sync-ttl:NVQXE23FNRXWO===")
  74. So(err, ShouldBeNil)
  75. So(id, ShouldEqual, 33)
  76. })
  77. Convey("when the cache key contains additional headers", func() {
  78. setting.AuthProxyHeaders = map[string]string{"Groups": "X-WEBAUTH-GROUPS"}
  79. group := "grafana-core-team"
  80. req.Header.Add("X-WEBAUTH-GROUPS", group)
  81. key := fmt.Sprintf(CachePrefix, base32.StdEncoding.EncodeToString([]byte(name+"-"+group)))
  82. store.Set(key, int64(33), 0)
  83. auth := prepareMiddleware(t, req, store)
  84. id, err := auth.Login()
  85. So(auth.getKey(), ShouldEqual, "auth-proxy-sync-ttl:NVQXE23FNRXWOLLHOJQWMYLOMEWWG33SMUWXIZLBNU======")
  86. So(err, ShouldBeNil)
  87. So(id, ShouldEqual, 33)
  88. })
  89. Convey("when the does not exist", func() {
  90. })
  91. })
  92. Convey("LDAP", func() {
  93. Convey("logs in via LDAP", func() {
  94. bus.AddHandler("test", func(cmd *models.UpsertUserCommand) error {
  95. cmd.Result = &models.User{
  96. Id: 42,
  97. }
  98. return nil
  99. })
  100. isLDAPEnabled = func() bool {
  101. return true
  102. }
  103. stub := &TestMultiLDAP{
  104. ID: 42,
  105. }
  106. getLDAPConfig = func() (*ldap.Config, error) {
  107. config := &ldap.Config{
  108. Servers: []*ldap.ServerConfig{
  109. {
  110. SearchBaseDNs: []string{"BaseDNHere"},
  111. },
  112. },
  113. }
  114. return config, nil
  115. }
  116. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  117. return stub
  118. }
  119. defer func() {
  120. newLDAP = multildap.New
  121. isLDAPEnabled = ldap.IsEnabled
  122. getLDAPConfig = ldap.GetConfig
  123. }()
  124. store := remotecache.NewFakeStore(t)
  125. auth := prepareMiddleware(t, req, store)
  126. id, err := auth.Login()
  127. So(err, ShouldBeNil)
  128. So(id, ShouldEqual, 42)
  129. So(stub.userCalled, ShouldEqual, true)
  130. })
  131. Convey("gets nice error if ldap is enabled but not configured", func() {
  132. isLDAPEnabled = func() bool {
  133. return true
  134. }
  135. getLDAPConfig = func() (*ldap.Config, error) {
  136. return nil, errors.New("Something went wrong")
  137. }
  138. defer func() {
  139. newLDAP = multildap.New
  140. isLDAPEnabled = ldap.IsEnabled
  141. getLDAPConfig = ldap.GetConfig
  142. }()
  143. store := remotecache.NewFakeStore(t)
  144. auth := prepareMiddleware(t, req, store)
  145. stub := &TestMultiLDAP{
  146. ID: 42,
  147. }
  148. newLDAP = func(servers []*ldap.ServerConfig) multildap.IMultiLDAP {
  149. return stub
  150. }
  151. id, err := auth.Login()
  152. So(err, ShouldNotBeNil)
  153. So(err.Error(), ShouldContainSubstring, "Failed to get the user")
  154. So(id, ShouldNotEqual, 42)
  155. So(stub.loginCalled, ShouldEqual, false)
  156. })
  157. })
  158. })
  159. }