|
| 1 | +// Copyright 2026 Hyperscale. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package connectrpcsec_test |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "connectrpc.com/connect" |
| 12 | + "github.com/hyperscale-stack/security" |
| 13 | + connectrpcsec "github.com/hyperscale-stack/security/connectrpc" |
| 14 | + "github.com/hyperscale-stack/security/voter" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | +) |
| 18 | + |
| 19 | +// adminADM grants only when the principal holds the ADMIN role. |
| 20 | +func adminADM() security.AccessDecisionManager { |
| 21 | + return security.NewAffirmativeDecisionManager(voter.HasRole("ADMIN")) |
| 22 | +} |
| 23 | + |
| 24 | +var adminAttrs = []security.Attribute{security.Role("ADMIN")} |
| 25 | + |
| 26 | +// chainUnary composes the authentication and authorization interceptors the |
| 27 | +// same way connect.WithInterceptors(authn, authz) would: authn is the |
| 28 | +// outermost, so it enriches the context before authz reads it. |
| 29 | +func chainUnary( |
| 30 | + authn *connectrpcsec.AuthenticationInterceptor, |
| 31 | + authz *connectrpcsec.AuthorizationInterceptor, |
| 32 | + handler connect.UnaryFunc, |
| 33 | +) connect.UnaryFunc { |
| 34 | + return authn.WrapUnary(authz.WrapUnary(handler)) |
| 35 | +} |
| 36 | + |
| 37 | +func TestUnaryAuthorizeGrantsWhenRolePresent(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + spy := &recordingUnary{} |
| 41 | + wrapped := chainUnary( |
| 42 | + connectrpcsec.NewAuthenticationInterceptor(newEngine("ROLE_ADMIN")), |
| 43 | + connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs), |
| 44 | + spy.fn, |
| 45 | + ) |
| 46 | + |
| 47 | + resp, err := wrapped(context.Background(), unaryReq("letmein")) |
| 48 | + require.NoError(t, err) |
| 49 | + assert.NotNil(t, resp) |
| 50 | + assert.True(t, spy.called) |
| 51 | +} |
| 52 | + |
| 53 | +func TestUnaryAuthorizeDeniesWhenRoleMissing(t *testing.T) { |
| 54 | + t.Parallel() |
| 55 | + |
| 56 | + spy := &recordingUnary{} |
| 57 | + wrapped := chainUnary( |
| 58 | + connectrpcsec.NewAuthenticationInterceptor(newEngine()), |
| 59 | + connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs), |
| 60 | + spy.fn, |
| 61 | + ) |
| 62 | + |
| 63 | + _, err := wrapped(context.Background(), unaryReq("letmein")) |
| 64 | + require.Error(t, err) |
| 65 | + assert.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 66 | + assert.False(t, spy.called) |
| 67 | +} |
| 68 | + |
| 69 | +func TestUnaryAuthorizeDeniesAnonymous(t *testing.T) { |
| 70 | + t.Parallel() |
| 71 | + |
| 72 | + spy := &recordingUnary{} |
| 73 | + // No authentication in the context: the voter denies the anonymous caller. |
| 74 | + wrapped := connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs).WrapUnary(spy.fn) |
| 75 | + |
| 76 | + _, err := wrapped(context.Background(), unaryReq("letmein")) |
| 77 | + require.Error(t, err) |
| 78 | + assert.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 79 | + assert.False(t, spy.called) |
| 80 | +} |
| 81 | + |
| 82 | +func TestUnaryAuthorizeSkipsClientCall(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + |
| 85 | + spy := &recordingUnary{} |
| 86 | + wrapped := connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs).WrapUnary(spy.fn) |
| 87 | + |
| 88 | + _, err := wrapped(context.Background(), clientRequest{connect.NewRequest(&struct{}{})}) |
| 89 | + require.NoError(t, err) |
| 90 | + assert.True(t, spy.called) |
| 91 | +} |
| 92 | + |
| 93 | +func TestStreamAuthorizeGrantsWhenRolePresent(t *testing.T) { |
| 94 | + t.Parallel() |
| 95 | + |
| 96 | + spy := &recordingStream{} |
| 97 | + authn := connectrpcsec.NewAuthenticationInterceptor(newEngine("ROLE_ADMIN")) |
| 98 | + authz := connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs) |
| 99 | + wrapped := authn.WrapStreamingHandler(authz.WrapStreamingHandler(spy.fn)) |
| 100 | + |
| 101 | + err := wrapped(context.Background(), newStreamConn(bearerHeader("letmein"))) |
| 102 | + require.NoError(t, err) |
| 103 | + assert.True(t, spy.called) |
| 104 | +} |
| 105 | + |
| 106 | +func TestStreamAuthorizeDeniesWhenRoleMissing(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + spy := &recordingStream{} |
| 110 | + authn := connectrpcsec.NewAuthenticationInterceptor(newEngine()) |
| 111 | + authz := connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs) |
| 112 | + wrapped := authn.WrapStreamingHandler(authz.WrapStreamingHandler(spy.fn)) |
| 113 | + |
| 114 | + err := wrapped(context.Background(), newStreamConn(bearerHeader("letmein"))) |
| 115 | + require.Error(t, err) |
| 116 | + assert.Equal(t, connect.CodePermissionDenied, connect.CodeOf(err)) |
| 117 | + assert.False(t, spy.called) |
| 118 | +} |
| 119 | + |
| 120 | +func TestStreamAuthorizeIsPassThroughForClient(t *testing.T) { |
| 121 | + t.Parallel() |
| 122 | + |
| 123 | + called := false |
| 124 | + |
| 125 | + next := func(_ context.Context, _ connect.Spec) connect.StreamingClientConn { |
| 126 | + called = true |
| 127 | + |
| 128 | + return nil |
| 129 | + } |
| 130 | + |
| 131 | + wrapped := connectrpcsec.NewAuthorizationInterceptor(adminADM(), adminAttrs).WrapStreamingClient(next) |
| 132 | + _ = wrapped(context.Background(), connect.Spec{}) |
| 133 | + |
| 134 | + assert.True(t, called) |
| 135 | +} |
0 commit comments