From 33a252ae9aae184535a9e9d6ab557366d103acc6 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Wed, 24 Jan 2024 16:51:04 +0100 Subject: [PATCH 1/2] Load `--grpc_auth_static_client_creds` file once Signed-off-by: Tim Vaillancourt --- go/vt/grpcclient/client_auth_static.go | 27 +++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/go/vt/grpcclient/client_auth_static.go b/go/vt/grpcclient/client_auth_static.go index 53be18cc4ff..95a4b480860 100644 --- a/go/vt/grpcclient/client_auth_static.go +++ b/go/vt/grpcclient/client_auth_static.go @@ -20,6 +20,7 @@ import ( "encoding/json" "flag" "os" + "sync" "context" @@ -31,6 +32,9 @@ var ( credsFile = flag.String("grpc_auth_static_client_creds", "", "when using grpc_static_auth in the server, this file provides the credentials to use to authenticate with server") // StaticAuthClientCreds implements client interface to be able to WithPerRPCCredentials _ credentials.PerRPCCredentials = (*StaticAuthClientCreds)(nil) + + credsFileOnce sync.Once + clientCreds *StaticAuthClientCreds ) // StaticAuthClientCreds holder for client credentials @@ -54,21 +58,30 @@ func (c *StaticAuthClientCreds) RequireTransportSecurity() bool { return false } -// AppendStaticAuth optionally appends static auth credentials if provided. -func AppendStaticAuth(opts []grpc.DialOption) ([]grpc.DialOption, error) { - if *credsFile == "" { - return opts, nil - } - data, err := os.ReadFile(*credsFile) +// loadStaticAuthCredsFromFile loads static auth credentials from a file. +func loadStaticAuthCredsFromFile(credsFile string) (*StaticAuthClientCreds, error) { + data, err := os.ReadFile(credsFile) if err != nil { return nil, err } clientCreds := &StaticAuthClientCreds{} err = json.Unmarshal(data, clientCreds) + return clientCreds, err +} + +// AppendStaticAuth optionally appends static auth credentials if provided. +func AppendStaticAuth(opts []grpc.DialOption) ([]grpc.DialOption, error) { + if credsFile == "" { + return opts, nil + } + var err error + credsFileOnce.Do(func() { + clientCreds, err = loadStaticAuthCredsFromFile(credsFile) + }) if err != nil { return nil, err } - creds := grpc.WithPerRPCCredentials(clientCreds) + creds := grpc.WithPerRPCCredentials(*clientCreds) opts = append(opts, creds) return opts, nil } From c236d3dbaddd23b25373c0e90d9b80e7c7888409 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Wed, 24 Jan 2024 16:58:07 +0100 Subject: [PATCH 2/2] Fix bad conflict resolution Signed-off-by: Tim Vaillancourt --- go/vt/grpcclient/client_auth_static.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/go/vt/grpcclient/client_auth_static.go b/go/vt/grpcclient/client_auth_static.go index 95a4b480860..f7f3feb87fa 100644 --- a/go/vt/grpcclient/client_auth_static.go +++ b/go/vt/grpcclient/client_auth_static.go @@ -71,17 +71,17 @@ func loadStaticAuthCredsFromFile(credsFile string) (*StaticAuthClientCreds, erro // AppendStaticAuth optionally appends static auth credentials if provided. func AppendStaticAuth(opts []grpc.DialOption) ([]grpc.DialOption, error) { - if credsFile == "" { + if *credsFile == "" { return opts, nil } var err error credsFileOnce.Do(func() { - clientCreds, err = loadStaticAuthCredsFromFile(credsFile) + clientCreds, err = loadStaticAuthCredsFromFile(*credsFile) }) if err != nil { return nil, err } - creds := grpc.WithPerRPCCredentials(*clientCreds) + creds := grpc.WithPerRPCCredentials(clientCreds) opts = append(opts, creds) return opts, nil }