From 88213c52d10dd3f679b61da14c9f885669ddec39 Mon Sep 17 00:00:00 2001 From: Haytham Abuelfutuh Date: Tue, 13 Sep 2022 14:44:59 -0700 Subject: [PATCH] Expose newAuthInterceptor to allow other clients to create authenticating clients (#319) Signed-off-by: Haytham Abuelfutuh Signed-off-by: Haytham Abuelfutuh --- clients/go/admin/auth_interceptor.go | 4 ++-- clients/go/admin/auth_interceptor_test.go | 8 ++++---- clients/go/admin/client.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/clients/go/admin/auth_interceptor.go b/clients/go/admin/auth_interceptor.go index 69c522850b..400293e937 100644 --- a/clients/go/admin/auth_interceptor.go +++ b/clients/go/admin/auth_interceptor.go @@ -46,7 +46,7 @@ func shouldAttemptToAuthenticate(errorCode codes.Code) bool { return errorCode == codes.Unauthenticated } -// newAuthInterceptor creates a new grpc.UnaryClientInterceptor that forwards the grpc call and inspects the error. +// NewAuthInterceptor creates a new grpc.UnaryClientInterceptor that forwards the grpc call and inspects the error. // It will first invoke the grpc pipeline (to proceed with the request) with no modifications. It's expected for the grpc // pipeline to already have a grpc.WithPerRPCCredentials() DialOption. If the perRPCCredentials has already been initialized, // it'll take care of refreshing when tokens expire... etc. @@ -56,7 +56,7 @@ func shouldAttemptToAuthenticate(errorCode codes.Code) bool { // more. It'll fail hard if it couldn't do so (i.e. it will no longer attempt to send an unauthenticated request). Once // a token source has been created, it'll invoke the grpc pipeline again, this time the grpc.PerRPCCredentials should // be able to find and acquire a valid AccessToken to annotate the request with. -func newAuthInterceptor(cfg *Config, tokenCache cache.TokenCache, credentialsFuture *PerRPCCredentialsFuture) grpc.UnaryClientInterceptor { +func NewAuthInterceptor(cfg *Config, tokenCache cache.TokenCache, credentialsFuture *PerRPCCredentialsFuture) grpc.UnaryClientInterceptor { return func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error { err := invoker(ctx, method, req, reply, cc, opts...) if err != nil { diff --git a/clients/go/admin/auth_interceptor_test.go b/clients/go/admin/auth_interceptor_test.go index b877793b5e..6cbf5441e5 100644 --- a/clients/go/admin/auth_interceptor_test.go +++ b/clients/go/admin/auth_interceptor_test.go @@ -117,7 +117,7 @@ func newAuthMetadataServer(t testing.TB, port int, impl service2.AuthMetadataSer func Test_newAuthInterceptor(t *testing.T) { t.Run("Other Error", func(t *testing.T) { f := NewPerRPCCredentialsFuture() - interceptor := newAuthInterceptor(&Config{}, &mocks.TokenCache{}, f) + interceptor := NewAuthInterceptor(&Config{}, &mocks.TokenCache{}, f) otherError := func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, opts ...grpc.CallOption) error { return status.New(codes.Canceled, "").Err() } @@ -149,7 +149,7 @@ func Test_newAuthInterceptor(t *testing.T) { assert.NoError(t, err) f := NewPerRPCCredentialsFuture() - interceptor := newAuthInterceptor(&Config{ + interceptor := NewAuthInterceptor(&Config{ Endpoint: config.URL{URL: *u}, UseInsecureConnection: true, AuthType: AuthTypeClientSecret, @@ -180,7 +180,7 @@ func Test_newAuthInterceptor(t *testing.T) { assert.NoError(t, err) f := NewPerRPCCredentialsFuture() - interceptor := newAuthInterceptor(&Config{ + interceptor := NewAuthInterceptor(&Config{ Endpoint: config.URL{URL: *u}, UseInsecureConnection: true, AuthType: AuthTypeClientSecret, @@ -219,7 +219,7 @@ func Test_newAuthInterceptor(t *testing.T) { assert.NoError(t, err) f := NewPerRPCCredentialsFuture() - interceptor := newAuthInterceptor(&Config{ + interceptor := NewAuthInterceptor(&Config{ Endpoint: config.URL{URL: *u}, UseInsecureConnection: true, AuthType: AuthTypeClientSecret, diff --git a/clients/go/admin/client.go b/clients/go/admin/client.go index fb0bfc642f..52959ceb33 100644 --- a/clients/go/admin/client.go +++ b/clients/go/admin/client.go @@ -164,7 +164,7 @@ func InitializeAdminClient(ctx context.Context, cfg *Config, opts ...grpc.DialOp func initializeClients(ctx context.Context, cfg *Config, tokenCache cache.TokenCache, opts ...grpc.DialOption) (*Clientset, error) { credentialsFuture := NewPerRPCCredentialsFuture() opts = append(opts, - grpc.WithChainUnaryInterceptor(newAuthInterceptor(cfg, tokenCache, credentialsFuture)), + grpc.WithChainUnaryInterceptor(NewAuthInterceptor(cfg, tokenCache, credentialsFuture)), grpc.WithPerRPCCredentials(credentialsFuture)) if cfg.DefaultServiceConfig != "" {