Skip to content

Commit

Permalink
Add support for basic auth username and password (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmajis authored Apr 3, 2020
1 parent 7ed6949 commit f7b66cb
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
6 changes: 5 additions & 1 deletion cli/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/ricardo-ch/go-kafka-connect/lib/connectors"
"log"

"github.com/ricardo-ch/go-kafka-connect/lib/connectors"
)

func printResponse(response interface{}) error {
Expand All @@ -25,6 +26,9 @@ func getClient() connectors.HighLevelClient {
if SSLInsecure {
client.SetInsecureSSL()
}
if basicAuthUsername != "" && basicAuthPassword != "" {
client.SetBasicAuth(basicAuthUsername, basicAuthPassword)
}
if len(SSLClientCertificate) > 0 && len(SSLClientPrivateKey) > 0 {
cert, err := tls.LoadX509KeyPair(SSLClientCertificate, SSLClientPrivateKey)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (
parallel int
SSLClientCertificate string
SSLClientPrivateKey string
basicAuthUsername string
basicAuthPassword string
)

var RootCmd = &cobra.Command{
Expand All @@ -60,4 +62,6 @@ func init() {
RootCmd.PersistentFlags().BoolVarP(&SSLInsecure, "insecure-skip-verify", "i", false, `skip SSL/TLS verification`)
RootCmd.PersistentFlags().StringVarP(&SSLClientCertificate, "ssl-client-certificate", "C", "", `path to client certificate, must contain PEM encoded data`)
RootCmd.PersistentFlags().StringVarP(&SSLClientPrivateKey, "ssl-client-key", "K", "", `path to client private key`)
RootCmd.PersistentFlags().StringVarP(&basicAuthUsername, "username", "U", "", `HTTP Basic Auth username`)
RootCmd.PersistentFlags().StringVarP(&basicAuthPassword, "password", "P", "", `HTTP Basic Auth password`)
}
10 changes: 8 additions & 2 deletions lib/connectors/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package connectors
import (
"crypto/tls"
"fmt"
"github.com/pkg/errors"
"gopkg.in/resty.v1"
"strconv"
"time"

"github.com/pkg/errors"
"gopkg.in/resty.v1"
)

// BaseClient implement the kafka-connect contract as a client
Expand All @@ -29,6 +30,7 @@ type BaseClient interface {
SetInsecureSSL()
SetDebug()
SetClientCertificates(certs ...tls.Certificate)
SetBasicAuth(username string, password string)
}

type baseClient struct {
Expand All @@ -47,6 +49,10 @@ func (c *baseClient) SetClientCertificates(certs ...tls.Certificate) {
c.restClient.SetCertificates(certs...)
}

func (c *baseClient) SetBasicAuth(username string, password string) {
c.restClient.SetBasicAuth(username, password)
}

//ErrorResponse is generic error returned by kafka connect
type ErrorResponse struct {
ErrorCode int `json:"error_code,omitempty"`
Expand Down
5 changes: 5 additions & 0 deletions lib/connectors/highlevel_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type HighLevelClient interface {
SetDebug()
SetClientCertificates(certs ...tls.Certificate)
SetParallelism(value int)
SetBasicAuth(username string, password string)
}

type highLevelClient struct {
Expand Down Expand Up @@ -65,6 +66,10 @@ func (c *highLevelClient) SetClientCertificates(certs ...tls.Certificate) {
c.client.SetClientCertificates(certs...)
}

func (c *highLevelClient) SetBasicAuth(username string, password string) {
c.client.SetBasicAuth(username, password)
}

//GetAll gets the list of all active connectors
func (c *highLevelClient) GetAll() (GetAllConnectorsResponse, error) {
return c.client.GetAll()
Expand Down
5 changes: 5 additions & 0 deletions lib/connectors/mock_base_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lib/connectors/mock_high_level_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f7b66cb

Please sign in to comment.