From 2d54b138d4dc710e951ce7c6ed687eea5d7bb903 Mon Sep 17 00:00:00 2001 From: Alessandro Zanatta Date: Fri, 17 Jan 2025 13:18:15 +0100 Subject: [PATCH] Keep the "library" namespace if it was specified in the image identifier. Fixes #1012. Signed-off-by: Alessandro Zanatta --- registry-scanner/pkg/image/image.go | 5 +++-- registry-scanner/pkg/image/image_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/registry-scanner/pkg/image/image.go b/registry-scanner/pkg/image/image.go index 01261be4..889fea5e 100644 --- a/registry-scanner/pkg/image/image.go +++ b/registry-scanner/pkg/image/image.go @@ -41,8 +41,9 @@ func NewFromIdentifier(identifier string) *ContainerImage { } img.ImageAlias = alias img.ImageName = reference.Path(parsed) - // if library/ was added to the image name, remove it - if !strings.HasPrefix(imgRef, "library/") { + // if library/ was added to the image name, + // but the original identifier did not have it, remove it + if !strings.Contains(imgRef, "library/") { img.ImageName = strings.TrimPrefix(img.ImageName, "library/") } if digested, ok := parsed.(reference.Digested); ok { diff --git a/registry-scanner/pkg/image/image_test.go b/registry-scanner/pkg/image/image_test.go index 17094619..92ef92bb 100644 --- a/registry-scanner/pkg/image/image_test.go +++ b/registry-scanner/pkg/image/image_test.go @@ -120,6 +120,18 @@ func Test_ParseImageTags(t *testing.T) { assert.Equal(t, "classic-web", image.ImageName) assert.Nil(t, image.ImageTag) }) + t.Run("#1012 Parse docker.io image and keep library if present", func(t *testing.T) { + image := NewFromIdentifier("docker.io/library/postgres") + assert.Equal(t, "docker.io", image.RegistryURL) + assert.Equal(t, "library/postgres", image.ImageName) + assert.Nil(t, image.ImageTag) + }) + t.Run("#1012 Parse docker.io image and do not keep library if absent", func(t *testing.T) { + image := NewFromIdentifier("docker.io/postgres") + assert.Equal(t, "docker.io", image.RegistryURL) + assert.Equal(t, "postgres", image.ImageName) + assert.Nil(t, image.ImageTag) + }) } func Test_ImageToString(t *testing.T) {