Auth: Add access token to in-proc communication and ServiceIdentity (#98926)

Use fake access token for in-proc grpc and add ServiceIdentity 
---------

Co-authored-by: gamab <gabriel.mabille@grafana.com>
Co-authored-by: Karl Persson <23356117+kalleep@users.noreply.github.com>
This commit is contained in:
Misi
2025-01-24 14:03:23 +01:00
committed by GitHub
co-authored by gamab Karl Persson
parent eb2d276a42
commit 437b7a565d
12 changed files with 193 additions and 179 deletions
+4 -21
View File
@@ -6,7 +6,6 @@ import (
"strconv"
"time"
claims "github.com/grafana/authlib/types"
"github.com/grafana/grafana/pkg/apimachinery/identity"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/dashboards"
@@ -45,7 +44,7 @@ type sqlStatsService struct {
func (ss *sqlStatsService) getDashboardCount(ctx context.Context, orgs []*org.OrgDTO) (int64, error) {
count := int64(0)
for _, org := range orgs {
ctx = identity.WithRequester(ctx, getStatsRequester(org.ID))
ctx, _ = identity.WithServiceIdentitiy(ctx, org.ID)
dashsCount, err := ss.dashSvc.CountDashboardsInOrg(ctx, org.ID)
if err != nil {
return 0, err
@@ -59,7 +58,7 @@ func (ss *sqlStatsService) getDashboardCount(ctx context.Context, orgs []*org.Or
func (ss *sqlStatsService) getTagCount(ctx context.Context, orgs []*org.OrgDTO) (int64, error) {
total := 0
for _, org := range orgs {
ctx = identity.WithRequester(ctx, getStatsRequester(org.ID))
ctx, _ = identity.WithServiceIdentitiy(ctx, org.ID)
tags, err := ss.dashSvc.GetDashboardTags(ctx, &dashboards.GetDashboardTagsQuery{
OrgID: org.ID,
})
@@ -75,11 +74,10 @@ func (ss *sqlStatsService) getTagCount(ctx context.Context, orgs []*org.OrgDTO)
func (ss *sqlStatsService) getFolderCount(ctx context.Context, orgs []*org.OrgDTO) (int64, error) {
total := 0
for _, org := range orgs {
backgroundUser := getStatsRequester(org.ID)
ctx = identity.WithRequester(ctx, backgroundUser)
ctx, ident := identity.WithServiceIdentitiy(ctx, org.ID)
folders, err := ss.folderSvc.GetFolders(ctx, folder.GetFoldersQuery{
OrgID: org.ID,
SignedInUser: backgroundUser,
SignedInUser: ident,
})
if err != nil {
return 0, err
@@ -438,18 +436,3 @@ func addToStats(base stats.UserStats, role org.RoleType, count int64) stats.User
return base
}
func getStatsRequester(orgId int64) *identity.StaticRequester {
return &identity.StaticRequester{
Type: claims.TypeServiceAccount,
UserID: 1,
OrgID: orgId,
Name: "stats-requester",
Login: "stats-requester",
Permissions: map[int64]map[string][]string{
orgId: {
"*": {"*"},
},
},
}
}