@@ -353,13 +353,17 @@ type PulsarAdminConfig struct {
353353 // Either Token or OAuth2 configuration must be provided
354354 // The Token used for authentication.
355355 Token string
356+ // TokenFilePath points to a local file that contains the token.
357+ TokenFilePath string
356358
357359 // OAuth2 related configuration used for authentication.
358360 IssuerEndpoint string
359361 ClientID string
360362 Audience string
361363 Key string
362- Scope string
364+ // KeyFilePath points to a local file that contains the OAuth2 credentials JSON.
365+ KeyFilePath string
366+ Scope string
363367
364368 // TLS Authentication related configuration
365369 ClientCertificatePath string
@@ -375,7 +379,7 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
375379 var err error
376380 var adminClient admin.Client
377381
378- config := & config.Config {
382+ cfg := & config.Config {
379383 WebServiceURL : conf .WebServiceURL ,
380384 TLSAllowInsecureConnection : conf .TLSAllowInsecureConnection ,
381385 TLSEnableHostnameVerification : conf .TLSEnableHostnameVerification ,
@@ -385,10 +389,11 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
385389 }
386390
387391 if conf .PulsarAPIVersion != nil {
388- config .PulsarAPIVersion = * conf .PulsarAPIVersion
392+ cfg .PulsarAPIVersion = * conf .PulsarAPIVersion
389393 }
390394
391- if conf .Key != "" {
395+ keyFilePath = conf .KeyFilePath
396+ if keyFilePath == "" && conf .Key != "" {
392397 keyFile , err = os .CreateTemp ("" , "oauth2-key-" )
393398 if err != nil {
394399 return nil , err
@@ -398,12 +403,14 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
398403 if err != nil {
399404 return nil , err
400405 }
406+ }
401407
402- config .IssuerEndpoint = conf .IssuerEndpoint
403- config .ClientID = conf .ClientID
404- config .Audience = conf .Audience
405- config .KeyFile = keyFilePath
406- config .Scope = conf .Scope
408+ if keyFilePath != "" {
409+ cfg .IssuerEndpoint = conf .IssuerEndpoint
410+ cfg .ClientID = conf .ClientID
411+ cfg .Audience = conf .Audience
412+ cfg .KeyFile = keyFilePath
413+ cfg .Scope = conf .Scope
407414
408415 oauthProvider , err := auth .NewAuthenticationOAuth2WithFlow (oauth2.Issuer {
409416 IssuerEndpoint : conf .IssuerEndpoint ,
@@ -417,27 +424,34 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
417424 if err != nil {
418425 return nil , err
419426 }
420- adminClient , err = admin .NewPulsarClientWithAuthProvider (config , oauthProvider )
427+ adminClient , err = admin .NewPulsarClientWithAuthProvider (cfg , oauthProvider )
428+ if err != nil {
429+ return nil , err
430+ }
431+ } else if conf .TokenFilePath != "" {
432+ cfg .TokenFile = conf .TokenFilePath
433+
434+ adminClient , err = admin .New (cfg )
421435 if err != nil {
422436 return nil , err
423437 }
424438 } else if conf .Token != "" {
425- config .Token = conf .Token
439+ cfg .Token = conf .Token
426440
427- adminClient , err = admin .New (config )
441+ adminClient , err = admin .New (cfg )
428442 if err != nil {
429443 return nil , err
430444 }
431445 } else if conf .ClientCertificatePath != "" {
432- config .AuthPlugin = auth .TLSPluginName
433- config .AuthParams = fmt .Sprintf ("{\" tlsCertFile\" : %q, \" tlsKeyFile\" : %q}" , conf .ClientCertificatePath , conf .ClientCertificateKeyPath )
446+ cfg .AuthPlugin = auth .TLSPluginName
447+ cfg .AuthParams = fmt .Sprintf ("{\" tlsCertFile\" : %q, \" tlsKeyFile\" : %q}" , conf .ClientCertificatePath , conf .ClientCertificateKeyPath )
434448
435- adminClient , err = admin .New (config )
449+ adminClient , err = admin .New (cfg )
436450 if err != nil {
437451 return nil , err
438452 }
439453 } else {
440- adminClient , err = admin .New (config )
454+ adminClient , err = admin .New (cfg )
441455 if err != nil {
442456 return nil , err
443457 }
0 commit comments