@@ -176,12 +176,30 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
176176 sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
177177 want : true ,
178178 },
179+ {
180+ name : "simple matching user with warrants and from this cluster" ,
181+ user : & user.DefaultInfo {Name : "user-a" , Extra : map [string ][]string {
182+ WarrantExtraKey : {`{"user":"user-b"}` },
183+ ClusterExtraKey : {"this" },
184+ }},
185+ sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
186+ want : true , // user is subject
187+ },
179188 {
180189 name : "simple non-matching user with matching warrants" ,
181190 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"user-a"}` }}},
182191 sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
183192 want : true ,
184193 },
194+ {
195+ name : "simple non-matching user with matching warrants but with cluster-name" ,
196+ user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {
197+ WarrantExtraKey : {`{"user":"user-a"}` },
198+ ClusterExtraKey : {"this" },
199+ }},
200+ sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
201+ want : false , // Warrants are ineffective on users with cluster
202+ },
185203 {
186204 name : "simple non-matching user with non-matching warrants" ,
187205 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"user-b"}` }}},
@@ -194,6 +212,15 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
194212 sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
195213 want : true ,
196214 },
215+ {
216+ name : "simple non-matching user with multiple warrants and cluster-name" ,
217+ user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {
218+ WarrantExtraKey : {`{"user":"user-b"}` , `{"user":"user-a"}` , `{"user":"user-c"}` },
219+ ClusterExtraKey : {"this" },
220+ }},
221+ sub : rbacv1.Subject {Kind : "User" , Name : "user-a" },
222+ want : false , // Warrants are ineffective on users with cluster
223+ },
197224 {
198225 name : "simple non-matching user with nested warrants" ,
199226 user : & user.DefaultInfo {Name : "user-b" , Extra : map [string ][]string {WarrantExtraKey : {`{"user":"user-b","extra":{"authorization.kcp.io/warrant":["{\"user\":\"user-a\"}"]}}` }}},
@@ -210,13 +237,13 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
210237 },
211238 {
212239 name : "non-cluster-aware service account with this scope" ,
213- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/scopes" : {"cluster:this" }}},
240+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ScopeExtraKey : {"cluster:this" }}},
214241 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
215242 want : true ,
216243 },
217244 {
218245 name : "non-cluster-aware service account with other scope" ,
219- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/scopes" : {"cluster:other" }}},
246+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ScopeExtraKey : {"cluster:other" }}},
220247 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
221248 want : false ,
222249 },
@@ -230,37 +257,46 @@ func TestAppliesToUserWithWarrantsAndScopes(t *testing.T) {
230257 // service accounts with cluster
231258 {
232259 name : "local service account" ,
233- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"this" }}},
260+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ClusterExtraKey : {"this" }}},
234261 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
235262 want : true ,
236263 },
237264 {
238265 name : "foreign service account" ,
239- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"other" }}},
266+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ClusterExtraKey : {"other" }}},
240267 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
241268 want : false ,
242269 },
243270 {
244271 name : "foreign service account with local warrant" ,
245- 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"]}}` }}},
272+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {
273+ ClusterExtraKey : {"other" },
274+ WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa","extra":{"authentication.kcp.io/cluster-name":["this"]}}` },
275+ }},
246276 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
247277 want : true ,
248278 },
249279 {
250280 name : "foreign service account with foreign warrant" ,
251- 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"]}}` }}},
281+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {
282+ ClusterExtraKey : {"other" },
283+ WarrantExtraKey : {`{"user":"system:serviceaccount:ns:sa","extra":{"authentication.kcp.io/cluster-name":["other"]}}` },
284+ }},
252285 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
253286 want : false ,
254287 },
255288 {
256289 name : "local service account with multiple clusters" ,
257- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"this" , "this" }}},
290+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {ClusterExtraKey : {"this" , "this" }}},
258291 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
259292 want : false ,
260293 },
261294 {
262295 name : "out-of-scope local service account" ,
263- user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {"authentication.kcp.io/cluster-name" : {"this" }, "authentication.kcp.io/scopes" : {"cluster:other" }}},
296+ user : & user.DefaultInfo {Name : "system:serviceaccount:ns:sa" , Extra : map [string ][]string {
297+ ClusterExtraKey : {"this" },
298+ ScopeExtraKey : {"cluster:other" },
299+ }},
264300 sub : rbacv1.Subject {Kind : "ServiceAccount" , Namespace : "ns" , Name : "sa" },
265301 want : false ,
266302 },
@@ -496,3 +532,68 @@ func TestPrefixUser(t *testing.T) {
496532 })
497533 }
498534}
535+
536+ func TestEffectiveUsers (t * testing.T ) {
537+ tests := map [string ]struct {
538+ in []string
539+ want []string
540+ }{
541+ "empty" : {
542+ in : []string {},
543+ want : []string {},
544+ },
545+ "one scope entry, one cluster" : {
546+ in : []string {"cluster:this" },
547+ want : []string {"cluster:this" },
548+ },
549+ "one scope entry, multiple clusters" : {
550+ in : []string {"cluster:this,cluster:that" },
551+ want : []string {"cluster:this" , "cluster:that" },
552+ },
553+ "multiple scope entries, multiple clusters, empty result" : {
554+ in : []string {
555+ "cluster:this,cluster:that" ,
556+ "cluster:other" ,
557+ },
558+ want : []string {},
559+ },
560+ "multiple scope entries, multiple clusters, non-empty result" : {
561+ in : []string {
562+ "cluster:this,cluster:that" ,
563+ "cluster:other,cluster:this" ,
564+ },
565+ want : []string {"cluster:this" },
566+ },
567+ "multiple scopes entries, multiple clusters, multiple others" : {
568+ in : []string {
569+ "cluster:this,foo:bar" ,
570+ "cluster:this,cluster:other,foo:bar" ,
571+ "cluster:third,foo:bar,foo:baz" ,
572+ },
573+ want : []string {
574+ "foo:bar" ,
575+ },
576+ },
577+ "multiple equal scopes entries" : {
578+ in : []string {
579+ "cluster:this,cluster:other,foo:bar" ,
580+ "cluster:this,cluster:other,foo:bar" ,
581+ "cluster:this,cluster:other,foo:bar" ,
582+ },
583+ want : []string {
584+ "cluster:this" ,
585+ "cluster:other" ,
586+ "foo:bar" ,
587+ },
588+ },
589+ }
590+ for name , tt := range tests {
591+ t .Run (name , func (t * testing.T ) {
592+ t .Parallel ()
593+ got := EffectiveScopes (tt .in )
594+ if diff := cmp .Diff (tt .want , got ); diff != "" {
595+ t .Errorf ("EffectiveScopes() mismatch (-want +got):\n %s" , diff )
596+ }
597+ })
598+ }
599+ }
0 commit comments