From 55f28124665e73f0ced273f854fe1eabfe225a8c Mon Sep 17 00:00:00 2001 From: Karl Persson <23356117+kalleep@users.noreply.github.com> Date: Mon, 24 Mar 2025 16:00:07 +0100 Subject: [PATCH] Annotations: Fix annotations scope resolver (#102612) * Fix annotations scope resolver --- pkg/api/annotations.go | 30 ++++++++++++++++-------------- pkg/api/annotations_test.go | 4 ++-- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/pkg/api/annotations.go b/pkg/api/annotations.go index 5873504d7c9..d0e56d1c052 100644 --- a/pkg/api/annotations.go +++ b/pkg/api/annotations.go @@ -633,23 +633,25 @@ func AnnotationTypeScopeResolver(annotationsRepo annotations.Repository, feature if annotation.DashboardID == 0 { return []string{accesscontrol.ScopeAnnotationsTypeOrganization}, nil } else { - dashboard, err := dashSvc.GetDashboard(ctx, &dashboards.GetDashboardQuery{ID: annotation.DashboardID, OrgID: orgID}) - if err != nil { - return nil, err - } - scopes := []string{dashboards.ScopeDashboardsProvider.GetResourceScopeUID(dashboard.UID)} - // Append dashboard parent scopes if dashboard is in a folder or the general scope if dashboard is not in a folder - if dashboard.FolderUID != "" { - scopes = append(scopes, dashboards.ScopeFoldersProvider.GetResourceScopeUID(dashboard.FolderUID)) - inheritedScopes, err := dashboards.GetInheritedScopes(ctx, orgID, dashboard.FolderUID, folderSvc) + return identity.WithServiceIdentityFn(ctx, orgID, func(ctx context.Context) ([]string, error) { + dashboard, err := dashSvc.GetDashboard(ctx, &dashboards.GetDashboardQuery{ID: annotation.DashboardID, OrgID: orgID}) if err != nil { return nil, err } - scopes = append(scopes, inheritedScopes...) - } else { - scopes = append(scopes, dashboards.ScopeFoldersProvider.GetResourceScopeUID(folder.GeneralFolderUID)) - } - return scopes, nil + scopes := []string{dashboards.ScopeDashboardsProvider.GetResourceScopeUID(dashboard.UID)} + // Append dashboard parent scopes if dashboard is in a folder or the general scope if dashboard is not in a folder + if dashboard.FolderUID != "" { + scopes = append(scopes, dashboards.ScopeFoldersProvider.GetResourceScopeUID(dashboard.FolderUID)) + inheritedScopes, err := dashboards.GetInheritedScopes(ctx, orgID, dashboard.FolderUID, folderSvc) + if err != nil { + return nil, err + } + scopes = append(scopes, inheritedScopes...) + } else { + scopes = append(scopes, dashboards.ScopeFoldersProvider.GetResourceScopeUID(folder.GeneralFolderUID)) + } + return scopes, nil + }) } }) } diff --git a/pkg/api/annotations_test.go b/pkg/api/annotations_test.go index fa4f119f13b..6eaf4317be6 100644 --- a/pkg/api/annotations_test.go +++ b/pkg/api/annotations_test.go @@ -436,8 +436,8 @@ func TestService_AnnotationTypeScopeResolver(t *testing.T) { dashSvc := &dashboards.FakeDashboardService{} rootDash := &dashboards.Dashboard{ID: 1, OrgID: 1, UID: rootDashUID} folderDash := &dashboards.Dashboard{ID: 2, OrgID: 1, UID: folderDashUID, FolderUID: folderUID} - dashSvc.On("GetDashboard", context.Background(), &dashboards.GetDashboardQuery{ID: rootDash.ID, OrgID: 1}).Return(rootDash, nil) - dashSvc.On("GetDashboard", context.Background(), &dashboards.GetDashboardQuery{ID: folderDash.ID, OrgID: 1}).Return(folderDash, nil) + dashSvc.On("GetDashboard", mock.Anything, &dashboards.GetDashboardQuery{ID: rootDash.ID, OrgID: 1}).Return(rootDash, nil) + dashSvc.On("GetDashboard", mock.Anything, &dashboards.GetDashboardQuery{ID: folderDash.ID, OrgID: 1}).Return(folderDash, nil) rootDashboardAnnotation := annotations.Item{ID: 1, DashboardID: rootDash.ID} folderDashboardAnnotation := annotations.Item{ID: 3, DashboardID: folderDash.ID}