RBAC: remove redundant role name field from plugin role registrations (#58166)

* RBAC: Remove name from role registration

* Inline accesscontrol service

* test fix

* use fmt

Co-Authored-By: marefr <marcus.efraimsson@gmail.com>

Co-authored-by: marefr <marcus.efraimsson@gmail.com>
This commit is contained in:
Gabriel MABILLE
2022-11-15 09:51:40 +01:00
committed by GitHub
co-authored by marefr
parent 80e80221b9
commit d999b5bda0
10 changed files with 37 additions and 48 deletions
@@ -1,6 +1,7 @@
package pluginutils
import (
"fmt"
"strings"
"github.com/grafana/grafana/pkg/plugins"
@@ -34,14 +35,14 @@ func ValidatePluginRole(pluginID string, role ac.RoleDTO) error {
return ValidatePluginPermissions(pluginID, role.Permissions)
}
func ToRegistrations(pluginName string, regs []plugins.RoleRegistration) []ac.RoleRegistration {
func ToRegistrations(pluginID, pluginName string, regs []plugins.RoleRegistration) []ac.RoleRegistration {
res := make([]ac.RoleRegistration, 0, len(regs))
for i := range regs {
res = append(res, ac.RoleRegistration{
Role: ac.RoleDTO{
Version: 1,
Name: regs[i].Role.Name,
DisplayName: regs[i].Role.DisplayName,
Name: roleName(pluginID, regs[i].Role.Name),
DisplayName: regs[i].Role.Name,
Description: regs[i].Role.Description,
Group: pluginName,
Permissions: toPermissions(regs[i].Role.Permissions),
@@ -53,6 +54,10 @@ func ToRegistrations(pluginName string, regs []plugins.RoleRegistration) []ac.Ro
return res
}
func roleName(pluginID, roleName string) string {
return fmt.Sprintf("%v%v:%v", ac.PluginRolePrefix, pluginID, strings.Replace(strings.ToLower(roleName), " ", "-", -1))
}
func toPermissions(perms []plugins.Permission) []ac.Permission {
res := make([]ac.Permission, 0, len(perms))
for i := range perms {
@@ -24,8 +24,7 @@ func TestToRegistrations(t *testing.T) {
regs: []plugins.RoleRegistration{
{
Role: plugins.Role{
Name: "test:name",
DisplayName: "Test",
Name: "Tester",
Description: "Test",
Permissions: []plugins.Permission{
{Action: "test:action"},
@@ -36,7 +35,7 @@ func TestToRegistrations(t *testing.T) {
},
{
Role: plugins.Role{
Name: "test:name",
Name: "Admin Validator",
Permissions: []plugins.Permission{},
},
},
@@ -45,10 +44,10 @@ func TestToRegistrations(t *testing.T) {
{
Role: ac.RoleDTO{
Version: 1,
Name: "test:name",
DisplayName: "Test",
Name: ac.PluginRolePrefix + "plugin-id:tester",
DisplayName: "Tester",
Description: "Test",
Group: "PluginName",
Group: "Plugin Name",
Permissions: []ac.Permission{
{Action: "test:action"},
{Action: "test:action", Scope: "test:scope"},
@@ -60,8 +59,9 @@ func TestToRegistrations(t *testing.T) {
{
Role: ac.RoleDTO{
Version: 1,
Name: "test:name",
Group: "PluginName",
Name: ac.PluginRolePrefix + "plugin-id:admin-validator",
DisplayName: "Admin Validator",
Group: "Plugin Name",
Permissions: []ac.Permission{},
OrgID: ac.GlobalOrgID,
},
@@ -71,7 +71,7 @@ func TestToRegistrations(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ToRegistrations("PluginName", tt.regs)
got := ToRegistrations("plugin-id", "Plugin Name", tt.regs)
require.Equal(t, tt.want, got)
})
}