2015-09-15 05:19:47 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2019-04-30 07:42:01 -05:00
|
|
|
"context"
|
2015-09-18 01:36:58 -05:00
|
|
|
"testing"
|
|
|
|
|
2022-02-10 15:17:50 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2020-02-28 05:50:58 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2022-02-10 15:17:50 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Global.User = 4
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Global.Session = 10
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Global.Session = 1
|
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) {
|
|
|
|
const quotaUsed = 4
|
|
|
|
setUp := func(sc *scenarioContext) {
|
|
|
|
sc.withTokenSessionCookie("token")
|
2022-08-11 06:28:55 -05:00
|
|
|
sc.mockSQLStore.ExpectedSignedInUser = &user.SignedInUser{UserID: 12}
|
2020-12-15 12:09:04 -06:00
|
|
|
sc.userAuthTokenService.LookupTokenProvider = func(ctx context.Context, unhashedToken string) (*models.UserToken, error) {
|
|
|
|
return &models.UserToken{
|
|
|
|
UserId: 12,
|
|
|
|
UnhashedToken: "",
|
|
|
|
}, nil
|
2020-12-03 01:28:54 -06:00
|
|
|
}
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Global.DataSource = quotaUsed
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.User.Org = quotaUsed + 1
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.User.Org = quotaUsed
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Org.Dashboard = quotaUsed + 1
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Org.Dashboard = quotaUsed
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Org.Dashboard = quotaUsed
|
|
|
|
cfg.Quota.Enabled = false
|
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
|
|
|
cfg.Quota.Org.AlertRule = quotaUsed
|
|
|
|
})
|
|
|
|
|
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
|
|
|
cfg.Quota.Org.AlertRule = quotaUsed + 1
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Org.AlertRule = quotaUsed
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
cfg.Quota.Org.AlertRule = quotaUsed + 1
|
|
|
|
})
|
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 {
|
|
|
|
qs := &mockQuotaService{
|
|
|
|
reached: reached,
|
2020-12-15 12:09:04 -06:00
|
|
|
}
|
|
|
|
return Quota(qs)(target)
|
|
|
|
}
|
|
|
|
|
|
|
|
func configure(cfg *setting.Cfg) {
|
|
|
|
cfg.AnonymousEnabled = false
|
|
|
|
cfg.Quota = setting.QuotaSettings{
|
|
|
|
Enabled: true,
|
|
|
|
Org: &setting.OrgQuota{
|
|
|
|
User: 5,
|
|
|
|
Dashboard: 5,
|
|
|
|
DataSource: 5,
|
|
|
|
ApiKey: 5,
|
2021-05-04 11:16:28 -05:00
|
|
|
AlertRule: 5,
|
2020-12-15 12:09:04 -06:00
|
|
|
},
|
|
|
|
User: &setting.UserQuota{
|
|
|
|
Org: 5,
|
|
|
|
},
|
|
|
|
Global: &setting.GlobalQuota{
|
|
|
|
Org: 5,
|
|
|
|
User: 5,
|
|
|
|
Dashboard: 5,
|
|
|
|
DataSource: 5,
|
|
|
|
ApiKey: 5,
|
|
|
|
Session: 5,
|
2021-05-04 11:16:28 -05:00
|
|
|
AlertRule: 5,
|
2020-12-15 12:09:04 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 05:42:06 -06:00
|
|
|
|
|
|
|
type mockQuotaService struct {
|
|
|
|
reached bool
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockQuotaService) QuotaReached(c *models.ReqContext, target string) (bool, error) {
|
|
|
|
return m.reached, m.err
|
|
|
|
}
|
2022-02-10 15:17:50 -06:00
|
|
|
|
|
|
|
func (m *mockQuotaService) CheckQuotaReached(c context.Context, target string, params *quota.ScopeParameters) (bool, error) {
|
|
|
|
return m.reached, m.err
|
|
|
|
}
|
2022-07-15 11:06:44 -05:00
|
|
|
|
|
|
|
func (m *mockQuotaService) DeleteByUser(c context.Context, userID int64) error {
|
|
|
|
return m.err
|
|
|
|
}
|