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

[Issue 345] Add a new method to create auth provider from tls cert supplier #347

Merged
merged 2 commits into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions pulsar/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package pulsar

import (
"crypto/tls"
"time"

"github.com/apache/pulsar-client-go/pulsar/internal/auth"
Expand Down Expand Up @@ -57,6 +58,11 @@ func NewAuthenticationTLS(certificatePath string, privateKeyPath string) Authent
return auth.NewAuthenticationTLS(certificatePath, privateKeyPath)
}

// Create new Authentication provider with specified TLS certificate supplier
func NewAuthenticationFromTLSCertSupplier(tlsCertSupplier func() (*tls.Certificate, error)) Authentication {
return auth.NewAuthenticationFromTLSCertSupplier(tlsCertSupplier)
}

func NewAuthenticationAthenz(authParams map[string]string) Authentication {
athenz, _ := auth.NewAuthenticationAthenzWithParams(authParams)
return athenz
Expand Down
23 changes: 23 additions & 0 deletions pulsar/client_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package pulsar

import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -162,6 +163,28 @@ func TestTLSAuth(t *testing.T) {
client.Close()
}

func TestTLSAuthWithCertSupplier(t *testing.T) {
supplier := func() (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(tlsClientCertPath, tlsClientKeyPath)
return &cert, err
}
client, err := NewClient(ClientOptions{
URL: serviceURLTLS,
TLSTrustCertsFilePath: caCertsPath,
Authentication: NewAuthenticationFromTLSCertSupplier(supplier),
})
assert.NoError(t, err)

producer, err := client.CreateProducer(ProducerOptions{
Topic: newAuthTopicName(),
})

assert.NoError(t, err)
assert.NotNil(t, producer)

client.Close()
}

func TestTokenAuth(t *testing.T) {
token, err := ioutil.ReadFile(tokenFilePath)
assert.NoError(t, err)
Expand Down
10 changes: 10 additions & 0 deletions pulsar/internal/auth/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import "crypto/tls"
type tlsAuthProvider struct {
certificatePath string
privateKeyPath string
tlsCertSupplier func() (*tls.Certificate, error)
}

// NewAuthenticationTLSWithParams initialize the authentication provider with map param.
Expand All @@ -40,6 +41,12 @@ func NewAuthenticationTLS(certificatePath string, privateKeyPath string) Provide
}
}

func NewAuthenticationFromTLSCertSupplier(tlsCertSupplier func() (*tls.Certificate, error)) Provider {
return &tlsAuthProvider{
tlsCertSupplier: tlsCertSupplier,
}
}

func (p *tlsAuthProvider) Init() error {
// Try to read certificates immediately to provide better error at startup
_, err := p.GetTLSCertificate()
Expand All @@ -51,6 +58,9 @@ func (p *tlsAuthProvider) Name() string {
}

func (p *tlsAuthProvider) GetTLSCertificate() (*tls.Certificate, error) {
if p.tlsCertSupplier != nil {
return p.tlsCertSupplier()
}
cert, err := tls.LoadX509KeyPair(p.certificatePath, p.privateKeyPath)
return &cert, err
}
Expand Down