@@ -30,7 +30,7 @@ type GRPCDialer struct {
30
30
URL string
31
31
KeepAliveOptions KeepAliveOptions
32
32
TLSConfig * tls.Config
33
- TokenFile string
33
+ Token string
34
34
mu sync.Mutex // Mutex to protect the connection.
35
35
conn * grpc.ClientConn // Cached gRPC client connection.
36
36
}
@@ -66,15 +66,10 @@ func (d *GRPCDialer) Dial() (*grpc.ClientConn, error) {
66
66
if d .TLSConfig != nil {
67
67
// Enable TLS
68
68
dialOpts = append (dialOpts , grpc .WithTransportCredentials (credentials .NewTLS (d .TLSConfig )))
69
- if len (d .TokenFile ) != 0 {
70
- // Use token-based authentication if token file is provided.
71
- token , err := os .ReadFile (d .TokenFile )
72
- if err != nil {
73
- return nil , fmt .Errorf ("failed to read token file %s, %v" , d .TokenFile , err )
74
- }
69
+ if len (d .Token ) != 0 {
75
70
perRPCCred := oauth.TokenSource {
76
71
TokenSource : oauth2 .StaticTokenSource (& oauth2.Token {
77
- AccessToken : string ( token ) ,
72
+ AccessToken : d . Token ,
78
73
})}
79
74
// Add per-RPC credentials to the dial options.
80
75
dialOpts = append (dialOpts , grpc .WithPerRPCCredentials (perRPCCred ))
@@ -130,6 +125,8 @@ type GRPCConfig struct {
130
125
ClientKeyFile string `json:"clientKeyFile,omitempty" yaml:"clientKeyFile,omitempty"`
131
126
// TokenFile is the file path to a token file for authentication.
132
127
TokenFile string `json:"tokenFile,omitempty" yaml:"tokenFile,omitempty"`
128
+ // Token is the token for authentication
129
+ Token string `json:"token" yaml:"token"`
133
130
// keepalive options
134
131
KeepAliveConfig KeepAliveConfig `json:"keepAliveConfig,omitempty" yaml:"keepAliveConfig,omitempty"`
135
132
}
@@ -175,14 +172,22 @@ func BuildGRPCOptionsFromFlags(configPath string) (*GRPCOptions, error) {
175
172
if config .ClientCertFile != "" && config .ClientKeyFile != "" && config .CAFile == "" {
176
173
return nil , fmt .Errorf ("setting clientCertFile and clientKeyFile requires caFile" )
177
174
}
178
- if config .TokenFile != "" && config .CAFile == "" {
175
+ token := config .Token
176
+ if config .Token == "" && config .TokenFile != "" {
177
+ tokenBytes , err := os .ReadFile (config .TokenFile )
178
+ if err != nil {
179
+ return nil , fmt .Errorf ("failed to read token file %s, %v" , config .TokenFile , err )
180
+ }
181
+ token = string (tokenBytes )
182
+ }
183
+ if token != "" && config .CAFile == "" {
179
184
return nil , fmt .Errorf ("setting tokenFile requires caFile" )
180
185
}
181
186
182
187
options := & GRPCOptions {
183
188
Dialer : & GRPCDialer {
184
- URL : config .URL ,
185
- TokenFile : config . TokenFile ,
189
+ URL : config .URL ,
190
+ Token : token ,
186
191
},
187
192
}
188
193
0 commit comments