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