Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Commit

Permalink
fix: fix tls insecure connection (#731)
Browse files Browse the repository at this point in the history
Signed-off-by: Zixuan Liu <[email protected]>
  • Loading branch information
nodece authored and maxsxu committed Mar 14, 2023
1 parent d7e895a commit 10c135b
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 29 deletions.
4 changes: 2 additions & 2 deletions pkg/auth/auth_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Transport struct {
T http.RoundTripper
}

func GetAuthProvider(config *common.Config) (*Provider, error) {
func GetAuthProvider(config *common.Config) (Provider, error) {
var provider Provider
defaultTransport, err := NewDefaultTransport(config)
if err != nil {
Expand Down Expand Up @@ -65,7 +65,7 @@ func GetAuthProvider(config *common.Config) (*Provider, error) {
config.IssuerEndpoint, config.ClientID, config.Audience, config.Scope, defaultTransport)
}
}
return &provider, err
return provider, err
}

// GetDefaultTransport gets a default transport.
Expand Down
53 changes: 27 additions & 26 deletions pkg/pulsar/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package pulsar

import (
"fmt"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -61,28 +60,11 @@ type pulsarClient struct {

// New returns a new client
func New(config *common.Config) (Client, error) {
if len(config.WebServiceURL) == 0 {
config.WebServiceURL = DefaultWebServiceURL
}

c := &pulsarClient{
APIVersion: config.PulsarAPIVersion,
Client: &cli.Client{
ServiceURL: config.WebServiceURL,
VersionInfo: ReleaseVersion,
HTTPClient: &http.Client{
Timeout: DefaultHTTPTimeOutDuration,
},
},
}

authProvider, err := auth.GetAuthProvider(config)
if !utils.IsNilFixed(authProvider) {
c.Client.HTTPClient.Transport = *authProvider
} else {
fmt.Printf("No Auth Provider found\n")
if err != nil {
return nil, err
}
return c, err
return NewPulsarClientWithAuthProvider(config, authProvider)
}

// NewWithAuthProvider creates a client with auth provider.
Expand All @@ -98,12 +80,31 @@ func NewWithAuthProvider(config *common.Config, authProvider auth.Provider) Clie
// NewPulsarClientWithAuthProvider create a client with auth provider.
func NewPulsarClientWithAuthProvider(config *common.Config,
authProvider auth.Provider) (Client, error) {
defaultTransport, err := auth.NewDefaultTransport(config)
if err != nil {
return nil, err
var transport http.RoundTripper

if authProvider != nil {
transport = authProvider.Transport()
if transport != nil {
transport = authProvider
}
}

if transport == nil {
defaultTransport, err := auth.NewDefaultTransport(config)
if err != nil {
return nil, err
}
if authProvider != nil {
authProvider.WithTransport(authProvider)
} else {
transport = defaultTransport
}
}

authProvider.WithTransport(defaultTransport)
webServiceURL := config.WebServiceURL
if len(webServiceURL) == 0 {
config.WebServiceURL = DefaultWebServiceURL
}

c := &pulsarClient{
APIVersion: config.PulsarAPIVersion,
Expand All @@ -112,7 +113,7 @@ func NewPulsarClientWithAuthProvider(config *common.Config,
VersionInfo: ReleaseVersion,
HTTPClient: &http.Client{
Timeout: DefaultHTTPTimeOutDuration,
Transport: authProvider,
Transport: transport,
},
},
}
Expand Down
75 changes: 74 additions & 1 deletion pkg/pulsar/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
package pulsar

import (
"net/http"
"testing"

"github.com/streamnative/pulsar-admin-go/pkg/auth"
"github.com/streamnative/pulsar-admin-go/pkg/pulsar/common"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPulsarClientEndpointEscapes(t *testing.T) {
Expand All @@ -31,3 +33,74 @@ func TestPulsarClientEndpointEscapes(t *testing.T) {
expected := "/admin/v2/myendpoint/abc%25%3F%20%2Fdef/ghi"
assert.Equal(t, expected, actual)
}

func TestNew(t *testing.T) {
config := &common.Config{}
admin, err := New(config)
require.NoError(t, err)
require.NotNil(t, admin)
}

func TestNewWithAuthProvider(t *testing.T) {
config := &common.Config{}

tokenAuth, err := auth.NewAuthenticationToken("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9."+
"eyJzdWIiOiJhZG1pbiIsImlhdCI6MTUxNjIzOTAyMn0.sVt6cyu3HKd89LcQvZVMNbqT0DTl3FvG9oYbj8hBDqU", nil)
require.NoError(t, err)
require.NotNil(t, tokenAuth)

admin, err := NewPulsarClientWithAuthProvider(config, tokenAuth)
require.NoError(t, err)
require.NotNil(t, admin)
}

type customAuthProvider struct {
transport http.RoundTripper
}

var _ auth.Provider = &customAuthProvider{}

func (c *customAuthProvider) RoundTrip(req *http.Request) (*http.Response, error) {
panic("implement me")
}

func (c *customAuthProvider) Transport() http.RoundTripper {
return c.transport
}

func (c *customAuthProvider) WithTransport(transport http.RoundTripper) {
c.transport = transport
}

func TestNewWithCustomAuthProviderWithTransport(t *testing.T) {
config := &common.Config{}
defaultTransport, err := auth.NewDefaultTransport(config)
require.NoError(t, err)

customAuthProvider := &customAuthProvider{
transport: defaultTransport,
}

admin, err := NewPulsarClientWithAuthProvider(config, customAuthProvider)
require.NoError(t, err)
require.NotNil(t, admin)

// Expected the transport of customAuthProvider will not be overwritten.
require.Equal(t, defaultTransport, admin.(*pulsarClient).Client.HTTPClient.Transport)
}

func TestNewWithTlsAllowInsecure(t *testing.T) {
config := &common.Config{
TLSAllowInsecureConnection: true,
}
admin, err := New(config)
require.NoError(t, err)
require.NotNil(t, admin)

pulsarClientS := admin.(*pulsarClient)
require.NotNil(t, pulsarClientS.Client.HTTPClient.Transport)
tr := pulsarClientS.Client.HTTPClient.Transport.(*http.Transport)
require.NotNil(t, tr)
require.NotNil(t, tr.TLSClientConfig)
require.True(t, tr.TLSClientConfig.InsecureSkipVerify)
}
6 changes: 6 additions & 0 deletions pkg/pulsar/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ func TestIsNilFixed(t *testing.T) {

var ch chan string
assert.True(t, IsNilFixed(ch))

var nilInterface People
assert.True(t, IsNilFixed(nilInterface))

// pointer to an interface, the IsNilFixed method cannot check this.
assert.False(t, IsNilFixed(&nilInterface))
}

0 comments on commit 10c135b

Please sign in to comment.