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) {