return failure details when unauthorized to push (#1131)

Co-authored-by: Jeffrey Morgan <jmorganca@gmail.com>
This commit is contained in:
Bruce MacDonald 2023-11-16 16:44:18 -05:00 committed by GitHub
parent a5ccf742c1
commit 4b3f4bc7d9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -954,6 +954,9 @@ func PushModel(ctx context.Context, name string, regOpts *RegistryOptions, fn fu
for _, layer := range layers { for _, layer := range layers {
if err := uploadBlob(ctx, mp, layer, regOpts, fn); err != nil { if err := uploadBlob(ctx, mp, layer, regOpts, fn); err != nil {
log.Printf("error uploading blob: %v", err) log.Printf("error uploading blob: %v", err)
if errors.Is(err, errUnauthorized) {
return fmt.Errorf("unable to push %s, make sure this namespace exists and you are authorized to push to it", ParseModelPath(name).GetNamespaceRepository())
}
return err return err
} }
} }
@ -1140,7 +1143,10 @@ func GetSHA256Digest(r io.Reader) (string, int64) {
return fmt.Sprintf("sha256:%x", h.Sum(nil)), n return fmt.Sprintf("sha256:%x", h.Sum(nil)), n
} }
var errUnauthorized = fmt.Errorf("unauthorized")
func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) { func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.ReadSeeker, regOpts *RegistryOptions) (*http.Response, error) {
lastErr := errMaxRetriesExceeded
for try := 0; try < maxRetries; try++ { for try := 0; try < maxRetries; try++ {
resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts) resp, err := makeRequest(ctx, method, requestURL, headers, body, regOpts)
if err != nil { if err != nil {
@ -1161,8 +1167,7 @@ func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.UR
if body != nil { if body != nil {
body.Seek(0, io.SeekStart) body.Seek(0, io.SeekStart)
} }
lastErr = errUnauthorized
continue
case resp.StatusCode == http.StatusNotFound: case resp.StatusCode == http.StatusNotFound:
return nil, os.ErrNotExist return nil, os.ErrNotExist
case resp.StatusCode >= http.StatusBadRequest: case resp.StatusCode >= http.StatusBadRequest:
@ -1177,7 +1182,7 @@ func makeRequestWithRetry(ctx context.Context, method string, requestURL *url.UR
} }
} }
return nil, errMaxRetriesExceeded return nil, lastErr
} }
func makeRequest(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) { func makeRequest(ctx context.Context, method string, requestURL *url.URL, headers http.Header, body io.Reader, regOpts *RegistryOptions) (*http.Response, error) {