Skip to content

Commit e8c97a8

Browse files
committed
add retry for 401 errors to image imported to try pull image without authentication. This is to eliminate case when we try pull public images with wrong/expired secret and it blocks all imports
1 parent d5f8d8b commit e8c97a8

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

pkg/image/importer/importer.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ func formatRepositoryError(ref imageapi.DockerImageReference, err error) error {
340340
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", ref.Exact()))
341341
case strings.HasSuffix(err.Error(), "no basic auth credentials"):
342342
err = kapierrors.NewUnauthorized(fmt.Sprintf("you may not have access to the Docker image %q", ref.Exact()))
343+
case strings.HasSuffix(err.Error(), "incorrect username or password"):
344+
err = kapierrors.NewUnauthorized(fmt.Sprintf("incorrect username or password for image %q", ref.Exact()))
343345
}
344346
return err
345347
}

pkg/image/registry/imagestreamimport/rest.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package imagestreamimport
33
import (
44
"fmt"
55
"net/http"
6+
"strings"
67
"time"
78

89
"github.com/golang/glog"
@@ -199,6 +200,34 @@ func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, createValidati
199200
return nil, kapierrors.NewInternalError(err)
200201
}
201202

203+
// check imported images status. If we get authentication error (401), try import same image without authentication.
204+
// Docker registry gives 401 on public images if you have wrong secret in your secret list.
205+
// this block was introduced by PR #18012
206+
var imageStatus []metav1.Status
207+
importFailed := false
208+
for _, image := range isi.Status.Images {
209+
//cache all imports status
210+
imageStatus = append(imageStatus, image.Status)
211+
if image.Status.Reason == metav1.StatusReasonUnauthorized && strings.Contains(strings.ToLower(image.Status.Message), "username or password") {
212+
importFailed = true
213+
}
214+
}
215+
// try import IS without auth if it failed before
216+
if importFailed {
217+
importCtx := registryclient.NewContext(r.transport, r.insecureTransport).WithCredentials(nil)
218+
imports := r.importFn(importCtx)
219+
//TODO add check if we get error and run import outside the loop
220+
if err := imports.Import(ctx.(gocontext.Context), isi, stream); err != nil {
221+
return nil, kapierrors.NewInternalError(err)
222+
}
223+
}
224+
//cycle through status and set old messages so not to confuse users
225+
for key, image := range isi.Status.Images {
226+
if image.Status.Reason == metav1.StatusReasonUnauthorized {
227+
isi.Status.Images[key].Status = imageStatus[key]
228+
}
229+
}
230+
202231
// if we encountered an error loading credentials and any images could not be retrieved with an access
203232
// related error, modify the message.
204233
// TODO: set a status cause

test/cmd/images.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,14 @@ os::cmd::expect_success 'oc delete project merge-tags'
309309
echo "apply new imagestream tags: ok"
310310
os::test::junit::declare_suite_end
311311

312+
# test importing images with wrong docker secrets
313+
os::test::junit::declare_suite_start "cmd/images${IMAGES_TESTS_POSTFIX:-}/import-public-images-with-fake-secret"
314+
os::cmd::expect_success 'oc new-project import-images'
315+
os::cmd::expect_success 'oc create secret docker-registry dummy-secret1 --docker-server=docker.io --docker-username=dummy1 --docker-password=dummy1 [email protected]'
316+
os::cmd::expect_success 'oc create secret docker-registry dummy-secret2 --docker-server=docker.io --docker-username=dummy2 --docker-password=dummy2 [email protected]'
317+
os::cmd::expect_success_and_text 'oc import-image example --from=openshift/hello-openshift --confirm' 'The import completed successfully'
318+
os::cmd::expect_success 'oc delete project import-images'
319+
echo "import public images with fake secret ok"
320+
os::test::junit::declare_suite_end
321+
312322
os::test::junit::declare_suite_end

0 commit comments

Comments
 (0)