Skip to content
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
9 changes: 5 additions & 4 deletions examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main

import (
"fmt"
"net/http"

"github.com/streamnative/pulsarctl/pkg/pulsar"
)
Expand All @@ -28,13 +27,15 @@ import (
func Examples() {
config := &pulsar.Config{
WebServiceURL: "http://localhost:8080",
HTTPClient: http.DefaultClient,

// If the server enable the TLSAuth
// Auth: auth.NewAuthenticationTLS()
// TLSCertFile: filepath,
Comment thread
zymap marked this conversation as resolved.
// TLSKeyFile: key_filepath,
// TLSAllowInsecureConnection: true,

// If the server enable the TokenAuth
// TokenAuth: auth.NewAuthenticationToken()
// Token: token_string,
// TokenFile: toke_filepath,
}

// the default NewPulsarClient will use v2 APIs. If you need to request other version APIs,
Expand Down
27 changes: 27 additions & 0 deletions pkg/auth/auth_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package auth

// Provider provide a general method to add auth message
type Provider interface {
Comment thread
zymap marked this conversation as resolved.
// HasDataForHTTP is used to check if data for HTTP are available
HasDataForHTTP() bool

// GetHTTPHeaders is used to get all auth headers
GetHTTPHeaders() (map[string]string, error)
}
67 changes: 53 additions & 14 deletions pkg/auth/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,28 @@

package auth

import "crypto/tls"
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"

type TLSAuthProvider struct {
certificatePath string
privateKeyPath string
}
"github.com/pkg/errors"
)

// NewAuthenticationTLSWithParams initialize the authentication provider with map param.
func NewAuthenticationTLSWithParams(params map[string]string) *TLSAuthProvider {
return NewAuthenticationTLS(
params["tlsCertFile"],
params["tlsKeyFile"],
)
type TLSAuthProvider struct {
certificatePath string
privateKeyPath string
allowInsecureConnection bool
}

// NewAuthenticationTLS initialize the authentication provider
func NewAuthenticationTLS(certificatePath string, privateKeyPath string) *TLSAuthProvider {
func NewAuthenticationTLS(certificatePath string, privateKeyPath string,
allowInsecureConnection bool) *TLSAuthProvider {

return &TLSAuthProvider{
certificatePath: certificatePath,
privateKeyPath: privateKeyPath,
certificatePath: certificatePath,
privateKeyPath: privateKeyPath,
allowInsecureConnection: allowInsecureConnection,
}
}

Expand All @@ -54,3 +56,40 @@ func (p *TLSAuthProvider) GetTLSCertificate() (*tls.Certificate, error) {
cert, err := tls.LoadX509KeyPair(p.certificatePath, p.privateKeyPath)
return &cert, err
}

func (p *TLSAuthProvider) GetTLSConfig(certFile string, allowInsecureConnection bool) (*tls.Config, error) {
tlsConfig := &tls.Config{
InsecureSkipVerify: allowInsecureConnection,
}

if certFile != "" {
caCerts, err := ioutil.ReadFile(certFile)
if err != nil {
return nil, err
}

tlsConfig.RootCAs = x509.NewCertPool()
if !tlsConfig.RootCAs.AppendCertsFromPEM(caCerts) {
return nil, errors.New("failed to parse root CAs certificates")
}
}

cert, err := p.GetTLSCertificate()
if err != nil {
return nil, err
}

if cert != nil {
tlsConfig.Certificates = []tls.Certificate{*cert}
}

return tlsConfig, nil
}

func (p *TLSAuthProvider) HasDataForHTTP() bool {
return false
}

func (p *TLSAuthProvider) GetHTTPHeaders() (map[string]string, error) {
return nil, errors.New("Unsupported operation")
}
26 changes: 14 additions & 12 deletions pkg/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,6 @@ type TokenAuthProvider struct {
tokenSupplier func() (string, error)
}

// NewAuthenticationTokenWithParams return a interface of Provider with string map.
func NewAuthenticationTokenWithParams(params map[string]string) (*TokenAuthProvider, error) {
switch {
case params["token"] != "":
return NewAuthenticationToken(params["token"]), nil
case params["file"] != "":
return NewAuthenticationTokenFromFile(params["file"]), nil
default:
return nil, errors.New("missing configuration for token auth")
}
}

// NewAuthenticationToken return a interface of Provider with a string token.
func NewAuthenticationToken(token string) *TokenAuthProvider {
return &TokenAuthProvider{
Expand Down Expand Up @@ -83,3 +71,17 @@ func (p *TokenAuthProvider) GetData() ([]byte, error) {
}
return []byte(t), nil
}

func (p *TokenAuthProvider) HasDataForHTTP() bool {
return true
}

func (p *TokenAuthProvider) GetHTTPHeaders() (map[string]string, error) {
data, err := p.GetData()
if err != nil {
return nil, err
}
headers := make(map[string]string)
headers["Authorization"] = "Bearer " + string(data)
return headers, nil
}
Loading