@ -10,7 +10,6 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
"github.com/grafana/grafana/pkg/services/accesscontrol/database"
@ -24,7 +23,7 @@ import (
type accessControlGuardianTestCase struct {
desc string
dashboardID int64
dashUID string
permissions [ ] * accesscontrol . Permission
viewersCanEdit bool
expected bool
@ -33,8 +32,8 @@ type accessControlGuardianTestCase struct {
func TestAccessControlDashboardGuardian_CanSave ( t * testing . T ) {
tests := [ ] accessControlGuardianTestCase {
{
desc : "should be able to save with dashboard wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to save with dashboard wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
@ -44,8 +43,8 @@ func TestAccessControlDashboardGuardian_CanSave(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to save with folder wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to save with folder wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
@ -55,45 +54,45 @@ func TestAccessControlDashboardGuardian_CanSave(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to save with dashboard scope" ,
dashboardID : 1 ,
desc : "should be able to save with dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
} ,
expected : true ,
} ,
{
desc : "should be able to save with folder scope" ,
dashboardID : 1 ,
desc : "should be able to save with folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "folders:id:0 " ,
Scope : "folders:uid:general " ,
} ,
} ,
expected : true ,
} ,
{
desc : "should not be able to save with incorrect dashboard scope" ,
dashboardID : 1 ,
desc : "should not be able to save with incorrect dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "dashboards:id:10" ,
Scope : "dashboards:u id:10" ,
} ,
} ,
expected : false ,
} ,
{
desc : "should not be able to save with incorrect folder scope" ,
dashboardID : 1 ,
desc : "should not be able to save with incorrect folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "folders:id:10" ,
Scope : "folders:u id:10 0" ,
} ,
} ,
expected : false ,
@ -102,7 +101,7 @@ func TestAccessControlDashboardGuardian_CanSave(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , tt . dashboard ID , tt . permissions )
guardian , _ := setupAccessControlGuardianTest ( t , tt . dashU ID , tt . permissions )
can , err := guardian . CanSave ( )
require . NoError ( t , err )
@ -110,12 +109,11 @@ func TestAccessControlDashboardGuardian_CanSave(t *testing.T) {
} )
}
}
func TestAccessControlDashboardGuardian_CanEdit ( t * testing . T ) {
tests := [ ] accessControlGuardianTestCase {
{
desc : "should be able to edit with dashboard wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to edit with dashboard wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
@ -125,8 +123,8 @@ func TestAccessControlDashboardGuardian_CanEdit(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to edit with folder wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to edit with folder wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
@ -136,56 +134,56 @@ func TestAccessControlDashboardGuardian_CanEdit(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to edit with dashboard scope" ,
dashboardID : 1 ,
desc : "should be able to edit with dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
} ,
expected : true ,
} ,
{
desc : "should be able to edit with folder scope" ,
dashboardID : 1 ,
desc : "should be able to edit with folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "folders:id:0 " ,
Scope : "folders:uid:general " ,
} ,
} ,
expected : true ,
} ,
{
desc : "should not be able to edit with incorrect dashboard scope" ,
dashboardID : 1 ,
desc : "should not be able to edit with incorrect dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "dashboards:id:10" ,
Scope : "dashboards:u id:10" ,
} ,
} ,
expected : false ,
} ,
{
desc : "should not be able to edit with incorrect folder scope" ,
dashboardID : 1 ,
desc : "should not be able to edit with incorrect folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsWrite ,
Scope : "folders:id:10" ,
Scope : "folders:u id:10" ,
} ,
} ,
expected : false ,
} ,
{
desc : "should be able to edit with read action when viewer_can_edit is true" ,
dashboardID : 1 ,
desc : "should be able to edit with read action when viewer_can_edit is true" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
} ,
viewersCanEdit : true ,
@ -195,25 +193,23 @@ func TestAccessControlDashboardGuardian_CanEdit(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , tt . dashboard ID , tt . permissions )
guardian , _ := setupAccessControlGuardianTest ( t , tt . dashU ID , tt . permissions )
if tt . viewersCanEdit {
setting . ViewersCanEdit = true
defer func ( ) { setting . ViewersCanEdit = false } ( )
}
can , err := guardian . CanEdit ( )
require . NoError ( t , err )
assert . Equal ( t , tt . expected , can )
} )
}
}
func TestAccessControlDashboardGuardian_CanView ( t * testing . T ) {
tests := [ ] accessControlGuardianTestCase {
{
desc : "should be able to view with dashboard wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to view with dashboard wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
@ -223,8 +219,8 @@ func TestAccessControlDashboardGuardian_CanView(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to view with folder wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to view with folder wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
@ -234,45 +230,45 @@ func TestAccessControlDashboardGuardian_CanView(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to view with dashboard scope" ,
dashboardID : 1 ,
desc : "should be able to view with dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
} ,
expected : true ,
} ,
{
desc : "should be able to view with folder scope" ,
dashboardID : 1 ,
desc : "should be able to view with folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
Scope : "folders:id:0 " ,
Scope : "folders:uid:general " ,
} ,
} ,
expected : true ,
} ,
{
desc : "should not be able to view with incorrect dashboard scope" ,
dashboardID : 1 ,
desc : "should not be able to view with incorrect dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
Scope : "dashboards:id:10" ,
Scope : "dashboards:u id:10" ,
} ,
} ,
expected : false ,
} ,
{
desc : "should not be able to view with incorrect folder scope" ,
dashboardID : 1 ,
desc : "should not be able to view with incorrect folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsRead ,
Scope : "folders:id:10" ,
Scope : "folders:u id:10" ,
} ,
} ,
expected : false ,
@ -281,7 +277,7 @@ func TestAccessControlDashboardGuardian_CanView(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , tt . dashboard ID , tt . permissions )
guardian , _ := setupAccessControlGuardianTest ( t , tt . dashU ID , tt . permissions )
can , err := guardian . CanView ( )
require . NoError ( t , err )
@ -289,12 +285,11 @@ func TestAccessControlDashboardGuardian_CanView(t *testing.T) {
} )
}
}
func TestAccessControlDashboardGuardian_CanAdmin ( t * testing . T ) {
tests := [ ] accessControlGuardianTestCase {
{
desc : "should be able to admin with dashboard wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to admin with dashboard wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsPermissionsRead ,
@ -308,8 +303,8 @@ func TestAccessControlDashboardGuardian_CanAdmin(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to admin with folder wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to admin with folder wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsPermissionsRead ,
@ -323,61 +318,61 @@ func TestAccessControlDashboardGuardian_CanAdmin(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to admin with dashboard scope" ,
dashboardID : 1 ,
desc : "should be able to admin with dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsPermissionsRead ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
{
Action : accesscontrol . ActionDashboardsPermissionsWrite ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
} ,
expected : true ,
} ,
{
desc : "should be able to admin with folder scope" ,
dashboardID : 1 ,
desc : "should be able to admin with folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsPermissionsRead ,
Scope : "folders:id:0 " ,
Scope : "folders:uid:general " ,
} ,
{
Action : accesscontrol . ActionDashboardsPermissionsWrite ,
Scope : "folders:id:0 " ,
Scope : "folders:uid:general " ,
} ,
} ,
expected : true ,
} ,
{
desc : "should not be able to admin with incorrect dashboard scope" ,
dashboardID : 1 ,
desc : "should not be able to admin with incorrect dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsPermissionsRead ,
Scope : "dashboards:id:10" ,
Scope : "dashboards:u id:10" ,
} ,
{
Action : accesscontrol . ActionDashboardsPermissionsWrite ,
Scope : "dashboards:id:10" ,
Scope : "dashboards:u id:10" ,
} ,
} ,
expected : false ,
} ,
{
desc : "should not be able to admin with incorrect folder scope" ,
dashboardID : 1 ,
desc : "should not be able to admin with incorrect folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsPermissionsRead ,
Scope : "folders:id:10" ,
Scope : "folders:u id:10" ,
} ,
{
Action : accesscontrol . ActionDashboardsPermissionsWrite ,
Scope : "folders:id:10" ,
Scope : "folders:u id:10" ,
} ,
} ,
expected : false ,
@ -386,7 +381,7 @@ func TestAccessControlDashboardGuardian_CanAdmin(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , tt . dashboard ID , tt . permissions )
guardian , _ := setupAccessControlGuardianTest ( t , tt . dashU ID , tt . permissions )
can , err := guardian . CanAdmin ( )
require . NoError ( t , err )
@ -394,12 +389,11 @@ func TestAccessControlDashboardGuardian_CanAdmin(t *testing.T) {
} )
}
}
func TestAccessControlDashboardGuardian_CanDelete ( t * testing . T ) {
tests := [ ] accessControlGuardianTestCase {
{
desc : "should be able to delete with dashboard wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to delete with dashboard wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsDelete ,
@ -409,8 +403,8 @@ func TestAccessControlDashboardGuardian_CanDelete(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to delete with folder wildcard scope" ,
dashboardID : 1 ,
desc : "should be able to delete with folder wildcard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsDelete ,
@ -420,45 +414,45 @@ func TestAccessControlDashboardGuardian_CanDelete(t *testing.T) {
expected : true ,
} ,
{
desc : "should be able to delete with dashboard scope" ,
dashboardID : 1 ,
desc : "should be able to delete with dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsDelete ,
Scope : "dashboards:id:1" ,
Scope : "dashboards:u id:1" ,
} ,
} ,
expected : true ,
} ,
{
desc : "should be able to delete with folder scope" ,
dashboardID : 1 ,
desc : "should be able to delete with folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsDelete ,
Scope : "folders:id:0 " ,
Scope : "folders:uid:general " ,
} ,
} ,
expected : true ,
} ,
{
desc : "should not be able to delete with incorrect dashboard scope" ,
dashboardID : 1 ,
desc : "should not be able to delete with incorrect dashboard scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsDelete ,
Scope : "dashboards:id:10" ,
Scope : "dashboards:u id:10" ,
} ,
} ,
expected : false ,
} ,
{
desc : "should not be able to delete with incorrect folder scope" ,
dashboardID : 1 ,
desc : "should not be able to delete with incorrect folder scope" ,
dashUID : "1" ,
permissions : [ ] * accesscontrol . Permission {
{
Action : accesscontrol . ActionDashboardsDelete ,
Scope : "folders:id:10" ,
Scope : "folders:u id:10" ,
} ,
} ,
expected : false ,
@ -467,7 +461,7 @@ func TestAccessControlDashboardGuardian_CanDelete(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , tt . dashboard ID , tt . permissions )
guardian , _ := setupAccessControlGuardianTest ( t , tt . dashU ID , tt . permissions )
can , err := guardian . CanDelete ( )
require . NoError ( t , err )
@ -487,18 +481,18 @@ type accessControlGuardianCanCreateTestCase struct {
func TestAccessControlDashboardGuardian_CanCreate ( t * testing . T ) {
tests := [ ] accessControlGuardianCanCreateTestCase {
{
desc : "should be able to create dashboard in folder 0 " ,
desc : "should be able to create dashboard in general folder" ,
isFolder : false ,
folderID : 0 ,
permissions : [ ] * accesscontrol . Permission {
{ Action : accesscontrol . ActionDashboardsCreate , Scope : "folders:id:0 " } ,
{ Action : accesscontrol . ActionDashboardsCreate , Scope : "folders:uid:general " } ,
} ,
expected : true ,
} ,
{
desc : "should be able to create dashboard in any folder" ,
isFolder : false ,
folderID : 10 0,
folderID : 0 ,
permissions : [ ] * accesscontrol . Permission {
{ Action : accesscontrol . ActionDashboardsCreate , Scope : "folders:*" } ,
} ,
@ -507,7 +501,7 @@ func TestAccessControlDashboardGuardian_CanCreate(t *testing.T) {
{
desc : "should not be able to create dashboard without permissions" ,
isFolder : false ,
folderID : 10 0,
folderID : 0 ,
permissions : [ ] * accesscontrol . Permission { } ,
expected : false ,
} ,
@ -523,7 +517,7 @@ func TestAccessControlDashboardGuardian_CanCreate(t *testing.T) {
{
desc : "should not be able to create folders without permissions" ,
isFolder : true ,
folderID : 10 0,
folderID : 0 ,
permissions : [ ] * accesscontrol . Permission { } ,
expected : false ,
} ,
@ -531,7 +525,7 @@ func TestAccessControlDashboardGuardian_CanCreate(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , 0 , tt . permissions )
guardian , _ := setupAccessControlGuardianTest ( t , "0" , tt . permissions )
can , err := guardian . CanCreate ( tt . folderID , tt . isFolder )
require . NoError ( t , err )
@ -563,16 +557,14 @@ func TestAccessControlDashboardGuardian_GetHiddenACL(t *testing.T) {
for _ , tt := range tests {
t . Run ( tt . desc , func ( t * testing . T ) {
guardian := setupAccessControlGuardianTest ( t , 1 , nil )
guardian , _ := setupAccessControlGuardianTest ( t , "1" , nil )
mocked := accesscontrolmock . NewPermissionsServicesMock ( )
guardian . permissionServices = mocked
mocked . Dashboards . On ( "MapActions" , mock . Anything ) . Return ( "View" )
mocked . Dashboards . On ( "GetPermissions" , mock . Anything , mock . Anything , mock . Anything ) . Return ( tt . permissions , nil )
cfg := setting . NewCfg ( )
cfg . HiddenUsers = tt . hiddenUsers
permissions , err := guardian . GetHiddenACL ( cfg )
require . NoError ( t , err )
var hiddenUserNames [ ] string
@ -587,21 +579,24 @@ func TestAccessControlDashboardGuardian_GetHiddenACL(t *testing.T) {
}
}
func setupAccessControlGuardianTest ( t * testing . T , dashID int64 , permissions [ ] * accesscontrol . Permission ) * AccessControlDashboardGuardian {
func setupAccessControlGuardianTest ( t * testing . T , uid string , permissions [ ] * accesscontrol . Permission ) ( * AccessControlDashboardGuardian , * models . Dashboard ) {
t . Helper ( )
store := sqlstore . InitTestDB ( t )
toSave := models . NewDashboard ( uid )
toSave . SetUid ( uid )
// seed dashboard
_ , err := dashdb . ProvideDashboardStore ( store ) . SaveDashboard ( models . SaveDashboardCommand {
Dashboard : & simplejson . Json { } ,
dash , err := dashdb . ProvideDashboardStore ( store ) . SaveDashboard ( models . SaveDashboardCommand {
Dashboard : toSave . Data ,
UserId : 1 ,
OrgId : 1 ,
FolderId : 0 ,
} )
require . NoError ( t , err )
ac := accesscontrolmock . New ( ) . WithPermissions ( permissions )
services , err := ossaccesscontrol . ProvidePermissionsServices ( setting . NewCfg ( ) , routing . NewRouteRegister ( ) , store , ac , database . ProvideService ( store ) )
require . NoError ( t , err )
return NewAccessControlDashboardGuardian ( context . Background ( ) , dashID , & models . SignedInUser { OrgId : 1 } , store , ac , services )
return NewAccessControlDashboardGuardian ( context . Background ( ) , dash . Id , & models . SignedInUser { OrgId : 1 } , store , ac , services ) , dash
}