Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slack-vitess-r14.0.5: load --grpc_auth_static_client_creds file once #193

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions go/vt/grpcclient/client_auth_static.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"flag"
"os"
"sync"

"context"

Expand All @@ -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
Expand All @@ -54,17 +58,26 @@ 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
}
Expand Down
Loading