Skip to content

Commit 4b802dd

Browse files
Use basic auth when token endpoint is not found (#356)
Signed-off-by: paulwilljones <paul.jones@jetstack.io>
1 parent 3b389b6 commit 4b802dd

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

pkg/client/selfhosted/selfhosted.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,15 @@ func configureAuth(ctx context.Context, client *Client, opts *Options) error {
114114

115115
token, err := client.setupBasicAuth(ctx, opts.Host, tokenPath)
116116
if httpErr, ok := selfhostederrors.IsHTTPError(err); ok {
117-
return fmt.Errorf("failed to setup token auth (%d): %s",
118-
httpErr.StatusCode, httpErr.Body)
119-
}
120-
if err != nil {
117+
if httpErr.StatusCode == http.StatusNotFound {
118+
client.log.Warnf("Token endpoint not found, using basic auth: %s%s %s", opts.Host, tokenPath, httpErr.Body)
119+
} else {
120+
return fmt.Errorf("failed to setup token auth (%d): %s",
121+
httpErr.StatusCode, httpErr.Body)
122+
}
123+
} else if err != nil {
121124
return fmt.Errorf("failed to setup token auth: %s", err)
122125
}
123-
124126
client.Bearer = token
125127
return nil
126128
}
@@ -229,7 +231,10 @@ func (c *Client) doRequest(ctx context.Context, url, header string, obj interfac
229231
req = req.WithContext(ctx)
230232
if len(c.Bearer) > 0 {
231233
req.Header.Add("Authorization", "Bearer "+c.Bearer)
234+
} else if c.Username != "" && c.Password != "" {
235+
req.SetBasicAuth(c.Username, c.Password)
232236
}
237+
233238
if len(header) > 0 {
234239
req.Header.Set("Accept", header)
235240
}

pkg/client/selfhosted/selfhosted_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package selfhosted
22

33
import (
44
"context"
5+
"encoding/base64"
56
"errors"
7+
"fmt"
68
"net/http"
79
"net/http/httptest"
810
"net/url"
@@ -243,6 +245,31 @@ func TestDoRequest(t *testing.T) {
243245
assert.Error(t, err)
244246
assert.Contains(t, err.Error(), "unexpected")
245247
})
248+
249+
t.Run("use basic auth in request", func(t *testing.T) {
250+
username := "foo"
251+
password := "bar"
252+
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
253+
assert.Equal(t, r.Header.Get("Authorization"), fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(username+":"+password))))
254+
assert.Equal(t, "/v2/repo/image/tags/list", r.URL.Path)
255+
w.WriteHeader(http.StatusOK)
256+
_, _ = w.Write([]byte(`{"tags":["v1","v2"]}`))
257+
}))
258+
defer server.Close()
259+
260+
h, err := url.Parse(server.URL)
261+
assert.NoError(t, err)
262+
263+
client.Username = username
264+
client.Password = password
265+
266+
var tagResponse TagResponse
267+
headers, err := client.doRequest(ctx, h.Host+"/v2/repo/image/tags/list", "", &tagResponse)
268+
269+
assert.NoError(t, err)
270+
assert.NotNil(t, headers)
271+
assert.Equal(t, []string{"v1", "v2"}, tagResponse.Tags)
272+
})
246273
}
247274

248275
func TestSetupBasicAuth(t *testing.T) {

0 commit comments

Comments
 (0)