Skip to content

Commit 014e17c

Browse files
authored
fix: improve new client with auth provider (streamnative#518)
Signed-off-by: Zixuan Liu <[email protected]>
1 parent d6febda commit 014e17c

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

pkg/auth/auth_provider.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ package auth
2020
import (
2121
"crypto/tls"
2222
"crypto/x509"
23-
"fmt"
2423
"io/ioutil"
2524
"net/http"
26-
"os"
2725

2826
"github.com/streamnative/pulsarctl/pkg/pulsar/common"
2927
)
@@ -41,8 +39,10 @@ type Transport struct {
4139

4240
func GetAuthProvider(config *common.Config) (*Provider, error) {
4341
var provider Provider
44-
defaultTransport := GetDefaultTransport(config)
45-
var err error
42+
defaultTransport, err := NewDefaultTransport(config)
43+
if err != nil {
44+
return nil, err
45+
}
4646
switch config.AuthPlugin {
4747
case TLSPluginShortName:
4848
fallthrough
@@ -68,21 +68,31 @@ func GetAuthProvider(config *common.Config) (*Provider, error) {
6868
return &provider, err
6969
}
7070

71+
// GetDefaultTransport gets a default transport.
72+
// Deprecated: Use NewDefaultTransport instead.
7173
func GetDefaultTransport(config *common.Config) http.RoundTripper {
74+
transport, err := NewDefaultTransport(config)
75+
if err != nil {
76+
panic(err)
77+
}
78+
79+
return transport
80+
}
81+
82+
func NewDefaultTransport(config *common.Config) (http.RoundTripper, error) {
7283
transport := http.DefaultTransport.(*http.Transport).Clone()
7384
tlsConfig := &tls.Config{
7485
InsecureSkipVerify: config.TLSAllowInsecureConnection,
7586
}
7687
if len(config.TLSTrustCertsFilePath) > 0 {
7788
rootCA, err := ioutil.ReadFile(config.TLSTrustCertsFilePath)
7889
if err != nil {
79-
fmt.Fprintln(os.Stderr, "error loading certificate authority:", err)
80-
os.Exit(1)
90+
return nil, err
8191
}
8292
tlsConfig.RootCAs = x509.NewCertPool()
8393
tlsConfig.RootCAs.AppendCertsFromPEM(rootCA)
8494
}
8595
transport.MaxIdleConnsPerHost = 10
8696
transport.TLSClientConfig = tlsConfig
87-
return transport
97+
return transport, nil
8898
}

pkg/pulsar/admin.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,24 @@ func New(config *common.Config) (Client, error) {
8585
return c, err
8686
}
8787

88+
// NewWithAuthProvider creates a client with auth provider.
89+
// Deprecated: Use NewPulsarClientWithAuthProvider instead.
8890
func NewWithAuthProvider(config *common.Config, authProvider auth.Provider) Client {
89-
defaultTransport := auth.GetDefaultTransport(config)
91+
client, err := NewPulsarClientWithAuthProvider(config, authProvider)
92+
if err != nil {
93+
panic(err)
94+
}
95+
return client
96+
}
97+
98+
// NewPulsarClientWithAuthProvider create a client with auth provider.
99+
func NewPulsarClientWithAuthProvider(config *common.Config,
100+
authProvider auth.Provider) (Client, error) {
101+
defaultTransport, err := auth.NewDefaultTransport(config)
102+
if err != nil {
103+
return nil, err
104+
}
105+
90106
authProvider.WithTransport(defaultTransport)
91107

92108
c := &pulsarClient{
@@ -100,7 +116,8 @@ func NewWithAuthProvider(config *common.Config, authProvider auth.Provider) Clie
100116
},
101117
},
102118
}
103-
return c
119+
120+
return c, nil
104121
}
105122

106123
func (c *pulsarClient) endpoint(componentPath string, parts ...string) string {

0 commit comments

Comments
 (0)