1
1
package accesscontrol_test
2
2
3
3
import (
4
- "context"
5
4
"fmt"
6
5
"net/http"
7
6
"net/http/httptest"
@@ -13,7 +12,6 @@ import (
13
12
"github.com/grafana/authlib/claims"
14
13
"github.com/grafana/grafana/pkg/services/accesscontrol"
15
14
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
16
- "github.com/grafana/grafana/pkg/services/accesscontrol/actest"
17
15
"github.com/grafana/grafana/pkg/services/authn"
18
16
"github.com/grafana/grafana/pkg/services/authn/authntest"
19
17
"github.com/grafana/grafana/pkg/services/authz/zanzana"
@@ -22,7 +20,6 @@ import (
22
20
"github.com/grafana/grafana/pkg/services/team"
23
21
"github.com/grafana/grafana/pkg/services/team/teamtest"
24
22
"github.com/grafana/grafana/pkg/services/user"
25
- "github.com/grafana/grafana/pkg/services/user/usertest"
26
23
"github.com/grafana/grafana/pkg/web"
27
24
)
28
25
@@ -37,8 +34,8 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
37
34
orgIDGetter accesscontrol.OrgIDGetter
38
35
evaluator accesscontrol.Evaluator
39
36
accessControl accesscontrol.AccessControl
40
- acService accesscontrol. Service
41
- userCache user. Service
37
+ userIdentities [] * authn. Identity
38
+ authnErrors [] error
42
39
ctxSignedInUser * user.SignedInUser
43
40
teamService team.Service
44
41
expectedStatus int
@@ -48,7 +45,6 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
48
45
targetOrgId : accesscontrol .GlobalOrgID ,
49
46
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
50
47
accessControl : ac ,
51
- userCache : & usertest.FakeUserService {},
52
48
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
53
49
targerOrgPermissions : []accesscontrol.Permission {{Action : "users:read" , Scope : "users:*" }},
54
50
teamService : & teamtest.FakeService {},
@@ -60,7 +56,6 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
60
56
targerOrgPermissions : []accesscontrol.Permission {{Action : "users:read" , Scope : "users:*" }},
61
57
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
62
58
accessControl : ac ,
63
- userCache : & usertest.FakeUserService {},
64
59
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
65
60
teamService : & teamtest.FakeService {},
66
61
expectedStatus : http .StatusOK ,
@@ -71,7 +66,6 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
71
66
targerOrgPermissions : []accesscontrol.Permission {},
72
67
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
73
68
accessControl : ac ,
74
- userCache : & usertest.FakeUserService {},
75
69
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {}},
76
70
teamService : & teamtest.FakeService {},
77
71
expectedStatus : http .StatusForbidden ,
@@ -82,7 +76,6 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
82
76
targerOrgPermissions : []accesscontrol.Permission {{Action : "users:read" , Scope : "users:*" }},
83
77
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
84
78
accessControl : ac ,
85
- userCache : & usertest.FakeUserService {},
86
79
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
87
80
teamService : & teamtest.FakeService {},
88
81
expectedStatus : http .StatusOK ,
@@ -93,47 +86,19 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
93
86
targerOrgPermissions : []accesscontrol.Permission {},
94
87
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
95
88
accessControl : ac ,
96
- userCache : & usertest.FakeUserService {},
97
89
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
98
90
teamService : & teamtest.FakeService {},
99
91
expectedStatus : http .StatusForbidden ,
100
92
},
101
- {
102
- name : "should return 403 when user org ID doesn't match and user does not exist in org 2" ,
103
- targetOrgId : 2 ,
104
- targerOrgPermissions : []accesscontrol.Permission {},
105
- evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
106
- accessControl : ac ,
107
- userCache : & usertest.FakeUserService {ExpectedError : fmt .Errorf ("user not found" )},
108
- ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
109
- teamService : & teamtest.FakeService {},
110
- expectedStatus : http .StatusForbidden ,
111
- },
112
- {
113
- name : "should return 403 early when api key org ID doesn't match" ,
114
- targetOrgId : 2 ,
115
- targerOrgPermissions : []accesscontrol.Permission {},
116
- evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
117
- accessControl : ac ,
118
- userCache : & usertest.FakeUserService {},
119
- ctxSignedInUser : & user.SignedInUser {ApiKeyID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
120
- teamService : & teamtest.FakeService {},
121
- expectedStatus : http .StatusForbidden ,
122
- },
123
93
{
124
94
name : "should fetch user permissions when org ID doesn't match" ,
125
95
targetOrgId : 2 ,
126
96
targerOrgPermissions : []accesscontrol.Permission {{Action : "users:read" , Scope : "users:*" }},
127
97
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
128
98
accessControl : ac ,
129
99
teamService : & teamtest.FakeService {},
130
- userCache : & usertest.FakeUserService {
131
- GetSignedInUserFn : func (ctx context.Context , query * user.GetSignedInUserQuery ) (* user.SignedInUser , error ) {
132
- return & user.SignedInUser {UserID : 1 , OrgID : 2 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}}, nil
133
- },
134
- },
135
- ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:write" : {"users:*" }}}},
136
- expectedStatus : http .StatusOK ,
100
+ ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:write" : {"users:*" }}}},
101
+ expectedStatus : http .StatusOK ,
137
102
},
138
103
{
139
104
name : "fails to fetch user permissions when org ID doesn't match" ,
@@ -142,16 +107,9 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
142
107
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
143
108
accessControl : ac ,
144
109
teamService : & teamtest.FakeService {},
145
- acService : & actest.FakeService {
146
- ExpectedErr : fmt .Errorf ("failed to get user permissions" ),
147
- },
148
- userCache : & usertest.FakeUserService {
149
- GetSignedInUserFn : func (ctx context.Context , query * user.GetSignedInUserQuery ) (* user.SignedInUser , error ) {
150
- return & user.SignedInUser {UserID : 1 , OrgID : 2 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}}, nil
151
- },
152
- },
153
- ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
154
- expectedStatus : http .StatusForbidden ,
110
+ authnErrors : []error {fmt .Errorf ("failed to get user permissions" )},
111
+ ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
112
+ expectedStatus : http .StatusForbidden ,
155
113
},
156
114
{
157
115
name : "unable to get target org" ,
@@ -160,24 +118,35 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
160
118
},
161
119
evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
162
120
accessControl : ac ,
163
- userCache : & usertest.FakeUserService {},
164
121
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:read" : {"users:*" }}}},
165
122
teamService : & teamtest.FakeService {},
166
123
expectedStatus : http .StatusForbidden ,
167
124
},
168
125
{
169
- name : "should fetch global user permissions when user is not a member of the target org" ,
170
- targetOrgId : 2 ,
171
- targerOrgPermissions : []accesscontrol.Permission {{Action : "users:read" , Scope : "users:*" }},
172
- evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
173
- accessControl : ac ,
174
- userCache : & usertest.FakeUserService {
175
- GetSignedInUserFn : func (ctx context.Context , query * user.GetSignedInUserQuery ) (* user.SignedInUser , error ) {
176
- return & user.SignedInUser {UserID : 1 , OrgID : - 1 , Permissions : map [int64 ]map [string ][]string {}}, nil
177
- },
126
+ name : "should fetch global user permissions when user is not a member of the target org" ,
127
+ targetOrgId : 2 ,
128
+ evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
129
+ accessControl : ac ,
130
+ ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:write" : {"users:*" }}}},
131
+ userIdentities : []* authn.Identity {
132
+ {ID : "1" , OrgID : - 1 , Permissions : map [int64 ]map [string ][]string {}},
133
+ {ID : "1" , OrgID : accesscontrol .GlobalOrgID , Permissions : map [int64 ]map [string ][]string {accesscontrol .GlobalOrgID : {"users:read" : {"users:*" }}}},
178
134
},
135
+ authnErrors : []error {nil , nil },
136
+ expectedStatus : http .StatusOK ,
137
+ },
138
+ {
139
+ name : "should fail if user is not a member of the target org and doesn't have the right permissions globally" ,
140
+ targetOrgId : 2 ,
141
+ evaluator : accesscontrol .EvalPermission ("users:read" , "users:*" ),
142
+ accessControl : ac ,
179
143
ctxSignedInUser : & user.SignedInUser {UserID : 1 , OrgID : 1 , Permissions : map [int64 ]map [string ][]string {1 : {"users:write" : {"users:*" }}}},
180
- expectedStatus : http .StatusOK ,
144
+ userIdentities : []* authn.Identity {
145
+ {ID : "1" , OrgID : - 1 , Permissions : map [int64 ]map [string ][]string {}},
146
+ {ID : "1" , OrgID : accesscontrol .GlobalOrgID , Permissions : map [int64 ]map [string ][]string {accesscontrol .GlobalOrgID : {"folders:read" : {"folders:*" }}}},
147
+ },
148
+ authnErrors : []error {nil , nil },
149
+ expectedStatus : http .StatusForbidden ,
181
150
},
182
151
}
183
152
@@ -194,9 +163,16 @@ func TestAuthorizeInOrgMiddleware(t *testing.T) {
194
163
Permissions : map [int64 ]map [string ][]string {},
195
164
}
196
165
expectedIdentity .Permissions [tc .targetOrgId ] = accesscontrol .GroupScopesByAction (tc .targerOrgPermissions )
166
+ var expectedErr error
167
+ if len (tc .authnErrors ) > 0 {
168
+ expectedErr = tc .authnErrors [0 ]
169
+ }
197
170
198
171
authnService := & authntest.FakeService {
199
- ExpectedIdentity : expectedIdentity ,
172
+ ExpectedIdentity : expectedIdentity ,
173
+ ExpectedIdentities : tc .userIdentities ,
174
+ ExpectedErr : expectedErr ,
175
+ ExpectedErrs : tc .authnErrors ,
200
176
}
201
177
202
178
var orgIDGetter accesscontrol.OrgIDGetter
0 commit comments