diff --git a/cli/cmd/helper.go b/cli/cmd/helper.go index 1af6f52..3b8680e 100644 --- a/cli/cmd/helper.go +++ b/cli/cmd/helper.go @@ -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 { @@ -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 { diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 27af691..7c8b8cc 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -35,6 +35,8 @@ var ( parallel int SSLClientCertificate string SSLClientPrivateKey string + basicAuthUsername string + basicAuthPassword string ) var RootCmd = &cobra.Command{ @@ -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`) } diff --git a/lib/connectors/base_client.go b/lib/connectors/base_client.go index 22a1a9c..b7c9964 100644 --- a/lib/connectors/base_client.go +++ b/lib/connectors/base_client.go @@ -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 @@ -29,6 +30,7 @@ type BaseClient interface { SetInsecureSSL() SetDebug() SetClientCertificates(certs ...tls.Certificate) + SetBasicAuth(username string, password string) } type baseClient struct { @@ -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"` diff --git a/lib/connectors/highlevel_client.go b/lib/connectors/highlevel_client.go index ca02254..5f92a38 100644 --- a/lib/connectors/highlevel_client.go +++ b/lib/connectors/highlevel_client.go @@ -35,6 +35,7 @@ type HighLevelClient interface { SetDebug() SetClientCertificates(certs ...tls.Certificate) SetParallelism(value int) + SetBasicAuth(username string, password string) } type highLevelClient struct { @@ -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() diff --git a/lib/connectors/mock_base_client.go b/lib/connectors/mock_base_client.go index 7d1b403..7cd1c44 100644 --- a/lib/connectors/mock_base_client.go +++ b/lib/connectors/mock_base_client.go @@ -264,6 +264,11 @@ func (_m *MockBaseClient) ResumeConnector(req ConnectorRequest) (EmptyResponse, return r0, r1 } +// SetBasicAuth provides a mock function with given fields: username, password +func (_m *MockBaseClient) SetBasicAuth(username string, password string) { + _m.Called(username, password) +} + // SetClientCertificates provides a mock function with given fields: certs func (_m *MockBaseClient) SetClientCertificates(certs ...tls.Certificate) { _va := make([]interface{}, len(certs)) diff --git a/lib/connectors/mock_high_level_client.go b/lib/connectors/mock_high_level_client.go index 31a6631..0d8e603 100644 --- a/lib/connectors/mock_high_level_client.go +++ b/lib/connectors/mock_high_level_client.go @@ -313,6 +313,11 @@ func (_m *MockHighLevelClient) ResumeConnector(req ConnectorRequest, sync bool) return r0, r1 } +// SetBasicAuth provides a mock function with given fields: username, password +func (_m *MockHighLevelClient) SetBasicAuth(username string, password string) { + _m.Called(username, password) +} + // SetClientCertificates provides a mock function with given fields: certs func (_m *MockHighLevelClient) SetClientCertificates(certs ...tls.Certificate) { _va := make([]interface{}, len(certs))