Skip to content

Commit 43054c7

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 3647e2b commit 43054c7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-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: 13 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"
@@ -208,6 +209,18 @@ func (r *REST) Create(ctx apirequest.Context, obj runtime.Object, createValidati
208209
return nil, kapierrors.NewInternalError(err)
209210
}
210211

212+
// check imported images status. If we get authentication error (401), try import same image without authentication.
213+
// Docker registry gives 401 on public images if you have wrong secret in your secret list.
214+
for _, image := range isi.Status.Images {
215+
if image.Status.Reason == metav1.StatusReasonUnauthorized && strings.Contains(image.Status.Message, "incorrect username or password") {
216+
importCtx := registryclient.NewContext(r.transport, r.insecureTransport).WithCredentials(nil)
217+
imports := r.importFn(importCtx)
218+
if err := imports.Import(ctx.(gocontext.Context), isi, stream); err != nil {
219+
return nil, kapierrors.NewInternalError(err)
220+
}
221+
}
222+
}
223+
211224
// if we encountered an error loading credentials and any images could not be retrieved with an access
212225
// related error, modify the message.
213226
// 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)