2015-09-15 18:19:47 +08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
|
|
import (
|
2015-09-18 08:36:58 +02:00
|
|
|
"testing"
|
|
|
|
|
|
2022-02-10 16:17:50 -05:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
|
2022-11-18 09:56:06 +01:00
|
|
|
"github.com/grafana/grafana/pkg/services/auth"
|
2023-08-09 08:54:52 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
2022-11-14 21:08:10 +02:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
2015-09-15 18:19:47 +08:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 14:30:59 +02:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-09-15 18:19:47 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMiddlewareQuota(t *testing.T) {
|
2020-12-03 08:28:54 +01:00
|
|
|
t.Run("With user not logged in", func(t *testing.T) {
|
2020-12-04 11:09:32 +01:00
|
|
|
middlewareScenario(t, "and global quota not reached", func(t *testing.T, sc *scenarioContext) {
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "user")
|
2020-12-15 19:09:04 +01:00
|
|
|
|
|
|
|
|
sc.m.Get("/user", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/user").exec()
|
2020-12-04 11:09:32 +01:00
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, configure)
|
2015-09-15 18:19:47 +08:00
|
|
|
|
2020-12-04 11:09:32 +01:00
|
|
|
middlewareScenario(t, "and global quota reached", func(t *testing.T, sc *scenarioContext) {
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "user")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/user", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/user").exec()
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
|
2020-12-04 11:09:32 +01:00
|
|
|
middlewareScenario(t, "and global session quota not reached", func(t *testing.T, sc *scenarioContext) {
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "session")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/user", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/user").exec()
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
2019-02-11 21:12:01 +01:00
|
|
|
|
2020-12-04 11:09:32 +01:00
|
|
|
middlewareScenario(t, "and global session quota reached", func(t *testing.T, sc *scenarioContext) {
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "session")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/user", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/user").exec()
|
2020-12-04 11:09:32 +01:00
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
})
|
2019-02-11 21:12:01 +01:00
|
|
|
|
2020-12-15 19:09:04 +01:00
|
|
|
t.Run("with user logged in", func(t *testing.T) {
|
|
|
|
|
setUp := func(sc *scenarioContext) {
|
2023-08-09 08:54:52 +02:00
|
|
|
sc.withIdentity(&authn.Identity{ID: "user:1", SessionToken: &auth.UserToken{UserId: 12}})
|
2020-12-15 19:09:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
middlewareScenario(t, "global datasource quota reached", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "data_source")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/ds", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/ds").exec()
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
|
2020-12-15 19:09:04 +01:00
|
|
|
middlewareScenario(t, "user Org quota not reached", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "org")
|
2020-12-15 19:09:04 +01:00
|
|
|
|
|
|
|
|
sc.m.Get("/org", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/org").exec()
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
|
2020-12-15 19:09:04 +01:00
|
|
|
middlewareScenario(t, "user Org quota reached", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "org")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/org", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/org").exec()
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
|
2020-12-15 19:09:04 +01:00
|
|
|
middlewareScenario(t, "org dashboard quota not reached", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "dashboard")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/dashboard", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/dashboard").exec()
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
|
2020-12-15 19:09:04 +01:00
|
|
|
middlewareScenario(t, "org dashboard quota reached", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "dashboard")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/dashboard", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/dashboard").exec()
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2020-12-03 08:28:54 +01:00
|
|
|
})
|
|
|
|
|
|
2020-12-15 19:09:04 +01:00
|
|
|
middlewareScenario(t, "org dashboard quota reached, but quotas disabled", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "dashboard")
|
2020-12-15 19:09:04 +01:00
|
|
|
sc.m.Get("/dashboard", quotaHandler, sc.defaultHandler)
|
2020-12-03 08:28:54 +01:00
|
|
|
sc.fakeReq("GET", "/dashboard").exec()
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
2020-12-15 19:09:04 +01:00
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
2015-09-15 18:19:47 +08:00
|
|
|
})
|
2021-05-04 19:16:28 +03:00
|
|
|
|
2021-09-29 17:16:40 +03:00
|
|
|
middlewareScenario(t, "org alert quota reached and unified alerting is enabled", func(t *testing.T, sc *scenarioContext) {
|
2021-05-04 19:16:28 +03:00
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "alert_rule")
|
2021-05-04 19:16:28 +03:00
|
|
|
sc.m.Get("/alert_rule", quotaHandler, sc.defaultHandler)
|
|
|
|
|
sc.fakeReq("GET", "/alert_rule").exec()
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
|
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
|
|
|
|
|
2021-11-24 20:56:07 +01:00
|
|
|
cfg.UnifiedAlerting.Enabled = new(bool)
|
|
|
|
|
*cfg.UnifiedAlerting.Enabled = true
|
2021-05-04 19:16:28 +03:00
|
|
|
})
|
|
|
|
|
|
2021-09-29 17:16:40 +03:00
|
|
|
middlewareScenario(t, "org alert quota not reached and unified alerting is enabled", func(t *testing.T, sc *scenarioContext) {
|
2021-05-04 19:16:28 +03:00
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "alert_rule")
|
2021-05-04 19:16:28 +03:00
|
|
|
sc.m.Get("/alert_rule", quotaHandler, sc.defaultHandler)
|
|
|
|
|
sc.fakeReq("GET", "/alert_rule").exec()
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
|
|
|
|
|
2021-11-24 20:56:07 +01:00
|
|
|
cfg.UnifiedAlerting.Enabled = new(bool)
|
|
|
|
|
*cfg.UnifiedAlerting.Enabled = true
|
2021-05-04 19:16:28 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
middlewareScenario(t, "org alert quota reached but ngalert disabled", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
// this scenario can only happen if the feature was enabled and later disabled
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(true, "alert_rule")
|
2021-05-04 19:16:28 +03:00
|
|
|
sc.m.Get("/alert_rule", quotaHandler, sc.defaultHandler)
|
|
|
|
|
sc.fakeReq("GET", "/alert_rule").exec()
|
|
|
|
|
assert.Equal(t, 403, sc.resp.Code)
|
|
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
middlewareScenario(t, "org alert quota not reached but ngalert disabled", func(t *testing.T, sc *scenarioContext) {
|
|
|
|
|
setUp(sc)
|
|
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
quotaHandler := getQuotaHandler(false, "alert_rule")
|
2021-05-04 19:16:28 +03:00
|
|
|
sc.m.Get("/alert_rule", quotaHandler, sc.defaultHandler)
|
|
|
|
|
sc.fakeReq("GET", "/alert_rule").exec()
|
|
|
|
|
assert.Equal(t, 200, sc.resp.Code)
|
|
|
|
|
}, func(cfg *setting.Cfg) {
|
|
|
|
|
configure(cfg)
|
|
|
|
|
})
|
2015-09-15 18:19:47 +08:00
|
|
|
})
|
|
|
|
|
}
|
2020-12-15 19:09:04 +01:00
|
|
|
|
2022-02-10 12:42:06 +01:00
|
|
|
func getQuotaHandler(reached bool, target string) web.Handler {
|
2022-11-14 21:08:10 +02:00
|
|
|
qs := quotatest.New(reached, nil)
|
2020-12-15 19:09:04 +01:00
|
|
|
return Quota(qs)(target)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func configure(cfg *setting.Cfg) {
|
|
|
|
|
cfg.AnonymousEnabled = false
|
2022-07-15 18:06:44 +02:00
|
|
|
}
|