Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.

Commit f5da2d4

Browse files
authored
Revert "RBAC - Remove built-in role assignments client (#79)" (#89)
This reverts commit 2f10df9.
1 parent 2f10df9 commit f5da2d4

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

builtin_role_assignments.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
const baseURL = "/api/access-control/builtin-roles"
10+
11+
type BuiltInRoleAssignment struct {
12+
BuiltinRole string `json:"builtInRole"`
13+
RoleUID string `json:"roleUid"`
14+
Global bool `json:"global"`
15+
}
16+
17+
// GetBuiltInRoleAssignments gets all built-in role assignments. Available only in Grafana Enterprise 8.+.
18+
func (c *Client) GetBuiltInRoleAssignments() (map[string][]*Role, error) {
19+
br := make(map[string][]*Role)
20+
err := c.request("GET", baseURL, nil, nil, &br)
21+
if err != nil {
22+
return nil, err
23+
}
24+
return br, nil
25+
}
26+
27+
// NewBuiltInRoleAssignment creates a new built-in role assignment. Available only in Grafana Enterprise 8.+.
28+
func (c *Client) NewBuiltInRoleAssignment(builtInRoleAssignment BuiltInRoleAssignment) (*BuiltInRoleAssignment, error) {
29+
body, err := json.Marshal(builtInRoleAssignment)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
br := &BuiltInRoleAssignment{}
35+
36+
err = c.request("POST", baseURL, nil, bytes.NewBuffer(body), &br)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return br, err
42+
}
43+
44+
// DeleteBuiltInRoleAssignment remove the built-in role assignments. Available only in Grafana Enterprise 8.+.
45+
func (c *Client) DeleteBuiltInRoleAssignment(builtInRole BuiltInRoleAssignment) error {
46+
data, err := json.Marshal(builtInRole)
47+
if err != nil {
48+
return err
49+
}
50+
51+
qp := map[string][]string{
52+
"global": {fmt.Sprint(builtInRole.Global)},
53+
}
54+
url := fmt.Sprintf("%s/%s/roles/%s", baseURL, builtInRole.BuiltinRole, builtInRole.RoleUID)
55+
err = c.request("DELETE", url, qp, bytes.NewBuffer(data), nil)
56+
57+
return err
58+
}

builtin_role_assignments_test.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package gapi
2+
3+
import (
4+
"testing"
5+
)
6+
7+
const (
8+
newBuiltInRoleAssignmentResponse = `
9+
{
10+
"message": "Built-in role grant added"
11+
}
12+
`
13+
getBuiltInRoleAssignmentsResponse = `
14+
{
15+
"Grafana Admin": [
16+
{
17+
"version": 1,
18+
"uid": "tJTyTNqMk",
19+
"name": "grafana:roles:users:admin:read",
20+
"description": "",
21+
"global": true
22+
}
23+
],
24+
"Viewer": [
25+
{
26+
"version": 2,
27+
"uid": "tJTyTNqMk1",
28+
"name": "custom:reports:editor",
29+
"description": "Role to allow users to create/read reports",
30+
"global": false
31+
}
32+
]
33+
}
34+
`
35+
36+
removeBuiltInRoleAssignmentResponse = `
37+
{
38+
"message": "Built-in role grant removed"
39+
}
40+
`
41+
)
42+
43+
func TestNewBuiltInRoleAssignment(t *testing.T) {
44+
server, client := gapiTestTools(t, 200, newBuiltInRoleAssignmentResponse)
45+
t.Cleanup(func() {
46+
server.Close()
47+
})
48+
49+
br := BuiltInRoleAssignment{
50+
Global: false,
51+
RoleUID: "test:policy",
52+
BuiltinRole: "Viewer",
53+
}
54+
55+
_, err := client.NewBuiltInRoleAssignment(br)
56+
if err != nil {
57+
t.Fatal(err)
58+
}
59+
}
60+
61+
func TestGetBuiltInRoleAssignments(t *testing.T) {
62+
server, client := gapiTestTools(t, 200, getBuiltInRoleAssignmentsResponse)
63+
t.Cleanup(func() {
64+
server.Close()
65+
})
66+
67+
resp, err := client.GetBuiltInRoleAssignments()
68+
69+
if err != nil {
70+
t.Error(err)
71+
}
72+
73+
expected := map[string][]*Role{
74+
"Grafana Admin": {
75+
{
76+
Version: 1,
77+
Global: true,
78+
Name: "grafana:roles:users:admin:read",
79+
UID: "tJTyTNqMk",
80+
Description: "",
81+
},
82+
},
83+
"Viewer": {
84+
{
85+
Version: 2,
86+
Global: false,
87+
Name: "custom:reports:editor",
88+
UID: "tJTyTNqMk1",
89+
Description: "Role to allow users to create/read reports",
90+
},
91+
},
92+
}
93+
94+
if len(expected["Viewer"]) != len(resp["Viewer"]) || len(expected["Grafana Admin"]) != len(resp["Grafana Admin"]) {
95+
t.Error("Unexpected built-in role assignments.")
96+
}
97+
}
98+
99+
func TestDeleteBuiltInRoleAssignment(t *testing.T) {
100+
server, client := gapiTestTools(t, 200, removeBuiltInRoleAssignmentResponse)
101+
t.Cleanup(func() {
102+
server.Close()
103+
})
104+
105+
br := BuiltInRoleAssignment{
106+
Global: false,
107+
RoleUID: "test:policy",
108+
BuiltinRole: "Viewer",
109+
}
110+
err := client.DeleteBuiltInRoleAssignment(br)
111+
if err != nil {
112+
t.Error(err)
113+
}
114+
}

0 commit comments

Comments
 (0)