Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ref detection for ca/ctf #607

Merged
merged 2 commits into from
Dec 27, 2023
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
38 changes: 36 additions & 2 deletions cmds/ocm/commands/ocmcmds/components/sign/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -239,15 +240,48 @@ 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
`))
})

It("sign archive", func() {
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)
`))
})
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/accessobj/accessstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/common/accessobj/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 10 additions & 1 deletion pkg/contexts/oci/repositories/ctf/uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/contexts/ocm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/contexts/ocm/internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
)

const (
KIND_COMPONENT = errors.KIND_COMPONENT
KIND_COMPONENTVERSION = "component version"
KIND_RESOURCE = "component resource"
KIND_SOURCE = "component source"
Expand Down
13 changes: 11 additions & 2 deletions pkg/contexts/ocm/repositories/comparch/uniform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/contexts/ocm/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
Loading