Skip to content

Commit f5b1213

Browse files
authored
fix(cas): Fix compatibility with Reflection API (#1074)
Signed-off-by: Jose I. Paris <[email protected]>
1 parent 265e2df commit f5b1213

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

app/artifact-cas/internal/server/grpc.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ func requireAuthentication() selector.MatchFunc {
155155
// Reflection API is called by clients like grpcurl to list services
156156
// and without this selector check it would require authentication
157157
func allButReflectionAPI(_ context.Context, callMeta interceptors.CallMeta) bool {
158-
return callMeta.Service != "grpc.reflection.v1alpha.ServerReflection"
158+
const skipRegexp = "(grpc.reflection.*)"
159+
r := regexp.MustCompile(skipRegexp)
160+
return !r.MatchString(callMeta.Service)
159161
}
160162

161163
// load key for verification

app/artifact-cas/internal/server/grpc_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
jwtMiddleware "github.com/go-kratos/kratos/v2/middleware/auth/jwt"
2727
jwt "github.com/golang-jwt/jwt/v4"
2828
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils"
29+
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors"
2930
"github.com/stretchr/testify/assert"
3031

3132
"github.com/stretchr/testify/require"
@@ -156,6 +157,30 @@ func TestRequireAuthentication(t *testing.T) {
156157
}
157158
}
158159

160+
func TestAllButReflectionAPI(t *testing.T) {
161+
testCases := []struct {
162+
callMeta interceptors.CallMeta
163+
expected bool
164+
}{
165+
{
166+
callMeta: interceptors.CallMeta{Service: "grpc.reflection.v1alpha.ServerReflection"},
167+
expected: false,
168+
},
169+
{
170+
callMeta: interceptors.CallMeta{Service: "grpc.reflection.v1.ServerReflection"},
171+
expected: false,
172+
},
173+
{
174+
callMeta: interceptors.CallMeta{Service: "grpc.other.service"},
175+
expected: true,
176+
},
177+
}
178+
179+
for _, op := range testCases {
180+
assert.Equal(t, allButReflectionAPI(context.TODO(), op.callMeta), op.expected)
181+
}
182+
}
183+
159184
func loadTestPublicKey(path string) jwt.Keyfunc {
160185
rawKey, _ := os.ReadFile(path)
161186
return func(token *jwt.Token) (interface{}, error) {

0 commit comments

Comments
 (0)