Skip to content

Commit

Permalink
Adding UnitTests for SelfHosted Client (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcollom committed Aug 27, 2024
1 parent 6f91b09 commit 15e945d
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 35 deletions.
4 changes: 1 addition & 3 deletions pkg/client/selfhosted/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package errors

import "fmt"

type HTTPError struct {
Body []byte
StatusCode int
Expand All @@ -15,7 +13,7 @@ func NewHTTPError(statusCode int, body []byte) *HTTPError {
}

func (h *HTTPError) Error() string {
return fmt.Sprintf("%s", h.Body)
return string(h.Body)
}

func IsHTTPError(err error) (*HTTPError, bool) {
Expand Down
1 change: 0 additions & 1 deletion pkg/client/selfhosted/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func TestRepoImage(t *testing.T) {
t.Errorf("%s: unexpected repo/image, exp=%s/%s got=%s/%s",
test.path, test.expRepo, test.expImage, repo, image)
}

})
}
}
90 changes: 59 additions & 31 deletions pkg/client/selfhosted/selfhosted.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,47 +85,72 @@ func New(ctx context.Context, log *logrus.Entry, opts *Options) (*Client, error)
log: log.WithField("client", opts.Host),
}

// Set up client with host matching if set
if opts.Host != "" {
hostRegex, scheme, err := parseURL(opts.Host)
if err != nil {
return nil, fmt.Errorf("failed parsing url: %s", err)
}
client.hostRegex = hostRegex
client.httpScheme = scheme
if err := configureHost(ctx, client, opts); err != nil {
return nil, err
}

// Setup Auth if username and password used.
if len(opts.Username) > 0 || len(opts.Password) > 0 {
if len(opts.Bearer) > 0 {
return nil, errors.New("cannot specify Bearer token as well as username/password")
}
if err := configureTLS(client, opts); err != nil {
return nil, err
}

tokenPath := opts.TokenPath
if tokenPath == "" {
tokenPath = defaultTokenPath
}
return client, nil
}

token, err := client.setupBasicAuth(ctx, opts.Host, tokenPath)
if httpErr, ok := selfhostederrors.IsHTTPError(err); ok {
return nil, fmt.Errorf("failed to setup token auth (%d): %s",
httpErr.StatusCode, httpErr.Body)
}
func configureHost(ctx context.Context, client *Client, opts *Options) error {
if opts.Host == "" {
return nil
}

if err != nil {
return nil, fmt.Errorf("failed to setup token auth: %s", err)
}
client.Bearer = token
}
hostRegex, scheme, err := parseURL(opts.Host)
if err != nil {
return fmt.Errorf("failed parsing url: %s", err)
}
client.hostRegex = hostRegex
client.httpScheme = scheme

if err := configureAuth(ctx, client, opts); err != nil {
return err
}

// Default to https if no scheme set
return nil
}

func configureAuth(ctx context.Context, client *Client, opts *Options) error {
if len(opts.Username) == 0 && len(opts.Password) == 0 {
return nil
}

if len(opts.Bearer) > 0 {
return errors.New("cannot specify Bearer token as well as username/password")
}

tokenPath := opts.TokenPath
if tokenPath == "" {
tokenPath = defaultTokenPath
}

token, err := client.setupBasicAuth(ctx, opts.Host, tokenPath)
if httpErr, ok := selfhostederrors.IsHTTPError(err); ok {
return fmt.Errorf("failed to setup token auth (%d): %s",
httpErr.StatusCode, httpErr.Body)
}
if err != nil {
return fmt.Errorf("failed to setup token auth: %s", err)
}

client.Bearer = token
return nil
}

func configureTLS(client *Client, opts *Options) error {
if client.httpScheme == "" {
client.httpScheme = "https"
}

if client.httpScheme == "https" {
tlsConfig, err := newTLSConfig(opts.Insecure, opts.CAPath)
if err != nil {
return nil, err
return err
}

client.Client.Transport = &http.Transport{
Expand All @@ -134,7 +159,7 @@ func New(ctx context.Context, log *logrus.Entry, opts *Options) (*Client, error)
}
}

return client, nil
return nil
}

// Name returns the name of the host URL for the selfhosted client
Expand Down Expand Up @@ -229,11 +254,13 @@ func (c *Client) doRequest(ctx context.Context, url, header string, obj interfac
if err != nil {
return nil, fmt.Errorf("failed to get docker image: %s", err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, selfhostederrors.NewHTTPError(resp.StatusCode, body)
Expand Down Expand Up @@ -268,6 +295,7 @@ func (c *Client) setupBasicAuth(ctx context.Context, url, tokenPath string) (str
return "", fmt.Errorf("failed to send basic auth request %q: %s",
req.URL, err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -296,7 +324,7 @@ func newTLSConfig(insecure bool, CAPath string) (*tls.Config, error) {
if CAPath != "" {
certs, err := os.ReadFile(CAPath)
if err != nil {
return nil, fmt.Errorf("Failed to append %q to RootCAs: %v", CAPath, err)
return nil, fmt.Errorf("failed to append %q to RootCAs: %v", CAPath, err)
}
rootCAs.AppendCertsFromPEM(certs)
}
Expand Down
Loading

0 comments on commit 15e945d

Please sign in to comment.