@@ -172,12 +172,30 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
172172 sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
173173 want : true ,
174174 },
175+ {
176+ name : "simple matching user with warrants and from this cluster" ,
177+ user : & user.DefaultInfo {Name : "user-a" , Extra : map [string ][]string {
178+ WarrantExtraKey : {`{"user":"user-b"}` },
179+ ClusterExtraKey : {"this" },
180+ }},
181+ sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
182+ want : true , // user is subject
183+ },
175184 {
176185 name : "simple non-matching user with matching warrants" ,
177186 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"user-a"}` }}},
178187 sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
179188 want : true ,
180189 },
190+ {
191+ name : "simple non-matching user with matching warrants but with cluster-name" ,
192+ user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {
193+ WarrantExtraKey : {`{"user":"user-a"}` },
194+ ClusterExtraKey : {"this" },
195+ }},
196+ sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
197+ want : false , // Warrants are ineffective on users with cluster
198+ },
181199 {
182200 name : "simple non-matching user with non-matching warrants" ,
183201 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"user-b"}` }}},
@@ -190,6 +208,15 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
190208 sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
191209 want : true ,
192210 },
211+ {
212+ name : "simple non-matching user with multiple warrants and cluster-name" ,
213+ user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {
214+ WarrantExtraKey : {`{"user":"user-b"}` , `{"user":"user-a"}` , `{"user":"user-c"}` },
215+ ClusterExtraKey : {"this" },
216+ }},
217+ sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
218+ want : false , // Warrants are ineffective on users with cluster
219+ },
193220 {
194221 name : "simple non-matching user with nested warrants" ,
195222 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"user-b","extra":{"authorization.kcp.io/warrant":["{\"user\":\"user-a\"}"]}}` }}},
@@ -206,18 +233,18 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
206233 },
207234 {
208235 name : "non-cluster-aware service account with this scope" ,
209- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/scopes" : {"cluster:this" }}},
236+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ScopeExtraKey : {"cluster:this" }}},
210237 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
211238 want : true ,
212239 },
213240 {
214241 name : "non-cluster-aware service account with other scope" ,
215- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/scopes" : {"cluster:other" }}},
242+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ScopeExtraKey : {"cluster:other" }}},
216243 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
217244 want : false ,
218245 },
219246 {
220- name : "non-cluster-aware service account as warrant" ,
247+ name : "non-cluster-aware service account as warrant" , // TODO what is this supposed to test?
221248 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa"}` }}},
222249 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
223250 want : false ,
@@ -226,37 +253,46 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
226253 // service accounts with cluster
227254 {
228255 name : "local service account" ,
229- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"this" }}},
256+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ClusterExtraKey : {"this" }}},
230257 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
231258 want : true ,
232259 },
233260 {
234261 name : "foreign service account" ,
235- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"other" }}},
262+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ClusterExtraKey : {"other" }}},
236263 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
237264 want : false ,
238265 },
239266 {
240267 name : "foreign service account with local warrant" ,
241- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"other" }, WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa","extra":{"authentication.kcp.io/cluster-name":["this"]}}` }}},
268+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {
269+ ClusterExtraKey : {"other" },
270+ WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa","extra":{"authentication.kcp.io/cluster-name":["this"]}}` },
271+ }},
242272 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
243273 want : true ,
244274 },
245275 {
246276 name : "foreign service account with foreign warrant" ,
247- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"other" }, WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa","extra":{"authentication.kcp.io/cluster-name":["other"]}}` }}},
277+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {
278+ ClusterExtraKey : {"other" },
279+ WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa","extra":{"authentication.kcp.io/cluster-name":["other"]}}` },
280+ }},
248281 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
249282 want : false ,
250283 },
251284 {
252285 name : "local service account with multiple clusters" ,
253- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"this" , "this" }}},
286+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ClusterExtraKey : {"this" , "this" }}},
254287 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
255288 want : false ,
256289 },
257290 {
258291 name : "out-of-scope local service account" ,
259- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"this" }, "authentication.kcp.io/scopes" : {"cluster:other" }}},
292+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {
293+ ClusterExtraKey : {"this" },
294+ ScopeExtraKey : {"cluster:other" },
295+ }},
260296 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
261297 want : false ,
262298 },
@@ -487,3 +523,68 @@ func TestPrefixUser(t *testing.T) {
487523 })
488524 }
489525}
526+
527+ func TestEffectiveUsers (t * testing.T ) {
528+ tests := map [string ]struct {
529+ in []string
530+ want []string
531+ }{
532+ "empty" : {
533+ in : []string {},
534+ want : []string {},
535+ },
536+ "one scope entry, one cluster" : {
537+ in : []string {"cluster:this" },
538+ want : []string {"cluster:this" },
539+ },
540+ "one scope entry, multiple clusters" : {
541+ in : []string {"cluster:this,cluster:that" },
542+ want : []string {"cluster:this" , "cluster:that" },
543+ },
544+ "multiple scope entries, multiple clusters, empty result" : {
545+ in : []string {
546+ "cluster:this,cluster:that" ,
547+ "cluster:other" ,
548+ },
549+ want : []string {},
550+ },
551+ "multiple scope entries, multiple clusters, non-empty result" : {
552+ in : []string {
553+ "cluster:this,cluster:that" ,
554+ "cluster:other,cluster:this" ,
555+ },
556+ want : []string {"cluster:this" },
557+ },
558+ "multiple scopes entries, multiple clusters, multiple others" : {
559+ in : []string {
560+ "cluster:this,foo:bar" ,
561+ "cluster:this,cluster:other,foo:bar" ,
562+ "cluster:third,foo:bar,foo:baz" ,
563+ },
564+ want : []string {
565+ "foo:bar" ,
566+ },
567+ },
568+ "multiple equal scopes entries" : {
569+ in : []string {
570+ "cluster:this,cluster:other,foo:bar" ,
571+ "cluster:this,cluster:other,foo:bar" ,
572+ "cluster:this,cluster:other,foo:bar" ,
573+ },
574+ want : []string {
575+ "cluster:this" ,
576+ "cluster:other" ,
577+ "foo:bar" ,
578+ },
579+ },
580+ }
581+ for name , tt := range tests {
582+ t .Run (name , func (t * testing.T ) {
583+ t .Parallel ()
584+ got := EffectiveScopes (tt .in )
585+ if diff := cmp .Diff (tt .want , got ); diff != "" {
586+ t .Errorf ("EffectiveScopes() mismatch (-want +got):\n %s" , diff )
587+ }
588+ })
589+ }
590+ }
0 commit comments