Skip to content

Commit

Permalink
Add Basic Auth support for Schema Registry (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Olivier authored and nrwiersma committed Sep 11, 2019
1 parent 199f352 commit 3cb6ca4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
18 changes: 18 additions & 0 deletions registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions registry/client_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
17 changes: 17 additions & 0 deletions registry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3cb6ca4

Please sign in to comment.