From 3cb6ca425fa90219cec8ac18ae1a1dacb37ca56e Mon Sep 17 00:00:00 2001 From: Nicolas Olivier Date: Wed, 11 Sep 2019 14:39:50 -0400 Subject: [PATCH] Add Basic Auth support for Schema Registry (#27) --- registry/client.go | 18 ++++++++++++++++++ registry/client_internal_test.go | 8 ++++++++ registry/client_test.go | 17 +++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/registry/client.go b/registry/client.go index 7fe6e736..70e26441 100644 --- a/registry/client.go +++ b/registry/client.go @@ -57,6 +57,11 @@ type idPayload struct { ID int `json:"id"` } +type credentials struct { + username string + password string +} + var defaultClient = &http.Client{ Transport: &http.Transport{ Proxy: http.ProxyFromEnvironment, @@ -78,11 +83,20 @@ func WithHTTPClient(client *http.Client) ClientFunc { } } +// WithBasicAuth sets the credentials to perform http basic auth. +func WithBasicAuth(username string, password string) ClientFunc { + return func(c *Client) { + c.creds = credentials{username: username, password: password} + } +} + // Client is an HTTP registry client type Client struct { client *http.Client base string + creds credentials + cache *concurrent.Map // map[int]avro.Schema } @@ -210,6 +224,10 @@ func (c *Client) request(method, uri string, in, out interface{}) error { req, _ := http.NewRequest(method, c.base+uri, body) // This error is not possible as we already parsed the url req.Header.Set("Content-Type", contentType) + if len(c.creds.username) > 0 || len(c.creds.password) > 0 { + req.SetBasicAuth(c.creds.username, c.creds.password) + } + resp, err := c.client.Do(req) if err != nil { return err diff --git a/registry/client_internal_test.go b/registry/client_internal_test.go index cdc4c9ee..bbd02563 100644 --- a/registry/client_internal_test.go +++ b/registry/client_internal_test.go @@ -14,3 +14,11 @@ func TestNewClient_WithHTTPClient(t *testing.T) { assert.Equal(t, client.client, httpClient) } + +func TestNewClient_WithBasicAuth(t *testing.T) { + creds := credentials{username:"username", password:"password"} + + client, _ := NewClient("http://example.com", WithBasicAuth("username", "password")) + + assert.Equal(t, client.creds, creds) +} \ No newline at end of file diff --git a/registry/client_test.go b/registry/client_test.go index f509b70c..862d053e 100644 --- a/registry/client_test.go +++ b/registry/client_test.go @@ -42,6 +42,23 @@ func TestClient_PopulatesError(t *testing.T) { assert.Equal(t, "schema may not be empty", regError.Message) } +func TestNewClient_BasicAuth(t *testing.T) { + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + username, password, ok := r.BasicAuth() + assert.True(t, ok) + assert.Equal(t, "username", username) + assert.Equal(t, "password", password) + + _, _ = w.Write([]byte(`[]`)) + })) + defer s.Close() + client, _ := registry.NewClient(s.URL, registry.WithBasicAuth("username", "password")) + + _, err := client.GetSubjects() + + assert.NoError(t, err) +} + func TestClient_GetSchema(t *testing.T) { s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "GET", r.Method)