From 2192c47813e0b8bfd970916980d90269c49bc345 Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Thu, 21 Dec 2023 12:15:26 +0100 Subject: [PATCH 1/2] fix ref detection for ca/ctf --- pkg/common/accessobj/accessstate.go | 2 +- pkg/common/accessobj/check.go | 7 +++++++ pkg/contexts/oci/repositories/ctf/uniform.go | 11 ++++++++++- pkg/contexts/ocm/repositories/comparch/uniform.go | 13 +++++++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/pkg/common/accessobj/accessstate.go b/pkg/common/accessobj/accessstate.go index abd2cc9fdd..a590ec8330 100644 --- a/pkg/common/accessobj/accessstate.go +++ b/pkg/common/accessobj/accessstate.go @@ -311,7 +311,7 @@ func (f *fileBasedAccess) Get() (blobaccess.BlobAccess, error) { return nil, err } if !ok { - return nil, errors.ErrNotFoundWrap(vfs.ErrNotExist, "file", f.path) + return nil, errors.ErrNotFoundWrap(vfs.ErrNotExist, "file", f.path, f.filesystem.Name()) } return blobaccess.ForFile(f.mimeType, f.path, f.filesystem), nil } diff --git a/pkg/common/accessobj/check.go b/pkg/common/accessobj/check.go index a2cdeffc34..f25109e6a2 100644 --- a/pkg/common/accessobj/check.go +++ b/pkg/common/accessobj/check.go @@ -40,6 +40,7 @@ func CheckFile(kind string, createHint string, forcedType bool, path string, fs return mapErr(forcedType, err) } defer file.Close() + forcedType = false r, _, err := compression.AutoDecompress(file) if err != nil { return mapErr(forcedType, err) @@ -63,6 +64,12 @@ func CheckFile(kind string, createHint string, forcedType bool, path string, fs } } } else { + if forcedType { + entries, err := vfs.ReadDir(fs, path) + if err == nil && len(entries) > 0 { + forcedType = false + } + } if ok, err := vfs.FileExists(fs, filepath.Join(path, descriptorname)); !ok || err != nil { if err != nil { return mapErr(forcedType, err) diff --git a/pkg/contexts/oci/repositories/ctf/uniform.go b/pkg/contexts/oci/repositories/ctf/uniform.go index aa95a0458a..2ecc3e462c 100644 --- a/pkg/contexts/oci/repositories/ctf/uniform.go +++ b/pkg/contexts/oci/repositories/ctf/uniform.go @@ -28,6 +28,15 @@ func (h *repospechandler) MapReference(ctx cpi.Context, u *cpi.UniformRepository return MapReference(ctx, u) } +func explicit(t string) bool { + for _, f := range SupportedFormats() { + if t == string(f) { + return true + } + } + return t == Type || t == AltType +} + func MapReference(ctx cpi.Context, u *cpi.UniformRepositorySpec) (cpi.RepositorySpec, error) { path := u.Info if u.Info == "" { @@ -43,7 +52,7 @@ func MapReference(ctx cpi.Context, u *cpi.UniformRepositorySpec) (cpi.Repository if !u.CreateIfMissing { hint = "" } - create, ok, err := accessobj.CheckFile(Type, hint, accessio.TypeForTypeSpec(typ) == Type, path, fs, ArtifactIndexFileName) + create, ok, err := accessobj.CheckFile(Type, hint, explicit(accessio.TypeForTypeSpec(u.Type)), path, fs, ArtifactIndexFileName) if !ok || (err != nil && typ == "") { if err != nil { return nil, err diff --git a/pkg/contexts/ocm/repositories/comparch/uniform.go b/pkg/contexts/ocm/repositories/comparch/uniform.go index 7577571316..81b679ea43 100644 --- a/pkg/contexts/ocm/repositories/comparch/uniform.go +++ b/pkg/contexts/ocm/repositories/comparch/uniform.go @@ -11,15 +11,24 @@ import ( "github.com/open-component-model/ocm/pkg/contexts/ocm/cpi" ) +const AltType = "ca" + func init() { h := &repospechandler{} cpi.RegisterRepositorySpecHandler(h, "") cpi.RegisterRepositorySpecHandler(h, Type) - cpi.RegisterRepositorySpecHandler(h, "ca") + cpi.RegisterRepositorySpecHandler(h, AltType) + for _, f := range GetFormats() { + cpi.RegisterRepositorySpecHandler(h, f) + } } type repospechandler struct{} +func explicit(t string) bool { + return t == Type || t == AltType +} + func (h *repospechandler) MapReference(ctx cpi.Context, u *cpi.UniformRepositorySpec) (cpi.RepositorySpec, error) { path := u.Info if u.Info == "" { @@ -35,7 +44,7 @@ func (h *repospechandler) MapReference(ctx cpi.Context, u *cpi.UniformRepository if !u.CreateIfMissing { hint = "" } - create, ok, err := accessobj.CheckFile(Type, hint, accessio.TypeForTypeSpec(typ) == Type, path, fs, ComponentDescriptorFileName) + create, ok, err := accessobj.CheckFile(Type, hint, explicit(accessio.TypeForTypeSpec(u.Type)), path, fs, ComponentDescriptorFileName) if !ok || (err != nil && typ == "") { if err != nil { return nil, err From 2666b7d9073f31180bb2ca0f10e1b44c8f3e94de Mon Sep 17 00:00:00 2001 From: Uwe Krueger Date: Fri, 22 Dec 2023 14:23:42 +0100 Subject: [PATCH 2/2] fix compound resolver error condition --- .../ocmcmds/components/sign/cmd_test.go | 38 ++++++++++++++++++- pkg/contexts/ocm/interface.go | 1 + pkg/contexts/ocm/internal/errors.go | 1 + pkg/contexts/ocm/resolver.go | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/cmds/ocm/commands/ocmcmds/components/sign/cmd_test.go b/cmds/ocm/commands/ocmcmds/components/sign/cmd_test.go index 2f4f98f5b3..05097d367e 100644 --- a/cmds/ocm/commands/ocmcmds/components/sign/cmd_test.go +++ b/cmds/ocm/commands/ocmcmds/components/sign/cmd_test.go @@ -30,6 +30,7 @@ import ( "github.com/open-component-model/ocm/pkg/signing/signutils" ) +const COMPARCH = "/tmp/ca" const ARCH = "/tmp/ctf" const ARCH2 = "/tmp/ctf2" const PROVIDER = "mandelsoft" @@ -239,7 +240,7 @@ Error: signing: github.com/mandelsoft/ref:v1: failed resolving component referen buf := bytes.NewBuffer(nil) Expect(env.CatchErrorOutput(buf).Execute("sign", "components", "-s", SIGNATURE, "-K", PRIVKEY, "--repo", ARCH, COMPONENTB+":"+VERSION)).To(HaveOccurred()) Expect(buf.String()).To(StringEqualTrimmedWithContext(` -Error: signing: github.com/mandelsoft/ref:v1: failed resolving component reference ref[github.com/mandelsoft/test:v1]: component "github.com/mandelsoft/test" not found in ComponentArchive +Error: signing: github.com/mandelsoft/ref:v1: failed resolving component reference ref[github.com/mandelsoft/test:v1]: ocm reference "github.com/mandelsoft/test:v1" not found `)) }) @@ -247,7 +248,40 @@ Error: signing: github.com/mandelsoft/ref:v1: failed resolving component referen buf := bytes.NewBuffer(nil) Expect(env.CatchErrorOutput(buf).Execute("sign", "components", "-s", SIGNATURE, "-K", PRIVKEY, ARCH)).To(HaveOccurred()) Expect(buf.String()).To(StringEqualTrimmedWithContext(` -Error: signing: github.com/mandelsoft/ref:v1: failed resolving component reference ref[github.com/mandelsoft/test:v1]: component "github.com/mandelsoft/test" not found in ComponentArchive +Error: signing: github.com/mandelsoft/ref:v1: failed resolving component reference ref[github.com/mandelsoft/test:v1]: ocm reference "github.com/mandelsoft/test:v1" not found +`)) + }) + }) + + Context("component archive", func() { + BeforeEach(func() { + env.OCMCommonTransport(ARCH, accessio.FormatDirectory, func() { + env.Component(COMPONENTA, func() { + env.Version(VERSION, func() { + env.Provider(PROVIDER) + env.Resource("testdata", "", "PlainText", metav1.LocalRelation, func() { + env.BlobStringData(mime.MIME_TEXT, "testdata") + }) + }) + }) + }) + + env.ComponentArchive(COMPARCH, accessio.FormatDirectory, COMPONENTB, VERSION, func() { + env.Reference("ref", COMPONENTA, VERSION) + }) + }) + + It("signs comp arch with lookup", func() { + buf := bytes.NewBuffer(nil) + + MustBeSuccessful(env.CatchOutput(buf).Execute("sign", "components", "-s", SIGNATURE, "-K", PRIVKEY, "--lookup", ARCH, "--repo", COMPARCH)) + Expect(buf.String()).To(StringEqualTrimmedWithContext(` +applying to version "github.com/mandelsoft/ref:v1"[github.com/mandelsoft/ref:v1]... + no digest found for "github.com/mandelsoft/test:v1" + applying to version "github.com/mandelsoft/test:v1"[github.com/mandelsoft/ref:v1]... + resource 0: "name"="testdata": digest SHA-256:810ff2fb242a5dee4220f2cb0e6a519891fb67f2f828a6cab4ef8894633b1f50[genericBlobDigest/v1] + reference 0: github.com/mandelsoft/test:v1: digest SHA-256:5923de2b3b68e904eecb58eca91727926b36623623555025dc5a8700edfa9daa[jsonNormalisation/v1] +successfully signed github.com/mandelsoft/ref:v1 (digest SHA-256:3d1bf98adce06320809393473bed3aaaccf8696418bd1ef5b4d35fa632082d05) `)) }) }) diff --git a/pkg/contexts/ocm/interface.go b/pkg/contexts/ocm/interface.go index 3548bced5c..92b09eaf02 100644 --- a/pkg/contexts/ocm/interface.go +++ b/pkg/contexts/ocm/interface.go @@ -22,6 +22,7 @@ import ( var ErrTempVersion = repocpi.ErrTempVersion const ( + KIND_COMPONENT = internal.KIND_COMPONENT KIND_COMPONENTVERSION = internal.KIND_COMPONENTVERSION KIND_COMPONENTREFERENCE = "component reference" KIND_RESOURCE = internal.KIND_RESOURCE diff --git a/pkg/contexts/ocm/internal/errors.go b/pkg/contexts/ocm/internal/errors.go index f7608bc1b0..19e9eb3f9f 100644 --- a/pkg/contexts/ocm/internal/errors.go +++ b/pkg/contexts/ocm/internal/errors.go @@ -12,6 +12,7 @@ import ( ) const ( + KIND_COMPONENT = errors.KIND_COMPONENT KIND_COMPONENTVERSION = "component version" KIND_RESOURCE = "component resource" KIND_SOURCE = "component source" diff --git a/pkg/contexts/ocm/resolver.go b/pkg/contexts/ocm/resolver.go index 63732374af..80fdf347b5 100644 --- a/pkg/contexts/ocm/resolver.go +++ b/pkg/contexts/ocm/resolver.go @@ -64,7 +64,7 @@ func (c *CompoundResolver) LookupComponentVersion(name string, version string) ( if err == nil && cv != nil { return cv, nil } - if !errors.IsErrNotFoundKind(err, KIND_COMPONENTVERSION) { + if !errors.IsErrNotFoundKind(err, KIND_COMPONENTVERSION) && !errors.IsErrNotFoundKind(err, KIND_COMPONENT) { return nil, err } }