@@ -15,6 +15,7 @@ import (
1515 "github.com/datawire/dlib/dgroup"
1616 "github.com/datawire/dlib/dlog"
1717 rpc "github.com/telepresenceio/telepresence/rpc/v2/manager"
18+ "github.com/telepresenceio/telepresence/v2/cmd/traffic/cmd/manager/managerutil"
1819 "github.com/telepresenceio/telepresence/v2/cmd/traffic/cmd/manager/state"
1920 "github.com/telepresenceio/telepresence/v2/pkg/k8sapi"
2021)
@@ -50,6 +51,16 @@ func TestRevokeIntercept_Authentication(t *testing.T) {
5051 wantErr : true ,
5152 errMessage : "not found" ,
5253 },
54+ {
55+ name : "authorized user with kubeadm:cluster-admins" ,
56+ token : "valid-kubeadm-token" ,
57+ authenticated : true ,
58+ username : "kubeadm-admin" ,
59+ groups : []string {"system:authenticated" , "kubeadm:cluster-admins" },
60+ wantCode : codes .NotFound , // Will fail on intercept lookup, but auth passes
61+ wantErr : true ,
62+ errMessage : "not found" ,
63+ },
5364 {
5465 name : "unauthorized user - not in allowed groups" ,
5566 token : "valid-user-token" ,
@@ -58,7 +69,7 @@ func TestRevokeIntercept_Authentication(t *testing.T) {
5869 groups : []string {"system:authenticated" , "developers" },
5970 wantCode : codes .PermissionDenied ,
6071 wantErr : true ,
61- errMessage : "user must be a member of telepresence:admin or system:masters group " ,
72+ errMessage : "user must be a member of one of the following groups " ,
6273 },
6374 {
6475 name : "unauthenticated token" ,
@@ -81,11 +92,11 @@ func TestRevokeIntercept_Authentication(t *testing.T) {
8192 errMessage : "authentication failed" ,
8293 },
8394 {
84- name : "user with both groups" ,
95+ name : "user with multiple admin groups" ,
8596 token : "super-admin-token" ,
8697 authenticated : true ,
8798 username : "super-admin" ,
88- groups : []string {"system:authenticated" , "system:masters" , "telepresence:admin" },
99+ groups : []string {"system:authenticated" , "system:masters" , "telepresence:admin" , "kubeadm:cluster-admins" },
89100 wantCode : codes .NotFound , // Will fail on intercept lookup, but auth passes
90101 wantErr : true ,
91102 errMessage : "not found" ,
@@ -123,6 +134,12 @@ func TestRevokeIntercept_Authentication(t *testing.T) {
123134 // Set up context with fake K8s client
124135 ctx = k8sapi .WithK8sInterface (ctx , fakeClient )
125136
137+ // Set up environment with default admin groups
138+ env := & managerutil.Env {
139+ AgentK8sAdminGroups : []string {"system:masters" , "telepresence:admin" , "kubeadm:cluster-admins" },
140+ }
141+ ctx = managerutil .WithEnv (ctx , env )
142+
126143 // Create a minimal service instance
127144 g := dgroup .NewGroup (ctx , dgroup.GroupConfig {})
128145 svc := & service {
@@ -176,6 +193,12 @@ func TestRevokeIntercept_SuccessfulRevocation(t *testing.T) {
176193 // Set up context with fake K8s client
177194 ctx = k8sapi .WithK8sInterface (ctx , fakeClient )
178195
196+ // Set up environment with default admin groups
197+ env := & managerutil.Env {
198+ AgentK8sAdminGroups : []string {"system:masters" },
199+ }
200+ ctx = managerutil .WithEnv (ctx , env )
201+
179202 // Create a service instance with state
180203 g := dgroup .NewGroup (ctx , dgroup.GroupConfig {})
181204 svc := & service {
@@ -229,7 +252,7 @@ func TestRevokeIntercept_SuccessfulRevocation(t *testing.T) {
229252}
230253
231254func TestRevokeIntercept_OnlySystemMasters (t * testing.T ) {
232- // This test specifically verifies that ONLY system:masters (or telepresence: admin) can revoke
255+ // This test specifically verifies that ONLY configured admin groups can revoke
233256 unauthorizedGroups := [][]string {
234257 {"system:authenticated" },
235258 {"system:authenticated" , "developers" },
@@ -261,6 +284,12 @@ func TestRevokeIntercept_OnlySystemMasters(t *testing.T) {
261284
262285 ctx = k8sapi .WithK8sInterface (ctx , fakeClient )
263286
287+ // Set up environment with default admin groups
288+ env := & managerutil.Env {
289+ AgentK8sAdminGroups : []string {"system:masters" , "telepresence:admin" , "kubeadm:cluster-admins" },
290+ }
291+ ctx = managerutil .WithEnv (ctx , env )
292+
264293 g := dgroup .NewGroup (ctx , dgroup.GroupConfig {})
265294 svc := & service {
266295 state : state .NewState (ctx , g ),
@@ -277,9 +306,59 @@ func TestRevokeIntercept_OnlySystemMasters(t *testing.T) {
277306 st , ok := status .FromError (err )
278307 require .True (t , ok )
279308 assert .Equal (t , codes .PermissionDenied , st .Code ())
280- assert .Contains (t , st .Message (), "user must be a member of telepresence:admin or system:masters group " )
309+ assert .Contains (t , st .Message (), "user must be a member of one of the following groups " )
281310
282311 t .Logf ("Correctly denied access for groups: %v" , groups )
283312 })
284313 }
285314}
315+
316+ func TestRevokeIntercept_CustomAdminGroups (t * testing.T ) {
317+ // Test with custom admin groups from environment
318+ ctx := dlog .NewTestContext (t , true )
319+
320+ fakeClient := fake .NewClientset ()
321+ fakeClient .PrependReactor ("create" , "tokenreviews" , func (action k8stesting.Action ) (bool , runtime.Object , error ) {
322+ createAction := action .(k8stesting.CreateAction )
323+ tr := createAction .GetObject ().(* authv1.TokenReview )
324+
325+ // Return authenticated user with test-admin service account
326+ tr .Status = authv1.TokenReviewStatus {
327+ Authenticated : true ,
328+ User : authv1.UserInfo {
329+ Username : "system:serviceaccount:ambassador:test-admin" ,
330+ Groups : []string {"system:serviceaccounts" , "system:serviceaccounts:ambassador" , "system:authenticated" },
331+ },
332+ }
333+
334+ return true , tr , nil
335+ })
336+
337+ ctx = k8sapi .WithK8sInterface (ctx , fakeClient )
338+
339+ // Set up environment with custom admin groups including the service account
340+ env := & managerutil.Env {
341+ AgentK8sAdminGroups : []string {"system:masters" , "system:serviceaccount:ambassador:test-admin" },
342+ }
343+ ctx = managerutil .WithEnv (ctx , env )
344+
345+ g := dgroup .NewGroup (ctx , dgroup.GroupConfig {})
346+ svc := & service {
347+ state : state .NewState (ctx , g ),
348+ }
349+
350+ req := & rpc.RevokeInterceptRequest {
351+ InterceptId : "test:intercept" ,
352+ Token : "test-token" ,
353+ }
354+
355+ _ , err := svc .RevokeIntercept (ctx , req )
356+
357+ // Should get NotFound (auth passed, but intercept doesn't exist) instead of PermissionDenied
358+ require .Error (t , err )
359+ st , ok := status .FromError (err )
360+ require .True (t , ok )
361+ // Auth should pass, so we get NotFound instead of PermissionDenied
362+ assert .Equal (t , codes .NotFound , st .Code (), "Expected NotFound since auth passed but intercept doesn't exist" )
363+ t .Logf ("Successfully verified that custom admin group (system:serviceaccount:ambassador:test-admin) is allowed" )
364+ }
0 commit comments