Skip to content

Commit dc24824

Browse files
authored
Merge pull request #164 from EmmEff/cleanly-handle-401
Return ErrUnauthorized
2 parents a90be3c + 577dfa2 commit dc24824

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

client/client.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2019-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2019-2023, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the LICENSE.md file
33
// distributed with the sources of this project regarding your rights to use or distribute this
44
// software.
@@ -7,6 +7,7 @@ package client
77

88
import (
99
"context"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"net/http"
@@ -16,6 +17,9 @@ import (
1617
"github.com/go-log/log"
1718
)
1819

20+
// ErrUnauthorized represents HTTP status "401 Unauthorized"
21+
var ErrUnauthorized = errors.New("unauthorized")
22+
1923
// Config contains the client configuration.
2024
type Config struct {
2125
// Base URL of the service.

client/oci.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,11 @@ func (r *ociRegistry) doRequestWithCredentials(req *http.Request, creds credenti
449449
if code := res.StatusCode; code/100 != 2 {
450450
defer res.Body.Close()
451451

452-
return nil, fmt.Errorf("unexpected HTTP status %v", res.StatusCode)
452+
if code == http.StatusUnauthorized {
453+
return nil, ErrUnauthorized
454+
}
455+
456+
return nil, fmt.Errorf("unexpected http status %v", res.StatusCode)
453457
}
454458

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

501+
if code == http.StatusUnauthorized {
502+
return nil, ErrUnauthorized
503+
}
504+
497505
return nil, fmt.Errorf("unexpected http status %v", code)
498506
}
499507

client/pull.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
1+
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
22
// This software is licensed under a 3-clause BSD license. Please consult the
33
// LICENSE.md file distributed with the sources of this project regarding your
44
// rights to use or distribute this software.
@@ -63,6 +63,9 @@ func (c *Client) DownloadImage(ctx context.Context, w io.Writer, arch, path, tag
6363
if err != nil {
6464
return fmt.Errorf("download did not succeed: %v", err)
6565
}
66+
if res.StatusCode == http.StatusUnauthorized {
67+
return ErrUnauthorized
68+
}
6669
return fmt.Errorf("unexpected http status code: %d", res.StatusCode)
6770
}
6871

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

230233
if res.StatusCode != http.StatusSeeOther {
231-
return fmt.Errorf("unexpected HTTP status %d: %v", res.StatusCode, err)
234+
if res.StatusCode == http.StatusUnauthorized {
235+
return ErrUnauthorized
236+
}
237+
return fmt.Errorf("unexpected http status %d", res.StatusCode)
232238
}
233239

234240
// Get image metadata to determine image size

0 commit comments

Comments
 (0)