From 7784782512b2509d27f3bd997397ccdc5b0447d2 Mon Sep 17 00:00:00 2001 From: Jo Date: Thu, 16 Mar 2023 15:34:43 +0000 Subject: [PATCH] AuthN: Add password and login form stats (#64868) * add password and login form stats * fix method handler * fix viewers can edit test --- pkg/services/authn/authnimpl/usage_stats.go | 2 + .../authn/authnimpl/usage_stats_test.go | 44 +++++++++++++++++++ pkg/services/authn/authntest/fake.go | 5 +++ 3 files changed, 51 insertions(+) create mode 100644 pkg/services/authn/authnimpl/usage_stats_test.go diff --git a/pkg/services/authn/authnimpl/usage_stats.go b/pkg/services/authn/authnimpl/usage_stats.go index d945602e4c1..aa086adefe2 100644 --- a/pkg/services/authn/authnimpl/usage_stats.go +++ b/pkg/services/authn/authnimpl/usage_stats.go @@ -16,6 +16,8 @@ func (s *Service) getUsageStats(ctx context.Context) (map[string]interface{}, er authTypes["auth_proxy"] = s.cfg.AuthProxyEnabled authTypes["anonymous"] = s.cfg.AnonymousEnabled authTypes["jwt"] = s.cfg.JWTAuthEnabled + authTypes["grafana_password"] = !s.cfg.DisableLogin + authTypes["login_form"] = !s.cfg.DisableLoginForm for authType, enabled := range authTypes { enabledValue := 0 diff --git a/pkg/services/authn/authnimpl/usage_stats_test.go b/pkg/services/authn/authnimpl/usage_stats_test.go new file mode 100644 index 00000000000..4e6b2644032 --- /dev/null +++ b/pkg/services/authn/authnimpl/usage_stats_test.go @@ -0,0 +1,44 @@ +package authnimpl + +import ( + "context" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/grafana/grafana/pkg/services/authn/authntest" +) + +func TestService_getUsageStats(t *testing.T) { + svc := setupTests(t, func(svc *Service) { + svc.RegisterClient( + &authntest.FakeClient{ExpectedErr: nil, ExpectedName: "test", ExpectedPriority: 1, ExpectedStats: map[string]interface{}{"stats.test.enabled.count": 1}}) + svc.RegisterClient( + &authntest.FakeClient{ExpectedErr: errCantAuthenticateReq, ExpectedName: "failing", ExpectedPriority: 1, ExpectedStats: nil}) + }) + + svc.cfg.DisableLoginForm = false + svc.cfg.DisableLogin = false + svc.cfg.BasicAuthEnabled = true + svc.cfg.AuthProxyEnabled = true + svc.cfg.JWTAuthEnabled = true + svc.cfg.LDAPEnabled = true + svc.cfg.EditorsCanAdmin = true + svc.cfg.ViewersCanEdit = true + + got, err := svc.getUsageStats(context.Background()) + require.NoError(t, err) + want := map[string]interface{}{"stats.auth_enabled.anonymous.count": 0, + "stats.auth_enabled.auth_proxy.count": 1, + "stats.auth_enabled.basic_auth.count": 1, + "stats.auth_enabled.grafana_password.count": 1, + "stats.auth_enabled.jwt.count": 1, + "stats.auth_enabled.ldap.count": 1, + "stats.auth_enabled.login_form.count": 1, + "stats.authz.editors_can_admin.count": 1, + "stats.authz.viewers_can_edit.count": 1, + "stats.test.enabled.count": 1, + } + + require.Equal(t, want, got) +} diff --git a/pkg/services/authn/authntest/fake.go b/pkg/services/authn/authntest/fake.go index a3e119ead9e..dce4ff9bfc6 100644 --- a/pkg/services/authn/authntest/fake.go +++ b/pkg/services/authn/authntest/fake.go @@ -18,6 +18,7 @@ type FakeClient struct { ExpectedTest bool ExpectedPriority uint ExpectedIdentity *authn.Identity + ExpectedStats map[string]interface{} } func (f *FakeClient) Name() string { @@ -36,6 +37,10 @@ func (f *FakeClient) Priority() uint { return f.ExpectedPriority } +func (f *FakeClient) UsageStatFn(ctx context.Context) (map[string]interface{}, error) { + return f.ExpectedStats, f.ExpectedErr +} + var _ authn.PasswordClient = new(FakePasswordClient) type FakePasswordClient struct {