-
Notifications
You must be signed in to change notification settings - Fork 701
/
auth_test.go
201 lines (171 loc) · 7.23 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright (c) The go-grpc-middleware Authors.
// Licensed under the Apache License 2.0.
package auth_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/metadata"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/testing/testpb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"golang.org/x/oauth2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials/oauth"
grpcMetadata "google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
var authedMarker struct{}
var (
commonAuthToken = "some_good_token"
overrideAuthToken = "override_token"
)
// TODO(mwitkow): Add auth from metadata client dialer, which requires TLS.
func buildDummyAuthFunction(expectedScheme string, expectedToken string) func(ctx context.Context) (context.Context, error) {
return func(ctx context.Context) (context.Context, error) {
token, err := auth.AuthFromMD(ctx, expectedScheme)
if err != nil {
return nil, err
}
if token != expectedToken {
return nil, status.Error(codes.PermissionDenied, "buildDummyAuthFunction bad token")
}
return context.WithValue(ctx, authedMarker, "marker_exists"), nil
}
}
func assertAuthMarkerExists(t *testing.T, ctx context.Context) {
assert.Equal(t, "marker_exists", ctx.Value(authedMarker).(string), "auth marker from buildDummyAuthFunction must be passed around")
}
type assertingPingService struct {
testpb.TestServiceServer
T *testing.T
}
func (s *assertingPingService) PingError(ctx context.Context, ping *testpb.PingErrorRequest) (*testpb.PingErrorResponse, error) {
assertAuthMarkerExists(s.T, ctx)
return s.TestServiceServer.PingError(ctx, ping)
}
func (s *assertingPingService) PingList(ping *testpb.PingListRequest, stream testpb.TestService_PingListServer) error {
assertAuthMarkerExists(s.T, stream.Context())
return s.TestServiceServer.PingList(ping, stream)
}
func ctxWithToken(ctx context.Context, scheme string, token string) context.Context {
md := grpcMetadata.Pairs("authorization", fmt.Sprintf("%s %v", scheme, token))
return metadata.MD(md).ToOutgoing(ctx)
}
func TestAuthTestSuite(t *testing.T) {
authFunc := buildDummyAuthFunction("bearer", commonAuthToken)
s := &AuthTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &assertingPingService{&testpb.TestPingService{}, t},
ServerOpts: []grpc.ServerOption{
grpc.StreamInterceptor(auth.StreamServerInterceptor(authFunc)),
grpc.UnaryInterceptor(auth.UnaryServerInterceptor(authFunc)),
},
},
}
suite.Run(t, s)
}
type AuthTestSuite struct {
*testpb.InterceptorTestSuite
}
func (s *AuthTestSuite) TestUnary_NoAuth() {
_, err := s.Client.Ping(s.SimpleCtx(), testpb.GoodPing)
assert.Error(s.T(), err, "there must be an error")
assert.Equal(s.T(), codes.Unauthenticated, status.Code(err), "must error with unauthenticated")
}
func (s *AuthTestSuite) TestUnary_BadAuth() {
_, err := s.Client.Ping(ctxWithToken(s.SimpleCtx(), "bearer", "bad_token"), testpb.GoodPing)
assert.Error(s.T(), err, "there must be an error")
assert.Equal(s.T(), codes.PermissionDenied, status.Code(err), "must error with permission denied")
}
func (s *AuthTestSuite) TestUnary_PassesAuth() {
_, err := s.Client.Ping(ctxWithToken(s.SimpleCtx(), "bearer", commonAuthToken), testpb.GoodPing)
require.NoError(s.T(), err, "no error must occur")
}
func (s *AuthTestSuite) TestUnary_PassesWithPerRpcCredentials() {
grpcCreds := oauth.TokenSource{TokenSource: &fakeOAuth2TokenSource{accessToken: commonAuthToken}}
client := s.NewClient(grpc.WithPerRPCCredentials(grpcCreds))
_, err := client.Ping(s.SimpleCtx(), testpb.GoodPing)
require.NoError(s.T(), err, "no error must occur")
}
func (s *AuthTestSuite) TestStream_NoAuth() {
stream, err := s.Client.PingList(s.SimpleCtx(), testpb.GoodPingList)
require.NoError(s.T(), err, "should not fail on establishing the stream")
_, err = stream.Recv()
assert.Error(s.T(), err, "there must be an error")
assert.Equal(s.T(), codes.Unauthenticated, status.Code(err), "must error with unauthenticated")
}
func (s *AuthTestSuite) TestStream_BadAuth() {
stream, err := s.Client.PingList(ctxWithToken(s.SimpleCtx(), "bearer", "bad_token"), testpb.GoodPingList)
require.NoError(s.T(), err, "should not fail on establishing the stream")
_, err = stream.Recv()
assert.Error(s.T(), err, "there must be an error")
assert.Equal(s.T(), codes.PermissionDenied, status.Code(err), "must error with permission denied")
}
func (s *AuthTestSuite) TestStream_PassesAuth() {
stream, err := s.Client.PingList(ctxWithToken(s.SimpleCtx(), "Bearer", commonAuthToken), testpb.GoodPingList)
require.NoError(s.T(), err, "should not fail on establishing the stream")
pong, err := stream.Recv()
require.NoError(s.T(), err, "no error must occur")
require.NotNil(s.T(), pong, "pong must not be nil")
}
func (s *AuthTestSuite) TestStream_PassesWithPerRpcCredentials() {
grpcCreds := oauth.TokenSource{TokenSource: &fakeOAuth2TokenSource{accessToken: commonAuthToken}}
client := s.NewClient(grpc.WithPerRPCCredentials(grpcCreds))
stream, err := client.PingList(s.SimpleCtx(), testpb.GoodPingList)
require.NoError(s.T(), err, "should not fail on establishing the stream")
pong, err := stream.Recv()
require.NoError(s.T(), err, "no error must occur")
require.NotNil(s.T(), pong, "pong must not be nil")
}
type authOverrideTestService struct {
testpb.TestServiceServer
T *testing.T
}
func (s *authOverrideTestService) AuthFuncOverride(ctx context.Context, fullMethodName string) (context.Context, error) {
assert.NotEmpty(s.T, fullMethodName, "method name of caller is passed around")
return buildDummyAuthFunction("bearer", overrideAuthToken)(ctx)
}
func TestAuthOverrideTestSuite(t *testing.T) {
authFunc := buildDummyAuthFunction("bearer", commonAuthToken)
s := &AuthOverrideTestSuite{
InterceptorTestSuite: &testpb.InterceptorTestSuite{
TestService: &authOverrideTestService{&assertingPingService{&testpb.TestPingService{}, t}, t},
ServerOpts: []grpc.ServerOption{
grpc.StreamInterceptor(auth.StreamServerInterceptor(authFunc)),
grpc.UnaryInterceptor(auth.UnaryServerInterceptor(authFunc)),
},
},
}
suite.Run(t, s)
}
type AuthOverrideTestSuite struct {
*testpb.InterceptorTestSuite
}
func (s *AuthOverrideTestSuite) TestUnary_PassesAuth() {
_, err := s.Client.Ping(ctxWithToken(s.SimpleCtx(), "bearer", overrideAuthToken), testpb.GoodPing)
require.NoError(s.T(), err, "no error must occur")
}
func (s *AuthOverrideTestSuite) TestStream_PassesAuth() {
stream, err := s.Client.PingList(ctxWithToken(s.SimpleCtx(), "Bearer", overrideAuthToken), testpb.GoodPingList)
require.NoError(s.T(), err, "should not fail on establishing the stream")
pong, err := stream.Recv()
require.NoError(s.T(), err, "no error must occur")
require.NotNil(s.T(), pong, "pong must not be nil")
}
// fakeOAuth2TokenSource implements a fake oauth2.TokenSource for the purpose of credentials test.
type fakeOAuth2TokenSource struct {
accessToken string
}
func (ts *fakeOAuth2TokenSource) Token() (*oauth2.Token, error) {
t := &oauth2.Token{
AccessToken: ts.accessToken,
Expiry: time.Now().Add(1 * time.Minute),
TokenType: "bearer",
}
return t, nil
}