Skip to content

Commit

Permalink
Merge pull request #164 from EmmEff/cleanly-handle-401
Browse files Browse the repository at this point in the history
Return ErrUnauthorized
  • Loading branch information
EmmEff authored Feb 21, 2023
2 parents a90be3c + 577dfa2 commit dc24824
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2019-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
// distributed with the sources of this project regarding your rights to use or distribute this
// software.
Expand All @@ -7,6 +7,7 @@ package client

import (
"context"
"errors"
"fmt"
"io"
"net/http"
Expand All @@ -16,6 +17,9 @@ import (
"github.com/go-log/log"
)

// ErrUnauthorized represents HTTP status "401 Unauthorized"
var ErrUnauthorized = errors.New("unauthorized")

// Config contains the client configuration.
type Config struct {
// Base URL of the service.
Expand Down
10 changes: 9 additions & 1 deletion client/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,11 @@ func (r *ociRegistry) doRequestWithCredentials(req *http.Request, creds credenti
if code := res.StatusCode; code/100 != 2 {
defer res.Body.Close()

return nil, fmt.Errorf("unexpected HTTP status %v", res.StatusCode)
if code == http.StatusUnauthorized {
return nil, ErrUnauthorized
}

return nil, fmt.Errorf("unexpected http status %v", res.StatusCode)
}

return res, nil
Expand Down Expand Up @@ -494,6 +498,10 @@ func (r *ociRegistry) doRequest(req *http.Request, creds credentials, opts ...mo
return r.retryRequestWithCredentials(req, creds, opts...)
}

if code == http.StatusUnauthorized {
return nil, ErrUnauthorized
}

return nil, fmt.Errorf("unexpected http status %v", code)
}

Expand Down
10 changes: 8 additions & 2 deletions client/pull.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -63,6 +63,9 @@ func (c *Client) DownloadImage(ctx context.Context, w io.Writer, arch, path, tag
if err != nil {
return fmt.Errorf("download did not succeed: %v", err)
}
if res.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return fmt.Errorf("unexpected http status code: %d", res.StatusCode)
}

Expand Down Expand Up @@ -228,7 +231,10 @@ func (c *Client) legacyDownloadImage(ctx context.Context, arch, name, tag string
}

if res.StatusCode != http.StatusSeeOther {
return fmt.Errorf("unexpected HTTP status %d: %v", res.StatusCode, err)
if res.StatusCode == http.StatusUnauthorized {
return ErrUnauthorized
}
return fmt.Errorf("unexpected http status %d", res.StatusCode)
}

// Get image metadata to determine image size
Expand Down

0 comments on commit dc24824

Please sign in to comment.