Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions alpha/declcfg/declcfg_to_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/blang/semver/v4"
"go.podman.io/image/v5/docker/reference"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation"

Expand Down Expand Up @@ -128,6 +129,15 @@ func ConvertToModel(cfg DeclarativeConfig) (model.Model, error) {
return nil, fmt.Errorf("package %q does not match %q property %q", b.Package, property.TypePackage, props.Packages[0].PackageName)
}

if err := validateImagePullSpec(b.Image, "package %q bundle %q image", b.Package, b.Name); err != nil {
return nil, err
}
for i, rel := range b.RelatedImages {
if err := validateImagePullSpec(rel.Image, "package %q bundle %q relatedImages[%d].image", b.Package, b.Name, i); err != nil {
return nil, err
}
}

// Parse version from the package property.
rawVersion := props.Packages[0].Version
ver, err := semver.Parse(rawVersion)
Expand Down Expand Up @@ -269,3 +279,15 @@ func relatedImagesToModelRelatedImages(in []RelatedImage) []model.RelatedImage {
}
return out
}

// validateImagePullSpec checks that a non-empty image pull spec is valid
// Empty pull specs are not validated.
func validateImagePullSpec(pullSpec, errFormat string, errArgs ...interface{}) error {
if pullSpec == "" {
return nil
}
if _, err := reference.ParseNormalizedNamed(pullSpec); err != nil {
return fmt.Errorf(errFormat+": invalid image pull spec %q: %w", append(errArgs, pullSpec, err)...)
}
return nil
}
76 changes: 76 additions & 0 deletions alpha/declcfg/declcfg_to_model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,71 @@ func TestConvertToModel(t *testing.T) {
})},
},
},
{
name: "Error/BundleImageInvalidPullSpecUnsupportedDigestSsha256",
assertion: hasErrorContaining("invalid image pull spec"),
cfg: DeclarativeConfig{
Packages: []Package{newTestPackage("foo", "alpha", svgSmallCircle)},
Channels: []Channel{newTestChannel("foo", "alpha", ChannelEntry{Name: testBundleName("foo", "0.1.0")})},
Bundles: []Bundle{newTestBundle("foo", "0.1.0", func(b *Bundle) {
// Misspelled digest algorithm: ssha256 instead of sha256 (unsupported hash type)
b.Image = "quay.io/operator-framework/foo-bundle@ssha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"
})},
},
},
{
name: "Error/BundleImageInvalidPullSpecUnsupportedDigestMd5",
assertion: hasErrorContaining("invalid image pull spec"),
cfg: DeclarativeConfig{
Packages: []Package{newTestPackage("foo", "alpha", svgSmallCircle)},
Channels: []Channel{newTestChannel("foo", "alpha", ChannelEntry{Name: testBundleName("foo", "0.1.0")})},
Bundles: []Bundle{newTestBundle("foo", "0.1.0", func(b *Bundle) {
b.Image = "quay.io/operator-framework/foo-bundle@md5:abcd1234abcd1234abcd1234abcd1234"
})},
},
},
{
name: "Error/BundleRelatedImageInvalidPullSpecSsha256",
assertion: hasErrorContaining("invalid image pull spec"),
cfg: DeclarativeConfig{
Packages: []Package{newTestPackage("foo", "alpha", svgSmallCircle)},
Channels: []Channel{newTestChannel("foo", "alpha", ChannelEntry{Name: testBundleName("foo", "0.1.0")})},
Bundles: []Bundle{newTestBundle("foo", "0.1.0", func(b *Bundle) {
b.RelatedImages = []RelatedImage{
{Name: "bundle", Image: testBundleImage("foo", "0.1.0")},
{Name: "operator", Image: "quay.io/operator-framework/my-operator@ssha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the same kind of invalid pull spec is being used here and in line 517. But here it's an invalid pull spec, and for the test case in 517 its an unsupported digest. Is there a difference between these two cases that we care about?

}
})},
},
},
{
name: "Success/BundleImageValidSha256Digest",
assertion: require.NoError,
cfg: DeclarativeConfig{
Packages: []Package{newTestPackage("foo", "alpha", svgSmallCircle)},
Channels: []Channel{newTestChannel("foo", "alpha", ChannelEntry{Name: testBundleName("foo", "0.1.0")})},
Bundles: []Bundle{newTestBundle("foo", "0.1.0", func(b *Bundle) {
b.Image = "quay.io/operator-framework/foo-bundle@sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"
b.RelatedImages = []RelatedImage{
{Name: "bundle", Image: "quay.io/operator-framework/foo-bundle@sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"},
}
})},
},
},
{
name: "Success/BundleImageValidTagWithDigest",
assertion: require.NoError,
cfg: DeclarativeConfig{
Packages: []Package{newTestPackage("foo", "alpha", svgSmallCircle)},
Channels: []Channel{newTestChannel("foo", "alpha", ChannelEntry{Name: testBundleName("foo", "0.1.0")})},
Bundles: []Bundle{newTestBundle("foo", "0.1.0", func(b *Bundle) {
b.Image = "quay.io/operator-framework/foo-bundle:v0.1.0@sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"
b.RelatedImages = []RelatedImage{
{Name: "bundle", Image: "quay.io/operator-framework/foo-bundle:v0.1.0@sha256:abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234"},
}
})},
},
},
}

for _, s := range specs {
Expand Down Expand Up @@ -577,3 +642,14 @@ func hasError(expectedError string) require.ErrorAssertionFunc {
t.FailNow()
}
}

// hasErrorContaining returns an ErrorAssertionFunc that passes when the error message contains the given substring.
func hasErrorContaining(substring string) require.ErrorAssertionFunc {
return func(t require.TestingT, actualError error, args ...interface{}) {
if stdt, ok := t.(*testing.T); ok {
stdt.Helper()
}
require.Error(t, actualError)
require.Contains(t, actualError.Error(), substring, "expected error to contain %q", substring)
}
}