@@ -15,12 +15,14 @@ import (
1515 "github.com/stretchr/testify/assert"
1616 "github.com/stretchr/testify/require"
1717 appsv1 "k8s.io/api/apps/v1"
18+ authorizationv1 "k8s.io/api/authorization/v1"
1819 v1 "k8s.io/api/core/v1"
1920 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2021 "k8s.io/apimachinery/pkg/runtime"
2122 "k8s.io/utils/ptr"
2223 "sigs.k8s.io/controller-runtime/pkg/client"
2324 "sigs.k8s.io/controller-runtime/pkg/client/fake"
25+ "sigs.k8s.io/controller-runtime/pkg/client/interceptor"
2426
2527 "github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/internal/config"
2628 "github.com/open-telemetry/opentelemetry-operator/cmd/operator-opamp-bridge/internal/operator"
@@ -45,13 +47,37 @@ func getFakeK8sClient(t *testing.T, objs ...client.Object) client.Client {
4547 scheme := runtime .NewScheme ()
4648 require .NoError (t , v1 .AddToScheme (scheme ))
4749 require .NoError (t , appsv1 .AddToScheme (scheme ))
50+ require .NoError (t , authorizationv1 .AddToScheme (scheme ))
4851 builder := fake .NewClientBuilder ().WithScheme (scheme )
4952 for _ , obj := range objs {
5053 builder = builder .WithObjects (obj )
5154 }
5255 return builder .Build ()
5356}
5457
58+ func getPermissionReviewClient (t * testing.T , allowed func (authorizationv1.ResourceAttributes ) bool ) client.Client {
59+ scheme := runtime .NewScheme ()
60+ require .NoError (t , v1 .AddToScheme (scheme ))
61+ require .NoError (t , appsv1 .AddToScheme (scheme ))
62+ require .NoError (t , authorizationv1 .AddToScheme (scheme ))
63+ return fake .NewClientBuilder ().
64+ WithScheme (scheme ).
65+ WithInterceptorFuncs (interceptor.Funcs {
66+ Create : func (ctx context.Context , c client.WithWatch , obj client.Object , opts ... client.CreateOption ) error {
67+ review , ok := obj .(* authorizationv1.SelfSubjectAccessReview )
68+ if ! ok {
69+ return c .Create (ctx , obj , opts ... )
70+ }
71+ review .Status .Allowed = allowed (* review .Spec .ResourceAttributes )
72+ if ! review .Status .Allowed {
73+ review .Status .Reason = "denied by test"
74+ }
75+ return nil
76+ },
77+ }).
78+ Build ()
79+ }
80+
5581func testConfigMap () * v1.ConfigMap {
5682 return & v1.ConfigMap {
5783 ObjectMeta : metav1.ObjectMeta {
@@ -284,6 +310,45 @@ func TestClientNotifyWorkloadHealthUpdate(t *testing.T) {
284310 assert .True (t , called )
285311}
286312
313+ func TestClientCheckPermissionsAllowsConfiguredStandaloneResources (t * testing.T ) {
314+ c := newTestClient (getPermissionReviewClient (t , func (_ authorizationv1.ResourceAttributes ) bool {
315+ return true
316+ }))
317+
318+ err := c .checkPermissions (context .Background (), []config.StandaloneAgentConfig {testAgentConfig ()}, true )
319+
320+ require .NoError (t , err )
321+ }
322+
323+ func TestClientCheckPermissionsRejectsMissingConfiguredResourcePermission (t * testing.T ) {
324+ c := newTestClient (getPermissionReviewClient (t , func (attrs authorizationv1.ResourceAttributes ) bool {
325+ return attrs .Verb != "update" || attrs .Resource != "deployments" || attrs .Name != "standalone-collector"
326+ }))
327+
328+ err := c .checkPermissions (context .Background (), []config.StandaloneAgentConfig {testAgentConfig ()}, true )
329+
330+ require .Error (t , err )
331+ assert .Contains (t , err .Error (), "standalone permission check failed" )
332+ assert .Contains (t , err .Error (), "missing update permission for apps/deployments default/standalone-collector" )
333+ }
334+
335+ func TestPermissionsCheckerRejectsDeniedPermission (t * testing.T ) {
336+ checker := newPermissionsChecker (getPermissionReviewClient (t , func (_ authorizationv1.ResourceAttributes ) bool {
337+ return false
338+ }))
339+
340+ err := checker .Check (context .Background (), []permission {{
341+ verb : "update" ,
342+ apiGroup : "apps" ,
343+ resource : "deployments" ,
344+ namespace : "default" ,
345+ name : "standalone-collector" ,
346+ }})
347+
348+ require .Error (t , err )
349+ assert .Contains (t , err .Error (), "missing update permission for apps/deployments default/standalone-collector" )
350+ }
351+
287352func TestScopedApplierApplyUpdatesConfiguredConfigMapKeyAndRestartsWorkload (t * testing.T ) {
288353 cm := testConfigMap ()
289354 deploy := testDeployment ()
0 commit comments