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

Validate method for oci/ocm repository specs #866

Merged
merged 5 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 15 additions & 6 deletions api/oci/extensions/repositories/artifactset/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mandelsoft/goutils/errors"
"github.com/mandelsoft/vfs/pkg/vfs"

"ocm.software/ocm/api/oci/artdesc"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
Expand Down Expand Up @@ -45,14 +46,22 @@ type accessObjectInfo struct {

var _ accessobj.AccessObjectInfo = (*accessObjectInfo)(nil)

var baseInfo = accessobj.DefaultAccessObjectInfo{
ObjectTypeName: "artifactset",
ElementDirectoryName: BlobsDirectoryName,
ElementTypeName: "blob",
DescriptorHandlerFactory: NewStateHandler,
DescriptorValidator: validateDescriptor,
}

func validateDescriptor(data []byte) error {
_, err := artdesc.DecodeIndex(data)
return err
}

func NewAccessObjectInfo(fmts ...string) accessobj.AccessObjectInfo {
a := &accessObjectInfo{
accessobj.DefaultAccessObjectInfo{
ObjectTypeName: "artifactset",
ElementDirectoryName: BlobsDirectoryName,
ElementTypeName: "blob",
DescriptorHandlerFactory: NewStateHandler,
},
baseInfo,
}
oci := IsOCIDefaultFormat()
if len(fmts) > 0 {
Expand Down
11 changes: 10 additions & 1 deletion api/oci/extensions/repositories/artifactset/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/mandelsoft/vfs/pkg/vfs"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/datacontext/attrs/vfsattr"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
Expand Down Expand Up @@ -78,11 +79,19 @@ func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentia
return NewRepository(ctx, a)
}

func (a *RepositorySpec) AsUniformSpec(cpi.Context) cpi.UniformRepositorySpec {
func (a *RepositorySpec) AsUniformSpec(ctx cpi.Context) cpi.UniformRepositorySpec {
opts, _ := NewOptions(&a.Options) // now unknown option possible (same Options type)
opts.Default(vfsattr.Get(ctx))
p, err := vfs.Canonical(opts.GetPathFileSystem(), a.FilePath, false)
if err != nil {
return cpi.UniformRepositorySpec{Type: a.GetKind(), Info: a.FilePath}
}
return cpi.UniformRepositorySpec{Type: a.GetKind(), Info: p}
}

func (a *RepositorySpec) Validate(ctx cpi.Context, creds credentials.Credentials, context ...credentials.UsageContext) error {
opts := a.Options
opts.Default(vfsattr.Get(ctx))

return accessobj.ValidateDescriptor(&baseInfo, a.FilePath, opts.GetPathFileSystem())
}
7 changes: 7 additions & 0 deletions api/oci/extensions/repositories/ctf/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/oci/extensions/repositories/ctf/format"
"ocm.software/ocm/api/oci/extensions/repositories/ctf/index"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
"ocm.software/ocm/api/utils/blobaccess/blobaccess"
Expand All @@ -26,6 +27,12 @@ var accessObjectInfo = &accessobj.DefaultAccessObjectInfo{
ElementDirectoryName: BlobsDirectoryName,
ElementTypeName: "blob",
DescriptorHandlerFactory: NewStateHandler,
DescriptorValidator: validateDescriptor,
}

func validateDescriptor(data []byte) error {
_, err := index.Decode(data)
return err
}

type Object = Repository
Expand Down
8 changes: 8 additions & 0 deletions api/oci/extensions/repositories/ctf/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/datacontext/attrs/vfsattr"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
Expand Down Expand Up @@ -81,3 +82,10 @@ func (s *RepositorySpec) UniformRepositorySpec() *cpi.UniformRepositorySpec {
func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentials) (cpi.Repository, error) {
return Open(ctx, a.AccessMode, a.FilePath, 0o700, &a.StandardOptions)
}

func (a *RepositorySpec) Validate(ctx cpi.Context, creds credentials.Credentials, context ...credentials.UsageContext) error {
opts := a.StandardOptions
opts.Default(vfsattr.Get(ctx))

return accessobj.ValidateDescriptor(accessObjectInfo, a.FilePath, opts.GetPathFileSystem())
}
20 changes: 20 additions & 0 deletions api/oci/extensions/repositories/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build docker_test
hilmarf marked this conversation as resolved.
Show resolved Hide resolved

package docker_test

import (
. "github.com/onsi/ginkgo/v2"

"github.com/mandelsoft/goutils/testutils"

"ocm.software/ocm/api/oci"
"ocm.software/ocm/api/oci/extensions/repositories/docker"
)

var _ = Describe("Local Docker Daemon", func() {
It("validated access", func() {
octx := oci.DefaultContext()
spec := docker.NewRepositorySpec()
testutils.MustBeSuccessful(spec.Validate(octx, nil))
})
})
13 changes: 13 additions & 0 deletions api/oci/extensions/repositories/docker/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package docker_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestConfig(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "docker daemon Test Suite")
}
12 changes: 12 additions & 0 deletions api/oci/extensions/repositories/docker/type.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package docker

import (
"context"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/utils"
Expand Down Expand Up @@ -46,3 +48,13 @@ func (a *RepositorySpec) UniformRepositorySpec() *cpi.UniformRepositorySpec {
func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentials) (cpi.Repository, error) {
return NewRepository(ctx, a)
}

func (a *RepositorySpec) Validate(ctx cpi.Context, creds credentials.Credentials, usageContext ...credentials.UsageContext) error {
client, err := newDockerClient(a.DockerHost)
if err != nil {
return err
}

_, err = client.Ping(context.Background())
return err
}
4 changes: 4 additions & 0 deletions api/oci/extensions/repositories/empty/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ func (a *RepositorySpec) UniformRepositorySpec() *cpi.UniformRepositorySpec {
func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentials) (cpi.Repository, error) {
return ctx.GetAttributes().GetOrCreateAttribute(ATTR_REPOS, func(datacontext.Context) interface{} { return NewRepository(ctx) }).(cpi.Repository), nil
}

func (a *RepositorySpec) Validate(cpi.Context, credentials.Credentials, ...credentials.UsageContext) error {
return nil
}
14 changes: 14 additions & 0 deletions api/oci/extensions/repositories/ocireg/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"strings"
"time"

"github.com/containerd/containerd/reference"

Expand All @@ -12,6 +13,7 @@ import (
"ocm.software/ocm/api/oci/cpi"
"ocm.software/ocm/api/utils"
"ocm.software/ocm/api/utils/runtime"
"ocm.software/ocm/api/utils/tcp"
)

const (
Expand Down Expand Up @@ -128,6 +130,18 @@ func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentia
return NewRepository(ctx, a, info)
}

func (a *RepositorySpec) Validate(ctx cpi.Context, creds credentials.Credentials, context ...credentials.UsageContext) error {
info, err := a.getInfo(creds)
if err != nil {
return err
}
h, p, _ := info.HostInfo()
if p == "" {
p = "443"
}
hilmarf marked this conversation as resolved.
Show resolved Hide resolved
return tcp.PingTCPServer(h+":"+p, time.Second)
}

func (a *RepositorySpec) GetConsumerId(uctx ...credentials.UsageContext) credentials.ConsumerIdentity {
info, err := a.getInfo(nil)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions api/oci/internal/repotypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type RepositorySpec interface {
Name() string
UniformRepositorySpec() *UniformRepositorySpec
Repository(Context, credentials.Credentials) (Repository, error)

Validate(Context, credentials.Credentials, ...credentials.UsageContext) error
}

type (
Expand Down Expand Up @@ -95,6 +97,10 @@ func (r *UnknownRepositorySpec) Repository(Context, credentials.Credentials) (Re
return nil, errors.ErrUnknown("repository type", r.GetType())
}

func (r *UnknownRepositorySpec) Validate(Context, credentials.Credentials, ...credentials.UsageContext) error {
return errors.ErrUnknown("repository type", r.GetType())
}

////////////////////////////////////////////////////////////////////////////////

type GenericRepositorySpec struct {
Expand Down Expand Up @@ -157,4 +163,12 @@ func (s *GenericRepositorySpec) Repository(ctx Context, creds credentials.Creden
return spec.Repository(ctx, creds)
}

func (s *GenericRepositorySpec) Validate(ctx Context, creds credentials.Credentials, context ...credentials.UsageContext) error {
spec, err := s.Evaluate(ctx)
if err != nil {
return err
}
return spec.Validate(ctx, creds)
}

////////////////////////////////////////////////////////////////////////////////
62 changes: 61 additions & 1 deletion api/ocm/extensions/repositories/comparch/comparch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,22 @@ var _ = Describe("Repository", func() {
// spec will not equal r as the filesystem cannot be serialized
})

It("validates component archive with resource stored as tar", func() {
// this is the typical use case
octx := ocm.DefaultContext()
spec := Must(comparch.NewRepositorySpec(accessobj.ACC_READONLY, TAR_COMPARCH))

MustBeSuccessful(spec.Validate(octx, nil))
})

It("validates component archive with resource stored as dir", func() {
// this is the typical use case
octx := ocm.DefaultContext()
spec := Must(comparch.NewRepositorySpec(accessobj.ACC_READONLY, DIR_COMPARCH))

MustBeSuccessful(spec.Validate(octx, nil))
})

It("component archive with resource stored as tar", func() {
// this is the typical use case
octx := ocm.DefaultContext()
Expand Down Expand Up @@ -87,7 +103,7 @@ var _ = Describe("Repository", func() {
Expect(bufferA).To(Equal(bufferB))
})

It("creates component archive", func() {
It("creates component archive directory", func() {
octx := ocm.DefaultContext()
memfs := memoryfs.New()

Expand All @@ -111,6 +127,50 @@ var _ = Describe("Repository", func() {
}))

MustBeSuccessful(finalize.Finalize())
Expect(vfs.DirExists(memfs, "test")).To(BeTrue())

spec := Must(comparch.NewRepositorySpec(accessobj.ACC_READONLY, "test", accessio.PathFileSystem(memfs)))
MustBeSuccessful(spec.Validate(octx, nil))

arch = Must(comparch.Open(octx, accessobj.ACC_WRITABLE, "test", 0o0700, accessio.PathFileSystem(memfs)))
finalize.Close(arch, "comparch)")

res = Must(arch.GetResourcesByName("blob"))
Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{
HashAlgorithm: sha256.Algorithm,
NormalisationAlgorithm: blob.GenericBlobDigestV1,
Value: D_TESTDATA,
}))
})

It("creates component archive tgz", func() {
octx := ocm.DefaultContext()
memfs := memoryfs.New()

var finalize finalizer.Finalizer
defer Defer(finalize.Finalize)

arch := Must(comparch.Create(octx, accessobj.ACC_WRITABLE, "test", 0o0700, accessio.FormatTGZ, accessio.PathFileSystem(memfs)))
finalize.Close(arch, "comparch)")

arch.SetName("acme.org/test")
arch.SetVersion("v1.0.1")

MustBeSuccessful(arch.SetResourceBlob(compdesc.NewResourceMeta("blob", resourcetypes.PLAIN_TEXT, metav1.LocalRelation),
blobaccess.ForString(mime.MIME_TEXT, S_TESTDATA), "", nil))

res := Must(arch.GetResourcesByName("blob"))
Expect(res[0].Meta().Digest).To(DeepEqual(&metav1.DigestSpec{
HashAlgorithm: sha256.Algorithm,
NormalisationAlgorithm: blob.GenericBlobDigestV1,
Value: D_TESTDATA,
}))

MustBeSuccessful(finalize.Finalize())
Expect(vfs.FileExists(memfs, "test")).To(BeTrue())

spec := Must(comparch.NewRepositorySpec(accessobj.ACC_READONLY, "test", accessio.PathFileSystem(memfs)))
MustBeSuccessful(spec.Validate(octx, nil))

arch = Must(comparch.Open(octx, accessobj.ACC_WRITABLE, "test", 0o0700, accessio.PathFileSystem(memfs)))
finalize.Close(arch, "comparch)")
Expand Down
8 changes: 7 additions & 1 deletion api/ocm/extensions/repositories/comparch/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ const BlobsDirectoryName = "blobs"

var accessObjectInfo = &accessobj.DefaultAccessObjectInfo{
DescriptorFileName: ComponentDescriptorFileName,
ObjectTypeName: "artifactset",
ObjectTypeName: "component archive",
ElementDirectoryName: BlobsDirectoryName,
ElementTypeName: "blob",
DescriptorHandlerFactory: NewStateHandler,
DescriptorValidator: validateDescriptor,
}

func validateDescriptor(data []byte) error {
_, err := compdesc.Decode(data)
return err
}

type Object = ComponentArchive
Expand Down
15 changes: 12 additions & 3 deletions api/ocm/extensions/repositories/comparch/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/mandelsoft/vfs/pkg/vfs"

"ocm.software/ocm/api/credentials"
"ocm.software/ocm/api/datacontext/attrs/vfsattr"
"ocm.software/ocm/api/ocm/cpi"
"ocm.software/ocm/api/utils/accessio"
"ocm.software/ocm/api/utils/accessobj"
Expand Down Expand Up @@ -63,12 +64,20 @@ func (a *RepositorySpec) Repository(ctx cpi.Context, creds credentials.Credentia
return NewRepository(ctx, a)
}

func (a *RepositorySpec) AsUniformSpec(cpi.Context) *cpi.UniformRepositorySpec {
opts := &accessio.StandardOptions{}
opts.Default()
func (a *RepositorySpec) AsUniformSpec(ctx cpi.Context) *cpi.UniformRepositorySpec {
opts := a.StandardOptions
opts.Default(vfsattr.Get(ctx))

p, err := vfs.Canonical(opts.GetPathFileSystem(), a.FilePath, false)
if err != nil {
return &cpi.UniformRepositorySpec{Type: a.GetKind(), SubPath: a.FilePath}
}
return &cpi.UniformRepositorySpec{Type: a.GetKind(), SubPath: p}
}

func (a *RepositorySpec) Validate(ctx cpi.Context, creds credentials.Credentials, context ...credentials.UsageContext) error {
opts := a.StandardOptions
opts.Default(vfsattr.Get(ctx))

return accessobj.ValidateDescriptor(accessObjectInfo, a.FilePath, opts.GetPathFileSystem())
}
4 changes: 4 additions & 0 deletions api/ocm/extensions/repositories/composition/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@
return NewRepository(ctx, r.Name), nil
}

func (a *RepositorySpec) Validate(ctx internal.Context, creds credentials.Credentials, context ...credentials.UsageContext) error {

Check failure on line 41 in api/ocm/extensions/repositories/composition/type.go

View workflow job for this annotation

GitHub Actions / Run tests

undefined: internal

Check failure on line 41 in api/ocm/extensions/repositories/composition/type.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: internal (typecheck)

Check failure on line 41 in api/ocm/extensions/repositories/composition/type.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: internal) (typecheck)
return nil
}

var _ cpi.RepositorySpec = (*RepositorySpec)(nil)
4 changes: 4 additions & 0 deletions api/ocm/extensions/repositories/genericocireg/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ func (s *RepositorySpec) GetIdentityMatcher() string {
return credentials.GetProvidedIdentityMatcher(s.RepositorySpec)
}

func (s *RepositorySpec) Validate(ctx cpi.Context, creds credentials.Credentials, uctx ...credentials.UsageContext) error {
return s.RepositorySpec.Validate(ctx.OCIContext(), creds, uctx...)
}

func DefaultComponentRepositoryMeta(meta *ComponentRepositoryMeta) *ComponentRepositoryMeta {
if meta == nil {
meta = &ComponentRepositoryMeta{}
Expand Down
Loading
Loading